Search code examples
javarecursioncompilationjavaccleft-recursion

Left recursion detected in .jj file


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!


Solution

  • 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.