[antlr-interest] a silly question?

=?gb2312?q?=CB=EF=BC=CD=B8=D5?= sunjigang1965 at yahoo.com.cn
Wed Feb 1 06:30:24 PST 2006


 
  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
   
  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 
   

		
---------------------------------
 Ïë³ÉΪ·ëС¸Õ¡¢³Â¿­¸è¡¢ÕżÍÖÐÈý´óµ¼ÑݵÄÖ÷½ÇÂð£¿
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20060201/ae8bcb01/attachment-0001.html


More information about the antlr-interest mailing list