Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java_cup.runtime.*;
- import java.io.*;
- import java.lang.String;
- import java.util.Hashtable;
- action code {:
- static Hashtable tabla=new Hashtable();
- static Hashtable variables=new Hashtable();
- static String patternContentStart = "${";
- static String patternContentEnd = "}";
- static String stringType = "STRING";
- static String intType = "INT";
- static String boolType = "BOOL";
- String andOrBoolean(String E1, String E2){
- if(!E1.equals(boolType)){
- System.err.println("Error at line " + parser.yy.getLine() + ": Left hand side AND/OR must be boolean");
- }
- if(!E2.equals(boolType)){
- System.err.println("Error at line " + parser.yy.getLine() + ": Right hand side AND/OR must be boolean");
- }
- return boolType;
- }
- String expInteger(String T, String operator, String E){
- if(!T.equals(intType)){
- System.err.println("Error at line " + parser.yy.getLine() + ": Left hand side of operator " + operator + " must be a integer but a "+ T.toString() +" was provided instead");
- }
- if(!E.equals(intType)){
- System.err.println("Error at line " + parser.yy.getLine() + ": Right hand side of operator " + operator + " must be a integer but a "+ E.toString() +" was provided instead");
- }
- return intType;
- }
- String comparation(String E2){
- if(E2.equals(stringType) || E2.equals(boolType)){
- System.err.println("Error at line " + parser.yy.getLine() + ": Right hand side of comparation must be integer");
- }
- return boolType;
- }
- :}
- parser code {:
- static FileInputStream FInStr = null;
- public static String fInName;
- // The lexer class is stored as a member field to allow
- // access to its getLine() method.
- public static Yylex yy;
- // This variable can be used in the grammar action rules
- // to count the number of patterns
- public static int numPatterns = 0;
- // This variable can be used in the grammar action rules
- // to count the number of variables referenced in an action
- public static int numVariables = 0;
- // fitxer entrada
- public static void main (String argv[]) throws Exception {
- parser analitzador;
- //Analitzador
- if (argv.length<1) {
- //Obtencio del nom del fitxer d.entrada
- System.out.println ("Numero parametres incorrecte!");
- } else {
- fInName = argv[0];
- try {
- FInStr = new FileInputStream(fInName); //Obre fitxer
- } catch (FileNotFoundException e) {
- System.out.println (fInName + ": Impossible obrir fitxer!");
- return;
- }
- yy = new Yylex(FInStr);
- analitzador = new parser(yy);
- analitzador.parse();
- }
- } //Fi main
- :}
- terminal Integer INTEGER_LITERAL;
- terminal PATTERN_START, WILDCARD, PATTERN_VAR, PATTERN_TEXT, PATTERN_END;
- terminal STRING_LITERAL, IDENTIFIER;
- terminal INTENT, ARGUMENTS, PATTERNS, STRING, INT, ACTION, ANSWER, IF, ELSE, EXECUTES;
- terminal ASSIGNMENT, CONCAT, PLUS, MINUS, PRODUCT, DIVISION, LESS, GREATER, LESS_EQ, AND, OR, NOT;
- terminal GREATER_EQ, NOT_EQUALS, EQUALS, OPEN_PAREN, CLOSE_PAREN;
- terminal OPEN_BRACKET, CLOSE_BRACKET, SEMICOLON, COLON, COMMA;
- non terminal program, declaration, intent_decl, intent_body, arguments_section, type, arguments_varlist, patterns_section, pattern_list, single_pattern, pattern_contents, action_decl;
- non terminal action_arguments, action_varlist, action_body, action_statement, variable_declaration, variable_assignment, if_statement, else_branch, expression, and_expr, not_expr, comp_expr;
- non terminal expr, term, factor, executes_decl, answer_statement;
- ///////////////////////////////////////
- // Your parser definition goes here: //
- ///////////////////////////////////////
- program ::= declaration program | /* Epsilon */;
- declaration ::= intent_decl | action_decl | executes_decl;
- // Intent declaration
- intent_decl ::= INTENT IDENTIFIER:E1 OPEN_BRACKET intent_body CLOSE_BRACKET
- {:
- variables.clear();
- if(tabla.containsKey(E1)){
- System.err.println("Error at line " + parser.yy.getLine() + ": Duplicated declaration of " + E1);
- }
- else{
- tabla.put(E1,"INTENT");
- }
- :};
- intent_body ::= arguments_section patterns_section | patterns_section arguments_section;
- arguments_section ::= ARGUMENTS COLON arguments_varlist;
- type ::= STRING
- {:
- RESULT = stringType;
- :}
- | INT
- {:
- RESULT = intType;
- :};
- arguments_varlist ::= /* Epsilon */
- | type IDENTIFIER:E1 SEMICOLON arguments_varlist
- {:
- String toAnalize = E1.toString();
- if(variables.containsKey(toAnalize)){
- System.err.println("Error at line " + parser.yy.getLine() + ": Redeclaration of " + toAnalize);
- }
- else{
- variables.put(toAnalize,toAnalize);
- }
- :};
- patterns_section ::= PATTERNS COLON pattern_list;
- pattern_list ::= single_pattern
- | single_pattern pattern_list
- | error {: System.err.println("Error at line "+parser.yy.getLine()+": pattern list cannot be empty"); :} ;
- single_pattern ::= PATTERN_START pattern_contents PATTERN_END SEMICOLON;
- pattern_contents ::= /* Epsilon */
- | WILDCARD pattern_contents
- | PATTERN_TEXT:E1 pattern_contents
- {:
- String toAnalize = E1.toString();
- toAnalize = toAnalize.replace(patternContentStart,"").replace(patternContentEnd,"").trim();
- if(toAnalize.length() > 0 && !variables.containsKey(toAnalize)){
- System.err.println("Error at line "+parser.yy.getLine()+": Intent declaration uses undeclared variables in patterns ["+ toAnalize + "]");
- }
- :}
- | PATTERN_VAR:E1 pattern_contents
- {:
- String toAnalize = E1.toString();
- toAnalize = toAnalize.replace(patternContentStart,"").replace(patternContentEnd,"").trim();
- if(toAnalize.length() > 0 && !variables.containsKey(toAnalize)){
- System.err.println("Error at line "+parser.yy.getLine()+": Intent declaration uses undeclared variables in patterns ["+ toAnalize + "]");
- }
- :};
- // Action declaration
- action_decl ::= ACTION IDENTIFIER:E1
- action_arguments OPEN_BRACKET action_body CLOSE_BRACKET
- {:
- variables.clear();
- if(tabla.containsKey(E1)){
- System.err.println("Error at line " + parser.yy.getLine() + ": Duplicated declaration of " + E1);
- }
- else{
- tabla.put(E1,"ACTION");
- }
- :};
- action_arguments ::= OPEN_PAREN action_varlist CLOSE_PAREN | OPEN_PAREN CLOSE_PAREN;
- action_varlist ::= type IDENTIFIER | type IDENTIFIER COMMA action_varlist;
- action_body ::= action_body action_statement | /* Epsilon */;
- action_statement ::= variable_declaration | variable_assignment | if_statement | answer_statement;
- // Action body statements
- variable_declaration ::= type:E0 IDENTIFIER:E1 SEMICOLON
- {:
- String toAnalize = E1.toString();
- if(variables.containsKey(toAnalize)){
- System.err.println("Error at line " + parser.yy.getLine() + ": Redeclaration of " + toAnalize);
- }
- else{
- variables.put(toAnalize,E0);
- }
- :}
- | type:E0 IDENTIFIER:E1 ASSIGNMENT expression:E2 SEMICOLON
- {:
- String toAnalize = E1.toString();
- if(variables.containsKey(toAnalize)){
- System.err.println("Error at line " + parser.yy.getLine() + ": Redeclaration of " + toAnalize);
- }
- else{
- variables.put(toAnalize, E0);
- }
- if(!variables.get(E1.toString()).equals(E2.toString())){
- System.err.println("Error at line " + parser.yy.getLine() + ": Trying to assign a " + E2.toString() + " expression to a variable of type " + variables.get(E1.toString()));
- }
- :};
- variable_assignment ::= IDENTIFIER:E1 ASSIGNMENT expression:E2 SEMICOLON
- {:
- String toAnalize = E1.toString();
- if(!variables.containsKey(toAnalize)){
- System.err.println("Error at line " + parser.yy.getLine() + ": Undeclared identifier " + toAnalize);
- }
- else if(!variables.get(E1.toString()).equals(E2.toString())){
- System.err.println("Error at line " + parser.yy.getLine() + ": Trying to assign a " + E2.toString() + " expression to a variable of type " + variables.get(E1.toString()));
- }
- :};
- if_statement ::= IF OPEN_PAREN expression:E1 CLOSE_PAREN OPEN_BRACKET action_body CLOSE_BRACKET else_branch
- {:
- if(!E1.equals(boolType)){
- System.err.println("Error at line "+parser.yy.getLine()+": The condition of an if statement must be a boolean. ");
- }
- :};
- answer_statement ::= ANSWER expression:E1 SEMICOLONç
- {:
- if(!E1.equals(stringType))
- System.err.println("Error at line "+parser.yy.getLine()+": An answer statement must be used with a string.");
- :}|
- IF OPEN_BRACKET
- error
- {: System.err.println("Error at line "+parser.yy.getLine()+": if statement without condition"); :}
- CLOSE_BRACKET;
- else_branch ::= ELSE OPEN_BRACKET action_body CLOSE_BRACKET | /* Epsilon */;
- // Expressions
- expression ::=
- and_expr:E1 OR expression:E2
- {:
- RESULT = andOrBoolean(E1.toString(), E2.toString());
- :}
- | and_expr:E1
- {:
- RESULT = E1;
- :};
- and_expr ::=
- not_expr:E1 AND and_expr:E2
- {:
- RESULT = andOrBoolean(E1.toString(), E2.toString());
- :}
- | not_expr:E1
- {:
- RESULT = E1;
- :};
- not_expr ::=
- NOT comp_expr:E1
- {:
- if(!E1.equals(boolType)){
- System.err.println("Error at line " + parser.yy.getLine() + ": Right hand side NOT must be boolean");
- }
- RESULT = boolType;
- :}
- | comp_expr:E1
- {:
- RESULT = E1;
- :};
- comp_expr ::=
- comp_expr LESS expr:E2
- | comp_expr GREATER expr:E2
- {:
- RESULT = comparation(E2.toString());
- :}
- | comp_expr LESS_EQ expr:E2
- {:
- RESULT = comparation(E2.toString());
- :}
- | comp_expr GREATER_EQ expr:E2
- {:
- RESULT = comparation(E2.toString());
- :}
- | comp_expr EQUALS expr:E2
- {:
- RESULT = comparation(E2.toString());
- :}
- | comp_expr NOT_EQUALS expr:E2
- {:
- RESULT = comparation(E2.toString());
- :}
- | expr:E1
- {:
- RESULT = E1;
- :};
- expr ::=
- (term:T PLUS expr:E
- {:
- RESULT = expInteger(T.toString(), "+", E.toString());
- :}
- | term:T MINUS expr:E
- {:
- RESULT = expInteger(T.toString(), "-", E.toString());
- :}
- | term:T CONCAT expr)
- {:
- if(T.equals(boolType)){
- System.err.println("Error at line " + parser.yy.getLine() + ": Left hand side of operator ++ must be a integer or a string but a BOOLEAN was provided instead");
- }
- RESULT = T;
- :}
- | term:T
- {:
- RESULT = T;
- :};
- term ::=
- (factor:F PRODUCT term:T
- {:
- RESULT = expInteger(F.toString(), "*", T.toString());
- :}
- | factor:F DIVISION term:T
- {:
- RESULT = expInteger(F.toString(), "//", T.toString());
- :}
- | factor:F)
- {:
- RESULT = F;
- :};
- factor ::=
- OPEN_PAREN expression:E1 CLOSE_PAREN
- {:
- RESULT = E1;
- :}
- | IDENTIFIER:E1
- {:
- String toAnalize = E1.toString();
- if(!variables.containsKey(toAnalize)){
- System.err.println("Error at line " + parser.yy.getLine() + ": Undeclared identifier " + toAnalize);
- RESULT = intType;
- }else{
- RESULT = variables.get(toAnalize);
- }
- :}
- | INTEGER_LITERAL {:RESULT = intType;:}
- | STRING_LITERAL {:RESULT = stringType;:};
- // Executes declarations
- executes_decl ::= IDENTIFIER:E1 EXECUTES IDENTIFIER:E2 SEMICOLON
- {:
- if(!tabla.containsKey(E1)){
- System.err.println("Error at line " + parser.yy.getLine() + ": In executes declaration, undeclared intent " + E1.toString());
- }else if(!tabla.get(E1).equals("INTENT")){
- System.err.println("Error at line " + parser.yy.getLine() + ": In executes declaration, left hand side, " + E1.toString() + ", must be an intent.");
- }
- if(!tabla.containsKey(E2)){
- System.err.println("Error at line " + parser.yy.getLine() + ": In executes declaration, undeclared action " + E2.toString());
- }else if(!tabla.get(E2).equals("ACTION")){
- System.err.println("Error at line " + parser.yy.getLine() + ": In executes declaration, left hand side, " + E2.toString() + ", must be an action.");
- }
- :}
- | IDENTIFIER EXECUTES error {: System.err.println("Error at line "+parser.yy.getLine()+": invalid executes declaration"); :} SEMICOLON;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement