Professional Documents
Culture Documents
Unit-II (Part-A) Basic ParsingTechniques
Unit-II (Part-A) Basic ParsingTechniques
• For example,
A→Aα
Here A can be any non-terminal and α denotes
some input string.
Consider a grammar
S → ACB|CbB|Ba
A → da|BC
B → g|Є
C → f|Є
Production Rules:
S → ACB|Cbb|Ba
A → da|BC
B → g|Є
C → h| Є
FIRST set:
FIRST(S) = FIRST(A) U FIRST(B) U FIRST(C) = { d, g, h, b, a, Є}
FIRST(A) = { d } U FIRST(B) = { d, g, h, Є }
FIRST(B) = { g, Є }
FIRST(C) = { h, Є }
FOLLOW Set:
FOLLOW(S) = { $ }
FOLLOW(A) = { h, g, $ }
FOLLOW(B) = { a, $, h, g }
FOLLOW(C) = { b, g, $, h }
FOLLOW set:
FOLLOW(E) = {), $}
FOLLOW(E’) = {), $}
FOLLOW(T) = {+, ), $}
FOLLOW(T’) = {+, ), $}
FOLLOW(F) = {*, +, ), $}
Compiler Design, KCS-502 48
Construction of Predictive Parsing
Table
• Once we know the FOLLOW set for each non-
terminal, we can construct a parse table.
• Initially, the stack has the start symbol of a grammar above the $.
Now E is top of the stack and input pointer is at the first ‘id’.
Hence M[E,id] is referred i.e., E→TE’.
Push E’ first into the stack then T.
Stack Input Moves
$E’T id+id*id$ E→TE’
Now T is top of stack and input pointer is at the first ‘id’.
Therefore, M[T,id] = T→FT’.
Push T’ first into the stack then F. Similarly, we get
Compiler Design, KCS-502 58
Construction of Predictive Parsing
Table
Stack Input Moves
$E id+id*id$
$E’T id+id*id$ E→TE’
$E’T’F id+id*id$ T→FT’
$E’T’id id+id*id$ F→id
$E’T’ +id*id$
$E’ +id*id$ T’→Є
$E’T+ +id*id$ E’→+TE’
$E’T id*id$
$E’T’F id*id$ T→FT’
$E’T’id id*id$ F→id
$E’T’ *id$