Advertisement
Guest User

tuaMamma

a guest
Oct 31st, 2014
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.49 KB | None | 0 0
  1. /*
  2. * cool.cup
  3. * Parser definition for the COOL language.
  4. *
  5. */
  6.  
  7. import java_cup.runtime.*;
  8.  
  9. /* Stuff enclosed in {: :} is copied verbatim to the class containing
  10. all parser actions. All the extra variables/functions you want to use
  11. in the semantic actions should go here. Don't remove or modify anything
  12. that was there initially. */
  13.  
  14. action code {:
  15.  
  16. int curr_lineno() {
  17. return ((CoolTokenLexer)parser.getScanner()).curr_lineno();
  18. }
  19.  
  20. AbstractSymbol curr_filename() {
  21. return ((CoolTokenLexer)parser.getScanner()).curr_filename();
  22. }
  23. :}
  24.  
  25. /************************************************************************/
  26. /* DONT CHANGE ANYTHING IN THIS SECTION */
  27.  
  28. parser code {:
  29. int omerrs = 0;
  30.  
  31. public void syntax_error(Symbol cur_token) {
  32. int lineno = action_obj.curr_lineno();
  33. String filename = action_obj.curr_filename().getString();
  34. System.err.print("\"" + filename + "\", line " + lineno +
  35. ": syntax error at or near ");
  36. Utilities.printToken(cur_token);
  37. omerrs++;
  38. if (omerrs>50) {
  39. System.err.println("More than 50 errors");
  40. System.exit(1);
  41. }
  42. }
  43.  
  44. public void unrecovered_syntax_error(Symbol cur_token) {
  45. }
  46. :}
  47.  
  48. /* Declare the terminals; a few have types for associated lexemes. The
  49. token ERROR is never used in the parser; thus, it is a parse error when
  50. the lexer returns it. */
  51.  
  52. terminal CLASS, ELSE, FI, IF, IN, INHERITS, LET, LET_STMT, LOOP, POOL, THEN, WHILE;
  53. terminal CASE, ESAC, OF, DARROW, NEW, ISVOID;
  54. terminal ASSIGN, NOT, LE, ERROR;
  55. terminal PLUS, DIV, MINUS, MULT, EQ, LT, DOT, NEG, COMMA, SEMI, COLON;
  56. terminal LPAREN, RPAREN, AT, LBRACE, RBRACE;
  57. terminal AbstractSymbol STR_CONST, INT_CONST;
  58. terminal Boolean BOOL_CONST;
  59. terminal AbstractSymbol TYPEID, OBJECTID;
  60.  
  61. /* DON'T CHANGE ANYTHING ABOVE THIS LINE, OR YOUR PARSER WONT WORK */
  62. /**************************************************************************/
  63.  
  64. /* Complete the nonterminal list below, giving a type for the semantic
  65. value of each non terminal. (See the CUP documentation for details. */
  66.  
  67. nonterminal Program program;
  68. nonterminal Classes class_list;
  69. nonterminal Class_ class;
  70. nonterminal Features dummy_feature_list, feature_list;
  71. nonterminal Feature feature;
  72. nonterminal Formals dummy_formal_list, formal_list;
  73. nonterminal Formal formal;
  74. nonterminal Expression expr,method,if_stmt,while_stmt;
  75. nonterminal Expressions expr_list;
  76. nonterminal Expressions dummy_expr_list;
  77. nonterminal Expressions expr_block;
  78. nonterminal Expression let_stmt, let_stmt_list;
  79. nonterminal branch case;
  80. nonterminal Cases case_list;
  81. nonterminal Integer ln;
  82.  
  83. /*MODIFICHE*/
  84. nonterminal Expression mapcar_stmt;
  85. nonterminal Expression for_stmt;
  86. nonterminal Expression repeat_stmt;
  87.  
  88. /* Precedence declarations go here. */
  89. precedence right ASSIGN;
  90. precedence left NOT;
  91. precedence nonassoc LE, LT, EQ;
  92. precedence left MINUS, PLUS;
  93. precedence left MULT, DIV;
  94. precedence left ISVOID;
  95. precedence left NEG;
  96. precedence left AT;
  97. precedence left DOT;
  98.  
  99. program
  100. ::= ln:ln class_list:cl
  101. {: RESULT = new programc(ln, cl); :}
  102. ;
  103.  
  104. class_list
  105. ::=
  106. /* single class */
  107. ln:ln class:c SEMI
  108. {: RESULT = (new Classes(ln)).appendElement(c); :}
  109. /* several classes */
  110. | class_list:cl class:c SEMI
  111. {: RESULT = cl.appendElement(c); :}
  112. | class_list:cl error SEMI
  113. {: RESULT = cl; :}
  114. | ln:ln error SEMI
  115. {: RESULT = new Classes(ln); :}
  116. ;
  117.  
  118. /* If no parent is specified, the class inherits from the Object class */
  119. class
  120. ::=
  121. ln:ln CLASS TYPEID:n LBRACE dummy_feature_list:fl RBRACE
  122. {: RESULT = new class_c(ln, n,
  123. AbstractTable.idtable.addString("Object"),
  124. fl, curr_filename()); :}
  125. | ln:ln CLASS TYPEID:n INHERITS TYPEID:p LBRACE dummy_feature_list:fl RBRACE
  126. {: RESULT = new class_c(ln, n, p, fl, curr_filename()); :}
  127. ;
  128.  
  129. /* Feature list may be empty, but no empty features in list. */
  130. dummy_feature_list
  131. ::=
  132. /* empty */
  133. {: RESULT = new Features(curr_lineno()); :}
  134. | feature_list:fl
  135. {: RESULT = fl; :}
  136. ;
  137.  
  138. feature_list
  139. /* single feature */
  140. ::= ln:ln feature:f SEMI
  141. {: RESULT = (new Features(ln)).appendElement(f); :}
  142. /* several features */
  143. | feature_list:fl feature:f SEMI
  144. {: RESULT = fl.appendElement(f); :}
  145. | feature_list:fl error SEMI
  146. {: RESULT = fl; :}
  147. | ln:ln error SEMI
  148. {: RESULT = new Features(ln); :}
  149. ;
  150.  
  151. feature
  152. ::=
  153. /* ID() : TYPE { expr } */
  154. ln:ln OBJECTID:o LPAREN dummy_formal_list:l RPAREN COLON TYPEID:t LBRACE expr:e RBRACE
  155. {: RESULT = new method(ln,o,l,t,e); :}
  156. /* ID : TYPE */
  157. | ln:ln OBJECTID:n COLON TYPEID:t
  158. {: RESULT = new attr(ln, n, t, new no_expr(0)); :}
  159. | /* ID : TYPE <- expr */
  160. ln:ln OBJECTID:o COLON TYPEID:t ASSIGN expr:e
  161. {: RESULT = new attr(ln,o,t,e); :}
  162. ;
  163.  
  164. dummy_formal_list
  165. ::=
  166. /*empty*/
  167. ln:ln
  168. {: RESULT = new Formals(ln); :}
  169.  
  170. | formal_list:fl
  171. {: RESULT = fl; :}
  172. ;
  173.  
  174. /* Formal list */
  175. formal_list
  176. ::=
  177. ln:ln formal:f
  178. {: RESULT = new Formals(ln).appendElement(f); :}
  179. /* ID1 : TYPE1, ID2 : TYPE2 */
  180. | formal_list:fl COMMA formal:f
  181. {: RESULT = fl.appendElement(f); :}
  182. | ln:ln error
  183. {: RESULT = new Formals(ln); :}
  184. | formal_list:fl COMMA error
  185. {: RESULT = fl; :}
  186. ;
  187.  
  188. /* Formal */
  189. formal
  190. ::=
  191. /* ID : TYPE */
  192. ln:ln OBJECTID:o COLON TYPEID:t
  193. {: RESULT = new formalc(ln, o, t); :}
  194. ;
  195.  
  196. dummy_expr_list
  197. ::=
  198. /* empty */
  199. {: RESULT = new Expressions(curr_lineno()); :}
  200.  
  201. | expr_list:el
  202. {: RESULT = el; :}
  203. ;
  204.  
  205. expr_list
  206. /* expr, expr, ... expr */
  207. ::=
  208. ln:ln expr:e
  209. {: RESULT = (new Expressions(ln)).appendElement(e); :}
  210.  
  211. | expr_list:el COMMA expr:e
  212. {: RESULT = el.appendElement(e); :}
  213. | ln:ln error
  214. {: RESULT = new Expressions(ln); :}
  215. | expr_list:el COMMA error
  216. {: RESULT = el; :}
  217. ;
  218.  
  219.  
  220. /* Expressions */
  221. expr
  222. ::=
  223. /* ID <- expr */
  224. ln:ln OBJECTID:n ASSIGN expr:e ln:ln1
  225. {: RESULT = new assign(ln, n, e); :}
  226.  
  227. /* metodo */
  228. | method:m
  229. {: RESULT = m; :}
  230.  
  231. /* if */
  232. | if_stmt:is
  233. {: RESULT = is; :}
  234.  
  235. /*MAPCAR*/
  236. | mapcar_stmt:mapst
  237. {: RESULT = mapst; :}
  238.  
  239. /*FOR*/
  240. | for_stmt:forst
  241. {: RESULT = forst; :}
  242.  
  243. /*REPEAT*/
  244. | repeat_stmt:rps
  245. {: RESULT = rps; :}
  246.  
  247. /* while */
  248. | while_stmt:ws
  249. {: RESULT = ws; :}
  250.  
  251. /* {expr1; expr2; ... exprN;} */
  252. | ln:ln LBRACE expr_block:b RBRACE
  253. {: RESULT = new block(ln, b); :}
  254.  
  255. /* let statement */
  256. | LET let_stmt:ls
  257. {: RESULT = ls; :}
  258.  
  259. /* case statement */
  260. | ln:ln CASE expr:e OF case_list:cl ESAC
  261. {: RESULT = new typcase(ln, e, cl); :}
  262.  
  263. /* new TYPE */
  264. | ln:ln NEW TYPEID:t
  265. {: RESULT = new new_(ln, t); :}
  266.  
  267. /* isvoid expr */
  268. | ln:ln ISVOID expr:e ln:ln1
  269. {: RESULT = new isvoid(ln, e); :}
  270.  
  271. /* e1 + e2 */
  272. | expr:e1 ln:ln PLUS expr:e2 ln:ln1
  273. {: RESULT = new plus(ln, e1, e2); :}
  274.  
  275. /* e1 - e2 */
  276. | expr:e1 ln:ln MINUS expr:e2 ln:ln1
  277. {: RESULT = new sub(ln, e1, e2); :}
  278.  
  279. /* e1 * e2 */
  280. | expr:e1 ln:ln MULT expr:e2 ln:ln1
  281. {: RESULT = new mul(ln, e1, e2); :}
  282.  
  283. /* e1 / e2 */
  284. | expr:e1 ln:ln DIV expr:e2 ln:ln1
  285. {: RESULT = new divide(ln, e1, e2); :}
  286.  
  287. /* ~expr */
  288. | ln:ln NEG expr:e ln:ln1
  289. {: RESULT = new neg(ln, e); :}
  290.  
  291. /* e1 < e2 */
  292. | expr:e1 ln:ln LT expr:e2 ln:ln1
  293. {: RESULT = new lt(ln, e1, e2); :}
  294.  
  295. /*MODIFICA MAGGIORE*/
  296. | expr:e1 ln:ln ISVOID expr:e2 ln:ln1
  297. {: RESULT = new lt(ln,e2,e1); :}
  298.  
  299. /* e1 <= e2 */
  300. | expr:e1 ln:ln LE expr:e2 ln:ln1
  301. {: RESULT = new leq(ln, e1, e2); :}
  302.  
  303. /* e1 = e2 */
  304. | expr:e1 ln:ln EQ expr:e2 ln:ln1
  305. {: RESULT = new eq(ln, e1, e2); :}
  306.  
  307. /* !expr */
  308. | ln:ln NOT expr:e ln:ln1
  309. {: RESULT = new comp(ln, e); :}
  310.  
  311. /* (expr) */
  312. | LPAREN expr:e RPAREN
  313. {: RESULT = e; :}
  314.  
  315. /* identificatori */
  316. | ln:ln OBJECTID:n
  317. {: RESULT = new object(ln, n); :}
  318.  
  319. /* costanti intere */
  320. | ln:ln INT_CONST:t
  321. {: RESULT = new int_const(ln, t); :}
  322.  
  323. /* costanti stringhe */
  324. | ln:ln STR_CONST:t
  325. {: RESULT = new string_const(ln, t); :}
  326.  
  327. /* costanti booleane */
  328. | ln:ln BOOL_CONST:v
  329. {: RESULT = new bool_const(ln, v); :}
  330.  
  331. ;
  332.  
  333. method
  334. ::=
  335. /* expr.ID() or expr.ID(arg0,arg1,arg2) */
  336. expr:e ln:ln DOT OBJECTID:o LPAREN dummy_expr_list:el RPAREN
  337. {: RESULT = new dispatch(ln, e, o, el); :}
  338.  
  339. /* expr@[TYPE].ID() or expr@[TYPE].ID(arg0,arg1,arg2) */
  340. | expr:e AT TYPEID:t ln:ln DOT OBJECTID:o LPAREN dummy_expr_list:el RPAREN
  341. {: RESULT = new static_dispatch(ln, e, t, o, el); :}
  342.  
  343.  
  344. /* ID() or ID(arg0,arg1,arg2) */
  345. | ln:ln OBJECTID:o LPAREN dummy_expr_list:el RPAREN
  346. {: RESULT = new dispatch(ln, new object(ln, TreeConstants.self), o, el); :}
  347. ;
  348.  
  349.  
  350.  
  351. if_stmt
  352. ::= ln:ln IF expr:p THEN expr:te ELSE expr:ee FI
  353. {: RESULT = new cond(ln, p, te, ee); :}
  354. ;
  355.  
  356. /*MODIFICA MAPCAR*/
  357. mapcar_stmt
  358. ::= ln:ln POOL OBJECTID:o DOT LPAREN expr_list:el RPAREN
  359. {:
  360. Expressions exprs = new Expressions(ln);
  361. for (int i=0; i<el.getLength(); i++){
  362. exprs.appendElement(new dispatch(ln,new object(ln,TreeConstants.self),o, new Expressions(ln).appendElement(el.getNth(i))));
  363. }
  364. RESULT = new block(ln,exprs);
  365. :}
  366. ;
  367.  
  368. /*MODIFICA FOR*/
  369. for_stmt
  370. ::= ln:ln POOL LPAREN expr:e1 SEMI expr:e2 SEMI expr:e3 RPAREN expr:e4 LOOP
  371. {:
  372. Expressions exprs = new Expressions(ln);
  373. exprs.appendElement(e4);
  374. exprs.appendElement(e3);
  375. Expression block = new block(ln, exprs);
  376.  
  377. Expression forBlock = new loop(ln, e2, block);
  378.  
  379. Expressions exprs2 = new Expressions(ln);
  380. exprs2.appendElement(e1);
  381. exprs2.appendElement(forBlock);
  382. RESULT = new block(ln, exprs2);
  383. :}
  384. ;
  385.  
  386. /*MODIFICA REPEAT UNTIL*/
  387. repeat_stmt
  388. ::=ln:ln LOOP expr:e1 WHILE expr:e2 ln:ln1
  389. {:
  390. Expressions exprs = new Expressions(ln);
  391. exprs.appendElement(e1);
  392. Expression repeatBlock = new loop(ln, e2, e1);
  393. exprs.appendElement(repeatBlock);
  394. RESULT = new block(ln, exprs);
  395.  
  396. :}
  397. ;
  398.  
  399. while_stmt
  400. ::= ln:ln WHILE expr:p LOOP expr:e POOL
  401. {: RESULT = new loop(ln, p, e); :}
  402. ;
  403.  
  404. expr_block
  405. /* {expr1; expr2; ... exprN;} */
  406. ::=
  407. ln:ln expr:e SEMI
  408. {: RESULT =(new Expressions(ln)).appendElement(e); :}
  409.  
  410. | expr_block:el expr:e SEMI
  411. {: RESULT = el.appendElement(e); :}
  412.  
  413. | ln:ln error SEMI
  414. {: RESULT = new Expressions(ln); :}
  415.  
  416. | expr_block:el error SEMI
  417. {: RESULT = el; :}
  418. ;
  419.  
  420. let_stmt
  421. ::=
  422. ln:ln OBJECTID:o COLON TYPEID:t let_stmt_list:rest_of_let
  423. {: RESULT = new let(ln, o, t, new no_expr(0),rest_of_let); :}
  424.  
  425. | ln:ln OBJECTID:o COLON TYPEID:t ASSIGN expr:val let_stmt_list:rest
  426. {: RESULT = new let(ln, o, t, val, rest); :}
  427.  
  428. | error let_stmt_list:rest
  429.  
  430. ;
  431.  
  432. let_stmt_list
  433. ::=
  434. /* 1. Implicit initializer */
  435. COMMA ln:ln OBJECTID:id COLON TYPEID:type let_stmt_list:rest
  436. {: RESULT = new let(ln, id, type, new no_expr(0), rest); :}
  437.  
  438. /* 2. Explicit initializer */
  439. | COMMA ln:ln OBJECTID:id COLON TYPEID:type ASSIGN expr:val let_stmt_list:rest
  440. {: RESULT = new let(ln, id, type, val, rest); :}
  441.  
  442. /* 3. End-of-list, i.e. reached the body */
  443. | IN expr:body ln:ln
  444. {: RESULT = body; :}
  445.  
  446. | COMMA error let_stmt_list:rest
  447.  
  448. ;
  449.  
  450. case_list
  451. ::=
  452. ln:ln case:c
  453. {: RESULT = new Cases(ln).appendElement(c); :}
  454.  
  455. | case_list:cl case:c
  456. {: RESULT = cl.appendElement(c); :}
  457. ;
  458.  
  459. case
  460. ::=
  461. ln:ln OBJECTID:o COLON TYPEID:t DARROW expr:e SEMI
  462. {: RESULT = new branch(ln, o, t, e); :}
  463. ;
  464.  
  465. ln
  466. ::=
  467. {: RESULT = curr_lineno(); :}
  468. ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement