cd2
cd2
Unit-2
The role of parser
toke
Source Lexical n Parse Rest of Front Intermediate
Parser
progra Analyzer tree End representatio
m getNext n
Token
Symbol
table
Error handling
• Common programming errors
– Lexical errors
– Syntactic errors
– Semantic errors
• Error handler goals
– Report the presence of errors clearly and accurately
– Recover from each error quickly enough to detect
subsequent errors
– Add minimal overhead to the processing of correct
programs
Derivations
• Productions are treated as rewriting rules to
generate a string
• Rightmost and leftmost derivations
– E -> E + E | E * E | -E | (E) | id
– Derivations for –(id+id)
• E => -E => -(E) => -(E+E) => -(id+E)=>-(id+id)
Parse trees
• -(id+id)
• E => -E => -(E) => -(E+E) => -(id+E)=>-(id+id)
Ambiguity
• For some strings there exist more than one
parse tree
• Or more than one leftmost derivation
• Or more than one rightmost derivation
• Example: id+id*id
Left recursion
• A grammar is left recursive if it has a non-terminal A
such that there is a derivation A=> Aα
+
• Top down parsing methods cant handle left-recursive
grammars
• A simple rule for direct left recursion elimination:
– For a rule like:
• A -> A α|β
– We may replace it with
• A -> β A’
• A’ -> α A’ | ɛ
Eliminate Left Recursion
E -> E + T | T
T -> T * F | F
F -> (E) | id
• E → E+T|T
α=+T and β=T
E → TEʹ
Eʹ→ +TEʹ|ϵ
• T → T*F|F
α=*F and β=F
T → FTʹ
Tʹ→ *FTʹ|ϵ
• After eliminating the left recursion, the final
production rules are as follows:
•
E → TEʹ
Eʹ→ +TEʹ|ϵ
T → FTʹ
Tʹ→ *FTʹ|ϵ
F → (E)|id
Left factoring
• Left factoring is a grammar transformation that is useful for
producing a grammar suitable for predictive or top-down
parsing.
• Consider following grammar:
– Stmt -> if expr then stmt else stmt
– | if expr then stmt
• On seeing input if it is not clear for the parser which
production to use
• We can easily perform left factoring:
– If we have A->αβ1 | αβ2 then we replace it with
• A -> αA’
• A’ -> β1 | β2
Left factoring (cont.)
• Example:
– S -> i E t S | i E t S e S | a
– E -> b
TOP DOWN PARSING
Source program
Non-recursive predicting parsing
Non-recursive predicting parsing
Predictive parsing algorithm
Set ip point to the first symbol of w;
Set X to the top stack symbol;
While (X<>$) { /* stack is not empty */
if (X is a) pop the stack and advance ip;
else if (X is a terminal) error();
else if (M[X,a] is an error entry) error();
else if (M[X,a] = X->Y1Y2..Yk) {
output the production X->Y1Y2..Yk;
pop the stack;
push Yk,…,Y2,Y1 on to the stack with Y1 on top;
}
set X to the top stack symbol;
}
Rules For Calculating First Function-
Rule-01:
For a production rule X → ∈,
First(X) = { ∈ }
Rule-02:
For any terminal symbol ‘a’,
First(a) = { a }
Rule-03:
For a production rule X → Y1Y2Y3,
Calculating First(X)
Calculating First(Y2Y3)
Rule-02:
Note-03:
Step-01:
Insert the following-
• $ symbol at the beginning and ending of the input
string.
• Precedence operator between every two symbols of
the string by referring the operator precedence table.
Step-02:
• Start scanning the string from LHS in the forward
direction until > symbol is encountered.
• Keep a pointer on that location.
Step-03:
• Start scanning the string from RHS in the backward direction
until < symbol is encountered.
• Keep a pointer on that location.
Step-04:
• Everything that lies in the middle of < and > forms the handle.
• Replace the handle with the head of the respective production.
Step-05:
• Keep repeating the cycle from Step-02 to Step-04 until the
start symbol is reached.
E’->E
E -> E + T | T
Example
acc
T -> T * F | F
F -> (E) | id
$ I6 I9
E->E+.T
I1 T->.T*F
T
+ E->E+T.
E’->E. T->.F
T->T.*F
E F->.(E)
E->E.+T F->.id
I0=closure({[E’->.E]} I2
E’->.E
T I7 F I10
E’->T. * T->T*.F
E->.E+T F->.(E)
T->T.*F T->T*F.
E->.T id F->.id
T->.T*F id
T->.F I5
F->.(E) F->id. +
F->.id
(
I4
F->(.E)
E->.E+T E I8 ) I11
E->.T E->E.+T
T->.T*F F->(E.) F->(E).
T->.F
F->.(E)
F->.id
I3
T>F.
Use of LR(0) automaton
• Example: id*id
Line Stack Symbols Input Action
(1) 0 $ id*id$ Shift to 5
(2) 05 $id *id$ Reduce by F->id
(3) 03 $F *id$ Reduce by T->F
(4) 02 $T *id$ Shift to 7
(5) 027 $T* id$ Shift to 5
(6) 0275 $T*id $ Reduce by F->id
(7) 02710 $T*F $ Reduce by T->T*F
(8) 02 $T $ Reduce by E->T
(9) 01 $E $ accept
(0) E’->E
Example (1) E -> E + T
(2) E-> T
STATE ACTON GOTO (3) T -> T * F
(4) T-> F
id + * ( ) $ E T F
(5) F -> (E)
0 S5 S4 1 2 3 (6) F->id
1 S6 Acc
2 R2 S7 R2 R2
3 R4 R7 R4 R4 id*id+id?
4 S5 S4 8 2 3
5 R6 R6 R6 R6
6 S5 S4 9 3
7 S5 S4 10
8 S6 S11
9 R1 S7 R1 R1
10 R3 R3 R3 R3
11 R5 R5 R5 R5
Example
Line Stack Symbols Input Action
(14) 01 E $ accept