Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*******************************
- ***** SECTION 1 - OPTIONS *****
- *******************************/
- options
- {
- JAVA_UNICODE_ESCAPE = true;
- //DEBUG_PARSER=true;
- MULTI = true;
- VISITOR = true;
- NODE_DEFAULT_VOID=true;
- }
- /*********************************
- ***** SECTION 2 - USER CODE *****
- *********************************/
- PARSER_BEGIN(Parser)
- import java.util.Hashtable;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.Enumeration;
- import java.util.LinkedList;
- //import org.apache.commons.lang3.ArrayUtils;
- //import java.util.*;
- public class Parser
- {
- public static HashMap<String, HashMap<String, STC>> ST = new HashMap();
- public static HashMap innerMap = new HashMap();
- public static void main(String args[])
- {
- Parser parser;
- String temp;
- STC temp2;
- if (args.length == 0)
- {
- System.out.println("Reading from standard input . . .");
- parser = new Parser(System.in);
- }
- else if (args.length == 1)
- {
- try
- {
- parser = new Parser(new java.io.FileInputStream(args[0]));
- }
- catch (java.io.FileNotFoundException e)
- {
- System.err.println("File " + args[0] + " not found.");
- return;
- }
- }
- else
- {
- System.out.println("Parser: Usage is one of:");
- System.out.println(" java Parser < inputfile");
- System.out.println("OR");
- System.out.println(" java Parser inputfile");
- return;
- }
- try
- {
- //parser.program();
- //AST
- SimpleNode root = parser.program();
- System.out.println("Abstract Syntax Tree: ");
- root.dump(" ");
- System.out.println();
- //Symbol Table
- System.out.println("Symbol Table: " );
- // go through each scope
- for(Map.Entry<String, HashMap<String, STC>> entry : ST.entrySet()){
- HashMap<String, STC> innerMap = new HashMap();
- String currentScope = entry.getKey();
- innerMap = entry.getValue();
- System.out.println("----------"+currentScope+"----------");
- // go through each item per scope
- for(Map.Entry<String, STC> innerEntry : innerMap.entrySet()){
- String type = innerEntry.getKey();
- STC holder = innerEntry.getValue();
- if(holder.type != null){
- System.out.println("type = " + holder.value);
- };
- if(holder.value != null){
- System.out.println("value = " + holder.type);
- }
- System.out.println();
- }
- System.out.println();
- }
- // Two Visitors
- /* Not needed, not in the assignment description
- System.out.println();
- System.out.println("Program: ");
- PrintVisitor pv = new PrintVisitor();
- root.jjtAccept(pv, null);
- */
- System.out.println("Type Checking: ");
- TypeCheckVisitor tc = new TypeCheckVisitor();
- root.jjtAccept(tc, ST);
- }
- catch(Exception e)
- {
- e.printStackTrace();
- }
- }
- }
- PARSER_END(Parser)
- /*****************************************
- ***** SECTION 3 - TOKEN DEFINITIONS *****
- *****************************************/
- TOKEN_MGR_DECLS :
- {
- static int commentNesting = 0;
- }
- SKIP : /*** Ignoring spaces/tabs/newlines ***/
- {
- " "
- | "\t"
- | "\n"
- | "\r"
- | "\f"
- }
- SKIP : /* COMMENTS */
- {
- < "--" (~["\r", "\n"]) *>
- |"/*" { commentNesting++; } : IN_COMMENT
- }
- <IN_COMMENT> SKIP :
- {
- "/*" { commentNesting++; }
- | "*/" { commentNesting--;
- if (commentNesting == 0)
- SwitchTo(DEFAULT);
- }
- | <~[]>
- }
- TOKEN : /* keywords */
- {
- < AND : "and" >
- |< BOOL : "bool" >
- |< CONST : "const" >
- |< DO : "do" >
- |< ELSE : "else">
- |< FALSE : "false">
- |< IF : "if" >
- |< INT : "int" >
- |< MAIN : "main" >
- |< NOT : "not" >
- |< OR : "or" >
- |< RETURN : "return" >
- |< THEN : "then" >
- |< TRUE : "true" >
- |< VAR : "var" >
- |< VOID : "void" >
- |< WHILE : "while" >
- |< BEGIN : "begin" >
- |< END : "end" >
- }
- TOKEN : /* operators and relations */
- {
- < SEMIC : ";" >
- | < ASSIGN : ":=" >
- | < COLON : ":" >
- | < LBR : "(" >
- | < RBR : ")" >
- | < COMMA : "," >
- | < PLUS_SIGN : "+" >
- | < MINUS_SIGN : "-" >
- | < MULT_SIGN : "*" >
- | < DIV_SIGN : "/" >
- | < EQUAL_SIGN : "=" >
- | < NOT_EQUAL_SIGN : "!=" >
- | < LESS_THAN : "<" >
- | < GREATER_THAN : ">" >
- | < LESS_EQUALS : "<=" >
- | < GREATER_EQUALS : ">=" >
- }
- TOKEN : /* Numbers */
- {
- < NUM : (<DIGIT>)+ >
- | < #DIGIT : ["0" - "9"] >
- }
- TOKEN : /* Identifiers */
- {
- < ID : (<LETTER>) (<LETTER> | <DIGIT> | "_")* >
- | < #LETTER : ["a" - "z"] | ["A" - "Z"] >
- }
- TOKEN : /* Anything not recognised so far */
- {
- < OTHER : ~[] >
- }
- /*************************************************************************************
- ***** SECTION 4 - THE GRAMMAR & PRODUCTION RULES *****
- **************************************************************************************/
- SimpleNode program() #Program : {}
- {
- ( decl("global") )*{
- ST.put("global", innerMap);
- HashMap tempMap = new HashMap();
- innerMap = tempMap;
- }
- ( function() )*
- main_prog()
- {
- return jjtThis;
- }
- }
- void decl (String scope) : {}
- {
- ( var_decl(scope) | const_decl(scope) )
- }
- void var_decl(String scope) #VarDecl : {String name; String nameHolder; Token t; LinkedList<String> names = new LinkedList<String>();}
- {
- <VAR> names = ident_list() <COLON> t =type() [ <COMMA> innerVar(scope) ] <SEMIC>
- {
- for(int i=0; i<names.size(); i++){
- name = names.get(i);
- if(t.toString() == "int" && name != ""){
- innerMap.put(name , new STC(name, "Int", "no"));
- }
- else if(t.toString() == "bool" && name != ""){
- innerMap.put(name , new STC(name, "Bool", "no"));
- }
- }// end of for loop
- }
- }
- void innerVar(String scope) : {String name; String nameHolder; Token t; LinkedList<String> names = new LinkedList<String>();}
- {
- names = ident_list() <COLON> t = type() [ <COMMA> innerVar(scope) ]
- {
- for(int i=0; i<names.size(); i++){
- name = names.get(i);
- if(t.toString() == "int" && name != ""){
- innerMap.put(name , new STC(name, "Int", "no"));
- }
- else if(t.toString() == "bool" && name != ""){
- innerMap.put(name , new STC(name, "Bool", "no"));
- }
- }// end of for loop
- }
- }
- void const_decl(String scope) #ConstDecl : {Token t; String name;}
- {
- <CONST> name = id() <COLON> t = type() equal() expression() [<COMMA> innerConst(scope)] <SEMIC>
- {
- if(t.toString() == "int" && name != ""){
- innerMap.put(name , new STC(name, "Int", "no"));
- }
- else if(t.toString() == "bool" && name != ""){
- innerMap.put(name , new STC(name, "Bool", "no"));
- }
- }
- }
- void innerConst (String scope) : {Token t; String name;}
- {
- name = id() <COLON> t = type() equal() expression() [ <COMMA> innerConst(scope) ]
- {
- if(t.toString() == "int" && name != ""){
- innerMap.put(name , new STC(name, "Int", "no"));
- }
- else if(t.toString() == "bool" && name != ""){
- innerMap.put(name , new STC(name, "Bool", "no"));
- }
- }
- }
- void equal() #Equal : {}
- {
- <EQUAL_SIGN> { jjtThis.value = token; }
- }
- void function() #Function : {String functionName; HashMap tempMap;}
- {
- type() functionName= id() <LBR> param_list()<RBR>
- <BEGIN>
- ( decl(functionName) )*{
- ST.put(functionName, innerMap);
- tempMap = new HashMap();
- innerMap = tempMap;
- }
- ( statement() <SEMIC> )*
- <RETURN> ( expression() | {} ) <SEMIC>
- <END>
- }
- void param_list() #ParamList : {String name = new String(); String nameHolder = new String(); Token t= new Token(); LinkedList<String> extras = new LinkedList<String>();}
- {
- ( nameHolder= id() <COLON> t= type() extras = comma_list() | {} )
- {
- extras.add(nameHolder);
- for(int i=0; i<extras.size(); i++){
- name = extras.get(i);
- if(t.toString() == "int" && name != ""){
- innerMap.put(name , new STC(name, "Int", "no"));
- }
- else if(t.toString() == "bool" && name != ""){
- innerMap.put(name , new STC(name, "Bool", "no"));
- }
- }
- }
- }
- Token type() #Type : {Token t;}
- {
- t = <INT>
- { jjtThis.value=token; return t; }
- | t = <BOOL>
- { jjtThis.value=token; return t; }
- | t = <VOID>
- { jjtThis.value=token; return t; }
- }
- void main_prog() #MainProg : {}
- {
- <MAIN>
- <BEGIN>
- decl("main"){
- ST.put("main", innerMap);
- HashMap tempMap = new HashMap();
- innerMap = tempMap;
- }
- ( statement() <SEMIC> )*
- <END>
- }
- void statement() : {}
- {
- id() ( assign() expression() | <LBR> arg_list() <RBR> )
- | <BEGIN> ( statement() <SEMIC> )* <END>
- | <IF> condition() <THEN> statement() [<SEMIC> <ELSE> statement()]
- | <WHILE> condition() <DO> statement()
- | {}
- }
- void assign() #Assign : {}
- {
- <ASSIGN>
- { jjtThis.value = token; }
- }
- void expression() : {}
- {
- fragment() [ mathExpression() ]
- | <LBR> expression() <RBR> [ mathExpression() ]
- }
- void mathExpression() : {}
- {
- mathSign() expression()
- }
- void mathSign() #MathSign : {}
- {
- <PLUS_SIGN>
- { jjtThis.value = token; }
- | <MINUS_SIGN>
- { jjtThis.value = token; }
- | <MULT_SIGN>
- { jjtThis.value = token; }
- | <DIV_SIGN>
- { jjtThis.value = token; }
- }
- void fragment() : {} // possibly combine with expression
- {
- id() [<LBR> arg_list() <RBR>]
- | bool()
- | num()
- }
- void condition() #Condition : {}
- {
- <LBR> condition() <RBR> [( <AND> | <OR>) condition()]
- |<NOT> condition()
- | expression() [( signs() ) expression()] [( <AND> | <OR>) condition()]
- }
- void signs() #Signs : {}
- {
- equal()
- | <NOT_EQUAL_SIGN>
- { jjtThis.value = token; }
- | <LESS_THAN>
- { jjtThis.value = token; }
- | <GREATER_THAN>
- { jjtThis.value = token; }
- | <LESS_EQUALS>
- { jjtThis.value = token; }
- | <GREATER_EQUALS>
- { jjtThis.value = token; }
- }
- LinkedList<String> ident_list() #IdentList : { String nameHolder; LinkedList<String> names = new LinkedList<String>(); LinkedList<String> extras = new LinkedList<String>(); }
- {
- nameHolder = id() extras = comma_list()
- {
- names.add(nameHolder);
- if(!extras.isEmpty()){
- names.addAll(extras);
- }
- return names; }
- }
- LinkedList<String> comma_list() : { String nameHolder= ""; LinkedList<String> names = new LinkedList<String>(); LinkedList<String> extras = new LinkedList<String>(); }
- {
- <COMMA> nameHolder = id() comma_list() | {}
- {
- names.add(nameHolder);
- if(!extras.isEmpty()){
- names.addAll(extras);
- }
- return names; }
- }
- void arg_list() #ArgList : {}
- {
- ( id() ( <COMMA> id() )* | {} )
- }
- void num() #Num : {}
- {
- <NUM>
- { jjtThis.value = token; }
- }
- void bool() #Bool : {}
- {
- <TRUE>
- { jjtThis.value = token; }
- | <FALSE>
- { jjtThis.value = token; }
- }
- String id() #Id : {Token t;}
- {
- t = <ID> { jjtThis.value = t.image; return t.image; }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement