Ejemplo básico de CUP

1
Universidad Tecnológica Centroamericana Ingeniería de Sistemas Compiladores I Ing. Egdares Futch H. Ejemplo básico de uso de CUP import java_cup.runtime.*; parser code {: /* Manejo de errores, antes de entrar al modo de recuperacion de errores, por medio del token de error */ public void syntax_error(Symbol s) { report_error("Error de sintaxis", null); } public void unrecovered_syntax_error(Symbol s) throws java.lang.Exception { report_fatal_error("No pude reparar el input, lo siento", null); } :}; init with {: scanner.init(); :}; scan with {: return scanner.next_token(); :}; terminal SEMI, PLUS, MINUS, TIMES, DIVIDE, MOD; terminal UMINUS, LPAREN, RPAREN; terminal Integer NUMBER; non terminal expr_list, expr_part; non terminal Integer expr; precedence left PLUS, MINUS; precedence left TIMES, DIVIDE, MOD; precedence left UMINUS; /* Start symbol */ start with expr_list; expr_list ::= expr_list expr_part | expr_part; expr_part ::= expr:e {: System.out.println("= " + e); :} SEMI ; expr ::= expr:e1 PLUS expr:e2 {: RESULT = new Integer(e1.intValue() + e2.intValue()); :} | expr:e1 MINUS expr:e2 {: RESULT = new Integer(e1.intValue() - e2.intValue()); :} | expr:e1 TIMES expr:e2 {: RESULT = new Integer(e1.intValue() * e2.intValue()); :} | expr:e1 DIVIDE expr:e2 {: RESULT = new Integer(e1.intValue() / e2.intValue()); :} | expr:e1 MOD expr:e2 {: RESULT = new Integer(e1.intValue() % e2.intValue()); :} | NUMBER:n {: RESULT = n; :} | MINUS expr:e {: RESULT = new Integer(0 - e.intValue()); :} %prec UMINUS | LPAREN expr:e RPAREN {: RESULT = e; :} | error SEMI ;

description

Ejemplo de uso de CUP, como generador de analizador sintáctico (parser)

Transcript of Ejemplo básico de CUP

Page 1: Ejemplo básico de CUP

Universidad Tecnológica Centroamericana Ingeniería de Sistemas Compiladores I Ing. Egdares Futch H.

Ejemplo básico de uso de CUP import java_cup.runtime.*; parser code {: /* Manejo de errores, antes de entrar al modo de recuperacion de errores, por medio del token de error */ public void syntax_error(Symbol s) { report_error("Error de sintaxis", null); } public void unrecovered_syntax_error(Symbol s) throws java.lang.Exception { report_fatal_error("No pude reparar el input, lo siento", null); } :}; init with {: scanner.init(); :}; scan with {: return scanner.next_token(); :}; terminal SEMI, PLUS, MINUS, TIMES, DIVIDE, MOD; terminal UMINUS, LPAREN, RPAREN; terminal Integer NUMBER; non terminal expr_list, expr_part; non terminal Integer expr; precedence left PLUS, MINUS; precedence left TIMES, DIVIDE, MOD; precedence left UMINUS; /* Start symbol */ start with expr_list; expr_list ::= expr_list expr_part | expr_part; expr_part ::= expr:e {: System.out.println("= " + e); :} SEMI ; expr ::= expr:e1 PLUS expr:e2 {: RESULT = new Integer(e1.intValue() + e2.intValue()); :} | expr:e1 MINUS expr:e2 {: RESULT = new Integer(e1.intValue() - e2.intValue()); :} | expr:e1 TIMES expr:e2 {: RESULT = new Integer(e1.intValue() * e2.intValue()); :} | expr:e1 DIVIDE expr:e2 {: RESULT = new Integer(e1.intValue() / e2.intValue()); :} | expr:e1 MOD expr:e2 {: RESULT = new Integer(e1.intValue() % e2.intValue()); :} | NUMBER:n {: RESULT = n; :} | MINUS expr:e {: RESULT = new Integer(0 - e.intValue()); :} %prec UMINUS | LPAREN expr:e RPAREN {: RESULT = e; :} | error SEMI ;