Skip to content

Instantly share code, notes, and snippets.

@RicardoLara
Last active December 14, 2017 01:08
Show Gist options
  • Save RicardoLara/9991ff5c293ce83b56df06debd513d9e to your computer and use it in GitHub Desktop.
Save RicardoLara/9991ff5c293ce83b56df06debd513d9e to your computer and use it in GitHub Desktop.
First & Follow v1.3 Correciones :v
package teest;
import java.util.*;
/*
NOTAS:
- Si se cambia la gramática tienen que modificarse los valores: numSimbNT, numSimbT, renglonesReglas y simbInicial [Constructor]
- Para contar los ST se considera ε
- Gramática sin rec x la izq o se buggea xD
- First recibe una cadena X y regresa una CADENA Y con los simbolos resultantes de First(X)
- Follow recibe una cadena X y regresa una CADENA Y con los simbolos resultantes de Follow(X)
- Para usar First y Follow es NECESARIO llamar a preFirst y preFollow (respectivamente) ya que
dichas funciones preparan las cadenas necesarias para el regreso de las funciones
*/
public class Teest {
int numSimbNT, numSimbT, renglonesReglas;
String simbInicial;
String STerminales[];
String SNTerminales[];
String expresiones[] = {
"E -> T EP", //0
"EP -> + T EP | - T EP | ε", //1
"T -> F TP", //2
"TP -> * F TP | / F TP | ε", //3
"F -> ( E ) | num" //4
}; //numSimbNT = 5, numST = 8, renglones = 5, simbIni = "E"
/*String expresiones[] = {
"S -> E", //0
"E -> b E a | a E b | b a" //1
};*/ //numSimbNT = 2, numST = 2, renglones = 2, simbIni = "S"
String cadFirst, cadFollow;
public Teest(){
numSimbNT = 5;
numSimbT = 8;
renglonesReglas = 5;
simbInicial = "E";
SimbolosNoTerminales(expresiones);
SimbolosTerminales(expresiones);
System.out.println("First(E) => |"+preFirst("E")+"|");
System.out.println("First(EP) => |"+preFirst("EP")+"|");
System.out.println("First(T) => |"+preFirst("T")+"|");
System.out.println("First(TP) => |"+preFirst("TP")+"|");
System.out.println("First(TP E) => |"+preFirst("TP E")+"|");
System.out.println("First(F) => |"+preFirst("F")+"|");
System.out.println("Follow(E) => |"+preFollow("E")+"|");
System.out.println("Follow(EP) => |"+preFollow("EP")+"|");
System.out.println("Follow(T) => |"+preFollow("T")+"|");
System.out.println("Follow(TP) => |"+preFollow("TP")+"|");
System.out.println("Follow(F) => |"+preFollow("F")+"|");
}
public int ExisteSNT(String cad){
for(int i=0; i<numSimbNT; i++)
if(SNTerminales[i].equals(cad))
return i;
return -1;
}
public int ExisteST(String cad){
for(int i=0; i<numSimbT; i++)
if(STerminales[i].equals(cad))
return i;
return -1;
}
public void SimbolosNoTerminales(String Array[]){
SNTerminales = new String[numSimbNT];
for(int i=0; i<numSimbNT; i++)
SNTerminales[i] = "";
for(int i = 0; i<renglonesReglas; i++){
String aux = ""; int x = 0;
while(Array[i].charAt(x) != ' ')
aux += Array[i].charAt(x++);
SNTerminales[i] += aux;
}
System.out.println("Simbolos No Terminales: "+Arrays.toString(SNTerminales));
}
public void SimbolosTerminales(String Array[]){
int j = 0;
STerminales = new String[numSimbT];
for(int i = 0; i < numSimbT; i++) STerminales[i] = "";
for(int i = 0; i < renglonesReglas; i++){
String aux = ""; int x = 0;
while(x < Array[i].length()){
while(Array[i].charAt(x) == ' ' || Array[i].charAt(x) == '|') x++;
while(x <= Array[i].length()-1 && Array[i].charAt(x) != ' ')
aux += Array[i].charAt(x++);
if( !aux.equals("->"))
if(aux.charAt(0) != ' ' && aux.charAt(0) != '|')
if(ExisteSNT(aux) == -1)
if(ExisteST(aux) == -1)
STerminales[j++] = aux;
aux = "";
}
}
System.out.println("Simbolos Terminales: "+Arrays.toString(STerminales));
}
public String preFirst(String cad){
cadFirst = "";
String F = First(cad);
return F.substring(1);
}
public String preFollow(String cad){
cadFirst = ""; cadFollow = "";
String F = Follow(cad);
return F.substring(1);
}
public String First(String cad){
int x = 0; String aux = "";
while(x < cad.length() && cad.charAt(x) != ' ')
aux += cad.charAt(x++);
if(ExisteST(aux) != -1){
if(!cadFirst.contains(aux))
cadFirst = cadFirst + " " + aux;
return cadFirst;
}
if(aux.equals('.')){
cadFirst = cadFirst + " " + aux;
return cadFirst;
}
int pos = ExisteSNT(aux);
int x2 = 0;
String aux2 = "";
int numReglas = 1;
while(expresiones[pos].charAt(x2) != ' ')
aux2 += expresiones[pos].charAt(x2++);
aux2 = ""; x2++;
while(expresiones[pos].charAt(x2) != ' ')
aux2 += expresiones[pos].charAt(x2++);
aux2 = ""; x2++;
for(int lastAux = 0; lastAux < expresiones[pos].length(); lastAux++)
if(expresiones[pos].charAt(lastAux) == '|') numReglas++;
while(numReglas>0){
while(x2 < expresiones[pos].length() && expresiones[pos].charAt(x2) != ' ')
aux2 += expresiones[pos].charAt(x2++);
String checkCad = First(aux2);
if(!cadFirst.contains(checkCad)) cadFirst += checkCad;
if(cadFirst.contains("ε")){
if(x < cad.length()){
x++;
cadFirst = cadFirst.replace("ε", ""); aux = "";
while(x < cad.length()) aux += cad.charAt(x++);
checkCad = First(aux);
if(!cadFirst.contains(checkCad)) cadFirst += checkCad;
}
}
if(numReglas > 1){
while(expresiones[pos].charAt(x2) != '|') x2++;
x2+=2;
}
aux2 = "";
numReglas--;
}
return cadFirst;
}
public String Follow(String cad){
if(cad.length() == 1) //Soy solo un caracter
if(cad.equals(simbInicial))
if(!cadFollow.contains("$")) cadFollow += " $";
for(int i = 0; i < renglonesReglas; i++){
int indice = 0; String aux = "";
while(expresiones[i].charAt(indice) != ' ')
aux += expresiones[i].charAt(indice++);
aux = ""; indice++;
while(expresiones[i].charAt(indice) != ' ')
aux += expresiones[i].charAt(indice++);
aux = ""; indice++;
String cut = expresiones[i].substring(indice);
if(cut.contains(cad)){
while(indice <= expresiones[i].length()){
if(indice == expresiones[i].length()){
if(cad.equals(aux)){
int indAux = 0; String auxAux = "";
while(expresiones[i].charAt(indAux) != ' ')
auxAux += expresiones[i].charAt(indAux++);
if(!auxAux.equals(cad)){
String auxFollow = Follow(auxAux);
if(!cadFollow.contains(auxFollow)) cadFollow += auxFollow;
}
}
}
else if(expresiones[i].charAt(indice) == ' '){
if(cad.equals(aux) && indice < expresiones[i].length()){
int indAux = indice+1; String auxAux = "";
while(indAux < expresiones[i].length() && expresiones[i].charAt(indAux) != ' ')
auxAux += expresiones[i].charAt(indAux++);
String auxFirst;
if(auxAux.equals("|") || auxAux.equals('|')) auxFirst = "ε";
else auxFirst = " " + preFirst(auxAux);
if(auxFirst.contains("ε")){
auxFirst = auxFirst.replace("ε", "");
indAux = 0; auxAux = "";
while(expresiones[i].charAt(indAux) != ' ')
auxAux += expresiones[i].charAt(indAux++);
if(!auxAux.equals(cad)){
if(!cadFollow.contains(auxFirst)) cadFollow += auxFirst;
String auxFollow = Follow(auxAux);
if(!cadFollow.contains(auxFollow)) cadFollow += auxFollow;
}
}
if(!cadFollow.contains(auxFirst)) cadFollow += auxFirst;
}
else if(cad.equals(aux) && indice >= expresiones[i].length()-1){
int indAux = 0; String auxAux = "";
while(expresiones[i].charAt(indAux) != ' ')
auxAux += expresiones[i].charAt(indAux++);
if(!auxAux.equals(cad)){
String auxFollow = Follow(auxAux);
if(!cadFollow.contains(auxFollow)) cadFollow += auxFollow;
}
}
aux = "";
}
if(indice <= expresiones[i].length()-1){
aux += expresiones[i].charAt(indice);
aux = aux.replace(" ", "");
}
indice++;
}
}
}
return cadFollow;
}
public static void main (String[] args) throws java.lang.Exception{
new Teest();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment