grm.cup
来自「Tiger语言的一种编译器,在大学CS课程必做的,不过可惜的是没有完全完成,做好」· CUP 代码 · 共 231 行
CUP
231 行
package Parse;action code {: static Symbol.Symbol sym(String s) { return Symbol.Symbol.symbol(s); } :};parser code {: public Absyn.Exp parseResult; Lexer lexer; public void syntax_error(java_cup.runtime.Symbol current) { report_error("Syntax error (" + current.sym + ")", current); } ErrorMsg.ErrorMsg errorMsg; public void report_error(String message, java_cup.runtime.Symbol info) { errorMsg.error(info.left, message); } public Grm(Lexer l, ErrorMsg.ErrorMsg err) { this(); errorMsg=err; lexer=l; } public void done_parsing() { _done_parsing = true; System.out.println(); System.out.println("Parsing End."); }:};init with {: System.out.println("Begin Parsing..."); System.out.println(); :}scan with {: java_cup.runtime.Symbol tok = lexer.nextToken(); System.out.println(tok.value + "#"); return tok; :};terminal String ID, STRING;terminal Integer INT;terminal COMMA, COLON, SEMICOLON, LPAREN, RPAREN, LBRACK, RBRACK, LBRACE, RBRACE, DOT, PLUS, MINUS,UMINUS, TIMES, DIVIDE, EQ, NEQ, LT, LE, GT, GE, AND, OR, ASSIGN, ARRAY, IF, THEN, ELSE, WHILE, FOR, TO, DO, LET, IN, END, OF, BREAK, NIL, FUNCTION, VAR, TYPE;non terminal Absyn.Exp program,assign_expr,binary_operation_expr,function_expr,paren_expr,record_expr,case_expr,scope_expr;non terminal Absyn.Dec dec,function_dec,variable_dec,type_dec;non terminal Absyn.Ty type;non terminal Absyn.Var lvalue;non terminal Absyn.ExpList expr_list,expr_seq;non terminal Absyn.FieldExpList fieldexpr_list;non terminal Absyn.FieldList field_list;non terminal Absyn.DecList dec_list;precedence left ELSE;precedence left OR;precedence left AND;precedence nonassoc EQ,LT,LE,GT,GE,NEQ;precedence left PLUS,MINUS;precedence left TIMES,DIVIDE;precedence left UMINUS;precedence left LBRACK;start with program;program ::= ID:i {: RESULT = new Absyn.VarExp(ileft, new Absyn.SimpleVar(ileft,sym(i))); :} |STRING:s {: RESULT = new Absyn.StringExp(sleft,s); :} |INT:it {: RESULT = new Absyn.IntExp(itleft,it.intValue()); :} |NIL:n {: RESULT = new Absyn.NilExp(nleft); :} |lvalue:i {: RESULT = new Absyn.VarExp(ileft,i); :} |binary_operation_expr:e {: RESULT = e; :} |assign_expr:e {: RESULT = e; :} |function_expr:e {: RESULT = e; :} |paren_expr:e {: RESULT = e; :} |record_expr:e {: RESULT = e; :} |ID:i LBRACK program:e1 RBRACK OF program:e2 {: RESULT = new Absyn.ArrayExp(ileft,sym(i),e1,e2); :} |case_expr:e {: RESULT = e; :} |WHILE:n program:e1 DO program:e2 {: RESULT = new Absyn.WhileExp(nleft,e1,e2); :} |FOR:n ID:i ASSIGN program:e1 TO program:e2 DO program:e3 {: RESULT = new Absyn.ForExp(nleft, new Absyn.VarDec(ileft,sym(i),null,e1),e2,e3); :} |BREAK:n {: RESULT = new Absyn.BreakExp(nleft); :} |scope_expr:e {: RESULT = e; :} |LPAREN error RPAREN |error SEMICOLON program ;lvalue ::= ID:i1 DOT ID:i2 {: RESULT = new Absyn.FieldVar(i1left, new Absyn.SimpleVar(i1left,sym(i1)), sym(i2)); :} |ID:i LBRACK program:e RBRACK {: RESULT = new Absyn.SubscriptVar(ileft, new Absyn.SimpleVar(ileft,sym(i)),e); :} |lvalue:i1 DOT ID:i2 {: RESULT = new Absyn.FieldVar(i1left,i1,sym(i2)); :} |lvalue:i LBRACK program:e RBRACK {: RESULT = new Absyn.SubscriptVar(ileft,i,e); :} ;binary_operation_expr ::= program:e1 OR program:e2 {: RESULT = new Absyn.IfExp(e1left,e1, new Absyn.IntExp(e1left,1),e2); :} |program:e1 AND program:e2 {: RESULT = new Absyn.IfExp(e1left,e1,e2, new Absyn.IntExp(e1left,0)); :} |program:e1 EQ program:e2 {: RESULT = new Absyn.OpExp(e1left,e1,Absyn.OpExp.EQ,e2); :} |program:e1 LT program:e2 {: RESULT = new Absyn.OpExp(e1left,e1,Absyn.OpExp.LT,e2); :} |program:e1 LE program:e2 {: RESULT = new Absyn.OpExp(e1left,e1,Absyn.OpExp.LE,e2); :} |program:e1 GT program:e2 {: RESULT = new Absyn.OpExp(e1left,e1,Absyn.OpExp.GT,e2); :} |program:e1 GE program:e2 {: RESULT = new Absyn.OpExp(e1left,e1,Absyn.OpExp.GE,e2); :} |program:e1 NEQ program:e2 {: RESULT = new Absyn.OpExp(e1left,e1,Absyn.OpExp.NE,e2); :} |program:e1 PLUS program:e2 {: RESULT = new Absyn.OpExp(e1left,e1,Absyn.OpExp.PLUS,e2); :} |program:e1 MINUS program:e2 {: RESULT = new Absyn.OpExp(e1left,e1,Absyn.OpExp.MINUS,e2); :} |program:e1 TIMES program:e2 {: RESULT = new Absyn.OpExp(e1left,e1,Absyn.OpExp.MUL,e2); :} |program:e1 DIVIDE program:e2 {: RESULT = new Absyn.OpExp(e1left,e1,Absyn.OpExp.DIV,e2); :} |MINUS:n program:e {: RESULT = new Absyn.OpExp(nleft, new Absyn.IntExp(nleft,0),Absyn.OpExp.MINUS,e); :} %prec UMINUS ;assign_expr ::= lvalue:i ASSIGN program:e {: RESULT = new Absyn.AssignExp(ileft,i,e); :} |ID:i ASSIGN program:e {: RESULT = new Absyn.AssignExp(ileft, new Absyn.SimpleVar(ileft,sym(i)),e); :} ;function_expr ::= ID:i LPAREN RPAREN {: RESULT = new Absyn.CallExp(ileft,sym(i),null); :} |ID:i LPAREN expr_list:el RPAREN {: RESULT = new Absyn.CallExp(ileft,sym(i),el); :} ;expr_list ::= program:e {: RESULT = new Absyn.ExpList(e,null); :} |program:e COMMA expr_list:el {: RESULT = new Absyn.ExpList(e,el); :} ;paren_expr ::= LPAREN:n RPAREN {: RESULT = new Absyn.SeqExp(nleft,null); :} |LPAREN:n expr_seq:el RPAREN {: RESULT = new Absyn.SeqExp(nleft,el); :} ;expr_seq ::= program:e {: RESULT = new Absyn.ExpList(e,null); :} |program:e SEMICOLON expr_seq:el {: RESULT = new Absyn.ExpList(e,el); :} ;record_expr ::= ID:i LBRACE RBRACE {: RESULT = new Absyn.RecordExp(ileft,sym(i),null); :} |ID:i LBRACE fieldexpr_list:fl RBRACE {: RESULT = new Absyn.RecordExp(ileft,sym(i),fl); :} ;fieldexpr_list ::= ID:i EQ program:e {: RESULT = new Absyn.FieldExpList(ileft,sym(i),e,null); :} |ID:i EQ program:e COMMA fieldexpr_list:fel {: RESULT = new Absyn.FieldExpList(ileft,sym(i),e,fel); :} ;case_expr ::= IF:n program:e1 THEN program:e2 {: RESULT = new Absyn.IfExp(nleft,e1,e2); :} |IF:n program:e1 THEN program:e2 ELSE program:e3 {: RESULT = new Absyn.IfExp(nleft,e1,e2,e3); :} ;scope_expr ::= LET:n dec_list:dl IN END {: RESULT = new Absyn.LetExp(nleft,dl,null); :} |LET:n dec_list:dl IN expr_seq:el END {: RESULT = new Absyn.LetExp(nleft,dl, new Absyn.SeqExp(elleft,el)); :} ;dec_list ::= dec:d {: RESULT = new Absyn.DecList(d,null); :} |dec:d dec_list:dl {: RESULT = new Absyn.DecList(d,dl); :} ;dec ::= type_dec:d {: RESULT = d; :} |variable_dec:d {: RESULT = d; :} |function_dec:d {: RESULT = d; :} ;type_dec ::= TYPE:n ID:i EQ type:t {: RESULT = new Absyn.TypeDec(nleft,sym(i),t,null); :} ;type ::= ID:i {: RESULT = new Absyn.NameTy(ileft,sym(i)); :} |LBRACE:n RBRACE {: RESULT = new Absyn.RecordTy(nleft,null); :} |LBRACE:n field_list:fl RBRACE {: RESULT = new Absyn.RecordTy(nleft,fl); :} |ARRAY:n OF ID:i {: RESULT = new Absyn.ArrayTy(nleft,sym(i)); :} ;field_list ::= ID:i1 COLON ID:i2 {: RESULT = new Absyn.FieldList(i1left,sym(i1),sym(i2),null); :} |ID:i1 COLON ID:i2 COMMA field_list:fl {: RESULT = new Absyn.FieldList(i1left,sym(i1),sym(i2),fl); :} ;variable_dec ::= VAR:n ID:i ASSIGN program:e {: RESULT = new Absyn.VarDec(nleft,sym(i),null,e); :} |VAR:n ID:i1 COLON ID:i2 ASSIGN program:e {: RESULT = new Absyn.VarDec(nleft,sym(i1), new Absyn.NameTy(i2left,sym(i2)),e); :} ;function_dec ::= FUNCTION:n ID:i LPAREN RPAREN EQ program:e {: RESULT = new Absyn.FunctionDec(nleft,sym(i),null,null,e,null); :} |FUNCTION:n ID:i1 LPAREN RPAREN COLON ID:i2 EQ program:e {: RESULT = new Absyn.FunctionDec(nleft,sym(i1),null, new Absyn.NameTy(i2left,sym(i2)),e,null); :} |FUNCTION:n ID:i LPAREN field_list:fl RPAREN EQ program:e {: RESULT = new Absyn.FunctionDec(nleft,sym(i),fl,null,e,null); :} |FUNCTION:n ID:i1 LPAREN field_list:fl RPAREN COLON ID:i2 EQ program:e {: RESULT = new Absyn.FunctionDec(nleft,sym(i1),fl, new Absyn.NameTy(i2left,sym(i2)),e,null); :} ;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?