Advertisement
KillianMills

Parser.jjt

Dec 11th, 2015
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.50 KB | None | 0 0
  1. /*******************************
  2. ***** SECTION 1 - OPTIONS *****
  3. *******************************/
  4.  
  5. options
  6. {
  7.   JAVA_UNICODE_ESCAPE = true;
  8.   //DEBUG_PARSER=true;
  9.   MULTI = true;
  10.   VISITOR = true;
  11.   NODE_DEFAULT_VOID=true;
  12. }
  13.  
  14. /*********************************
  15. ***** SECTION 2 - USER CODE *****
  16. *********************************/
  17.  
  18. PARSER_BEGIN(Parser)
  19.  
  20. import java.util.Hashtable;
  21. import java.util.HashMap;
  22. import java.util.Map;
  23. import java.util.Enumeration;
  24. import java.util.LinkedList;
  25. //import org.apache.commons.lang3.ArrayUtils;
  26. //import java.util.*;
  27.  
  28. public class Parser
  29. {
  30.     public static HashMap<String, HashMap<String, STC>> ST = new HashMap();
  31.     public static HashMap innerMap = new HashMap();
  32.  
  33.     public static void main(String args[])
  34.     {
  35.         Parser parser;
  36.  
  37.         String temp;
  38.         STC temp2;
  39.  
  40.         if (args.length == 0)
  41.         {
  42.             System.out.println("Reading from standard input . . .");
  43.             parser = new Parser(System.in);
  44.         }
  45.         else if (args.length == 1)
  46.         {
  47.             try
  48.             {
  49.                 parser = new Parser(new java.io.FileInputStream(args[0]));
  50.             }
  51.             catch (java.io.FileNotFoundException e)
  52.             {
  53.                 System.err.println("File " + args[0] + " not found.");
  54.                 return;
  55.             }
  56.         }
  57.         else
  58.         {
  59.             System.out.println("Parser: Usage is one of:");
  60.             System.out.println(" java Parser < inputfile");
  61.             System.out.println("OR");
  62.             System.out.println(" java Parser inputfile");
  63.             return;
  64.         }
  65.         try
  66.         {
  67.             //parser.program();
  68.  
  69.             //AST
  70.             SimpleNode root = parser.program();
  71.             System.out.println("Abstract Syntax Tree: ");
  72.             root.dump(" ");
  73.  
  74.             System.out.println();
  75.  
  76.             //Symbol Table
  77.             System.out.println("Symbol Table: " );
  78.  
  79.  
  80.             // go through each scope
  81.             for(Map.Entry<String, HashMap<String, STC>> entry : ST.entrySet()){
  82.  
  83.                 HashMap<String, STC> innerMap = new HashMap();
  84.                 String currentScope = entry.getKey();
  85.                 innerMap = entry.getValue();
  86.  
  87.                 System.out.println("----------"+currentScope+"----------");
  88.                 // go through each item per scope
  89.                 for(Map.Entry<String, STC> innerEntry : innerMap.entrySet()){
  90.                     String type = innerEntry.getKey();
  91.                     STC holder = innerEntry.getValue();
  92.  
  93.                     if(holder.type != null){
  94.                         System.out.println("type = " + holder.value);
  95.                     };
  96.  
  97.                     if(holder.value != null){
  98.                         System.out.println("value = " + holder.type);
  99.                     }
  100.                     System.out.println();
  101.                 }
  102.                 System.out.println();
  103.             }
  104.  
  105.             // Two Visitors
  106.             /* Not needed, not in the assignment description
  107.             System.out.println();
  108.             System.out.println("Program: ");
  109.             PrintVisitor pv = new PrintVisitor();
  110.             root.jjtAccept(pv, null);
  111.             */
  112.  
  113.             System.out.println("Type Checking: ");
  114.             TypeCheckVisitor tc = new TypeCheckVisitor();
  115.             root.jjtAccept(tc, ST);
  116.  
  117.         }
  118.         catch(Exception e)
  119.         {
  120.           e.printStackTrace();
  121.         }
  122.     }
  123. }
  124. PARSER_END(Parser)
  125.  
  126. /*****************************************
  127. ***** SECTION 3 - TOKEN DEFINITIONS *****
  128. *****************************************/
  129.  
  130. TOKEN_MGR_DECLS :
  131. {
  132.     static int commentNesting = 0;
  133. }
  134.  
  135. SKIP : /*** Ignoring spaces/tabs/newlines ***/
  136. {
  137.     " "
  138.     | "\t"
  139.     | "\n"
  140.     | "\r"
  141.     | "\f"
  142. }
  143.  
  144. SKIP : /* COMMENTS */
  145. {
  146.     < "--" (~["\r", "\n"]) *>
  147.     |"/*" { commentNesting++; } : IN_COMMENT
  148. }
  149.  
  150. <IN_COMMENT> SKIP :
  151. {
  152.     "/*" { commentNesting++; }
  153.     | "*/" { commentNesting--;
  154.             if (commentNesting == 0)
  155.                 SwitchTo(DEFAULT);
  156.             }
  157.     | <~[]>
  158. }
  159.  
  160. TOKEN : /* keywords */
  161. {
  162.     < AND : "and" >
  163.     |< BOOL : "bool" >
  164.     |< CONST : "const" >
  165.     |< DO : "do" >
  166.     |< ELSE : "else">
  167.     |< FALSE : "false">
  168.     |< IF : "if" >
  169.     |< INT : "int" >
  170.     |< MAIN : "main" >
  171.     |< NOT : "not" >
  172.     |< OR : "or" >
  173.     |< RETURN : "return" >
  174.     |< THEN : "then" >
  175.     |< TRUE : "true" >
  176.     |< VAR : "var" >
  177.     |< VOID : "void" >
  178.     |< WHILE : "while" >
  179.     |< BEGIN : "begin" >
  180.     |< END : "end" >
  181. }
  182.  
  183. TOKEN : /* operators and relations */
  184. {
  185.     < SEMIC : ";" >
  186.     | < ASSIGN : ":=" >
  187.     | < COLON : ":" >
  188.     | < LBR : "(" >
  189.     | < RBR : ")" >
  190.     | < COMMA : "," >
  191.     | < PLUS_SIGN : "+" >
  192.     | < MINUS_SIGN : "-" >
  193.     | < MULT_SIGN : "*" >
  194.     | < DIV_SIGN : "/" >
  195.     | < EQUAL_SIGN : "=" >
  196.     | < NOT_EQUAL_SIGN : "!=" >
  197.     | < LESS_THAN : "<" >
  198.     | < GREATER_THAN : ">" >
  199.     | < LESS_EQUALS : "<=" >
  200.     | < GREATER_EQUALS : ">=" >
  201. }
  202.  
  203. TOKEN : /* Numbers */
  204. {
  205.     < NUM : (<DIGIT>)+ >
  206.     | < #DIGIT : ["0" - "9"] >
  207.  
  208. }
  209.  
  210. TOKEN : /* Identifiers */
  211. {
  212.     < ID : (<LETTER>) (<LETTER> | <DIGIT> | "_")* >
  213.     | < #LETTER : ["a" - "z"] | ["A" - "Z"] >
  214. }
  215.  
  216. TOKEN : /* Anything not recognised so far */
  217. {
  218.     < OTHER : ~[] >
  219. }
  220.  
  221. /*************************************************************************************
  222. ***** SECTION 4 - THE GRAMMAR & PRODUCTION RULES  *****
  223. **************************************************************************************/
  224.  
  225. SimpleNode program() #Program : {}
  226. {
  227.     ( decl("global") )*{
  228.            ST.put("global", innerMap);
  229.            HashMap tempMap = new HashMap();
  230.            innerMap = tempMap;
  231.     }
  232.  
  233.     ( function() )*
  234.     main_prog()
  235.     {
  236.         return jjtThis;
  237.     }
  238. }
  239.  
  240. void decl (String scope) : {}
  241. {
  242.     ( var_decl(scope) | const_decl(scope) )
  243. }
  244.  
  245. void var_decl(String scope) #VarDecl : {String name; String nameHolder; Token t; LinkedList<String> names = new LinkedList<String>();}
  246. {
  247.     <VAR> names = ident_list() <COLON> t =type() [ <COMMA> innerVar(scope) ] <SEMIC>
  248.  
  249.     {
  250.         for(int i=0; i<names.size(); i++){
  251.             name = names.get(i);
  252.  
  253.             if(t.toString() == "int" && name != ""){
  254.  
  255.                innerMap.put(name , new STC(name, "Int", "no"));
  256.             }
  257.             else if(t.toString() == "bool" && name != ""){
  258.  
  259.                innerMap.put(name , new STC(name, "Bool", "no"));
  260.             }
  261.         }// end of for loop
  262.     }
  263. }
  264.  
  265. void innerVar(String scope) : {String name; String nameHolder; Token t; LinkedList<String> names = new LinkedList<String>();}
  266. {
  267.     names = ident_list() <COLON> t = type() [ <COMMA> innerVar(scope) ]
  268.  
  269.      {
  270.         for(int i=0; i<names.size(); i++){
  271.             name = names.get(i);
  272.  
  273.             if(t.toString() == "int" && name != ""){
  274.  
  275.                 innerMap.put(name , new STC(name, "Int", "no"));
  276.             }
  277.  
  278.             else if(t.toString() == "bool" && name != ""){
  279.  
  280.                 innerMap.put(name , new STC(name, "Bool", "no"));
  281.             }
  282.         }// end of for loop
  283.     }
  284. }
  285.  
  286. void const_decl(String scope) #ConstDecl : {Token t; String name;}
  287. {
  288.     <CONST>  name = id() <COLON> t = type() equal() expression() [<COMMA> innerConst(scope)] <SEMIC>
  289.  
  290.     {
  291.         if(t.toString() == "int" && name != ""){
  292.  
  293.             innerMap.put(name , new STC(name, "Int", "no"));
  294.         }
  295.  
  296.         else if(t.toString() == "bool" && name != ""){
  297.  
  298.             innerMap.put(name , new STC(name, "Bool", "no"));
  299.         }
  300.     }
  301. }
  302.  
  303. void innerConst (String scope) : {Token t; String name;}
  304. {
  305.     name = id() <COLON> t = type() equal() expression() [ <COMMA> innerConst(scope) ]
  306.  
  307.     {
  308.         if(t.toString() == "int" && name != ""){
  309.  
  310.             innerMap.put(name , new STC(name, "Int", "no"));
  311.         }
  312.  
  313.         else if(t.toString() == "bool" && name != ""){
  314.  
  315.             innerMap.put(name , new STC(name, "Bool", "no"));
  316.         }
  317.     }
  318. }
  319.  
  320. void equal() #Equal : {}
  321. {
  322.     <EQUAL_SIGN> { jjtThis.value = token; }
  323. }
  324.  
  325. void function() #Function : {String functionName; HashMap tempMap;}
  326. {
  327.     type() functionName= id() <LBR> param_list()<RBR>
  328.     <BEGIN>
  329.     ( decl(functionName) )*{
  330.         ST.put(functionName, innerMap);
  331.         tempMap = new HashMap();
  332.         innerMap = tempMap;
  333.     }
  334.     ( statement() <SEMIC> )*
  335.     <RETURN> ( expression() | {} ) <SEMIC>
  336.     <END>
  337.  
  338. }
  339.  
  340. void param_list() #ParamList : {String name = new String(); String nameHolder = new String(); Token t= new Token(); LinkedList<String> extras = new LinkedList<String>();}
  341. {
  342.     ( nameHolder= id() <COLON> t= type() extras = comma_list() | {}  )
  343.  
  344.     {
  345.         extras.add(nameHolder);
  346.         for(int i=0; i<extras.size(); i++){
  347.             name = extras.get(i);
  348.             if(t.toString() == "int" && name != ""){
  349.  
  350.                 innerMap.put(name , new STC(name, "Int", "no"));
  351.             }
  352.             else if(t.toString() == "bool" && name != ""){
  353.  
  354.                 innerMap.put(name , new STC(name, "Bool", "no"));
  355.             }
  356.         }
  357.     }
  358. }
  359.  
  360. Token type() #Type : {Token t;}
  361. {
  362.     t = <INT>
  363.     { jjtThis.value=token; return t; }
  364.     | t = <BOOL>
  365.     { jjtThis.value=token; return t; }
  366.     | t = <VOID>
  367.     { jjtThis.value=token; return t; }
  368. }
  369.  
  370. void main_prog() #MainProg : {}
  371. {
  372.     <MAIN>
  373.     <BEGIN>
  374.     decl("main"){
  375.         ST.put("main", innerMap);
  376.         HashMap tempMap = new HashMap();
  377.         innerMap = tempMap;
  378.     }
  379.     ( statement() <SEMIC> )*
  380.     <END>
  381. }
  382.  
  383. void statement() : {}
  384. {
  385.     id() ( assign() expression() | <LBR> arg_list() <RBR> )
  386.     | <BEGIN> ( statement() <SEMIC> )* <END>
  387.     | <IF> condition() <THEN> statement() [<SEMIC> <ELSE> statement()]
  388.     | <WHILE> condition() <DO> statement()
  389.     | {}
  390. }
  391.  
  392. void assign() #Assign : {}
  393. {
  394.     <ASSIGN>
  395.     { jjtThis.value = token; }
  396. }
  397.  
  398. void expression() : {}
  399. {
  400.     fragment() [ mathExpression() ]
  401.     | <LBR> expression() <RBR> [ mathExpression() ]
  402. }
  403.  
  404. void mathExpression() : {}
  405. {
  406.     mathSign() expression()
  407. }
  408.  
  409. void mathSign() #MathSign : {}
  410. {
  411.     <PLUS_SIGN>
  412.     { jjtThis.value = token; }
  413.     | <MINUS_SIGN>
  414.     { jjtThis.value = token; }
  415.     | <MULT_SIGN>
  416.     { jjtThis.value = token; }
  417.     | <DIV_SIGN>
  418.     { jjtThis.value = token; }
  419. }
  420.  
  421. void fragment() : {} // possibly combine with expression
  422. {
  423.     id() [<LBR> arg_list() <RBR>]
  424.     | bool()
  425.     | num()
  426. }
  427.  
  428. void condition() #Condition : {}
  429. {
  430.     <LBR> condition() <RBR> [( <AND> | <OR>) condition()]
  431.     |<NOT> condition()
  432.     | expression() [( signs() ) expression()] [( <AND> | <OR>) condition()]
  433. }
  434.  
  435. void signs() #Signs : {}
  436. {
  437.     equal()
  438.     | <NOT_EQUAL_SIGN>
  439.     { jjtThis.value = token; }
  440.     | <LESS_THAN>
  441.     { jjtThis.value = token; }
  442.     | <GREATER_THAN>
  443.     { jjtThis.value = token; }
  444.     | <LESS_EQUALS>
  445.     { jjtThis.value = token; }
  446.     | <GREATER_EQUALS>
  447.     { jjtThis.value = token; }
  448. }
  449.  
  450. LinkedList<String> ident_list() #IdentList : { String nameHolder; LinkedList<String> names = new LinkedList<String>(); LinkedList<String> extras = new LinkedList<String>(); }
  451. {
  452.     nameHolder = id() extras = comma_list()
  453.     {
  454.         names.add(nameHolder);
  455.         if(!extras.isEmpty()){
  456.             names.addAll(extras);
  457.         }
  458.     return names; }
  459. }
  460.  
  461. LinkedList<String> comma_list() : { String nameHolder= ""; LinkedList<String> names = new LinkedList<String>(); LinkedList<String> extras = new LinkedList<String>(); }
  462. {
  463.     <COMMA> nameHolder = id() comma_list() | {}
  464.     {
  465.     names.add(nameHolder);
  466.     if(!extras.isEmpty()){
  467.         names.addAll(extras);
  468.     }
  469.     return names; }
  470. }
  471.  
  472. void arg_list() #ArgList : {}
  473. {
  474.     ( id() ( <COMMA> id() )* | {}  )
  475. }
  476.  
  477. void num() #Num : {}
  478. {
  479.     <NUM>
  480.     { jjtThis.value = token; }
  481. }
  482.  
  483. void bool() #Bool : {}
  484. {
  485.     <TRUE>
  486.     { jjtThis.value = token; }
  487.     | <FALSE>
  488.     { jjtThis.value = token; }
  489. }
  490.  
  491. String id() #Id : {Token t;}
  492. {
  493.     t = <ID> { jjtThis.value = t.image; return t.image; }
  494. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement