0% found this document useful (0 votes)
2 views106 pages

cd2

The document discusses syntax analysis, focusing on the role of parsers, error handling, derivations, parse trees, and grammar transformations such as left recursion elimination and left factoring. It also covers top-down and bottom-up parsing techniques, including predictive parsing algorithms and operator precedence relations. Additionally, it provides examples of parsing strings and using LR(0) automaton for parsing actions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views106 pages

cd2

The document discusses syntax analysis, focusing on the role of parsers, error handling, derivations, parse trees, and grammar transformations such as left recursion elimination and left factoring. It also covers top-down and bottom-up parsing techniques, including predictive parsing algorithms and operator precedence relations. Additionally, it provides examples of parsing strings and using LR(0) automaton for parsing actions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 106

Syntax Analysis

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)

If ∈ ∉ First(Y1), then First(X) = First(Y1)


If ∈ ∈ First(Y1), then First(X) = { First(Y1) – ∈ } ∪
First(Y2Y3)

Calculating First(Y2Y3)

If ∈ ∉ First(Y2), then First(Y2Y3) = First(Y2)


If ∈ ∈ First(Y2), then First(Y2Y3) = { First(Y2) – ∈ } ∪
First(Y3)
Rules For Calculating Follow Function-
Rule-01:

For the start symbol S, place $ in Follow(S).

Rule-02:

For any production rule A → αB,


Follow(B) = Follow(A)
Rule-03:

For any production rule A → αBβ,

If ∈ ∉ First(β), then Follow(B) = First(β)


If ∈ ∈ First(β), then Follow(B) = { First(β) – ∈ }
∪ Follow(A)
Important Notes-
Note-01:
∈ may appear in the first function of a non-terminal.
∈ will never appear in the follow function of a non-terminal.
Note-02:

Before calculating the first and follow functions,


eliminate Left Recursion from the grammar, if present.

Note-03:

We calculate the follow function of a non-terminal by looking


where it is present on the RHS of a production rule.
BOTTOM UP PARSING
There are the three operator
precedence relations:
• a ⋗ b means that terminal "a" has the higher
precedence than terminal "b".
• a ⋖ b means that terminal "a" has the lower
precedence than terminal "b".
• a ≐ b means that the terminal "a" and "b" both
have same precedence.
Parsing A Given String-
The given input string is parsed using the following steps-

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

(1) 0 id*id+id$ Shift to 5

(2) 05 id *id+id$ Reduce by F->id

(3) 03 F *id+id$ Reduce by T->F

(4) 02 T *id+id$ Shift to 7

(5) 027 T* id+id$ Shift to 5

(6) 0275 T*id +id$ Reduce by F->id

(7) 02710 T*F +id$ Reduce by T->T*F

(8) 02 T +id$ Reduce by E->T

(9) 01 E +id$ Shift

(10) 016 E+ id$ Shift

(11) 0165 E+id $ Reduce by F->id

(12) 0163 E+F $ Reduce by T->F

(13) 0169 E+T` $ Reduce by E->E+T

(14) 01 E $ accept

You might also like