Ejemplo básico de CUP

  • 3,293 views
Uploaded on

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

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

More in: Education
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
3,293
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
76
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 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 ;