Professional Documents
Culture Documents
CS-4207
Role of Parser
2
Role of Parser
token
Source Lexical
Parse tree Rest of Front Intermediate
Parser
program Analyzer End representation
getNext
Token
Symbol
table
3
Role of Parser
programming language
4
The Role of Parser
5
Syntax Directed Translation
6
Representative Grammar
7
Notation Conventions
Terminals
a,b,c,… T
specific terminals: 0, 1, id, +
Nonterminals
A,B,C,… N
specific nonterminals: expr, term, stmt
Grammar symbols
X,Y,Z (VT)
Strings of terminals
u,v,w,x,y,z T*
8
Chomsky Hierarchy : Language Classification
A grammar G is said to be
Regular if it is right linear where each production is of the form
AwB or Aw
A
where A N and (VT)*
A
where A V, ,, (VT)*, || > 0
Unrestricted 9
Chomsky Hierarchy : Language Classification
Examples:
Every finite language is regular! (construct a FSA for strings in L(G))
L1 = { anbn | n 1 } is context free
L2 = { anbncn | n 1 } is context sensitive
10
Chomsky Hierarchy : Language Classification
Type 3 –
Regular
11
Derivation
A
In addition, we define
L(G) = {w T* | S + w}
12
Derivation - Example
productions P = E E + E
EE*E
E(E)
E-E
E id
Example derivations:
E - E - id
E * E
E * id + id
E + id * id + id 13
Recursive Decent Parsing
The procedure recognizes a prefix of the input that can be generated by the
corresponding non-terminal, it consumes the prefix, and returns a parse tree for
14
General Structure
Each right hand side of a production provides part of the body of the function
Each non terminal on the right hand side is translated into a call to the function
Each terminal on the right hand side is translated into a call to the lexical
15
Complication : Left Recursion
recursive decent
16
Eliminating Left Recursion
17
Left Recursion
18
Left Recursion
How about left recursion occurred for derivation with more than two steps?
e.g., S → Aa | b A → Ac | Sd | ε
Now, S → Aa | b
A →Ac | Aad | bd | ε
After transformation:
19
Left Recursion Elimination Method
20
Left Recursion involving several Non-Terminal
21
Further Complications
22
Error Handling
23
Error Recovery Strategies
Panic-Mode Recovery
Discards input symbols one at a time until one of the designated set of
synchronized token (such as semicolon or }) is found
Skips a considerable amount of input without checking it for additional
errors
Phrase Level Recovery
Perform local correction on the remaining input – may replace prefix of
remaining input with some string
Typical local correction is to replace a comma by a semicolon, delete an
extraneous semicolon, or insert a missing semicolon
Error Production
Augment (Expand) grammar with production for erroneous constructs
24
Error Recovery Strategies
Global Correction
Given an incorrect input symbol x for grammar G try to find a parse tree for a
related string y – transform x into y
25
Types of Parser
A top-down parser starts with the root of the parse tree. The root node is
labeled with the goal (start) symbol of the grammar. The top-down parsing
2. Repeat until the fringe of the parse tree matches input string
c. When a terminal symbol is added to the fringe and it does not match
LL Methods (Left to right . Left most derivation) and recursive decent parsing
28
Top Down Parsing : Backtracking Problem
1. Left recursion
2. Backtracking
It undo not only the movement but also the semantic entry in the symbol
table
29
Ambiguity
Example : id + id * id
EE+E EE*E
id + E E+E*E
id + E * E id + E * E
id + id * E id + id * E
id + id * id id + id * id
30
Eliminating Ambiguity
31
Eliminating Ambiguity
The grammar is ambiguous as the below given string generates two different
parse trees
32
Preferred
Eliminating Ambiguity
33
34