[antlr-interest] Problem of having "+" or "-" used both as number signs and operator symbols

Johannes Luber jaluber at gmx.de
Wed Jun 6 02:20:30 PDT 2007


YiQing Yang wrote:
> Hi,
> 
>  
> 
> I need to write a grammar for a general form of expression where the
> operator symbols are not predefined and operators semantics are not
> built into the grammar . Basically the operator symbols could be any
> combination of characters from a fixed set of symbolic characters.
> 
>  
> 
> Now I have a problem for the “+” or “-“. In the parser, they are string
> literals used as signs of integer or float ; In the lexer, they are
> SYMBOL_CHARCTERs intentionly to form unary or binary operator symbols
> “+” or “-“. But I found that once they are defined as string literals,
> they can never be recognized as OPERATOR_SYMBOL. In other words, in the
> following grammar, 3+4 is not a valid expression any more.
> 
>  
> 
> Any idea how could I solve this problem? Please help!

Usually this kind of problem is caused by a wrong ordering of the
grammar rules. In your case I would move the lexer rules to the top and
try again.

Best regards,
Johannes Luber


More information about the antlr-interest mailing list