Advertisement
KillianMills

ParserBackup3.jj

Nov 5th, 2015
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.21 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 if(t.kind == BOOL){
  47.                 System.out.println("Boolean");
  48.                 System.out.println("(" + t.image + ")");
  49.             }
  50.             else if(t.kind == VOID){
  51.                 System.out.println("Void");
  52.                 System.out.println("(" + t.image + ")");
  53.             }
  54.             else
  55.             System.out.print(t.image+" ");
  56.         }
  57.     }
  58. }
  59. PARSER_END(SLPTokeniser)
  60.  
  61. /*****************************************
  62. ***** SECTION 3 - TOKEN DEFINITIONS *****
  63. *****************************************/
  64. TOKEN_MGR_DECLS :
  65. {
  66.     static int commentNesting = 0;
  67. }
  68.  
  69. SKIP : /*** Ignoring spaces/tabs/newlines ***/
  70. {
  71.     " "
  72.     | "\t"
  73.     | "\n"
  74.     | "\r"
  75.     | "\f"
  76. }
  77.  
  78. SKIP : /* COMMENTS */
  79. {
  80.     "/*" { commentNesting++; } : IN_COMMENT
  81.     |"--" { } : IN_BASIC_COMMENT
  82. }
  83.  
  84. /* ENDS ON -STAR FORWARD SLASH-  ALSO ALLOWS FOR NESTING */
  85. <IN_COMMENT> SKIP :
  86. {
  87.     "/*" { commentNesting++; }
  88.     | "*/" { commentNesting--;
  89.         if (commentNesting == 0)
  90.             SwitchTo(DEFAULT);
  91.         }
  92.     | <~[]>
  93. }
  94.  
  95. /* ENDS ON NEW LINE */
  96. <IN_BASIC_COMMENT> SKIP :
  97. {
  98.     "\n" { SwitchTo(DEFAULT); }
  99. }
  100.  
  101. TOKEN : /* keywords */
  102. {
  103.     < AND : "and" >
  104.     |< BOOL : "bool" >
  105.     |< CONST : "const" >
  106.     |< DO : "do" >
  107.     |< ELSE : "else">
  108.     |< FALSE : "false">
  109.     |< IF : "if" >
  110.     |< INT : "int" >
  111.     |< MAIN : "main" >
  112.     |< NOT : "not" >
  113.     |< OR : "or" >
  114.     |< RETURN : "return" >
  115.     |< THEN : "then" >
  116.     |< TRUE : "true" >
  117.     |< VAR : "var" >
  118.     |< VOID : "void" >
  119.     |< WHILE : "while" >
  120.     |< BEGIN : "begin" >
  121.     |< END : "end" >
  122.  
  123. }
  124.  
  125. TOKEN : /* operators and relations */
  126. {
  127.     < SEMIC : ";" >
  128.     | < ASSIGN : ":=" >
  129.     | < COLON : ":" >
  130.     | < LBR : "(" >
  131.     | < RBR : ")" >
  132.     | < COMMA : "," >
  133.     | < PLUS_SIGN : "+" >
  134.     | < MINUS_SIGN : "-" >
  135.     | < MULT_SIGN : "*" >
  136.     | < DIV_SIGN : "/" >
  137.     | < EQUAL_SIGN : "=" >
  138.     | < NOT_EQUAL_SIGN : "!=" >
  139.     | < LESS_THAN : "<" >
  140.     | < GREATER_THAN : ">" >
  141.     | < LESS_EQUALS : "<=" >
  142.     | < GREATER_EQUALS : ">=" >
  143. }
  144.  
  145. TOKEN : /* Numbers */
  146. {
  147.     < NUM : (<DIGIT>)+ >
  148.     | < #DIGIT : ["0" - "9"] >
  149. }
  150.  
  151. TOKEN : /* Identifiers */
  152. {
  153.     < ID : (<LETTER>) (<LETTER> | <DIGIT> | "_")* >
  154.     | < #LETTER : ["a" - "z"] | ["A" - "Z"] >
  155. }
  156.  
  157. //TOKEN :
  158. //{
  159. //    //<#DIGIT: ["0"-"9"] >
  160. //    | <#CHAR: ["a"-"z"]|["A"-"Z"] >
  161. //    | <ID: (<CHAR>)(<CHAR> | <DIGIT>)* >
  162. //    | <NUM: (<DIGIT>)+ >
  163. //    | <REAL: ( (<DIGIT>)+ "." (<DIGIT>)* ) |
  164. //    ( (<DIGIT>)* "." (<DIGIT>)* ) >
  165. //}
  166.  
  167. TOKEN : /* Anything not recognised so far */
  168. {
  169.     < OTHER : ~[] >
  170. }
  171.  
  172. /*************************************************************************************
  173. ***** SECTION 4 - THE GRAMMAR & PRODUCTION RULES  *****
  174. **************************************************************************************/
  175.  
  176. void program() : {}
  177. {
  178.     ( decl() )*
  179.     ( function() )*
  180.     main_prog()
  181. }
  182.  
  183. void decl () : {}
  184. {
  185.     ( var_decl() | const_decl() )
  186. }
  187.  
  188. void var_decl() : {}
  189. {
  190.     <VAR> ident_list() <COMMA> type() ( <COMMA> ident_list() <COLON> type() )* <SEMIC>
  191. }
  192.  
  193. void const_decl() : {}
  194. {
  195.     <CONST> <ID> <COLON> type() <EQUAL_SIGN> expression() ( <COMMA> <ID> <COLON> type() <EQUAL_SIGN> expression() )* <SEMIC>
  196. }
  197.  
  198. void function() : {}
  199. {
  200.     type() <ID> <LBR> param_list() <RBR>
  201.     <BEGIN>
  202.     decl()
  203.     statement()
  204.     <RETURN> ( expression() | {} ) <SEMIC>
  205.     <END>
  206. }
  207.  
  208. void param_list() : {}
  209. {
  210.     ( <ID> <COLON> type() ( <COMMA> <ID> <COLON> type() )* | {}  )
  211. }
  212.  
  213. void type() : {}
  214. {
  215.     <INT> | <BOOL> | <VOID>
  216. }
  217.  
  218. void main_prog() : {}
  219. {
  220.     <MAIN>
  221.     <BEGIN>
  222.     decl()
  223.     ( statement() <SEMIC> )*
  224.     <END>
  225. }
  226.  
  227. void statement() : {}
  228. {
  229.     <ID> (<ASSIGN> expression() | <LBR> arg_list() <RBR>)
  230.     //| <ID> <LBR> arg_list() <RBR> // combined with above line
  231.     | <BEGIN> ( statement() <SEMIC> )* <END>
  232.     | <IF> condition() <THEN> statement() <ELSE> statement()
  233.     | <WHILE> condition() <DO> statement()
  234.     | {}
  235. }
  236.  
  237. void expression() : {}
  238. {
  239.     //[<LBR> arg_list() <RBR>] | <NUM> ( <PLUS_SIGN> | <MINUS_SIGN> ) ( ( <PLUS_SIGN> | <MINUS_SIGN> | <MULT_SIGN> | <DIV_SIGN> ) fragment())*
  240.     //fragment() ( ( <PLUS_SIGN> | <MINUS_SIGN> | <MULT_SIGN> | <DIV_SIGN> )  fragment() )*
  241.  
  242.     fragment() ( ( <PLUS_SIGN> | <MINUS_SIGN> | <MULT_SIGN> | <DIV_SIGN> )  fragment() )*
  243.     | <LBR> expression() <RBR>
  244.     | <ID> <LBR> arg_list() <RBR>
  245. }
  246.  
  247. void fragment() : {} // possibly combine with expression
  248. {
  249.     <ID> | <TRUE> | <FALSE> | <NUM> | ( <PLUS_SIGN> | <MINUS_SIGN> ) fragment() | expression()
  250. }
  251.  
  252. //void condition() : {}
  253. //{
  254. //    <NOT> expression()
  255. //    | expression() ( <EQUAL_SIGN> | <NOT_EQUAL_SIGN> | <LESS_THAN> | <GREATER_THAN> | <LESS_EQUALS> | <GREATER_EQUALS> | <AND> | <OR>) expression()
  256. //    | <ID>
  257. //}
  258.  
  259. void condition() : {}
  260. {
  261.     <NOT> condition()
  262.     | expression() ( <EQUAL_SIGN> | <NOT_EQUAL_SIGN> | <LESS_THAN> | <GREATER_THAN> | <LESS_EQUALS> | <GREATER_EQUALS> ) expression()
  263.     //| condition() ( <AND> | <OR>) condition()
  264.     | conditionPrime()
  265.     | <ID> <LBR> arg_list() <RBR>
  266. }
  267.  
  268. void conditionPrime() : {}
  269. {
  270.     <NOT> condition() ( <AND> | <OR> ) condition()
  271.     | ( expression() ( <EQUAL_SIGN> | <NOT_EQUAL_SIGN> | <LESS_THAN> | <GREATER_THAN> | <LESS_EQUALS> | <GREATER_EQUALS> ) expression() ) ( <AND> | <OR> ) condition()
  272.     | {}
  273. }
  274.  
  275. void ident_list() : {}
  276. {
  277.     <ID> ( <COMMA> <ID> )*
  278. }
  279.  
  280. void arg_list() : {}
  281. {
  282.     ( <ID> ( <COMMA> <ID> )* | {}  )
  283. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement