Question

I have very simple grammar:

E->E+T|T
T->T*F|F
F->(E)|id

And i want to extend it to support unar operations(IMHO this is correct grammar but it may be wrong because i'm real n00b in grammars, parsers, lexers, etc.):

E->E+T|T
T->T*F|F
F->+F|(E)|id

The real problem comes when i'm trying to update parsing table: enter image description here

So the question is how should i edit this table to provide unary operations support(based on described grammar) ?

P.S. Anyway, i will be very appreciate for any help with parsing ariphmetic expression in Java(or any other OO language) using LR(k)(or LALR) parser ^_^

P.S.2. Parser generators are not suitable in this case.

Was it helpful?

Solution

To understand how to adjust the parsing table, you need to know what the LR item sets are for each state, and understand how your addition to the grammar changes the item sets -- each new partial parse item in a state's item set gives you an additional action for the parsing table. For example, in state 7, your original item set (prior to adding the F -> + F rule) was:

T -> T * . F
F -> . ( E )
F -> . id

(I'm ignoring the lookahead in this case as it doesn't matter, but for general LR(k) you need to keep track of it.)

So your new rule adds the item F -> . + F, which gives you a new action for state 7 (shift on an input of +.) This in turn gives you an entirely new state for your table.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top