On 03/21/2011 12:26 AM, Wojciech Tomasz Cichon wrote:
> i have in my grammar rules:
> 
> stmt :
> | ident '=' lexp SEMI  -> ^(SET ident lexp);
> 
> 
> factor  : 
>       '-'?  (NUMBER |ident )^
>         ....
>        ;
> 
> and
> lexp : term (SIMOP^  lexp)?;
> term  : factor (OP^  term)?;
> 
> OP :  '*' | '/' | '%';
> SIMOP : '+' | '-';
> 
> and i tried it on different inputs
> and for
> ID = –5; , ID = 5+3; etc it works, and it build correct tree
> bur if i’ll try 
> ID = 5-3;
> i get error: 
> mismatched input '-' expecting SEMI
> 
> i’m using options:
> options {
>   language = Java;
>   output = AST;
>     k  = 3;
> }
> 
> can anyone tell me what i should fix?

'-' can't be both used in a factor in the parser and converted to a
SIMOP in the lexer.  You can't have it both ways.

lexp : term ( ('+'^ | '-'^) lexp)?;
term : factor ( ('*'^ | '/'^ | '%'^) factor )?;

may be closer to what you want, but I think you really want:

lexp    : term ( ( PLUS^ | MINUS^ ) term )*
        ;
term    : factor ( ( STAR^ | SLASH^ | PERCENT^ ) factor)*
        ;
factor  : ( MINUS^ )? ( NUMBER | ident)
        ;

PLUS    : '+';
MINUS   : '-';
STAR    : '*';
SLASH   : '/';
PERCENT : '%';

(Change the token names as you see fit.)

If you do it this way, '-' is always a MINUS token in the lexer and you
can use MINUS in different rules in the parser.  (Note that your token
definitions of OP and SIMOP get expanded into the subrules in the
parser, which is necessary of you want them to be the roots of your
generated AST.)

> regards

-- 
Kevin J. Cummings
[email protected]
[email protected]
[email protected]
Registered Linux User #1232 (http://counter.li.org)

List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe: 
http://www.antlr.org/mailman/options/antlr-interest/your-email-address

-- 
You received this message because you are subscribed to the Google Groups 
"il-antlr-interest" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/il-antlr-interest?hl=en.

Reply via email to