I'm trying to realize a Java-Python translator. I'm using flex to recognize some java tokens. I managed integer number with this regular expression:
...
digit [0-9]
number (\+|\-)?{digit}+
...
%%
...
{number} {yylval.i= atoi (yytext);return(NUMBER);}
"+" {return (ADD);}
In the parser I define this production :
ArithmeticExpression
: ExpressionStatement ADD ExpressionStatement
| ExpressionStatement SUB ExpressionStatement
| ExpressionStatement MULT ExpressionStatement
| ExpressionStatement DIV ExpressionStatement
| ExpressionStatement MOD ExpressionStatement
;
ExpressionStatement
: NUMBER
;
if I give in input to translator expressions like this:
int a = 5 ++67; (syntax error in java)
how can I manage this situation so that 5 ++67 is recognized like an error and not like 5 + +67 (therefor an ArithmeticExpression) by the translator ?
As has already been said in the comments, you need to introduce a ++
token.
You say that you don't need to handle unary operators. Even so, you still need the token. 5 ++67
is an error in Java exactly because ++
is its own token. If it weren't, 5 ++67
would be equivalent to 5 + +67
. So if you want to get the same error as in Java in this case, you also need to have a ++
token - even if you never use it.