Note that it is not "8" that is causing the problem, but "8;". Although "8" is an INT_CONST, "8;" is not. So the longest match rule is kicking in and some other token production that does match "8;" is winning. See the FAQ http://www.engr.mun.ca/~theo/JavaCC-FAQ/ question 3.3. Without seeing all of your .jj file, I can't tell you which token it is, but if you put a break point on the code that constructs the error message you can easily see what the .kind field of the unexpected token holds.
JavaCC: encountered... Was expecting one of... error
-
06-08-2022 - |
Question
I'm getting this error when trying to run a parser written in JavaCC on a sample (syntactically valid) file:
Exception in thread "main" ParseException: Encountered "8;" at line 13, column 17.
Was expecting one of:
<INT_CONST> ...
"<" ...at jimpleParser.generateParseException(jimpleParser.java:2421)
at jimpleParser.jj_consume_token(jimpleParser.java:2292)
at jimpleParser.expr(jimpleParser.java:1038)
(shortened for conciseness)
I cannot work out why it is throwing an error. "8" should be a valid token. Here is the function in question:
String expr():
{
Token t1 = null, t2 = null;
String f1 = null, f2 = null, f3 = null;
}
{
(LOOKAHEAD(3)
f1 = imm() {System.out.println(f1);}
| f1 = new_expr()
| t1 = <LBR> f2 = nonvoid_type() t2 = <RBR> f3 = imm()
{f1 = t1.image.concat(f2.concat(t2.image.concat(f3)));}
| LOOKAHEAD(2)
f2 = imm() t1 = <INSTANCEOF> f3 = nonvoid_type()
{f1 = f2.concat(t1.image.concat(f3));}
| f1 = invoke_expr()
| LOOKAHEAD(2)
f1 = reference()
| LOOKAHEAD(2)
f1 = binop_expr()
| f1 = unop_expr())
{return f1;}
}
which should in turn call imm shown here:
String imm():
{
String f1 = null;
}
{
(f1 = constant()
| f1 = local_name())
{return f1;}
}
which should in turn call constant shown here:
String constant():
{
Token t1 = null, t2 = null;
String f1 = null;
}
{
(t1 = <INT_CONST> {f1 = t1.image; System.out.println(f1);}
| t1 = <FLOAT_CONST> {f1 = t1.image;}
| t1 = <MIN_INT_CONST> {f1 = t1.image;}
| t1 = <MIN_FLOAT_CONST> {f1 = t1.image;}
| t1 = <STRING_CONST> {f1 = t1.image;}
| t1 = <CLASS> t2 = <STRING_CONST> {f1 = t1.image.concat(t2.image);}
| t1 = <NULL> {f1 = t1.image;})
{return f1;}
}
8 should be an INT_CONST. Relevant token specifications are shown here:
<INT_CONST: ((<OCT_CONST> | <DEC_CONST> | <HEX_CONST>) ("L")?)>
<DEC_CONST: (<DEC_DIGIT>)+>
<DEC_DIGIT: ["0"-"9"]>
Any help would be much appreciated. Thanks
Solution
OTHER TIPS
Try this in your token section, it would definitely solve your problem:
TOKEN:
{
<DEC_DIGIT: (["0"-"9"])>
|
<INT_CONST: ((<OCT_CONST> | <DEC_CONST> | <HEX_CONST>) ("L")?)>
|
<DEC_CONST: (<DEC_DIGIT>)+>
// The rest of your tokens....
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow