CS143-Lab/assignments/PA3J/cool.cup
2023-03-16 15:55:37 +00:00

103 lines
3.1 KiB
Plaintext

/*
* 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 +
": parse 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 programc program;
nonterminal Classes class_list;
nonterminal class_c class;
nonterminal Features dummy_feature_list;
/* Precedence declarations go here. */
program
::= class_list:cl
{: RESULT = new programc(curr_lineno(), cl); :}
;
class_list
/* single class */
::= class:c
{: RESULT = (new Classes(curr_lineno())).appendElement(c); :}
/* several classes */
| class_list:cl class:c
{: RESULT = cl.appendElement(c); :}
;
/* If no parent is specified, the class inherits from the Object class */
class
::= CLASS TYPEID:n LBRACE dummy_feature_list:f RBRACE SEMI
{: RESULT = new class_c(curr_lineno(), n,
AbstractTable.idtable.addString("Object"),
f, curr_filename()); :}
| CLASS TYPEID:n INHERITS TYPEID:p LBRACE dummy_feature_list:f RBRACE SEMI
{: RESULT = new class_c(curr_lineno(), n, p, f, curr_filename()); :}
;
/* Feature list may be empty, but no empty features in list. */
dummy_feature_list
::= /* empty */
{: RESULT = new Features(curr_lineno()); :}
;