Advertisement
KillianMills

ParserInitial.jj

Oct 22nd, 2015
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.81 KB | None | 0 0
  1. /*******************************
  2. ***** SECTION 1 - OPTIONS *****
  3. *******************************/
  4. options { JAVA_UNICODE_ESCAPE = true; }
  5. /*********************************
  6. ***** SECTION 2 - USER CODE *****
  7. *********************************/
  8. PARSER_BEGIN(SLPTokeniser)
  9. public class SLPTokeniser {
  10.     public static void main(String args[]) {
  11.         SLPTokeniser tokeniser;
  12.         if (args.length == 0) {
  13.             System.out.println("Reading from standard input . . .");
  14.             tokeniser = new SLPTokeniser(System.in);
  15.         } else if (args.length == 1) {
  16.             try {
  17.                 tokeniser = new SLPTokeniser(new java.io.FileInputStream(args[0]));
  18.              } catch (java.io.FileNotFoundException e) {
  19.         System.err.println("File " + args[0] + " not found.");
  20.         return;
  21.          }
  22.         }
  23.         else {
  24.             System.out.println("SLP Tokeniser: Usage is one of:");
  25.             System.out.println(" java SLPTokeniser < inputfile");
  26.             System.out.println("OR");
  27.             System.out.println(" java SLPTokeniser inputfile");
  28.         return;
  29.         }
  30.         /*
  31.         * We’ve now initialised the tokeniser to read from the appropriate place,
  32.         * so just keep reading tokens and printing them until we hit EOF
  33.         */
  34.         for (Token t = getNextToken(); t.kind!=EOF; t = getNextToken()) {
  35.             // Print out the actual text for the constants, identifiers etc.
  36.             if (t.kind==NUM)
  37.             {
  38.                 System.out.print("Number");
  39.                 System.out.print("("+t.image+") ");
  40.             }
  41.             else if (t.kind==ID)
  42.             {
  43.                 System.out.print("Identifier");
  44.                 System.out.print("("+t.image+") ");
  45.             }
  46.             else
  47.             System.out.print(t.image+" ");
  48.         }
  49.     }
  50. }
  51. PARSER_END(SLPTokeniser)
  52.  
  53. /*****************************************
  54. ***** SECTION 3 - TOKEN DEFINITIONS *****
  55. *****************************************/
  56. TOKEN_MGR_DECLS :
  57. {
  58.     static int commentNesting = 0;
  59. }
  60.  
  61. SKIP : /*** Ignoring spaces/tabs/newlines ***/
  62. {
  63.     " "
  64.     | "\t"
  65.     | "\n"
  66.     | "\r"
  67.     | "\f"
  68. }
  69.  
  70. SKIP : /* COMMENTS */
  71. {
  72.     "/*" { commentNesting++; } : IN_COMMENT
  73.     |"--" { } : IN_BASIC_COMMENT
  74. }
  75.  
  76. /* ENDS ON -STAR FORWARD SLASH-  ALSO ALLOWS FOR NESTING */
  77. <IN_COMMENT> SKIP :
  78. {
  79.     "/*" { commentNesting++; }
  80.     | "*/" { commentNesting--;
  81.         if (commentNesting == 0)
  82.             SwitchTo(DEFAULT);
  83.         }
  84.     | <~[]>
  85. }
  86.  
  87. /* ENDS ON NEW LINE */
  88. <IN_BASIC_COMMENT> SKIP :
  89. {
  90.     "\n" { SwitchTo(DEFAULT); }
  91. }
  92.  
  93. TOKEN : /* keywords */
  94. {
  95.     < AND : "and" >
  96.     |< BOOL : "bool" >
  97.     |< CONST : "const" >
  98.     |< DO : "do" >
  99.     |< ELSE : "else">
  100.     |< FALSE : "false">
  101.     |< IF : "if" >
  102.     |< INT : "int" >
  103.     |< MAIN : "main" >
  104.     |< NOT : "not" >
  105.     |< OR : "or" >
  106.     |< RETURN : "return" >
  107.     |< THEN : "then" >
  108.     |< TRUE : "true" >
  109.     |< VAR : "var" >
  110.     |< VOID : "void" >
  111.     |< WHILE : "while" >
  112.     |< BEGIN : "begin" >
  113.     |< END : "end" >
  114.  
  115. }
  116.  
  117. TOKEN : /* operators and relations */
  118. {
  119.     < SEMIC : ";" >
  120.     | < ASSIGN : ":=" >
  121.     | < COLON : ":" >
  122.     | < LBR : "(" >
  123.     | < RBR : ")" >
  124.     | < COMMA : "," >
  125.     | < PLUS_SIGN : "+" >
  126.     | < MINUS_SIGN : "-" >
  127.     | < MULT_SIGN : "*" >
  128.     | < DIV_SIGN : "/" >
  129.     | < EQUAL_SIGN : "=" >
  130.     | < NOT_EQUAL_SIGN : "!=" >
  131.     | < LESS_THAN : "<" >
  132.     | < GREATER_THAN : ">" >
  133.     | < LESS_EQUALS : "<=" >
  134.     | < GREATER_EQUALS : ">=" >
  135. }
  136.  
  137. TOKEN : /* Numbers */
  138. {
  139.     < NUM : (<DIGIT>)+ >
  140.     | < #DIGIT : ["0" - "9"] >
  141. }
  142.  
  143. TOKEN : /* Identifiers */
  144. {
  145.     < ID : (<LETTER>)+ >
  146.     | < #LETTER : ["a" - "z", "A" - "Z", "_", "0" - "9"] >
  147. }
  148.  
  149. TOKEN : /* epsilon empty string */
  150. {
  151.     <EPSILON : (<EMPTY>)+ >
  152.     | < #EMPTY: [] >
  153. }
  154.  
  155. TOKEN : /* Anything not recognised so far */
  156. {
  157.     < OTHER : ~[] >
  158. }
  159.  
  160. /*************************************************************************************
  161. ***** SECTION 4 - THE GRAMMAR & PRODUCTION RULES  *****
  162. **************************************************************************************/
  163.  
  164. void program() : {}
  165. {
  166.     ( decl() )*
  167.     ( function() )*
  168.     main_prog()
  169. }
  170.  
  171. void decl () : {}
  172. {
  173.     ( var_decl() | const_decl() )*
  174. }
  175.  
  176. void var_decl() : {}
  177. {
  178.     <VAR> ident_list()<COMMA>type() ( <COMMA> ident_list()<COLON>type() )* <SEMIC>
  179. }
  180.  
  181. void const_decl() : {}
  182. {
  183.     <CONST> <ID> <COLON>type = expression() ( <COMMA> <ID> <COLON> type = expression() )* <SEMIC>
  184. }
  185.  
  186. void function() : {}
  187. {
  188.     type() <ID> (param_list())
  189.     <BEGIN>
  190.     ( decl() )*
  191.     ( statement() )*
  192.     <RETURN> ( expression() | <EPSILON>  ) <SEMIC>
  193.     <END>
  194. }
  195.  
  196. void param_list() : {}
  197. {
  198.     ( <ID><COLON>type() ( <COMMA> <ID> <COLON>type() )* | <EPSILON>  )
  199. }
  200.  
  201. void type() : {}
  202. {
  203.     <INT> | <BOOL> | <VOID>
  204. }
  205.  
  206. void main_prog() : {}
  207. {
  208.     <MAIN>
  209.     <BEGIN>
  210.     ( decl() )*
  211.     ( statement() <SEMIC> )*
  212.     <END>
  213. }
  214.  
  215. void statement() : {}
  216. {
  217.     <ID> <ASSIGN> expression()
  218.     | <ID> ( arg_list() )
  219.     | <BEGIN> ( statement() <SEMIC> )* <END>
  220.     | <IF> condition() <THEN> statement() <ELSE> statement()
  221.     | <WHILE> condition() <DO> statement()
  222.     | <EPSILON>
  223. }
  224.  
  225. void expression() : {}
  226. {
  227.     fragment() ( ( <PLUS_SIGN> | <MINUS_SIGN> | <MULT_SIGN> | <DIV_SIGN> )  fragment() )*
  228.     | <ID> ( arg_list() )
  229. }
  230.  
  231. void fragment() : {}
  232. {
  233.     <ID> | <TRUE> | <FALSE> | <NUM> | ( <PLUS_SIGN> | <MINUS_SIGN> ) fragment() | expression()
  234. }
  235.  
  236. void condition() : {}
  237. {
  238.     <NOT> expression()
  239.     | expression() ( <EQUAL_SIGN> | <NOT_EQUAL_SIGN> | <LESS_THAN> | <GREATER_THAN> | <LESS_EQUALS> | <GREATER_EQUALS>  | <AND> | <OR>) expression()
  240.     | <ID>
  241. }
  242.  
  243. void ident_list() : {}
  244. {
  245.     <ID> ( <COMMA> <ID> )*
  246. }
  247.  
  248. void arg_list() : {}
  249. {
  250.     ( <ID> ( <COMMA> <ID> )* | <EPSILON>  )
  251. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement