0% found this document useful (0 votes)
14 views

BKS Unit II-I - Introduction To Parsing

compiler notes

Uploaded by

siyadogra98
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

BKS Unit II-I - Introduction To Parsing

compiler notes

Uploaded by

siyadogra98
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

Learn CD: From B K Sharma

Unit II

Syntax Analysis and Syntax Directed


Translation
Learn Compiler Design: From B. K. Sharma

Unit II: Syllabus


• Syntax Analysis and Syntax Directed
Translation
• Syntax Analysis:
– CFGs
– Top Down Parsing
– Brute Force Approach
– Recursive Descent Parsing
– Transformation on the grammars
Learn Compiler Design: From B. K. Sharma

Unit II: Syllabus


– Predictive Parsing
– Bottom Up Parsing
– Operator Precedence Parsing
– LR Parsers
• SLR,
• LALR
• LR
• Parser Generator(yacc)yet
Learn Compiler Design: From B. K. Sharma

Unit II: Syllabus


• Syntax Directed Definitions
– Construction of syntax trees
– Bottom Up Evaluation of S-attributed
definition
– L-attribute Definition
– Top Town translation
– Bottom Up Evaluation of inherited attributes
– Recursive Evaluation
– Analysis of Syntax Directed Definition
Learn Compiler Design: From B. K. Sharma
Summary of Unit I: Introduction to Compiler
1:A compiler reads a program written in the source
language and translate it into an equivalent program in
target language.
2: There are six phases of compiler: Lexical Analysis, Syntax
Analysis, Semantic Analysis, Intermediate Code Generation ,
Code Optimization and Target Code Generation.
3: There are two models of compilation: Analysis and Synthesis
model and Front-End and Back-End Model.
4: Different types of compilers are: One-Pass, Two-Pass,
Multi-Pass, Incremental Compiler, Native Compiler, Cross-
Compiler and JIT Compiler.
5. Cousins of Compilers are: Pre-processor, Assembler, Linker
and Loader.
6: Major Data Structures in Compiler are: Tokens, Syntax Tree,
Symbol Table and Literal Table.
Learn Compiler Design: From B. K. Sharma
Summary of Unit I: Introduction to Compiler
7: There are two types of errors: Compiler Time and Run Time
errors.
8: Lexical Errors, Syntax Errors and Semantic Errors are
Compile Time Errors
9: Tokens are specified by Regular Expression and recognized by
Finite Automata.
10: Thompson’s Construction method is used to convert regular
expression to NFA with ε-moves.
11: Subset Construction Method is used to convert NFA with ε-
moves to DFA
12: Hopcroft’s Algorithm is used to minimize the number of
states in DFA.
13. Lex is a tool to design lexical analyzer or scanner generator.
Learn Compiler Design: From B. K. Sharma

Mapping of Lesson with Course Outcome


(CO)
Lesson CO3
Lesson 13: Parsing or Understand LL, LR,
Syntax Analysis and SLR parsing
techniques.
Learn Compiler Design: From B. K. Sharma
Lesson 13: Parsing or Syntax Analysis:
Learning Outcomes
At the end of this lesson, students will be able to

1: Define parsing and syntax analysis.

2: Explain working of Parser with example.

3: Differentiate between Parse Tree and Syntax


Tree.
Learn Compiler Design: From B. K. Sharma

Syntax Analysis (Parsing)


regular
expressions errors

lexical token
source parser parse rest of interm
program analyzer get next tree front end repres
token

symbol
table • also technically part of
• uses a grammar to
parsing
check structure of tokens
• produces a parse tree • includes augmenting
• syntactic errors and info on tokens in source,
recovery type checking, semantic
• recognize correct
analysis
syntax of tokens
• report errors
Learn Compiler Design: From B. K. Sharma

To Parse Means
To parse a word or a sentence means to
break it into parts that conform to a given
grammar.

What is Parsing?
Parsing is a process to determine if a string
of tokens can be generated by a grammar.

The process of deriving a string from a


given grammar using the productions of the
grammar is known as parsing.
Learn Compiler Design: From B. K. Sharma

Parsing or Syntax Analysis


Parser determines if the tokens given by
scanner is syntactically valid.

That is, is the structure correct?


Also called syntax analysis

Syntax is given by a set of grammar rules


of a context-free-grammar.
Learn Compiler Design: From B. K. Sharma

What is Syntax Analysis?

To find a derivation sequence in a grammar


G for the input token stream (or say that
none exists).

The parser finds the derivation of a


particular input.
Learn Compiler Design: From B. K. Sharma

Syntax Analysis: Parser


Receives tokens from the scanner.

Recognizes the structure of the program as


a parse tree.

Parse tree is recognized according to a


context-free grammar.

Syntax errors are reported if the program


is syntactically incorrect
Learn Compiler Design: From B. K. Sharma

What is Parsing?
Input String Parsing Derivation

CFG

A parser knows the grammar of the


programming language.
Parser
Input
Grammar Derivation
String
Learn Compiler Design: From B. K. Sharma

Parsing

Derivation
Input String S
Parsing S cAd
cad S cad

CFG Production: S  c A d
Production: A a

G S  c A d
A  ab | a
Learn Compiler Design: From B. K. Sharma

Parsing

Derivation
Stream of Tokens
E
Parsing E + E
Id + Id * Id
Id + E
CFG Id + E * E
Id + Id * E
Id + Id * Id
E E * E Production: E E + E
G | E + E Production: E Id
| Id Production: E E * E
Production: E Id
Production: E Id
Learn Compiler Design: From B. K. Sharma

Parsing
Input String

Stream of
Parsing Derivation
Tokens

CFG
Learn Compiler Design: From B. K. Sharma

Parsing
Input String

Stream of
Parsing Derivation
Tokens Tree

CFG
Learn Compiler Design: From B. K. Sharma

Parsing
Input String

Stream of
Parsing Parse Tree
Tokens

CFG
Learn Compiler Design: From B. K. Sharma

Parsing
G
S  c A d Input: cad
A  ab | a

S S
Production: S  c A d
S c A d
Production: A a
 c a d c A d

a
Learn Compiler Design: From B. K. Sharma

Compiler and Grammar and Parser


Learn Compiler Design: From B. K. Sharma

Active Learning Activity


One- Minute Paper
Consider the following input to the parser:
Parser
Input
E -> E + E
10 + 2 * 5 | E * E
| INT

Write the derivation steps to derive the given


input.
Learn Compiler Design: From B. K. Sharma

Compiler and Grammar and Parser

Derivation
Parser E
Input => E + E
E -> E + E
| E * E => 10 + E
10 + 2 * 5 => 10 + E*E
| INT
=> 10 + 2 * E
=> 10 + 2 * 5
Learn Compiler Design: From B. K. Sharma

Derivation and Derivation Tree / Parse Tree

Derivation tree
Derivation
E
E
=> E + E +
E E
=> 10 +E
=> 10 +E *E
10
=> 10 + 2 * E E E
*
=> 10 + 2 * 5
2 5
Learn Compiler Design: From B. K. Sharma

What is Derivation Tree/ Parse Tree?

Represents derivations as trees.

Each internal node is labeled with a non-


terminal.

Each leaf node is labeled with a terminal.


Learn Compiler Design: From B. K. Sharma

Syntax Analysis: Parser

A parse tree is inefficient to represent the


structure of a program.

A syntax tree is a more condensed version


of the parse tree.

A syntax tree is usually generated as output


by the parser.
Learn Compiler Design: From B. K. Sharma

Parse Tree Vs Syntax Tree


E E * E Input String: Id + Id * Id
| E + E
E
| Id => E + E
Parse Tree Syntax Tree => Id + E
E => Id + E*E
+
=> Id + Id * E
E E => Id + Id * Id
+ *
Id
E * E
Id Id Id

Id Id
Learn Compiler Design: From B. K. Sharma
Difference between Parse Tree and Syntax Tree

Parse Tree Syntax Tree

Interior Nodes are Non- Interior nodes are


Terminals, leaves are operators, leaves are
terminals operands
Represents the concrete Represents the abstract
syntax of the Program syntax of the program.

Difference: abstract syntax vs concrete (parse)


syntax
Learn Compiler Design: From B. K. Sharma
Summary of Lesson 13: Introduction Parsing or
Syntax Analysis
1: Parsing is the process of deriving a given input string
according to the productions of the grammar.

2: Parser determines if the tokens given by scanner is


syntactically valid according to the productions of CFG.

3: The output of parsing is syntax tree which is condensed


version of parse tree after removing the Non-terminals of
the grammar; containing operators as internal nodes and
operands as external nodes (leaves).
Learn Compiler Design: From B. K. Sharma

Read PPT Named Syntax Analysis and


CFG

You might also like