Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * cool.cup
- * Parser definition for the COOL language.
- *
- */
- import java_cup.runtime.*;
- /* Stuff enclosed in {: :} is copied verbatim to the class containing
- all parser actions. All the extra variables/functions you want to use
- in the semantic actions should go here. Don't remove or modify anything
- that was there initially. */
- action code {:
- int curr_lineno() {
- return ((CoolTokenLexer)parser.getScanner()).curr_lineno();
- }
- AbstractSymbol curr_filename() {
- return ((CoolTokenLexer)parser.getScanner()).curr_filename();
- }
- :}
- /************************************************************************/
- /* DONT CHANGE ANYTHING IN THIS SECTION */
- parser code {:
- int omerrs = 0;
- public void syntax_error(Symbol cur_token) {
- int lineno = action_obj.curr_lineno();
- String filename = action_obj.curr_filename().getString();
- System.err.print("\"" + filename + "\", line " + lineno +
- ": syntax error at or near ");
- Utilities.printToken(cur_token);
- omerrs++;
- if (omerrs>50) {
- System.err.println("More than 50 errors");
- System.exit(1);
- }
- }
- public void unrecovered_syntax_error(Symbol cur_token) {
- }
- :}
- /* Declare the terminals; a few have types for associated lexemes. The
- token ERROR is never used in the parser; thus, it is a parse error when
- the lexer returns it. */
- terminal CLASS, ELSE, FI, IF, IN, INHERITS, LET, LET_STMT, LOOP, POOL, THEN, WHILE;
- terminal CASE, ESAC, OF, DARROW, NEW, ISVOID;
- terminal ASSIGN, NOT, LE, ERROR;
- terminal PLUS, DIV, MINUS, MULT, EQ, LT, DOT, NEG, COMMA, SEMI, COLON;
- terminal LPAREN, RPAREN, AT, LBRACE, RBRACE;
- terminal AbstractSymbol STR_CONST, INT_CONST;
- terminal Boolean BOOL_CONST;
- terminal AbstractSymbol TYPEID, OBJECTID;
- /* DON'T CHANGE ANYTHING ABOVE THIS LINE, OR YOUR PARSER WONT WORK */
- /**************************************************************************/
- /* Complete the nonterminal list below, giving a type for the semantic
- value of each non terminal. (See the CUP documentation for details. */
- nonterminal Program program;
- nonterminal Classes class_list;
- nonterminal Class_ class;
- nonterminal Features dummy_feature_list, feature_list;
- nonterminal Feature feature;
- nonterminal Formals dummy_formal_list, formal_list;
- nonterminal Formal formal;
- nonterminal Expression expr,method,if_stmt,while_stmt;
- nonterminal Expressions expr_list;
- nonterminal Expressions dummy_expr_list;
- nonterminal Expressions expr_block;
- nonterminal Expression let_stmt, let_stmt_list;
- nonterminal branch case;
- nonterminal Cases case_list;
- nonterminal Integer ln;
- /*MODIFICHE*/
- nonterminal Expression mapcar_stmt;
- nonterminal Expression for_stmt;
- nonterminal Expression repeat_stmt;
- /* Precedence declarations go here. */
- precedence right ASSIGN;
- precedence left NOT;
- precedence nonassoc LE, LT, EQ;
- precedence left MINUS, PLUS;
- precedence left MULT, DIV;
- precedence left ISVOID;
- precedence left NEG;
- precedence left AT;
- precedence left DOT;
- program
- ::= ln:ln class_list:cl
- {: RESULT = new programc(ln, cl); :}
- ;
- class_list
- ::=
- /* single class */
- ln:ln class:c SEMI
- {: RESULT = (new Classes(ln)).appendElement(c); :}
- /* several classes */
- | class_list:cl class:c SEMI
- {: RESULT = cl.appendElement(c); :}
- | class_list:cl error SEMI
- {: RESULT = cl; :}
- | ln:ln error SEMI
- {: RESULT = new Classes(ln); :}
- ;
- /* If no parent is specified, the class inherits from the Object class */
- class
- ::=
- ln:ln CLASS TYPEID:n LBRACE dummy_feature_list:fl RBRACE
- {: RESULT = new class_c(ln, n,
- AbstractTable.idtable.addString("Object"),
- fl, curr_filename()); :}
- | ln:ln CLASS TYPEID:n INHERITS TYPEID:p LBRACE dummy_feature_list:fl RBRACE
- {: RESULT = new class_c(ln, n, p, fl, curr_filename()); :}
- ;
- /* Feature list may be empty, but no empty features in list. */
- dummy_feature_list
- ::=
- /* empty */
- {: RESULT = new Features(curr_lineno()); :}
- | feature_list:fl
- {: RESULT = fl; :}
- ;
- feature_list
- /* single feature */
- ::= ln:ln feature:f SEMI
- {: RESULT = (new Features(ln)).appendElement(f); :}
- /* several features */
- | feature_list:fl feature:f SEMI
- {: RESULT = fl.appendElement(f); :}
- | feature_list:fl error SEMI
- {: RESULT = fl; :}
- | ln:ln error SEMI
- {: RESULT = new Features(ln); :}
- ;
- feature
- ::=
- /* ID() : TYPE { expr } */
- ln:ln OBJECTID:o LPAREN dummy_formal_list:l RPAREN COLON TYPEID:t LBRACE expr:e RBRACE
- {: RESULT = new method(ln,o,l,t,e); :}
- /* ID : TYPE */
- | ln:ln OBJECTID:n COLON TYPEID:t
- {: RESULT = new attr(ln, n, t, new no_expr(0)); :}
- | /* ID : TYPE <- expr */
- ln:ln OBJECTID:o COLON TYPEID:t ASSIGN expr:e
- {: RESULT = new attr(ln,o,t,e); :}
- ;
- dummy_formal_list
- ::=
- /*empty*/
- ln:ln
- {: RESULT = new Formals(ln); :}
- | formal_list:fl
- {: RESULT = fl; :}
- ;
- /* Formal list */
- formal_list
- ::=
- ln:ln formal:f
- {: RESULT = new Formals(ln).appendElement(f); :}
- /* ID1 : TYPE1, ID2 : TYPE2 */
- | formal_list:fl COMMA formal:f
- {: RESULT = fl.appendElement(f); :}
- | ln:ln error
- {: RESULT = new Formals(ln); :}
- | formal_list:fl COMMA error
- {: RESULT = fl; :}
- ;
- /* Formal */
- formal
- ::=
- /* ID : TYPE */
- ln:ln OBJECTID:o COLON TYPEID:t
- {: RESULT = new formalc(ln, o, t); :}
- ;
- dummy_expr_list
- ::=
- /* empty */
- {: RESULT = new Expressions(curr_lineno()); :}
- | expr_list:el
- {: RESULT = el; :}
- ;
- expr_list
- /* expr, expr, ... expr */
- ::=
- ln:ln expr:e
- {: RESULT = (new Expressions(ln)).appendElement(e); :}
- | expr_list:el COMMA expr:e
- {: RESULT = el.appendElement(e); :}
- | ln:ln error
- {: RESULT = new Expressions(ln); :}
- | expr_list:el COMMA error
- {: RESULT = el; :}
- ;
- /* Expressions */
- expr
- ::=
- /* ID <- expr */
- ln:ln OBJECTID:n ASSIGN expr:e ln:ln1
- {: RESULT = new assign(ln, n, e); :}
- /* metodo */
- | method:m
- {: RESULT = m; :}
- /* if */
- | if_stmt:is
- {: RESULT = is; :}
- /*MAPCAR*/
- | mapcar_stmt:mapst
- {: RESULT = mapst; :}
- /*FOR*/
- | for_stmt:forst
- {: RESULT = forst; :}
- /*REPEAT*/
- | repeat_stmt:rps
- {: RESULT = rps; :}
- /* while */
- | while_stmt:ws
- {: RESULT = ws; :}
- /* {expr1; expr2; ... exprN;} */
- | ln:ln LBRACE expr_block:b RBRACE
- {: RESULT = new block(ln, b); :}
- /* let statement */
- | LET let_stmt:ls
- {: RESULT = ls; :}
- /* case statement */
- | ln:ln CASE expr:e OF case_list:cl ESAC
- {: RESULT = new typcase(ln, e, cl); :}
- /* new TYPE */
- | ln:ln NEW TYPEID:t
- {: RESULT = new new_(ln, t); :}
- /* isvoid expr */
- | ln:ln ISVOID expr:e ln:ln1
- {: RESULT = new isvoid(ln, e); :}
- /* e1 + e2 */
- | expr:e1 ln:ln PLUS expr:e2 ln:ln1
- {: RESULT = new plus(ln, e1, e2); :}
- /* e1 - e2 */
- | expr:e1 ln:ln MINUS expr:e2 ln:ln1
- {: RESULT = new sub(ln, e1, e2); :}
- /* e1 * e2 */
- | expr:e1 ln:ln MULT expr:e2 ln:ln1
- {: RESULT = new mul(ln, e1, e2); :}
- /* e1 / e2 */
- | expr:e1 ln:ln DIV expr:e2 ln:ln1
- {: RESULT = new divide(ln, e1, e2); :}
- /* ~expr */
- | ln:ln NEG expr:e ln:ln1
- {: RESULT = new neg(ln, e); :}
- /* e1 < e2 */
- | expr:e1 ln:ln LT expr:e2 ln:ln1
- {: RESULT = new lt(ln, e1, e2); :}
- /*MODIFICA MAGGIORE*/
- | expr:e1 ln:ln ISVOID expr:e2 ln:ln1
- {: RESULT = new lt(ln,e2,e1); :}
- /* e1 <= e2 */
- | expr:e1 ln:ln LE expr:e2 ln:ln1
- {: RESULT = new leq(ln, e1, e2); :}
- /* e1 = e2 */
- | expr:e1 ln:ln EQ expr:e2 ln:ln1
- {: RESULT = new eq(ln, e1, e2); :}
- /* !expr */
- | ln:ln NOT expr:e ln:ln1
- {: RESULT = new comp(ln, e); :}
- /* (expr) */
- | LPAREN expr:e RPAREN
- {: RESULT = e; :}
- /* identificatori */
- | ln:ln OBJECTID:n
- {: RESULT = new object(ln, n); :}
- /* costanti intere */
- | ln:ln INT_CONST:t
- {: RESULT = new int_const(ln, t); :}
- /* costanti stringhe */
- | ln:ln STR_CONST:t
- {: RESULT = new string_const(ln, t); :}
- /* costanti booleane */
- | ln:ln BOOL_CONST:v
- {: RESULT = new bool_const(ln, v); :}
- ;
- method
- ::=
- /* expr.ID() or expr.ID(arg0,arg1,arg2) */
- expr:e ln:ln DOT OBJECTID:o LPAREN dummy_expr_list:el RPAREN
- {: RESULT = new dispatch(ln, e, o, el); :}
- /* expr@[TYPE].ID() or expr@[TYPE].ID(arg0,arg1,arg2) */
- | expr:e AT TYPEID:t ln:ln DOT OBJECTID:o LPAREN dummy_expr_list:el RPAREN
- {: RESULT = new static_dispatch(ln, e, t, o, el); :}
- /* ID() or ID(arg0,arg1,arg2) */
- | ln:ln OBJECTID:o LPAREN dummy_expr_list:el RPAREN
- {: RESULT = new dispatch(ln, new object(ln, TreeConstants.self), o, el); :}
- ;
- if_stmt
- ::= ln:ln IF expr:p THEN expr:te ELSE expr:ee FI
- {: RESULT = new cond(ln, p, te, ee); :}
- ;
- /*MODIFICA MAPCAR*/
- mapcar_stmt
- ::= ln:ln POOL OBJECTID:o DOT LPAREN expr_list:el RPAREN
- {:
- Expressions exprs = new Expressions(ln);
- for (int i=0; i<el.getLength(); i++){
- exprs.appendElement(new dispatch(ln,new object(ln,TreeConstants.self),o, new Expressions(ln).appendElement(el.getNth(i))));
- }
- RESULT = new block(ln,exprs);
- :}
- ;
- /*MODIFICA FOR*/
- for_stmt
- ::= ln:ln POOL LPAREN expr:e1 SEMI expr:e2 SEMI expr:e3 RPAREN expr:e4 LOOP
- {:
- Expressions exprs = new Expressions(ln);
- exprs.appendElement(e4);
- exprs.appendElement(e3);
- Expression block = new block(ln, exprs);
- Expression forBlock = new loop(ln, e2, block);
- Expressions exprs2 = new Expressions(ln);
- exprs2.appendElement(e1);
- exprs2.appendElement(forBlock);
- RESULT = new block(ln, exprs2);
- :}
- ;
- /*MODIFICA REPEAT UNTIL*/
- repeat_stmt
- ::=ln:ln LOOP expr:e1 WHILE expr:e2 ln:ln1
- {:
- Expressions exprs = new Expressions(ln);
- exprs.appendElement(e1);
- Expression repeatBlock = new loop(ln, e2, e1);
- exprs.appendElement(repeatBlock);
- RESULT = new block(ln, exprs);
- :}
- ;
- while_stmt
- ::= ln:ln WHILE expr:p LOOP expr:e POOL
- {: RESULT = new loop(ln, p, e); :}
- ;
- expr_block
- /* {expr1; expr2; ... exprN;} */
- ::=
- ln:ln expr:e SEMI
- {: RESULT =(new Expressions(ln)).appendElement(e); :}
- | expr_block:el expr:e SEMI
- {: RESULT = el.appendElement(e); :}
- | ln:ln error SEMI
- {: RESULT = new Expressions(ln); :}
- | expr_block:el error SEMI
- {: RESULT = el; :}
- ;
- let_stmt
- ::=
- ln:ln OBJECTID:o COLON TYPEID:t let_stmt_list:rest_of_let
- {: RESULT = new let(ln, o, t, new no_expr(0),rest_of_let); :}
- | ln:ln OBJECTID:o COLON TYPEID:t ASSIGN expr:val let_stmt_list:rest
- {: RESULT = new let(ln, o, t, val, rest); :}
- | error let_stmt_list:rest
- ;
- let_stmt_list
- ::=
- /* 1. Implicit initializer */
- COMMA ln:ln OBJECTID:id COLON TYPEID:type let_stmt_list:rest
- {: RESULT = new let(ln, id, type, new no_expr(0), rest); :}
- /* 2. Explicit initializer */
- | COMMA ln:ln OBJECTID:id COLON TYPEID:type ASSIGN expr:val let_stmt_list:rest
- {: RESULT = new let(ln, id, type, val, rest); :}
- /* 3. End-of-list, i.e. reached the body */
- | IN expr:body ln:ln
- {: RESULT = body; :}
- | COMMA error let_stmt_list:rest
- ;
- case_list
- ::=
- ln:ln case:c
- {: RESULT = new Cases(ln).appendElement(c); :}
- | case_list:cl case:c
- {: RESULT = cl.appendElement(c); :}
- ;
- case
- ::=
- ln:ln OBJECTID:o COLON TYPEID:t DARROW expr:e SEMI
- {: RESULT = new branch(ln, o, t, e); :}
- ;
- ln
- ::=
- {: RESULT = curr_lineno(); :}
- ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement