/********************************************************************** Java CUP specification for a parser for C-- programs **********************************************************************/ import java_cup.runtime.*; import java.util.*; parser code {: public void syntax_error(Symbol currToken) { if (currToken.value == null) { Errors.fatal(0,0, "Syntax error at end of file"); } else { Errors.fatal(((TokenVal)currToken.value).linenum, ((TokenVal)currToken.value).charnum, "Syntax error"); } System.exit(-1); } :}; /* Terminals (tokens returned by the scanner) * * NOTE: You will need to change the types of the values returned for * ID and STRINGLITERAL tokens if you used types other than * IdTokenVal and StrLitTokenVal in your scanner. * * You will also need to add types for any other terminals whose * values you use. */ terminal INT; terminal BOOL; terminal VOID; terminal TRUE; terminal FALSE; terminal IF; terminal ELSE; terminal WHILE; terminal CIN; terminal COUT; terminal RETURN; terminal IdTokenVal ID; terminal IntLitTokenVal INTLITERAL; terminal StrLitTokenVal STRINGLITERAL; terminal LCURLY; terminal RCURLY; terminal LSQBRACKET; terminal RSQBRACKET; terminal LPAREN; terminal RPAREN; terminal COMMA; terminal ASSIGN; terminal SEMICOLON; terminal PLUS; terminal MINUS; terminal TIMES; terminal DIVIDE; terminal NOT; terminal AND; terminal OR; terminal EQUALS; terminal NOTEQUALS; terminal LESS; terminal GREATER; terminal LESSEQ; terminal GREATEREQ; terminal READ; terminal WRITE; /* Nonterminals * * NOTE: You will need to add more nonterminals to this list as you * add productions to the grammar below. */ non terminal ProgramNode program; non terminal LinkedList declList; non terminal DeclNode decl; non terminal VarDeclNode varDecl; non terminal TypeNode type; non terminal IdNode id; /* NOTE: Add precedence and associativity declarations here */ start with program; /* * grammar with actions * * NOTE: add more grammar rules below */ program ::= declList: d {: RESULT = new ProgramNode(new DeclListNode(d)); :} ; declList ::= declList:dl decl:d {: dl.addLast(d); RESULT = dl; :} | /* epsilon */ {: RESULT = new LinkedList(); :} ; decl ::= varDecl:v {: RESULT = v; :} ; varDecl ::= type: t id: i SEMICOLON {: RESULT = new VarDeclNode(t, i, VarDeclNode.NOT_ARRAY); :} ; type ::= INT {: RESULT = new IntNode(); :} ; id ::= ID:i {: RESULT = new IdNode(i.linenum, i.charnum, i.idVal); :} ;