Reputation: 69
I'm trying to create a simple parser but I'm having a bit of trouble when trying to take into account the optional sign before the first element:
void expr5() : {}
{
[addop()] expr6() (addop() expr6())*
}
e.g. [-] 5 + 3 - 4
here the issue is with the first optional [addop()] which is defined as:
void addop() : {}
{
<PLUS> | <MINUS>
}
I'm currently getting a choice conflict and recommended to use a LOOKAHEAD() but there is no choice conflict which javacc recognizes when using a LOOKAHEAD()
Thanks in advance!
Upvotes: 1
Views: 160
Reputation: 16221
I think more clarification is needed. I get no conflict with the following
void sum() : {}
{
[ addOp() ]
term()
( addOp() term() )*
}
void addOp() : {}
{ < PLUS > | < MINUS > }
void term() : {}
{
element()
( ( < MULTIPLY > | < DIVIDE > ) element() )*
}
void element() : {}
{
< CONSTANT > | "(" sum() ")"
}
Upvotes: 1
Reputation: 170148
You usually want to give unary-minus and -plus a higher precedence than their binary counterparts. So, I'd imagine you'd want something like this:
// ...
void add() : {}
{
mul() ( <PLUS> mul() | <MINUS> mul() )*
}
void mul() : {}
{
unary() ( <MULTIPLY> unary() | <DIVIDE> unary() )*
}
void unary() : {}
{
<PLUS> atom() | <MINUS> atom() | atom()
}
// ...
which won't need any LOOKAHEAD(...)
.
Upvotes: 0