Advertisement
KillianMills

ParserBackup1.java

Nov 2nd, 2015
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.24 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>) (<LETTER> | <DIGIT> | "_")* >
  146.     | < #LETTER : ["a" - "z"] | ["A" - "Z"] >
  147. }
  148.  
  149. //TOKEN :
  150. //{
  151. //    //<#DIGIT: ["0"-"9"] >
  152. //    | <#CHAR: ["a"-"z"]|["A"-"Z"] >
  153. //    | <ID: (<CHAR>)(<CHAR> | <DIGIT>)* >
  154. //    | <NUM: (<DIGIT>)+ >
  155. //    | <REAL: ( (<DIGIT>)+ "." (<DIGIT>)* ) |
  156. //    ( (<DIGIT>)* "." (<DIGIT>)* ) >
  157. //}
  158.  
  159. TOKEN : /* Anything not recognised so far */
  160. {
  161.     < OTHER : ~[] >
  162. }
  163.  
  164. /*************************************************************************************
  165. ***** SECTION 4 - THE GRAMMAR & PRODUCTION RULES  *****
  166. **************************************************************************************/
  167.  
  168. void program() : {}
  169. {
  170.     decl() //removed * as decl has a * already,pick one or the other
  171.     ( function() )*
  172.     main_prog()
  173. }
  174.  
  175. void decl () : {}
  176. {
  177.     ( var_decl() | const_decl() )*
  178. }
  179.  
  180. void var_decl() : {}
  181. {
  182.     <VAR> ident_list()<COMMA>type() ( <COMMA> ident_list()<COLON>type() )* <SEMIC>
  183. }
  184.  
  185. void const_decl() : {}
  186. {
  187.     <CONST> <ID> <COLON> type() <EQUAL_SIGN> expression() ( <COMMA> <ID> <COLON> type() <EQUAL_SIGN> expression() )* <SEMIC>
  188. }
  189.  
  190. void function() : {}
  191. {
  192.     type() <ID> <LBR> param_list() <RBR>
  193.     <BEGIN>
  194.     decl()
  195.     statement()
  196.     <RETURN> ( expression() | {} ) <SEMIC>
  197.     <END>
  198. }
  199.  
  200. void param_list() : {}
  201. {
  202.     ( <ID> <COLON> type() ( <COMMA> <ID> <COLON> type() )* | {}  )
  203. }
  204.  
  205. void type() : {}
  206. {
  207.     <INT> | <BOOL> | <VOID>
  208. }
  209.  
  210. void main_prog() : {}
  211. {
  212.     <MAIN>
  213.     <BEGIN>
  214.     decl()
  215.     ( statement() <SEMIC> )*
  216.     <END>
  217. }
  218.  
  219. void statement() : {}
  220. {
  221.     <ID> (<ASSIGN> expression() | <LBR> arg_list() <RBR>)
  222.     //| <ID> <LBR> arg_list() <RBR> // combine line with lower line
  223.     | <BEGIN> ( statement() <SEMIC> )* <END>
  224.     | <IF> condition() <THEN> statement() <ELSE> statement()
  225.     | <WHILE> condition() <DO> statement()
  226.     | {}
  227. }
  228.  
  229. void expression() : {}
  230. {
  231.     [<LBR> arg_list() <RBR>] | <NUM> ( <PLUS_SIGN> | <MINUS_SIGN> ) ( ( <PLUS_SIGN> | <MINUS_SIGN> | <MULT_SIGN> | <DIV_SIGN> ) fragment())*
  232.     //fragment() ( ( <PLUS_SIGN> | <MINUS_SIGN> | <MULT_SIGN> | <DIV_SIGN> )  fragment() )*
  233. }
  234.  
  235. void fragment() : {} // possibly combine with expression
  236. {
  237.     <ID> | <TRUE> | <FALSE> | <NUM> | ( <PLUS_SIGN> | <MINUS_SIGN> ) fragment() | expression()
  238. }
  239.  
  240. void condition() : {}
  241. {
  242.     <NOT> expression()
  243.     | expression() ( <EQUAL_SIGN> | <NOT_EQUAL_SIGN> | <LESS_THAN> | <GREATER_THAN> | <LESS_EQUALS> | <GREATER_EQUALS> | <AND> | <OR>) expression()
  244.     | <ID>
  245. }
  246.  
  247. void ident_list() : {}
  248. {
  249.     <ID> ( <COMMA> <ID> )*
  250. }
  251.  
  252. void arg_list() : {}
  253. {
  254.     ( <ID> ( <COMMA> <ID> )* | {}  )
  255. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement