0

I have two inputs

  1. a - b
  2. a += b

And I have a production with a choice

void AssignmentExpression() : {}
{
  LOOKAHEAD(3) ConditionalExpression()
| LOOKAHEAD(3) UnaryExpression() AssignmentOperator() AssignmentExpression()
}

With this production input (1) works, but input (2) does not work.

If I swap the choice in the production so that it becomes

void AssignmentExpression() : {}
{
  LOOKAHEAD(3) UnaryExpression() AssignmentOperator() AssignmentExpression()
| LOOKAHEAD(3) ConditionalExpression()
}

Then input (2) works, but input (1) does not work.

How do I fix this? Increasing the LOOKAHEAD parameter does not help.

2 Answers2

0

See Expression Parsing by Recursive Descent. Follow the "classic solution".

Since you are using JJTree, the answer to the question Make a calculator's grammar that make a binary tree with javacc will be helpful.

Community
  • 1
  • 1
Theodore Norvell
  • 15,366
  • 6
  • 31
  • 45
0

You might try

void AssignmentExpression() : {}
{
  LOOKAHEAD(UnaryExpression() AssignmentOperator() )
  UnaryExpression() AssignmentOperator() AssignmentExpression()
| ConditionalExpression()
}

Without seeing more of the grammar it is hard to know whether this will work. Since the use of the lookahead specification will suppress any warnings from JavaCC --JavaCC "assumes" you know what you are doing-- you have to do the analysis yourself.

My other answer is better.

Theodore Norvell
  • 15,366
  • 6
  • 31
  • 45