Professional Documents
Culture Documents
Shashank Gupta
BITS Pilani Assistant Professor
Pilani Campus
Department of Computer Science and Information Systems
Parsing
S ABCDE
A a | First (S ) First ( ABCDE ) (First ( A) ) (First (B) ) (First (C )) {a, b, c }
B b |
C c
D d |
E e |
CS F363 Compiler Construction 11
BITS Pilani, Pilani Campus
Example
S Bb | Cd
B aB |
C cC |
S ACB | CbB | Ba
A da | BC
B g |
C h |
E i E '
E i E |
' '
E i E '
E ' i E ' |
15
CS F363 Compiler Construction BITS Pilani, Pilani Campus
Example
A A |
' '
19
CS F363 Compiler Construction BITS Pilani, Pilani Campus
Example
E E T |T
T T * F | F
F ( E ) | id
A A1 | A 2 | A 3 | | A n | 1 | 2 | 3 | | m
A AC | A a d |b d |c
S A a |b
A Ac | Sd |
S c Ad
A a b|a
A A '
A 1 | 2
'
25
CS F363 Compiler Construction BITS Pilani, Pilani Campus
Example of Left Factoring
28
CS F363 Compiler Construction BITS Pilani, Pilani Campus
Follow Sets
S ABCDE
A a |
B b |
C c
D d |
E e |
CS F363 Compiler Construction 30
BITS Pilani, Pilani Campus
Example
S Bb | Cd
B aB |
C cC |
A da | BC S e S |
'
B g | E b
C h |
Predictive Parser
S S |
Predictive Parser
E T E ' Non-Terminal First Follow
E T E |
' ' E {id, ( } {$, ) }
E’ {+, } {$, ) }
T FT' T {id, ( } {+, ), $}
T’ {*, } {+, ), $}
T ' * F T ' | F {id, ( } {*, +, ), $}
F id | ( E )
Non-Terminal First Follow
E {id, ( } {$, ) }
E’ {+, } {$, ) }
T {id, ( } {+, ), $}
T’ {*, } {+, ), $}
F {id, ( } {*, +, ), $}
A 1 | 2 | 3 | | n
A |
First ( ) ( Follow( A)
Place the symbols in follow (A) in syn set. Skip the tokens until
an element in follow (A) is seen. Pop (A) and continue parsing.
44
CS F363 Compiler Construction BITS Pilani, Pilani Campus
Error Recovery through Panic Mode
Keep discarding the token until you hit any token, that
comes in the follow set of any non-terminal of grammar
that is placed on the top of stack. Pop that non-terminal
from the stack and continue parsing from that token.
Example
Consider the following predictive parsing table and the following subset of input string
* ) id The current status of the stack is $ E ' T " ) E ' F T G . Parse this given input and
if error state exist then show error recovery through panic mode.
Example
1. b exp r btermb exp r ' NT First Set Follow Set
2. b exp r ' or btermb exp r ' bexpr { not, (, true, false } { $, ) }
bexp’ { or, } { $, ) }
3. b exp r ' bterm { not, (, true, false } { or, $, ) }
4. bterm bfactor bterm' bterm’ { and, } { or, $, ) }
bfactor { not, (, true, false } { and, or, $, ) }
5. bterm' and bfactor bterm'
6. bterm'
7. bfactor not bfactor
8. bfactor ( b exp r )
9. bfactor true “not (true and or false)”
10. bfactor false
or and not ( ) true false $
Bexpr 1 1 syn 1 1 syn
bexp’ 2 3 3
bterm syn 4 4 syn 4 4 syn
bterm’ 6 5 6 6
bfactor syn syn 7 8 syn 9 10 syn 48
CS F363 Compiler Construction BITS Pilani, Pilani Campus
Example
matrix rows
rows row semicolonrows | row
row num remainingelements
remainingelements comma num remainingelements|
The modified LL (1) Grammar is
matrix rows
rows row remainingrows
remainingrows semicolonrows |
row num remainingelements
remainingelements comma num remainingelements|
13
CS F363 Compiler
BITS Pilani, Pilani Campus