KillianMills

ParserComplete.jj

Nov 13th, 2015
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.28 KB | None | 0 0
  1. /*******************************
  2. ***** SECTION 1 - OPTIONS *****
  3. *******************************/
  4.  
  5. options
  6. {
  7.   JAVA_UNICODE_ESCAPE = true; DEBUG_PARSER=true;
  8. }
  9.  
  10. /*********************************
  11. ***** SECTION 2 - USER CODE *****
  12. *********************************/
  13.  
  14. PARSER_BEGIN(Parser)
  15.  
  16. public class Parser
  17. {
  18.     public static void main(String args[])
  19.     {
  20.         Parser parser;
  21.  
  22.         if (args.length == 0)
  23.         {
  24.             System.out.println("Reading from standard input . . .");
  25.             parser = new Parser(System.in);
  26.         }
  27.         else if (args.length == 1)
  28.         {
  29.             try
  30.             {
  31.                 parser = new Parser(new java.io.FileInputStream(args[0]));
  32.             }
  33.             catch (java.io.FileNotFoundException e)
  34.             {
  35.                 System.err.println("File " + args[0] + " not found.");
  36.                 return;
  37.             }
  38.         }
  39.         else
  40.         {
  41.             System.out.println("Parser: Usage is one of:");
  42.             System.out.println(" java Parser < inputfile");
  43.             System.out.println("OR");
  44.             System.out.println(" java Parser inputfile");
  45.             return;
  46.         }
  47.         try
  48.         {
  49.             parser.program();
  50.         }
  51.         catch(Exception e)
  52.         {
  53.           e.printStackTrace();
  54.         }
  55.     }
  56. }
  57. PARSER_END(Parser)
  58.  
  59. /*****************************************
  60. ***** SECTION 3 - TOKEN DEFINITIONS *****
  61. *****************************************/
  62.  
  63. TOKEN_MGR_DECLS :
  64. {
  65.     static int commentNesting = 0;
  66. }
  67.  
  68. SKIP : /*** Ignoring spaces/tabs/newlines ***/
  69. {
  70.     " "
  71.     | "\t"
  72.     | "\n"
  73.     | "\r"
  74.     | "\f"
  75. }
  76.  
  77. SKIP : /* COMMENTS */
  78. {
  79.     < "--" (~["\r", "\n"]) *>
  80.     |"/*" { commentNesting++; } : IN_COMMENT
  81. }
  82.  
  83. <IN_COMMENT> SKIP :
  84. {
  85.         "/*" { commentNesting++; }
  86.     | "*/" { commentNesting--;
  87.             if (commentNesting == 0)
  88.                 SwitchTo(DEFAULT);
  89.             }
  90.     | <~[]>
  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>) (<LETTER> | <DIGIT> | "_")* >
  146.     | < #LETTER : ["a" - "z"] | ["A" - "Z"] >
  147. }
  148.  
  149. TOKEN : /* Anything not recognised so far */
  150. {
  151.     < OTHER : ~[] >
  152. }
  153.  
  154. /*************************************************************************************
  155. ***** SECTION 4 - THE GRAMMAR & PRODUCTION RULES  *****
  156. **************************************************************************************/
  157.  
  158. void program() : {}
  159. {
  160.     ( decl() )*
  161.     ( function() )*
  162.     main_prog()
  163. }
  164.  
  165. void decl () : {}
  166. {
  167.     ( var_decl() | const_decl() )
  168. }
  169.  
  170. void var_decl() : {}
  171. {
  172.     <VAR> ident_list() <COLON> type() ( <COMMA> ident_list() <COLON> type() )* <SEMIC>
  173. }
  174.  
  175. void const_decl() : {}
  176. {
  177.     <CONST> <ID> <COLON> type() <EQUAL_SIGN> expression() ( <COMMA> <ID> <COLON> type() <EQUAL_SIGN> expression() )* <SEMIC>
  178. }
  179.  
  180. void function() : {}
  181. {
  182.     type() <ID> <LBR> param_list() <RBR>
  183.     <BEGIN>
  184.     ( decl() )*
  185.     ( statement() <SEMIC> )*
  186.     <RETURN> ( expression() | {} ) <SEMIC>
  187.     <END>
  188. }
  189.  
  190. void param_list() : {}
  191. {
  192.     ( <ID> <COLON> type() ( <COMMA> <ID> <COLON> type() )* | {}  )
  193. }
  194.  
  195. void type() : {}
  196. {
  197.     <INT> | <BOOL> | <VOID>
  198. }
  199.  
  200. void main_prog() : {}
  201. {
  202.     <MAIN>
  203.     <BEGIN>
  204.     decl()
  205.     ( statement() <SEMIC> )*
  206.     <END>
  207. }
  208.  
  209. void statement() : {}
  210. {
  211.     <ID> (<ASSIGN> expression() | <LBR> arg_list() <RBR>)
  212.     //| <ID> <LBR> arg_list() <RBR> // combined with above line
  213.     | <BEGIN> ( statement() <SEMIC> )* <END>
  214.     | <IF> condition() <THEN> statement() [<SEMIC> <ELSE> statement()]
  215.     | <WHILE> condition() <DO> statement()
  216.     | {}
  217. }
  218.  
  219. void expression() : {}
  220. {
  221.     //<LBR> expression() <RBR>
  222.     fragment() [ ( <PLUS_SIGN> | <MINUS_SIGN> | <MULT_SIGN> | <DIV_SIGN> )  expression() ]
  223.     | <LBR> expression() <RBR> [ ( <PLUS_SIGN> | <MINUS_SIGN> | <MULT_SIGN> | <DIV_SIGN> )  expression() ]
  224.     //| <ID> <LBR> arg_list() <RBR>
  225. }
  226.  
  227. void fragment() : {} // possibly combine with expression
  228. {
  229.     <ID> [<LBR> arg_list() <RBR>]
  230.     | <TRUE>
  231.     | <FALSE>
  232.     | <NUM>
  233.  
  234.     //| ( <PLUS_SIGN> | <MINUS_SIGN> ) fragment()
  235. }
  236.  
  237. void condition() : {}
  238. {
  239.     <LBR> condition() <RBR> [( <AND> | <OR>) condition()]
  240.     |<NOT> condition()
  241.     | expression() [( <EQUAL_SIGN> | <NOT_EQUAL_SIGN> | <LESS_THAN> | <GREATER_THAN> | <LESS_EQUALS> | <GREATER_EQUALS> ) expression()] [( <AND> | <OR>) condition()]
  242.     //| expression() ( <AND> | <OR>) condition()
  243.     //| <ID> <LBR> arg_list() <RBR> // might need to include this in conditionPrime
  244. }
  245.  
  246. void ident_list() : {}
  247. {
  248.     <ID> ( <COMMA> <ID> )*
  249. }
  250.  
  251. void arg_list() : {}
  252. {
  253.     ( <ID> ( <COMMA> <ID> )* | {}  )
  254. }
Advertisement
Add Comment
Please, Sign In to add comment