home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / jikepg12.zip / jikespg / examples / expr / expr.g < prev    next >
Encoding:
Text File  |  1999-01-27  |  5.1 KB  |  251 lines

  1. %Options gp=java,act,tab,an=expract.java,hn=exprhdr.java,tab=space,fp=expr,prefix=TK_,
  2. %Options nogoto-default,output-size=125,name=max,error-maps
  3. %Define
  4.  
  5. -- This software is subject to the terms of the IBM Jikes Parser
  6. -- Generator License Agreement available at the following URL:
  7. -- http://www.ibm.com/research/jikes.
  8. -- Copyright (C) 1983, 1999, International Business Machines Corporation
  9. -- and others.  All Rights Reserved.
  10. -- You must accept the terms of that agreement to use this software.
  11.  
  12. -- This grammar has been augmented with productions that captures
  13. -- most errors that a user is likely to make. This saves the need
  14. -- to have an error recovery system.
  15. -- 
  16. -- This macro is used to initialize the rule_action array
  17. -- to the null_action function.
  18. --
  19. %null_action
  20. /.
  21.         new NullAction(),
  22. ./
  23.  
  24. -- 
  25. -- This macro is used to initialize the rule_action array
  26. -- to the no_action function.
  27. --
  28. %no_action
  29. /.
  30.         new NoAction(),
  31. ./
  32.  
  33. %Terminals
  34.  
  35.     PLUS MINUS STAR SLASH NUMBER LPAREN RPAREN
  36.     EOF ERROR
  37.  
  38. %Alias
  39.  
  40.     '+'  ::= PLUS
  41.     '-'  ::= MINUS
  42.     '*'  ::= STAR
  43.     '/'  ::= SLASH
  44.     '('  ::= LPAREN
  45.     ')'  ::= RPAREN
  46.  
  47.     %EOF   ::= EOF
  48.     %ERROR ::= ERROR
  49.  
  50. %Rules
  51. /:
  52. class exprhdr extends expract
  53. {
  54.     Action rule_action[] = {
  55.         null, // no element 0
  56. :/
  57.  
  58. /.
  59. class expract
  60. {
  61.     Parser parser;
  62.  
  63.     expract(Parser parser)
  64.     {
  65.         this.parser = parser;
  66.     }
  67.  
  68.     interface Action
  69.     {
  70.         public void action();
  71.     }
  72.  
  73.     final class NoAction implements Action
  74.     {
  75.         public void action() {}
  76.     }
  77.  
  78.     final class NullAction implements Action
  79.     {
  80.         public void action() { parser.setSYM1(null); }
  81.     }
  82.  
  83. ./
  84.  
  85. Goal ::= %empty
  86.   /:%null_action:/
  87.  
  88. Goal ::= Expression
  89.   /:%no_action:/
  90.  
  91. Expression ::= Expression '+' Term
  92.   /:        new act%rule_number(),:/
  93.   /.
  94.     // 
  95.     // Rule %rule_number:  %rule_text
  96.     //
  97.     final class act%rule_number implements Action
  98.     {
  99.         public void action()
  100.         {
  101.             AstPlus node = new AstPlus();
  102.             node.left  = parser.SYM(1);
  103.             node.op    = parser.TOKEN(2);
  104.             node.right = parser.SYM(3);
  105.  
  106.             parser.setSYM1(node);
  107.             return;
  108.         }
  109.     }
  110.   ./
  111.  
  112. Expression ::= Expression '-' Term
  113.   /:        new act%rule_number(),:/
  114.   /.
  115.     // 
  116.     // Rule %rule_number:  %rule_text
  117.     //
  118.     final class act%rule_number implements Action
  119.     {
  120.         public void action()
  121.         {
  122.             AstMinus node = new AstMinus();
  123.             node.left  = parser.SYM(1);
  124.             node.op    = parser.TOKEN(2);
  125.             node.right = parser.SYM(3);
  126.  
  127.             parser.setSYM1(node);
  128.         }
  129.     }
  130.   ./
  131.  
  132. Expression ::= Term
  133.   /:%no_action:/
  134.  
  135. Term ::= Term '*' Factor
  136.   /:        new act%rule_number(),:/
  137.   /.
  138.     // 
  139.     // Rule %rule_number:  %rule_text
  140.     //
  141.     final class act%rule_number implements Action
  142.     {
  143.         public void action()
  144.         {
  145.             AstStar node = new AstStar();
  146.             node.left  = parser.SYM(1);
  147.             node.op    = parser.TOKEN(2);
  148.             node.right = parser.SYM(3);
  149.  
  150.             parser.setSYM1(node);
  151.         }
  152.     }
  153.   ./
  154.  
  155. Term ::= Term '/' Factor
  156.   /:        new act%rule_number(),:/
  157.   /.
  158.     // 
  159.     // Rule %rule_number:  %rule_text
  160.     //
  161.     final class act%rule_number implements Action
  162.     {
  163.         public void action()
  164.         {
  165.             AstSlash node = new AstSlash();
  166.             node.left  = parser.SYM(1);
  167.             node.op    = parser.TOKEN(2);
  168.             node.right = parser.SYM(3);
  169.  
  170.             parser.setSYM1(node);
  171.         }
  172.     }
  173.   ./
  174.  
  175. Term ::= Factor
  176.   /:%no_action:/
  177.  
  178. Factor ::= NUMBER
  179.   /:        new act%rule_number(),:/
  180.   /.
  181.     // 
  182.     // Rule %rule_number:  %rule_text
  183.     //
  184.     final class act%rule_number implements Action
  185.     {
  186.         public void action()
  187.         {
  188.             AstNumber node = new AstNumber();
  189.             node.token = parser.TOKEN(1);
  190.             node.value = Integer.parseInt(parser.lex_stream.Name(node.token));
  191.  
  192.             parser.setSYM1(node);
  193.         }
  194.     }
  195.   ./
  196.  
  197. Factor ::= '-' NUMBER
  198.   /:        new act%rule_number(),:/
  199.   /.
  200.     // 
  201.     // Rule %rule_number:  %rule_text
  202.     //
  203.     final class act%rule_number implements Action
  204.     {
  205.         public void action()
  206.         {
  207.             AstNegativeNumber node = new AstNegativeNumber();
  208.             node.op = parser.TOKEN(1);
  209.             node.token = parser.TOKEN(2);
  210.             node.value = - Integer.parseInt(parser.lex_stream.Name(node.token));
  211.  
  212.             parser.setSYM1(node);
  213.         }
  214.     }
  215.   ./
  216.  
  217. Factor ::= '(' Expression ')'
  218.   /:        new act%rule_number(),:/
  219.   /.
  220.     // 
  221.     // Rule %rule_number:  %rule_text
  222.     //
  223.     final class act%rule_number implements Action
  224.     {
  225.         public void action()
  226.         {
  227.             AstParen node = new AstParen();
  228.             node.expression = parser.SYM(2);
  229.  
  230.             parser.setSYM1(node);
  231.         }
  232.     }
  233.   ./
  234.  
  235. /:
  236.     };
  237.  
  238.     exprhdr(Parser parser)
  239.     {
  240.         super(parser);
  241.     }
  242. }
  243. :/
  244.  
  245. /.
  246. }
  247. ./
  248. %End
  249.  
  250.  
  251.