Skip to content

Instantly share code, notes, and snippets.

@MacoChave
Created October 3, 2016 17:29
Show Gist options
  • Save MacoChave/5f204950ba7d49ac26edce81c968fea1 to your computer and use it in GitHub Desktop.
Save MacoChave/5f204950ba7d49ac26edce81c968fea1 to your computer and use it in GitHub Desktop.
ERROR
package Compilador;
import java_cup.runtime.*;
import java.util.ArrayList;
parser code
{:
/*No tiene mucha utilidad*/
private ArrayList<String> erroresSintacticos=new ArrayList();
/* Metodo al que se llama automáticamente ante algún error sintactico. */
public void syntax_error(Symbol s)
{
System.out.println("Linea: " + (s.right+1) +" Columna: "+s.left+ ". Error Sintáctico, Identificador " +s.value + " no Esperado.");
/*String que permite guardar los errores generados*/
String errores=""+"Simbolo:"+s.value+" Fila:"+(s.right+1)+" Col:"+(s.left);
erroresSintacticos.add(errores);
}
/**Metodo al que se llama en el momento en que ya no es posible una recuperación de errores.*/
public void unrecovered_syntax_error(Symbol s) throws java.lang.Exception
{
System.out.println("Linea " + (s.right+1)+ "Columna "+s.left+". Error Sintactico, Identificador " + s.value + " no Esperado (unrecovered).");
}
public ArrayList<String> obtenerErrores()
{
return erroresSintacticos;
}
/* PARA PROPIEDADES -- GET Y SET DE LOS ARRAYLIST*/
/*
* HACER UN ARRAYLIST PARA LAS VARIABLES
* TDVARIABLE: variable, tipo, dato, nivel
*/
private List<TDVariable> listaVariables = new ArrayList();
public void agregarVariables(TDVariable variable)
{
listaVariables.add(variable);
}
public List<TDVariable> obtenerVariable
{
return listaVariables;
}
/*
* HACER UN ARRAYLIST PARA LOS VARIABLES DE REFERENCIA
* TDVarRef: variable, referencia, contexto
*/
private List<TDVarRef> listaVarRef = new ArrayList();
public void agregarVarRef(TDVarRef varRef)
{
listaVarRef.add(varRef);
}
public List<TDVarRef> obtenerVarRef()
{
return listaVarRef;
}
:}
action code
{:
/* DATOS VIVOS DENTRO DEL ANALIZADOR */
:}
/* SIMBOLOS TERMINALES */
terminal String mas, menos, por, div;
terminal String coma, ptcoma, llaveA, llaveC, parA, parC, igual;
terminal String proyecto, var, printer, context, varref, rq, ptn, variable;
terminal String iden, cadena;
terminal Double decimal;
/* SIMBOLOS NO TERMINALES*/
non terminal String INICIA, CUERPO, VARIABLE, CONTEXT, CUERPOCTX;
non terminal Double EXP, A, B, C, D;
non terminal VALOR;
/* PRECEDENCIA DE OPERADORES */
precedence left mas;
precedence left menos;
precedence left por;
precedence left div;
/*Inicio de la gramatica*/
start with INICIA;
INICIA::= proyecto:p iden:i llaveA CUERPO llaveC {: System.out.println(p + " " + i) :};
CUERPO::= VARIABLE ptcoma CUERPO
| CONTEXT CUERPO
| ;
VARIABLE::= var variable:v igual VALOR {: System.out.println("variable=" + v) :};
CONTEXT::= context iden:i llaveA CUERPOCTX llaveC {: System.out.println("identificador=" + i); :};
CUERPOCTX::= VARIABLE ptcoma CUERPOCTX
| printer parA VALOR parC CUERPOCTX
| varref llaveA VALOR coma VALOR llaveC CUERPOCTX
| ;
VALOR::= variable:v {: System.out.println("variable=" + v); :}
| cadena:c {: System.out.println("cadena=" + c); :}
| decimal:d {: System.out.println("numero=" + d); :}
| rq parA EXP parC
| ptn parA EXP coma EXP parC
| EXP:e {: System.out.println("= " + e); :};
/****** ARITMETICA ******/
EXP::= EXP:l mas A:r {: RESULT = new Double(doubleValue(l) + doubleValue(r)); :}
| A:a {: RESULT = a; :};
A::= A:l menos B:r {: RESULT = new Double(doubleValue(l) - doubleValue(r)); :}
| B:b {: RESULT = b; :};
B::= B:l por C:r {: RESULT = new Double(doubleValue(l) * doubleValue(r)); :}
| C:c {: RESULT = c; :};
C::= C:l div D:r {: RESULT = new Double(doubleValue(l) / doubleValue(r)); :}
| D:d {: RESULT = d; :};
D::= decimal:num {: RESULT = num; :};
$ java -jar cup.jar -parser parser1 -symbols simbolos sintactico.cup
Warning : *** Reduce/Reduce conflict found in state #32
between VALOR ::= decimal (*)
and D ::= decimal (*)
under symbols: {coma, ptcoma, llaveC, parC}
Resolved in favor of the first production.
Warning : *** Shift/Reduce conflict found in state #32
between VALOR ::= decimal (*)
under symbol coma
Resolved in favor of shifting.
Warning : *** Shift/Reduce conflict found in state #32
between VALOR ::= decimal (*)
under symbol ptcoma
Resolved in favor of shifting.
Warning : *** Shift/Reduce conflict found in state #32
between VALOR ::= decimal (*)
under symbol llaveC
Resolved in favor of shifting.
Warning : *** Shift/Reduce conflict found in state #32
between VALOR ::= decimal (*)
under symbol parC
Resolved in favor of shifting.
Warning : *** Shift/Reduce conflict found in state #32
between D ::= decimal (*)
under symbol coma
Resolved in favor of shifting.
Warning : *** Shift/Reduce conflict found in state #32
between D ::= decimal (*)
under symbol ptcoma
Resolved in favor of shifting.
Warning : *** Shift/Reduce conflict found in state #32
between D ::= decimal (*)
under symbol llaveC
Resolved in favor of shifting.
Warning : *** Shift/Reduce conflict found in state #32
between D ::= decimal (*)
under symbol parC
Resolved in favor of shifting.
Error : *** More conflicts encountered than expected -- parser generation aborted
------- CUP v0.11a beta 20060608 Parser Generation Summary -------
1 error and 9 warnings
24 terminals, 11 non-terminals, and 26 productions declared,
producing 64 unique parse states.
0 terminals declared but not used.
0 non-terminals declared but not used.
0 productions never reduced.
9 conflicts detected (0 expected).
No code produced.
---------------------------------------------------- (v0.11a beta 20060608)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment