Académique Documents
Professionnel Documents
Culture Documents
Fall 2008
Andrej Bogdanov
http://www.cse.cuhk.edu.hk/~andrejb/csc3130
Parsing algorithms
How long would it take to parse this?
exhaustive algorithm CYK algorithm about 1080 years (longer than life of universe) about 1 week!
Scientist:
Find an algorithm that can parse strings in any grammar
Engineer:
Design your grammar so it has a very fast parsing algorithm
An example
Stack a ab A T Ta Taa Taab TaA TaT TaTb TA T Tc S Input abaabbc baabbc aabbc aabbc aabbc abbc bbc bc bc bc c c c
Action
shift shift reduce (5) reduce (3) shift shift shift reduce (5) reduce (3) shift reduce (4) reduce (2) shift reduce (1)
input: abaabbc
S T T A a b A T A
Items
S Tc(1) S Tc S Tc S Tc
Stack a ab A T Ta
T A T A
Action
shift shift reduce (5) reduce (3) shift shift
Some terminology
Stack a ab A T Ta Taa Taab TaA TaT TaTb TA T Tc S Input abaabbc baabbc aabbc aabbc aabbc abbc bbc bc bc bc c c c
Action
shift shift reduce (5) reduce (3) shift shift shift reduce (5) reduce (3) shift reduce (4) reduce (2) shift reduce (1)
input: abaabbc
handle
valid items: aTb, ab valid items: Ta, Tc, aTb
shift
reduce
A aAb | ab
A aAb aabb
A aAb | ab
A aAb aabb
is updated to
A abb
disappears if X b
After these updates, for every valid item A aCb and production C d, we also add C d as a valid item
C d
Example
A aAb | ab
a A A aAb b A aAb A ab b A ab
A aAb q0
A aAb
A ab a
5 A aAb
b
3 A ab
die
states correspond to sets of valid items transitions are labeled by variables / terminals
A aAb | ab
A aAb aabb
A aAb | ab
A aAb aabb
Example: PDA for w#wR is deterministic, but PDA for wwR is not
reduce (R)
S / R conflict
R / R conflict
LR() grammars
to be continued java
LR(1) grammars LR(0) grammars
parse using LR(0) algorithm perl python