// jBASIC parser with basic actions import java_cup.runtime.*; parser code {: Lexer scanner = new Lexer(System.in); :}; /*action code {: Lexer scanner = new Lexer(System.in); :}; */ /*init with {: scanner.init(); :}; */ /*parser code {: public parser (java.io.Reader input) { super(new Lexer(input)); } :}; */ scan with {: return scanner.next_token(); :}; /* grammer symbols: */ /* types are defined in jBASIC.java */ terminal String ID; terminal Integer NUM; terminal String STRINGe; terminal SEMI, PLUS, TIMES, LPAREN, RPAREN, LABEL, TRUE, FALSE; terminal ANDop, NOT, IF, THEN, GOTO, PRINT, ASSIGN, EQUAL; terminal WHILE, BEGIN, END; non terminal prog PROGRAM; non terminal stmt STAT; non terminal stmtlist STATLIST; non terminal expr EXP; precedence left PLUS, TIMES, ANDop, EQUAL; precedence left NOT; /* the grammer and actions to build abstract syntax tree */ PROGRAM ::= STATLIST:sl1 {: RESULT = new prog(sl1); :}; STATLIST ::= STAT:s1 SEMI {: RESULT = new stmtlist(s1,null); :} | STAT:s1 SEMI STATLIST:sr {: RESULT = new stmtlist(s1,sr); :}; EXP ::= NUM:n1 {: RESULT = new intexp(n1.intValue()); :} | ID:s1 {: RESULT = new varexp(s1); :} | STRINGe:s1 {: RESULT = new strexp(s1); :} | TRUE {: RESULT = new boolexp(true); :} | FALSE {: RESULT = new boolexp(false); :} | EXP:e1 PLUS EXP:e2 {: RESULT = new sumexp(e1,e2); :} | EXP:e1 TIMES EXP:e2 {: RESULT = new multexp(e1,e2); :} | EXP:e1 ANDop EXP:e2 {: RESULT = new andexp(e1,e2); :} | EXP:e1 EQUAL EXP:e2 {: RESULT = new eqexp(e1,e2); :} | NOT EXP:e1 {: RESULT = new negexp(e1); :} | LPAREN EXP:e1 RPAREN {: RESULT = e1; :}; STAT ::= LABEL ID:s1 {: RESULT = new labelst(s1); :} | PRINT EXP:e1 {: RESULT = new printst(e1); :} | ID:s1 ASSIGN EXP:e1 {: RESULT = new assignst(s1,e1); :} | GOTO ID:s1 {: RESULT = new gotost(s1); :} | IF EXP:e1 THEN STAT:s1 {: RESULT = new ifst(e1,s1); :} | WHILE EXP:e1 BEGIN STATLIST:sl1 END {: RESULT = new whilest(e1,sl1); :};