Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java_cup.runtime.*;
- import java.io.*;
- import java.util.*;
- parser code {:
- Yylex analizadorLexico;
- public static void main(String[] argv){
- if (argv.length == 0) {
- System.out.println("Uso : java Parser <inputfile)>");
- }
- else{
- try {
- java.io.FileInputStream stream = new java.io.FileInputStream(argv[0]);
- java.io.Reader reader = new java.io.InputStreamReader(stream);
- Yylex analizadorLexico= new Yylex(reader);
- Parser parserObj = new Parser();
- parserObj.analizadorLexico= analizadorLexico;
- parserObj.setScanner(parserObj.analizadorLexico);
- parserObj.parse();
- }
- catch(Exception x) {
- System.out.println("Error fatal.");
- }
- }
- }
- public void error_sintactico(Symbol current_token){
- report_error("Error de sintaxis con token: "+sym.terminalNames[current_token.sym]+" en linea "+analizadorLexico.linea()+" columna "+analizadorLexico.columna(),current_token);
- }
- :};
- /*Terminales tokens que puede devolver el lexico*/
- terminal INTEGER,REAL,CHAR,BOOLEAN;
- terminal POINTER,TO,ARRAY,OF,PROCEDURE;
- terminal CARACTER;
- terminal ABREPAR,CIERRAPAR,ABRECOR,CIERRACOR,DOSPUNTOS,COMA,PTOYCOMA, IGUAL;
- terminal String ID;
- terminal Integer NUM;
- terminal Float NUMREAL;
- non terminal Stack tipo, expr, declaracion;
- non terminal String listaargumentos;
- non terminal programa, valor, funcion;
- /*Gramatica*/
- programa ::= /* Epsilon*/
- | programa declaracion:d PTOYCOMA
- {: System.out.println(Simbolo.tipotoString(d)); :}
- | programa error PTOYCOMA
- {: this.parser.error_sintactico(this.parser.cur_token);:}
- ;
- declaracion ::= ID:id COMA declaracion:t
- {: RESULT=t;
- /* Se busca en tabla de simbolos y se inserta o devuelve error*/
- System.out.println("ID:"+id+" en no final");
- :}
- | ID:id DOSPUNTOS tipo:t
- {: RESULT=t;
- /* Se busca en tabla de simbolos y se inserta o devuelve error*/
- System.out.println(id);
- :}
- | ID:id DOSPUNTOS tipo:t IGUAL valor:v
- {: RESULT=t;
- /* Se busca en tabla de simbolos y se inserta o devuelve error*/
- System.out.println(id);
- :}
- ;
- valor ::= NUM
- {: System.out.println("numeral");:}
- | NUMREAL
- {: System.out.println("real");:}
- | CARACTER
- {: System.out.println("char");:}
- ;
- tipo ::= INTEGER
- {: RESULT= new Stack(); RESULT.push(new Character('i'));:}
- | REAL
- {: RESULT= new Stack(); RESULT.push(new Character('r'));:}
- | CHAR
- {: RESULT= new Stack(); RESULT.push(new Character('c'));:}
- | BOOLEAN
- {: RESULT= new Stack(); RESULT.push(new Character('b'));:}
- | POINTER TO tipo:t
- {: RESULT= t; RESULT.push(new Character('p'));:}
- | PROCEDURE ABREPAR listaargumentos CIERRAPAR DOSPUNTOS tipo:t
- {: RESULT= t; RESULT.push(new Character('f'));:}
- | ARRAY ABRECOR NUM CIERRACOR OF tipo:t
- {: RESULT= t; RESULT.push(new Character('m'));:}
- ;
- listaargumentos::= {: RESULT=new String("Sin argumentos");
- System.out.println(RESULT); :}
- ;
- funcion ::= ID:id DOSPUNTOS PROCEDURE ABREPAR CIERRAPAR DOSPUNTOS tipo:t
- {:
- System.out.println("Funcion:"+id+" del tipo "+t);
- :}
- ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement