[antlr-interest] a silly question?
Bryan Ewbank
ewbank at gmail.com
Wed Feb 1 14:26:51 PST 2006
One solution is to assume that "true" is 1, and "false" is zero, then keep
the declarations as written. If this is unacceptable - i.e., if you want to
support a boolean type - then you must provlde a value object that can hold
either an integer or a boolean.
You must also decide if it's an error to see "10 AND 20" or "true +
false"...
On 2/1/06, Ëï¼Í¸Õ <sunjigang1965 at yahoo.com.cn> wrote:
>
>
> If I make extension with boolean type data to Terence's sample programmar
> on http://www.cs.usfca.edu/~parrt/course/652/lectures/antlr.html<http://www.cs.usfca.edu/%7Eparrt/course/652/lectures/antlr.html>
>
> class ExprParser extends Parser;
>
> expr: mexpr ((PLUS|MINUS) mexpr)*
> ;
>
> mexpr
> :  ; atom (STAR atom)*
> ;
>
> atom: INT
> | LPAREN expr RPAREN
> ;
>
> adding atomic data "true" and "false", logical operator AND and OR,
>
> class ExprParser extends Parser;
>
> expr: mexpr ((PLUS|MINUS|OR) mexpr)*
> ;
>
> mexpr
> : atom (STAR|AND atom)*
> ;
>
> atom: INT
> | LPAREN expr RPAREN
> | "true"
> |"false"
> ;
>
>
> What modification should be added to the interpreter
>
> class ExprParser extends Parser;
>
> expr returns [int value=0]
> {int x;}
> : value=mexpr
> ( PLUS x=mexpr {value += x;}
> | MINUS x=mexpr {value -= x;}
> )*
> ;
>
> mexpr returns [int value=0]
> {int x;}
> : value=atom ( STAR x=atom {value *= x;} )*
> ;
>
> atom returns [int value=0]
> : i:INT {value=Integer.parseInt(i.getText());}
> | LPAREN value=expr RPAREN
> ;
>
> to make generated method atom, mexpr and expr polymorphic? That is
>
> class ExprParser extends Parser;
>
> expr returns [? value=?]
> {int x;bool y;}
> : value=mexpr
> ( PLUS x=mexpr {value += x;}
> | MINUS x=mexpr {value -= x;}
> |OR y=mepr {value=value or y;})*
> ;
>
> mexpr returns [? value=?]
> {int x;bool y;}
> : value=atom ( STAR x=atom {value *= x;}
> |AND y=atom {value=y && value;} )*
> ;
>
> atom returns [? value=?]
> : i:INT {value=Integer.parseInt(i.getText());}
> | LPAREN value=expr RPAREN
> |"true" {value=true;}
> |"false" {value=false;}
> ;
>
>
> I did a lot of search on the Internet but have not got the answer.
>
> Jigang
>
>
> ------------------------------
> Ïë³ÉΪ·ëС¸Õ¡¢³Â¿¸è¡¢ÕżÍÖÐÈý´óµ¼ÑݵÄÖ÷½ÇÂ𣿠<http://sostar.cn.yahoo.com>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20060201/a813b9b7/attachment-0001.html
More information about the antlr-interest
mailing list