Gramática y expresiones:
<> : : = <>
<> : : = <>
<> : : = <>
<> : : = <> <> <> <> <>
<> <>
<> : : = if <> { <> }
<> : : = if <> { <> } else { <> }
<> : : = if <> { <> } elseif <> { <> } else { <> }
<> : : = switch <> { case 1: <> case 2: <>
case 3: <> case n: <> default: <> }
<> : : = while <> { <> }
<> : : = do { <> } while <>
<> : : = for <> <> <> {
<> : : = <>
<> : : = <>
<> : : = <>
<> : : = <>
<> : : = vacio
<> : : = <> = <>
<> : : = $ <>
<> : : = <>
<> : : = <>
<> : : = < . > < , > < : > < ; > < - > <> < # > < ¿ > …
<> : : = > < = = ! = > = < = <> : : = AND && OR !! XOR !
<> : : = ++ --
<> : : = <> <> <>
<> : : = <>
<> : : = <> <> <>
<> : : = <> <> <>
<> <> <>
<> : : = <> <> <>
<> : : = function <> ( <> ) { <> }
<> : : =
<> : : = <> <>
Analizador Lexico:
public class AnalisisLexico {
int contador;
public String Analizalexphp (String palabra, int lex) {
int matriz[][] = new int [5][35];
// Letter (_) Numero ( ) + * - / ^ { } " " # ! & $ < > = . , : ; ? fin de cad
matriz [0][0] = 1; matriz [0][1] = 1; matriz [0][2] = 1; matriz [0][3] = 1; matriz [0][4] = 1; matriz [0][5] = 1; matriz [0][6] = 1; matriz [0][7] = 1; matriz [0][8] = 1; matriz [0][9] = 1; matriz [0][10] = 1; matriz [0][11] = 1; matriz [0][12] = 1; matriz [0][13] = 1; matriz [0][14] = 1; matriz [0][15] = 1; matriz [0][16] = 1; matriz [0][17] = 1; matriz [0][18] = 1; matriz [0][19] = 1; matriz [0][20] = 1; matriz [0][21] = 1; matriz [0][22] = 1; matriz [0][23] = 1; matriz [0][24] = 1; matriz [0][25] = 1; matriz [0][26] = 3;
matriz [1][0] = 1; matriz [1][1] = 1; matriz [1][2] = 1; matriz [1][3] = 1; matriz [1][4] = 1; matriz [1][5] = 1; matriz [1][6] = 1; matriz [1][7] = 1; matriz [1][8] = 1; matriz [1][9] = 1; matriz [1][10] = 1; matriz [1][11] = 1; matriz [1][12] = 1; matriz [1][13] = 1; matriz [1][14] = 1; matriz [1][15] = 1; matriz [1][16] = 1; matriz [1][17] = 1; matriz [1][18] = 1; matriz [1][19] = 1; matriz [1][20] = 1; matriz [1][21] = 1; matriz [1][22] = 1; matriz [1][23] = 1; matriz [1][24] = 1; matriz [1][25] = 1; matriz [0][26] = 2;
matriz [2][0] = 2; matriz [2][1] = 2; matriz [2][2] = 2; matriz [2][3] = 2; matriz [2][4] = 2; matriz [2][5] = 2; matriz [2][6] = 2; matriz [2][7] = 2; matriz [2][8] = 2; matriz [2][9] = 2; matriz [2][10] = 2; matriz [2][11] = 2; matriz [2][12] = 2; matriz [2][13] = 2; matriz [2][14] = 2; matriz [2][15] = 2; matriz [2][16] = 2; matriz [2][17] = 2; matriz [2][18] = 2; matriz [2][19] = 2; matriz [2][20] = 2; matriz [2][21] = 2; matriz [2][22] = 2; matriz [2][23] = 2; matriz [2][24] = 2; matriz [2][25] = 2; matriz [0][26] = 2;
matriz [3][0] = 3; matriz [3][1] = 3; matriz [3][2] = 3; matriz [3][3] = 3; matriz [3][4] = 3; matriz [3][5] = 3; matriz [3][6] = 3; matriz [3][7] = 3; matriz [3][8] = 3; matriz [3][9] = 3; matriz [3][10] = 3; matriz [3][11] = 3; matriz [3][12] = 3; matriz [3][13] = 3; matriz [3][14] = 3; matriz [3][15] = 3; matriz [3][16] = 3; matriz [3][17] = 3; matriz [3][18] = 3; matriz [3][19] = 3; matriz [3][20] = 3; matriz [3][21] = 3; matriz [3][22] = 3; matriz [3][23] = 3; matriz [3][24] = 3; matriz [3][25] = 3; matriz [0][26] = 3;
String cad = palabra;
String resultado = null;
int posicion = 0;
try{
for ( lex =0 ; lex
{
posicion = matriz[posicion][19];
}
else if( cad.charAt(lex) =='=' )
{
posicion = matriz[posicion][20];
}
else if( cad.charAt(lex) =='.' )
{
posicion = matriz[posicion][21];
}
else if( cad.charAt(lex) ==',' )
{
posicion = matriz[posicion][22];
}
else if( cad.charAt(lex) ==':' )
{
posicion = matriz[posicion][23];
}
else if( cad.charAt(lex) ==';' )
{
posicion = matriz[posicion][24];
}
else if( cad.charAt(lex) =='?' )
{
posicion = matriz[posicion][25];
}
else {
posicion = 3;
break;
}
}
contador = lex - 1;
if( posicion !=1 && posicion != 2 )
{
resultado = "Existe Un Error Lexico";
}
else
{
resultado = "No hay Error Lexico";
}
}catch(ArrayIndexOutOfBoundsException e){}
return resultado;
}
}
Grafico:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
JOptionPane.showMessageDialog(null, jTextArea1.getText());
AnalisisLexico lex= new AnalisisLexico();
jTextField2.setText(lex.Analizalexphp(jTextArea1.getText(),0));
}

No hay comentarios:
Publicar un comentario