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

YiQing Yang yiqing at objectivity.com
Tue Jun 5 16:39:21 PDT 2007


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!

 

Thanks a lot,

 

Yiqing

 

 

----------------------------------------------------------------------------
-----------------------------------------------------

Parser Rules:

 

expression

  : unary_operator_exp

  | (atom_expression OPERATOR_SYMBOL) => binary_operator_exp

  | atom_expression

  ;

 

atom_expression

  : literal_expression

  | LPAREN expression RPAREN

  ;

 

unary_operator_exp

  :  OPERATOR_SYMBOL atom_expression 

  ;

 

binary_operator_exp

  :  atom_expression bo:OPERATOR_SYMBOL atom_expression 

  ;

 

literal_expression

  : unsigned_integer_literal

  | integer_literal 

  | float_literal 

  | bool_literal 

  | string_literal 

  ;

 

unsigned_integer_literal :  DECIMAL ;

 

integer_literal: ("+" | "-" ) DECIMAL;

 

float_literal: ("+" | "-" )? FLOAT;

 

.

 

Lexer Rules:

 

OPERATOR_SYMBOL: (SYMBOL_CHARCTER)+;

 

protected 

SYMBOL_CHARCTER: '+' | '-' | '*' | '%' | '=' | '<' | '>' | '|' | '~' | '@' |
'&' | '#' | '$'  | '^';

 

----------------------------------------------------------------------------
-------------------------

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20070605/41d0fc48/attachment.html 


More information about the antlr-interest mailing list