2

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!

Ayohaych
  • 5,099
  • 7
  • 29
  • 51
  • 1
    FYI: +++++----+++6 would be a valid fragment – Paperwaste Dec 09 '13 at 23:19
  • This assignment has been discussed in several recent stack overflow questions. Check out questions http://stackoverflow.com/questions/20287086/left-factoring-removing-left-recursion-javacc and http://stackoverflow.com/questions/20364288/left-recursion-elimination-in-an-ll1-grammar . – Theodore Norvell Dec 10 '13 at 13:10

2 Answers2

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

Conor Murphy
  • 90
  • 1
  • 8
1

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

Paperwaste
  • 665
  • 2
  • 6
  • 14
  • Thanks for the reply. I tried what you said and the error disapered, however I get 4 warnings now as follows: Warning: Choice conflict involving two expansions at line 243, column 11 and line 244, column 11 respectively. A common prefix is: Consider using a lookahead of 2 for earlier expansion. They are all similar to this, another one mentions a common prefix being if and not. Any ideas what this is telling me? – Ayohaych Dec 09 '13 at 23:43
  • 1
    No sorry not able to help you there i'm not familiar with .jj i was only able to help your recursion problem because i have a little experience with haskell. Goodluck – Paperwaste Dec 09 '13 at 23:57