Вы находитесь на странице: 1из 5

Parsing (()), () () with M2

(())
l
l
l
l
l
l
l

Start in qo
Push $ on top of stack (initialisation), move to q1
Consume ( and push ( , stay in q1
Consume ( and push ( , stay in q1
Consume ) and pop (, move to q2
Consume ) and pop (, stay in q2
Pop $ from stack, move to q3

()()
l
l
l
l
l
l
l

Start in qo
Push $ on top of stack (initialisation), move to q1
Consume ( and push ( , stay in q1
Consume ) and pop ( , move to q2
Consume ( and push ( , move to q1
Consume ) and pop ( , stay in q2
Pop $ from stack, move to q3
COMP2022 sem1, 2015, Kalina Yacef

11

Lookahead symbols
Lets consider a grammar with 2 rules for the start symbol
S-> XY | YZ
(other rules of the grammar not shown)

In a derivation, which rule to choose to replace the variable S?


S=>XY or S=>YZ?
Looking ahead in the input string helps choosing the rule to use
Suppose that XY can only derive strings starting with a
And YZ can only derive strings starting with b or c
by looking ahead one symbol, we know which rule to select
eg If we need to derive abc : S=> XY
If we need to derive cab : S=> YZ

COMP2022 sem1, 2015, Kalina Yacef

24

Examples calculating FIRST sets


B (B) B |

FIRST( (B) B) = { ( }
FIRST ( B ) = FIRST( (B)B ) FIRST() ={ (, }
S BC | a
B bB |
C cC |
FIRST() = {}
FIRST(c) = {c}
FIRTST(cC) = {c}
FIRST(C) = FIRST (cC) FIRST() = {c, }
FIRST(B) = FIRST (bB) FIRST() ={b, }
FIRST(S) = FIRST(BC) FIRST(a)= {a,b,c, }
COMP2022 sem1, 2015, Kalina Yacef

35

Examples calculating FOLLOW sets


S BC | a
B bB |
C cC |
FOLLOW(S)= {$}
FOLLOW(B)= {c, $ }
FOLLOW(C)= {$}
B (B) B |
FOLLOW( B ) = { ), $ }
COMP2022 sem1, 2015, Kalina Yacef

39

Example of a Parsing Table


S BC
Sa
B bB
B
C cC
C

FIRST(BC) = {b, c, }
FIRST(a) = {a}
FIRST(bB) = {b}
FIRST() = {}
FOLLOW(B)={c,$}
FIRST(cC) = {c}
FIRST() = {}
FOLLOW(C)={$}
a

Sa

B
C
COMP2022 sem1, 2015, Kalina Yacef

S BC

S BC

S BC

B bB

C cC

42

Вам также может понравиться