SHARE
TWEET

Untitled

a guest Mar 20th, 2019 74 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. tree grammar TExpr1;
  2.  
  3. options {
  4.   tokenVocab=Expr;
  5.  
  6.   ASTLabelType=CommonTree;
  7.  
  8. }
  9.  
  10. @header {
  11.  package liczydlo_226162;
  12.  import java.util.HashMap;
  13. }
  14.  
  15. @members  {
  16.   HashMap variables = new HashMap();
  17. }
  18.  
  19. prog    : (e=expr {System.out.println ($e.out);})* ;
  20.  
  21. expr returns [Integer out]
  22.         : ^(PLUS  e1=expr e2=expr) {$out = $e1.out + $e2.out;}
  23.         | ^(MINUS e1=expr e2=expr) {$out = $e1.out - $e2.out;}
  24.         | ^(MUL   e1=expr e2=expr) {$out = $e1.out * $e2.out;}
  25.         | ^(DIV   e1=expr e2=expr) {$out = $e1.out / $e2.out;}
  26.         | ^(POW   e1=expr e2=expr) {$out = (int)Math.pow($e1.out, $e2.out);}
  27.         | ^(PODST i1=ID   e2=expr) {variables.put($i1.text, $e2.out); $out =$e2.out;}
  28.         //| ^(DEC   i1=DEC  i2=ID  ) {variables.put($i1.text, 0); $out =0;}
  29.         | INT                      {$out = Integer.parseInt($INT.text);}
  30.         | ID                       {$out = (Integer)variables.get($ID.text);}
  31.         ;
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top