Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.85 KB | None | 0 0
  1. import java_cup.runtime.*;
  2. import java.io.*;
  3. import java.lang.String;
  4. import java.util.Hashtable;
  5.  
  6.  
  7. action code {:
  8.     static Hashtable tabla=new Hashtable();
  9.     static Hashtable variables=new Hashtable();
  10.     static String patternContentStart = "${";
  11.     static String patternContentEnd = "}";
  12.     static String stringType = "STRING";
  13.     static String intType = "INT";
  14.     static String boolType = "BOOL";
  15.    
  16.     String andOrBoolean(String E1, String E2){
  17.         if(!E1.equals(boolType)){
  18.              System.err.println("Error at line " + parser.yy.getLine() + ": Left hand side AND/OR must be boolean");
  19.         }
  20.         if(!E2.equals(boolType)){
  21.             System.err.println("Error at line " + parser.yy.getLine() + ": Right hand side AND/OR must be boolean");
  22.         }
  23.         return boolType;
  24.     }
  25.     String expInteger(String T, String operator, String E){
  26.         if(!T.equals(intType)){
  27.             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");
  28.         }
  29.         if(!E.equals(intType)){
  30.             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");
  31.         }
  32.         return intType;
  33.     }
  34.     String comparation(String E2){
  35.         if(E2.equals(stringType) || E2.equals(boolType)){
  36.              System.err.println("Error at line " + parser.yy.getLine() + ": Right hand side of comparation must be integer");
  37.         }
  38.         return boolType;
  39.     }
  40. :}
  41.  
  42. parser code {:
  43.  
  44.         static FileInputStream FInStr = null;
  45.         public static String fInName;
  46.  
  47.         // The lexer class is stored as a member field to allow
  48.         // access to its getLine() method.
  49.         public static Yylex yy;
  50.  
  51.         // This variable can be used in the grammar action rules
  52.         // to count the number of patterns
  53.         public static int numPatterns = 0;
  54.  
  55.         // This variable can be used in the grammar action rules
  56.         // to count the number of variables referenced in an action
  57.         public static int numVariables = 0;
  58.         // fitxer entrada
  59.         public static void main (String argv[]) throws Exception {
  60.             parser analitzador;
  61.             //Analitzador
  62.             if (argv.length<1) {
  63.                 //Obtencio del nom del fitxer d.entrada
  64.                 System.out.println ("Numero parametres incorrecte!");
  65.             } else {
  66.                 fInName = argv[0];
  67.                 try {
  68.                     FInStr = new FileInputStream(fInName); //Obre fitxer
  69.                 } catch (FileNotFoundException e) {
  70.                     System.out.println (fInName + ": Impossible obrir fitxer!");
  71.                     return;
  72.                 }
  73.                 yy = new Yylex(FInStr);
  74.                 analitzador = new parser(yy);
  75.                 analitzador.parse();
  76.             }
  77.         } //Fi main
  78.  
  79. :}
  80.  
  81. terminal Integer INTEGER_LITERAL;
  82. terminal PATTERN_START, WILDCARD, PATTERN_VAR, PATTERN_TEXT, PATTERN_END;
  83. terminal STRING_LITERAL, IDENTIFIER;
  84. terminal INTENT, ARGUMENTS, PATTERNS, STRING, INT, ACTION, ANSWER, IF, ELSE, EXECUTES;
  85. terminal ASSIGNMENT, CONCAT, PLUS, MINUS, PRODUCT, DIVISION, LESS, GREATER, LESS_EQ, AND, OR, NOT;
  86. terminal GREATER_EQ, NOT_EQUALS, EQUALS, OPEN_PAREN, CLOSE_PAREN;
  87. terminal OPEN_BRACKET, CLOSE_BRACKET, SEMICOLON, COLON, COMMA;
  88. non terminal program, declaration, intent_decl, intent_body, arguments_section, type, arguments_varlist, patterns_section, pattern_list, single_pattern, pattern_contents, action_decl;
  89. 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;
  90. non terminal expr, term, factor, executes_decl, answer_statement;
  91.  
  92.  
  93. ///////////////////////////////////////
  94. // Your parser definition goes here: //
  95. ///////////////////////////////////////
  96. program ::= declaration program | /* Epsilon */;
  97. declaration ::= intent_decl | action_decl | executes_decl;
  98.  
  99. // Intent declaration
  100. intent_decl ::= INTENT IDENTIFIER:E1 OPEN_BRACKET intent_body CLOSE_BRACKET
  101. {:
  102.     variables.clear();
  103.     if(tabla.containsKey(E1)){
  104.         System.err.println("Error at line " + parser.yy.getLine() + ": Duplicated declaration of " + E1);
  105.     }
  106.     else{
  107.         tabla.put(E1,"INTENT");
  108.     }
  109. :};
  110. intent_body ::= arguments_section patterns_section | patterns_section arguments_section;
  111. arguments_section ::= ARGUMENTS COLON arguments_varlist;
  112. type ::= STRING
  113.     {:
  114.         RESULT = stringType;
  115.     :}
  116.     | INT
  117.     {:
  118.         RESULT = intType;
  119.     :};
  120. arguments_varlist ::= /* Epsilon */
  121.     | type IDENTIFIER:E1 SEMICOLON arguments_varlist
  122.     {:
  123.         String toAnalize = E1.toString();
  124.         if(variables.containsKey(toAnalize)){
  125.             System.err.println("Error at line " + parser.yy.getLine() + ": Redeclaration of " + toAnalize);
  126.         }
  127.         else{
  128.             variables.put(toAnalize,toAnalize);
  129.         }
  130.     :};
  131. patterns_section ::= PATTERNS COLON pattern_list;
  132. pattern_list ::= single_pattern
  133.     | single_pattern pattern_list
  134.     | error {: System.err.println("Error at line "+parser.yy.getLine()+": pattern list cannot be empty"); :} ;
  135. single_pattern ::= PATTERN_START pattern_contents PATTERN_END SEMICOLON;
  136. pattern_contents ::= /* Epsilon */
  137.     | WILDCARD pattern_contents
  138.     | PATTERN_TEXT:E1 pattern_contents
  139.     {:
  140.         String toAnalize = E1.toString();
  141.         toAnalize = toAnalize.replace(patternContentStart,"").replace(patternContentEnd,"").trim();
  142.         if(toAnalize.length() > 0 && !variables.containsKey(toAnalize)){
  143.             System.err.println("Error at line "+parser.yy.getLine()+": Intent declaration uses undeclared variables in patterns ["+ toAnalize + "]");
  144.         }
  145.     :}
  146.     | PATTERN_VAR:E1 pattern_contents
  147.     {:
  148.         String toAnalize = E1.toString();
  149.         toAnalize = toAnalize.replace(patternContentStart,"").replace(patternContentEnd,"").trim();
  150.         if(toAnalize.length() > 0 && !variables.containsKey(toAnalize)){
  151.             System.err.println("Error at line "+parser.yy.getLine()+": Intent declaration uses undeclared variables in patterns ["+ toAnalize + "]");
  152.         }
  153.     :};
  154.  
  155.  
  156. // Action declaration
  157. action_decl ::= ACTION IDENTIFIER:E1
  158. action_arguments OPEN_BRACKET action_body CLOSE_BRACKET
  159. {:
  160.     variables.clear();
  161.     if(tabla.containsKey(E1)){
  162.         System.err.println("Error at line " + parser.yy.getLine() + ": Duplicated declaration of " + E1);
  163.     }
  164.     else{
  165.         tabla.put(E1,"ACTION");
  166.     }
  167. :};
  168.  
  169. action_arguments ::= OPEN_PAREN action_varlist CLOSE_PAREN | OPEN_PAREN CLOSE_PAREN;
  170. action_varlist ::= type IDENTIFIER | type IDENTIFIER COMMA action_varlist;
  171. action_body ::= action_body action_statement | /* Epsilon */;
  172. action_statement ::= variable_declaration | variable_assignment | if_statement | answer_statement;
  173.  
  174. // Action body statements
  175. variable_declaration ::= type:E0 IDENTIFIER:E1 SEMICOLON
  176.     {:
  177.         String toAnalize = E1.toString();
  178.         if(variables.containsKey(toAnalize)){
  179.             System.err.println("Error at line " + parser.yy.getLine() + ": Redeclaration of " + toAnalize);
  180.         }
  181.         else{
  182.             variables.put(toAnalize,E0);
  183.         }
  184.     :}
  185.     | type:E0 IDENTIFIER:E1 ASSIGNMENT expression:E2 SEMICOLON
  186.     {:
  187.         String toAnalize = E1.toString();
  188.         if(variables.containsKey(toAnalize)){
  189.             System.err.println("Error at line " + parser.yy.getLine() + ": Redeclaration of " + toAnalize);
  190.         }
  191.         else{
  192.             variables.put(toAnalize, E0);
  193.         }
  194.         if(!variables.get(E1.toString()).equals(E2.toString())){
  195.             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()));
  196.         }
  197.     :};
  198. variable_assignment ::= IDENTIFIER:E1 ASSIGNMENT expression:E2 SEMICOLON
  199.     {:
  200.         String toAnalize = E1.toString();
  201.         if(!variables.containsKey(toAnalize)){
  202.             System.err.println("Error at line " + parser.yy.getLine() + ": Undeclared identifier " + toAnalize);
  203.         }
  204.         else if(!variables.get(E1.toString()).equals(E2.toString())){
  205.             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()));
  206.         }
  207.     :};
  208. if_statement ::= IF OPEN_PAREN expression:E1 CLOSE_PAREN OPEN_BRACKET action_body CLOSE_BRACKET else_branch
  209. {:
  210.     if(!E1.equals(boolType)){
  211.         System.err.println("Error at line "+parser.yy.getLine()+": The condition of an if statement must be a boolean. ");
  212.     }
  213. :};
  214. answer_statement ::= ANSWER expression:E1 SEMICOLONç
  215. {:
  216.     if(!E1.equals(stringType))
  217.         System.err.println("Error at line "+parser.yy.getLine()+": An answer statement must be used with a string.");
  218. :}|
  219.     IF OPEN_BRACKET
  220.     error
  221.     {: System.err.println("Error at line "+parser.yy.getLine()+": if statement without condition"); :}
  222.     CLOSE_BRACKET;
  223. else_branch ::= ELSE OPEN_BRACKET action_body CLOSE_BRACKET | /* Epsilon */;
  224.  
  225. // Expressions
  226. expression ::=
  227.     and_expr:E1 OR expression:E2
  228.     {:
  229.         RESULT = andOrBoolean(E1.toString(), E2.toString());
  230.     :}
  231.     | and_expr:E1
  232.     {:
  233.         RESULT = E1;
  234.     :};
  235. and_expr ::=
  236.     not_expr:E1 AND and_expr:E2
  237.     {:
  238.         RESULT = andOrBoolean(E1.toString(), E2.toString());
  239.     :}
  240.     | not_expr:E1
  241.     {:
  242.         RESULT = E1;
  243.     :};
  244. not_expr ::=
  245.     NOT comp_expr:E1
  246.     {:
  247.         if(!E1.equals(boolType)){
  248.              System.err.println("Error at line " + parser.yy.getLine() + ": Right hand side NOT must be boolean");
  249.         }  
  250.         RESULT = boolType;
  251.     :}
  252.     | comp_expr:E1
  253.     {:
  254.         RESULT = E1;
  255.     :};
  256. comp_expr ::=
  257.     comp_expr LESS expr:E2
  258.     | comp_expr GREATER expr:E2
  259.     {:
  260.         RESULT = comparation(E2.toString());
  261.     :}
  262.     | comp_expr LESS_EQ expr:E2
  263.     {:
  264.         RESULT = comparation(E2.toString());
  265.     :}
  266.     | comp_expr GREATER_EQ expr:E2
  267.     {:
  268.         RESULT = comparation(E2.toString());
  269.     :}
  270.     | comp_expr EQUALS expr:E2
  271.     {:
  272.         RESULT = comparation(E2.toString());
  273.     :}
  274.     | comp_expr NOT_EQUALS expr:E2
  275.     {:
  276.         RESULT = comparation(E2.toString());
  277.     :}
  278.     | expr:E1
  279.     {:
  280.         RESULT = E1;
  281.     :};
  282. expr ::=
  283.     (term:T PLUS expr:E
  284.     {:
  285.         RESULT = expInteger(T.toString(), "+", E.toString());
  286.     :}
  287.     | term:T MINUS expr:E
  288.     {:
  289.         RESULT = expInteger(T.toString(), "-", E.toString());
  290.     :}
  291.     | term:T CONCAT expr)
  292.     {:
  293.         if(T.equals(boolType)){
  294.             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");
  295.         }
  296.         RESULT = T;
  297.     :}
  298.     | term:T
  299.     {:
  300.         RESULT = T;
  301.     :};
  302. term ::=
  303.     (factor:F PRODUCT term:T
  304.     {:
  305.         RESULT = expInteger(F.toString(), "*", T.toString());
  306.     :}
  307.     | factor:F DIVISION term:T
  308.     {:
  309.         RESULT = expInteger(F.toString(), "//", T.toString());
  310.     :}
  311.     | factor:F)
  312.     {:
  313.         RESULT = F;
  314.     :};
  315. factor ::=
  316.     OPEN_PAREN expression:E1 CLOSE_PAREN
  317.     {:
  318.         RESULT = E1;
  319.     :}
  320.     | IDENTIFIER:E1
  321.         {:
  322.         String toAnalize = E1.toString();
  323.         if(!variables.containsKey(toAnalize)){
  324.             System.err.println("Error at line " + parser.yy.getLine() + ": Undeclared identifier " + toAnalize);
  325.             RESULT = intType;
  326.         }else{
  327.             RESULT = variables.get(toAnalize);
  328.         }
  329.     :}
  330.     | INTEGER_LITERAL {:RESULT = intType;:}
  331.     | STRING_LITERAL {:RESULT = stringType;:};
  332.  
  333. // Executes declarations
  334. executes_decl ::= IDENTIFIER:E1 EXECUTES IDENTIFIER:E2 SEMICOLON
  335. {:
  336.     if(!tabla.containsKey(E1)){
  337.         System.err.println("Error at line " + parser.yy.getLine() + ": In executes declaration, undeclared intent " + E1.toString());
  338.     }else if(!tabla.get(E1).equals("INTENT")){
  339.         System.err.println("Error at line " + parser.yy.getLine() + ": In executes declaration, left hand side, " + E1.toString() + ", must be an intent.");
  340.     }
  341.     if(!tabla.containsKey(E2)){
  342.         System.err.println("Error at line " + parser.yy.getLine() + ": In executes declaration, undeclared action " + E2.toString());
  343.     }else if(!tabla.get(E2).equals("ACTION")){
  344.         System.err.println("Error at line " + parser.yy.getLine() + ": In executes declaration, left hand side, " + E2.toString() + ", must be an action.");
  345.     }
  346. :}
  347.     | 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