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

  1. %Options la=2,gp=java,act,an=bnfact.java,hn=bnfhdr.java,tab=space,fp=bnf,prefix=TK_,
  2. %Options nogoto-default,output-size=125,names=max,error-maps
  3. %Define
  4. -- This software is subject to the terms of the IBM Jikes Parser
  5. -- Generator License Agreement available at the following URL:
  6. -- http://www.ibm.com/research/jikes.
  7. -- Copyright (C) 1983, 1999, International Business Machines Corporation
  8. -- and others.  All Rights Reserved.
  9. -- You must accept the terms of that agreement to use this software.
  10.  
  11. -- This grammar has been augmented with productions that captures
  12. -- most errors that a user is likely to make. This saves the need
  13. -- to have an error recovery system.
  14.  
  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.     SYMBOL PRODUCES OR EOF ERROR
  36.  
  37. %Alias
  38.  
  39.     '::='  ::= PRODUCES
  40.     '|'    ::= OR
  41.     %EOF   ::= EOF
  42.     %ERROR ::= ERROR
  43.  
  44. %Rules
  45. /:
  46. class bnfhdr extends bnfact
  47. {
  48.     Action rule_action[] = {
  49.         null, // no element 0
  50. :/
  51.  
  52. /.
  53. class bnfact
  54. {
  55.     Parser parser;
  56.  
  57.     bnfact(Parser parser)
  58.     {
  59.         this.parser = parser;
  60.     }
  61.  
  62.     void print_rule(int rule_no)
  63.     {
  64.         String rule = new String();
  65.         rule = parser.name[parser.non_terminal_index[parser.lhs[rule_no]]] + " ::=";
  66.         if (parser.rhs[rule_no] == 0)
  67.             rule += " %empty";
  68.         else
  69.         {
  70.             for (int i = 1; i <= parser.rhs[rule_no]; i++)
  71.             {
  72.                 int non_term = parser.SYM(i),
  73.                     term = parser.lex_stream.Kind(parser.TOKEN(i));
  74.                 rule += (" " + (non_term == 0 ? parser.name[parser.terminal_index[term]]
  75.                                               : parser.name[parser.non_terminal_index[non_term]]));
  76.             }
  77.         }
  78.  
  79.         System.out.println("Reducing rule number " + rule_no + ": " + rule);
  80.  
  81.         return;
  82.     }
  83.  
  84.     interface Action
  85.     {
  86.         public void action();
  87.     }
  88.  
  89.     final class NoAction implements Action
  90.     {
  91.         public void action() {}
  92.     }
  93.  
  94.     final class NullAction implements Action
  95.     {
  96.         public void action()
  97.         {
  98.             System.out.println("A null production");
  99.         }
  100.     }
  101. ./
  102.  
  103. bnf ::= %empty
  104.   /:        new act%rule_number(),:/
  105.   /.
  106.     // 
  107.     // Rule %rule_number:  %rule_text
  108.     //
  109.     final class act%rule_number implements Action
  110.     {
  111.         public void action()
  112.         {
  113.             print_rule(%rule_number);
  114.             parser.setSYM1((int) parser.lhs[%rule_number]);
  115.         }
  116.     }
  117.   ./
  118.  
  119. bnf ::= bnf rules
  120.   /:        new act%rule_number(),:/
  121.   /.
  122.     // 
  123.     // Rule %rule_number:  %rule_text
  124.     //
  125.     final class act%rule_number implements Action
  126.     {
  127.         public void action()
  128.         {
  129.             print_rule(%rule_number);
  130.             parser.setSYM1((int) parser.lhs[%rule_number]);
  131.         }
  132.     }
  133.   ./
  134.  
  135. rules ::= rule
  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.             print_rule(%rule_number);
  146.             parser.setSYM1((int) parser.lhs[%rule_number]);
  147.         }
  148.     }
  149.   ./
  150.  
  151. rules ::= rules '|' symbol_list
  152.   /:        new act%rule_number(),:/
  153.   /.
  154.     // 
  155.     // Rule %rule_number:  %rule_text
  156.     //
  157.     final class act%rule_number implements Action
  158.     {
  159.         public void action()
  160.         {
  161.             print_rule(%rule_number);
  162.             parser.setSYM1((int) parser.lhs[%rule_number]);
  163.         }
  164.     }
  165.   ./
  166.  
  167. rule ::= SYMBOL '::=' symbol_list
  168.   /:        new act%rule_number(),:/
  169.   /.
  170.     // 
  171.     // Rule %rule_number:  %rule_text
  172.     //
  173.     final class act%rule_number implements Action
  174.     {
  175.         public void action()
  176.         {
  177.             print_rule(%rule_number);
  178.             parser.setSYM1((int) parser.lhs[%rule_number]);
  179.         }
  180.     }
  181.   ./
  182.  
  183. symbol_list ::= %empty
  184.   /:        new act%rule_number(),:/
  185.   /.
  186.     // 
  187.     // Rule %rule_number:  %rule_text
  188.     //
  189.     final class act%rule_number implements Action
  190.     {
  191.         public void action()
  192.         {
  193.             print_rule(%rule_number);
  194.             parser.setSYM1((int) parser.lhs[%rule_number]);
  195.         }
  196.     }
  197.   ./
  198.  
  199. symbol_list ::= symbol_list SYMBOL
  200.   /:        new act%rule_number(),:/
  201.   /.
  202.     // 
  203.     // Rule %rule_number:  %rule_text
  204.     //
  205.     final class act%rule_number implements Action
  206.     {
  207.         public void action()
  208.         {
  209.             print_rule(%rule_number);
  210.             parser.setSYM1((int) parser.lhs[%rule_number]);
  211.         }
  212.     }
  213.   ./
  214.  
  215. /:
  216.     };
  217.  
  218.     bnfhdr(Parser parser)
  219.     {
  220.         super(parser);
  221.     }
  222. }
  223. :/
  224.  
  225. /.
  226. }
  227. ./
  228. %End
  229.  
  230.  
  231.