I have a grammar which I have to use JJTree and JavaCC to create a symbol table and an AST. While I fully understand the sections of my assignment to create the table and tree, the grammar I was given is ambiguous, contains left recursion and indirect left recusion. It also needs to be left factored. I have trawled all over the internet to try find methods that would work for me.
For example:
A ::= Aα | β
can be changed to:
A ::= βA'
A' ::= αA' | ε
But I don't know how to apply this to my grammar.
Here is a section of the production rules I wrote from the grammar that contains the problems aforementioned.
void statement() #STM : {}
{
identifier() <ASSIGNMENT> expression()
| identifier() <ASSIGNMENT> <STRING>
| <EXCLAMATION> expression()
| <QUESTION> identifier()
| identifier() <LBR> arg_list() <RBR>
| <BEGIN> (statement() <SEMIC>)+ <END>
| matched()
| unmatched()
| <WHILE> <LBR> condition() <RBR> <DO> statement()
| {}
}
void matched() #void : {}
{
<IF> condition() <THEN> matched() <ELSE> matched()
}
void unmatched() #void : {}
{
<IF> condition() <THEN> statement()
| <IF> condition() <THEN> matched() <ELSE> unmatched()
}
void expression() #EXPR : {}
{
fragment() ((<PLUS>|<MINUS>|<MULT>|<DIV>) fragment())*
}
void fragment() #FRAG : {}
{
(identifier() | <NUM> | (<PLUS>|<MINUS>) fragment() | expression())
}