简体   繁体   中英

LL(1) Parsing table with grammar

I'm trying to get LL(1) Parsing Table with grammar below,

S -> ( L ) | a
L -> L , S | S

I think it has indirect left recursion because of this,

L -> S

so I change it to,

S -> ( L ) | a
L -> L , ( L ) | L , a | ( L ) | a

then like this,

S -> ( L ) | a
L -> ( L ) L' | aL'
L' -> , ( L ) L' | ,aL' | epsilon

with this, I get FIRSTs and FOLLOWs

FIRST(S) = { (, a }          FOLLOW(S) = { $ }
FIRST(L) = { (, a }          FOLLOW(L) = { ) }
FIRST(L') = { , , epsilon}   FOLLOW(L') = { ) }

But when I draw LL parsing table, It doesn't go to $.

Did I make mistake or misunderstanding?

(My parsing theory is rusty, so I may have made some mistakes here.)

L -> S is part of indirect recursion, but not left recursion. The production can only "expand" into L ->+ ( L ) or L ->+ a , both of which start with a terminal. The only left recursion here is in the production L -> L , S . After removing this direct left recursion , I end up with the following grammar:

S -> ( L ) | a
L -> S L'
L' -> , S L' | ε

With this, the FIRST and FOLLOW sets you have computed are the same, except that FOLLOW(S) is incomplete. Besides $ (because S is the start state), FOLLOW(S) must include all elements of FIRST(L') (because L -> S L' and L' -> , S L' ) and FOLLOW(L') (because L' -> ε ). I get FOLLOW(S) = { $, ,, ), ε } .

The parsing table I get is as follows:

    |  (     |  a     |  ,       |  )  |  $  |
----+--------+--------+----------+-----+-----+
S   |  L )   |  a     |          |     |     |
L   |  S L'  |  S L'  |          |     |     |
L'  |        |        |  , S L'  |  ε  |     |

I'm not sure what you mean by "it doesn't go to $ ". All I can say is that the column $ is empty, because all productions of S are non-empty and end in a terminal.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM