fragment ... -> e..." /> fragment ... -> e..." /> fragment ... -> e..."/>

Left recursion detected in .jj file

1.9k views Asked by At

Not sure what is causing this really. When I try to compile the file I get an error saying "Left recursion detected expression... -> fragment ... -> expression.

The area of code that has this is this section

void statement() : {}

{
    identifier() <ASSIGN> expression()
    | identifier() <ASSIGN> <STRING>
    | <EMARK> expression()
    | <QMARK> identifier()
    | identifier(arg_list())
    | <BEGIN>(statement() <SEMIC>)+ <END>
    | <IF> condition() <THEN> statement()
    | <IF> condition() <THEN> statement() <ELSE> statement()
    | <WHILE> (condition()) <DO> statement()
    | {}
}

void expression () : {}
{
    fragment()((<PLUS_SIGN> | <MINUS_SIGN> | <MULT_SIGN> | <DIV_SIGN>) fragment())*
}

void fragment () : {}
{
    identifier() | <NUM> | (<PLUS_SIGN> | <MINUS_SIGN>)fragment() | expression()
}

I'm not really sure how to go about fixing this problem and would appreciate any help with it! Thanks!

2

There are 2 answers

0
Conor Murphy On BEST ANSWER
void fragment() #void : {}
|<LBR> expression() <RBR>

Change the fragment production rule, where it has expression to include brackets on either side. This should solve your recursion problem.

2
Paperwaste On

I think your abstraction is just a little off.

(<PLUS_SIGN> | <MINUS_SIGN>)fragment()

is more of an expression which can negate a fragment

I would consider

void expression () : {}
{
    fragment()((<PLUS_SIGN> | <MINUS_SIGN> | <MULT_SIGN> | <DIV_SIGN>) fragment())*
    | <MINUS_SIGN> fragment()
    | <PLUS_SIGN> fragment()
}

I also belive that fragment does not need to be an expression() either the recursion is already encapsulated by ((<PLUS_SIGN> | <MINUS_SIGN> | <MULT_SIGN> | <DIV_SIGN>) fragment())* being able to repeat