saludos
esto seria el AFD, el léxico del compilador
Código:
/**
* Analizador léxico implementado mediante un
* autómata finito determinista.
* <p>Cada estado del autómata se implementa con un objeto Runnable.
*/
public class AFD extends ALex
{
/**
* Construye el autómata.
* @param fichero Fichero de texto que se debe analizar.
* @throws IOException
*/
public AFD(String fichero) throws IOException
{
super(fichero);
setInicio(s_inicio);
}
private Runnable s_inicio = new Runnable() {@Override public void run()
{
if(isChar('s'))
estado(s_si);
else if(isChar('m'))
estado(s_mi);
else if(isChar('c'))
estado(s_ca);
else if(isChar('e'))
estado(s_en);
else if(isChar('i'))
estado(s_im);
else if(isChar('f'))
estado(s_fi);
else if(isChar(':'))
estado(s_asign);
else if(isChar('"'))
estadoNoChar(s_strval);
else if(isChar('<'))
estado(s_rel);
else if(isChar('>'))
estado(s_rel2);
else if(isChar('='))
estado(s_rel1);
else if(isChar('+')||isChar('-'))
estado(s_sum);
else if(isChar('*'))
estado(s_mul);
else if(isChar('/'))
estado(s_mulcom);
else if(isChar('!'))
estado(s_neg);
else if(isChar('|'))
estado(s_or);
else if(isChar('&'))
estado(s_and);
else if(isChar('('))
estado(s_ipar);
else if(isChar(')'))
estado(s_dpar);
else if(isIdCharInicio())
estado(s_id);
else if(isDigitChar())
estado(s_intval);
else if(isSpaceChar())
estadoNoChar(s_inicio);
else if(isEofChar())
token(EOF);
else
error();
}};
//-----------------------------------------------------------------
private Runnable s_si = new Runnable() {@Override public void run(){
if(isChar('i'))
estado(s_si1);
else if(isIdChar())
estado(s_id);
else
token(ID);
}};
private Runnable s_id = new Runnable() {@Override public void run()
{
if(isIdChar())
estado(s_id);
else
token(ID);
}};
private Runnable s_si1 = new Runnable() {@Override public void run()
{
if(isChar('n'))
estado(s_sino);
else if(isIdChar())
estado(s_id);
else
token(SI);
}};
//-----------------------------------------------------------------
private Runnable s_sino = new Runnable() {@Override public void run()
{
if(isChar('o'))
estado(s_sino1);
else if(isIdChar())
estado(s_id);
else
token(ID);
}};
private Runnable s_sino1 = new Runnable() {@Override public void run()
{
if(isIdChar())
estado(s_id);
else
token(SINO);
}};
//-----------------------------------------------------------------
private Runnable s_mi = new Runnable() {@Override public void run(){
if(isChar('i'))
estado(s_mie);
else if(isIdChar())
estado(s_id);
else
token(ID);
}};
private Runnable s_mie = new Runnable() {@Override public void run(){
if(isChar('e'))
estado(s_mien);
else if(isIdChar())
estado(s_id);
else
token(ID);
}};
private Runnable s_mien = new Runnable() {@Override public void run(){
if(isChar('n'))
estado(s_mient);
else if(isIdChar())
estado(s_id);
else
token(ID);
}};
private Runnable s_mient = new Runnable() {@Override public void run(){
if(isChar('t'))
estado(s_mientr);
else if(isIdChar())
estado(s_id);
else
token(ID);
}};
private Runnable s_mientr = new Runnable() {@Override public void run(){
if(isChar('r'))
estado(s_mientra);
else if(isIdChar())
estado(s_id);
else
token(ID);
}};
private Runnable s_mientra = new Runnable() {@Override public void run(){
if(isChar('a'))
estado(s_mientras);
else if(isIdChar())
estado(s_id);
else
token(ID);
}};
private Runnable s_mientras = new Runnable() {@Override public void run(){
if(isChar('s'))
estado(s_mientras1);
else if(isIdChar())
estado(s_id);
else
token(ID);
}};
private Runnable s_mientras1 = new Runnable() {@Override public void run(){
if(isIdChar())
estado(s_id);
else
token(MIENTRAS);
}};
//-----------------------------------------------------------------
private Runnable s_ca = new Runnable() {@Override public void run(){
if(isChar('a'))
estado(s_cad);
else if(isIdChar())
estado(s_id);
else
token(ID);
}};
private Runnable s_cad = new Runnable() {@Override public void run(){
if(isChar('d'))
estado(s_cade);
else if(isIdChar())
estado(s_id);
else
token(ID);
}};
private Runnable s_cade = new Runnable() {@Override public void run(){
if(isChar('e'))
estado(s_caden);
else if(isIdChar())
estado(s_id);
else
token(ID);
}};
private Runnable s_caden = new Runnable() {@Override public void run(){
if(isChar('n'))
estado(s_cadena);
else if(isIdChar())
estado(s_id);
else
token(ID);
}};
private Runnable s_cadena = new Runnable() {@Override public void run(){
if(isChar('a'))
estado(s_cadena1);
else if(isIdChar())
estado(s_id);
else
token(ID);
}};
private Runnable s_cadena1 = new Runnable() {@Override public void run(){
if(isIdChar())
estado(s_id);
else
token(CADENA);
}};
//-----------------------------------------------------------------
private Runnable s_en = new Runnable() {@Override public void run(){
if(isChar('n'))
estado(s_ent);
else if(isIdChar())
estado(s_id);
else
token(ID);
}};
private Runnable s_ent = new Runnable() {@Override public void run(){
if(isChar('t'))
estado(s_ente);
else if(isIdChar())
estado(s_id);
else
token(ID);
}};
private Runnable s_ente = new Runnable() {@Override public void run(){
if(isChar('e'))
estado(s_enter);
else if(isIdChar())
estado(s_id);
else
token(ID);
}};
private Runnable s_enter = new Runnable() {@Override public void run(){
if(isChar('r'))
estado(s_entero);
else if(isIdChar())
estado(s_id);
else
token(ID);
}};
private Runnable s_entero = new Runnable() {@Override public void run(){
if(isChar('o'))
estado(s_entero1);
else if(isIdChar())
estado(s_id);
else
token(ID);
}};
private Runnable s_entero1 = new Runnable() {@Override public void run(){
if(isIdChar())
estado(s_id);
else
token(ENTERO);
}};
//-----------------------------------------------------------------
private Runnable s_im = new Runnable() {@Override public void run(){
if(isChar('m'))
estado(s_imp);
else if(isIdChar())
estado(s_id);
else
token(ID);
}};
private Runnable s_imp = new Runnable() {@Override public void run(){
if(isChar('p'))
estado(s_impr);
else if(isIdChar())
estado(s_id);
else
token(ID);
}};
private Runnable s_impr = new Runnable() {@Override public void run(){
if(isChar('r'))
estado(s_impri);
else if(isIdChar())
estado(s_id);
else
token(ID);
}};
private Runnable s_impri = new Runnable() {@Override public void run(){
if(isChar('i'))
estado(s_imprim);
else if(isIdChar())
estado(s_id);
else
token(ID);
}};
private Runnable s_imprim = new Runnable() {@Override public void run(){
if(isChar('m'))
estado(s_imprimi);
else if(isIdChar())
estado(s_id);
else
token(ID);
}};
private Runnable s_imprimi = new Runnable() {@Override public void run(){
if(isChar('i'))
estado(s_imprimir);
else if(isIdChar())
estado(s_id);
else
token(ID);
}};
private Runnable s_imprimir = new Runnable() {@Override public void run(){
if(isChar('r'))
estado(s_imprimir1);
else if(isIdChar())
estado(s_id);
else
token(ID);
}};
private Runnable s_imprimir1 = new Runnable() {@Override public void run(){
if(isIdChar())
estado(s_id);
else
token(IMPRIMIR);
}};
//-----------------------------------------------------------------
private Runnable s_fi = new Runnable() {@Override public void run(){
if(isChar('i'))
estado(s_fin);
else if(isIdChar())
estado(s_id);
else
token(ID);
}};
private Runnable s_fin = new Runnable() {@Override public void run(){
if(isChar('n'))
estado(s_fin1);
else if(isIdChar())
estado(s_id);
else
token(ID);
}};
private Runnable s_fin1 = new Runnable() {@Override public void run(){
if(isIdChar())
estado(s_id);
else
token(FIN);
}};
//-----------------------------------------------------------------
private Runnable s_intval = new Runnable() {@Override public void run()
{
if(isDigitChar())
estado(s_intval);
else if(isIdChar())
error();
else
token(INTVAL);
}};
//-----------------------------------------------------------------
private Runnable s_strval = new Runnable() {@Override public void run()
{
if(isChar('"'))
estadoNoChar(s_strval1);
else
estado(s_strval);
}};
private Runnable s_strval1 = new Runnable() {@Override public void run()
{
token(STRVAL);
}};
//-----------------------------------------------------------------
private Runnable s_asign = new Runnable() {@Override public void run(){
if(isChar('='))
estado(s_asign1);
else
error();
}};
private Runnable s_asign1 = new Runnable() {@Override public void run(){
token(ASIGN);
}};
//-----------------------------------------------------------------
private Runnable s_rel = new Runnable() {@Override public void run(){
if(isChar('=') || isChar('>'))
estado(s_rel1);
else
token(REL);
}};
private Runnable s_rel1 = new Runnable() {@Override public void run(){
token (REL);
}};
private Runnable s_rel2 = new Runnable() {@Override public void run(){
if(isChar('='))
estado(s_rel1);
else
token(REL);
}};
//-----------------------------------------------------------------
private Runnable s_sum = new Runnable() {@Override public void run(){
token(SUM);
}};
//-----------------------------------------------------------------
private Runnable s_mul = new Runnable() {@Override public void run(){
token(MUL);
}};
//-----------------------------------------------------------------
private Runnable s_mulcom = new Runnable() {@Override public void run(){
if(isChar('/'))
estadoNoChar(s_comlin);
else if(isChar('*'))
estadoNoChar(s_combloq);
else
token(MUL);
}};
//-----------------------------------------------------------------
private Runnable s_comlin = new Runnable() {@Override public void run(){
if(isChar('\n'))
estadoNoChar(s_inicio);
else
estadoNoChar(s_comlin);
}};
//-----------------------------------------------------------------
private Runnable s_combloq = new Runnable() {@Override public void run(){
if(isChar('*'))
estadoNoChar(s_combloq1);
else
estadoNoChar(s_combloq);
}};
//-----------------------------------------------------------------
private Runnable s_combloq1 = new Runnable() {@Override public void run(){
if(isChar('/'))
estadoNoChar(s_inicio);
else if(isChar('*'))
estadoNoChar(s_combloq1);
else
estadoNoChar(s_combloq);
}};
//-----------------------------------------------------------------
private Runnable s_neg = new Runnable() {@Override public void run(){
token(NEG);
}};
//-----------------------------------------------------------------
private Runnable s_or = new Runnable() {@Override public void run(){
if(isChar('|'))
estado(s_or1);
else
error();
}};
//-----------------------------------------------------------------
private Runnable s_or1 = new Runnable() {@Override public void run(){
token(OR);
}};
//-----------------------------------------------------------------
private Runnable s_and = new Runnable() {@Override public void run(){
if(isChar('&'))
estado(s_and1);
else
error();
}};
//-----------------------------------------------------------------
private Runnable s_and1 = new Runnable() {@Override public void run(){
token(AND);
}};
//-----------------------------------------------------------------
private Runnable s_ipar = new Runnable() {@Override public void run(){
token(IPAR);
}};
//-----------------------------------------------------------------
private Runnable s_dpar = new Runnable() {@Override public void run(){
token(DPAR);
}};
} // Afd
Esta parte sería la parte de la sintaxis
el ADR
Código:
/**
* Analizador sintáctico implementado mediante el método
* descendente recursivo.
* <p>En esta clase se debe implementar la gramática del lenguaje.
*/
public class ADR extends ASin
{
/**
* Construye un analizador descendente recursivo.
* @param lex Analizador léxico.
*/
public ADR(ALex lex)
{
super(lex);
}
/** SÃmbolo inicial de la gramática. */
public void programa()
{
declaracion();
bloque();
tokenCome(EOF);
}
//------------------------------------------------------------------------
private void declaracion()
{
switch(tokenTipo())
{
case ENTERO:
tokenCome(ENTERO);
tokenCome(ID);
codeVariableInteger();
declaracion();
break;
case CADENA:
tokenCome(CADENA);
tokenCome(ID);
codeVariableString();
declaracion();
break;
}
}
//------------------------------------------------------------------------
private void bloque()
{
switch(tokenTipo())
{
case ID:
asignacion();
bloque();
break;
case IMPRIMIR:
impresion();
bloque();
break;
case SI:
condicion();
bloque();
break;
case MIENTRAS:
iteracion();
bloque();
break;
}
}
//-----------------------------------------------------------------
private void asignacion()
{
tokenCome(ID);
codeVariableAsignacion();
tokenCome(ASIGN);
expresion();
codeAsignacion();
}
//-----------------------------------------------------------------
private void impresion()
{
tokenCome(IMPRIMIR);
expresion();
codePrint();
}
//-----------------------------------------------------------------
private void condicion()
{
tokenCome(SI);
expresion();
codeIf();
bloque();
sino();
codeEnd();
tokenCome(FIN);
}
//-----------------------------------------------------------------
private void sino()
{
if(tokenTipo()==SINO){
tokenCome(SINO);
codeElse();
bloque();
}
}
//-----------------------------------------------------------------
private void iteracion()
{
tokenCome(MIENTRAS);
codeWhile();
expresion();
codeIf();
bloque();
codeEnd();
tokenCome(FIN);
}
//-----------------------------------------------------------------
private void expresion()
{
vor();
vor1();
}
//-----------------------------------------------------------------
private void vor()
{
vand();
vand1();
}
//-----------------------------------------------------------------
private void vor1()
{
if(tokenTipo()==OR){
String op = tokenNombre();
tokenCome(OR);
vor();
codeOperador(op);
vor1();
}
}
//-----------------------------------------------------------------
private void vand()
{
if(tokenTipo()==NEG){
tokenCome(NEG);
vrel();
codeOperador("!");
vrel1();
}
else{
vrel();
vrel1();
}
}
//-----------------------------------------------------------------
private void vand1()
{
if(tokenTipo()==AND){
String op = tokenNombre();
tokenCome(AND);
vand();
codeOperador(op);
vand1();
}
}
//-----------------------------------------------------------------
private void vrel()
{
if(tokenTipo()==SUM){
boolean negar = tokenNombre().equals("-");
tokenCome(SUM);
vsum();
if(negar)
codeOperador("-1");
vsum1();
}
else{
vsum();
vsum1();
}
}
//-----------------------------------------------------------------
private void vrel1()
{
if(tokenTipo()==REL){
String op=tokenNombre();
tokenCome(REL);
vrel();
codeOperador(op);
}
}
//-----------------------------------------------------------------
private void vsum()
{
vmul();
vmul1();
}
//-----------------------------------------------------------------
private void vsum1()
{
if(tokenTipo()==SUM){
String op=tokenNombre();
tokenCome(SUM);
vsum();
codeOperador(op);
vsum1();
}
}
//-----------------------------------------------------------------
private void vmul()
{
if(tokenTipo()==IPAR){
tokenCome(IPAR);
expresion();
tokenCome(DPAR);
}
else{
valor();
}
}
//-----------------------------------------------------------------
private void vmul1()
{
if(tokenTipo()==MUL){
String op=tokenNombre();
tokenCome(MUL);
vmul();
codeOperador(op);
vmul1();
}
}
//-----------------------------------------------------------------
private void valor()
{
if(tokenTipo()==ID){
tokenCome(ID);
codeVariableExpresion();
}
else if(tokenTipo()==INTVAL){
tokenCome(INTVAL);
codeInteger();
}
else{
tokenCome(STRVAL);
codeString();
}
}
//-----------------------------------------------------------------
} //