[antlr-interest] How to handle negation...

Pete Forman pete.forman at westerngeco.com
Wed Oct 29 01:18:32 PST 2003


At 2003-10-28 16:23 -0700, Jeff Vincent wrote:
>I am trying to work around an issue in my tree parser with number
>negation.

Here is a fragment of a grammar that generates a single token for a
negative number.

multiplicative_expression
   : unary_expression ((TIMES^|DIVIDE^|MODULO^) unary_expression)*
   ;

unary_expression
   : (unary_primitive_expression) => unary_primitive_expression
   | MINUS^ {#MINUS.setType(UNARY_MINUS);} unary_expression
   | PLUS^  {#PLUS.setType(UNARY_PLUS);} unary_expression
   | NOT^ unary_expression
   | primitive_expression
   ;

unary_primitive_expression
   : MINUS! i:INTEGER_LITERAL {#i.setText("-" + #i.getText());}
   | MINUS! r:REAL_LITERAL    {#r.setText("-" + #r.getText());}
   | PLUS! INTEGER_LITERAL    // discard PLUS token
   | PLUS! REAL_LITERAL       // discard PLUS token
   ;

primitive_expression
   : LPAREN! expression RPAREN!
   | function_call
   | literal
   ;

-- 
Pete Forman                -./\.-  Disclaimer: This post is originated
WesternGeco                  -./\.-   by myself and does not represent
pete.forman at westerngeco.com    -./\.-   opinion of Schlumberger, Baker
http://petef.port5.com           -./\.-   Hughes or their divisions.


 

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/ 




More information about the antlr-interest mailing list