1
I am doing a work of LFA (formal languages and automata), which recognizes GLUD languages only that now I would like to make it recognize GLUE languages, which would basically just change the position of the uppercase letter ( state ).
PS: the program receives a.gr grammatical file where it contains the transition rules.
Ex: GLUD Gram = ({S, A, B}, {a, b}, { S -> aa, A -> bb, A -> λ, B -> aa, }, S)
Ex: GLUE Gram = ({S, A, B}, {a, b}, { S -> Aa, A -> Bb, A -> λ, B -> Aa, }, S)
The only difference is the position of the stateDestin and stateAtual.
The method that takes care of this is the setRules, at first I tried to change the values of substring, to change the positions of the values it picks. Instead of (0,1) I changed to (1,0) only it still gives error... n know more how to change to be able to invert the values...
Rodando:
Currently when I try to use GLUE
Main java.
package verificador;
import java.util.ArrayList;
import java.util.Arrays;
public class Verificador {
private final ArrayList<State> states;
private final ArrayList<String> alphabet;
private final ArrayList<Rule> rules;
private State inicial;
public Verificador() {
states = new ArrayList<>();
alphabet = new ArrayList<>();
rules = new ArrayList<>();
}
public void setStates(String states) {
states = states.replaceAll("\\{|\\}", ""); // Tira {}
for (String e : states.split(",")) {
this.states.add(new State(e));
}
this.states.add(new State("DEFAULT", true));
}
public State getStateByName(String name) {
for (State e : states) {
if (e.getName().equals(name)) {
return e;
}
}
return null;
}
public void setAlphabet(String alphabet) {
alphabet = alphabet.replaceAll("\\{|\\}", ""); // Tira {}
this.alphabet.addAll(Arrays.asList(alphabet.split(",")));
this.alphabet.add("#");
this.alphabet.add("λ");
}
public boolean checkLetter(String letter) {
for (String l : alphabet) {
if (l.equals(letter)) {
return true;
}
}
return false;
}
public void setRules(String regras) throws Exception {
regras = regras.replaceAll("\\{|\\}", ""); // Tira {}
for (String regra : regras.split(",")) {
String[] r = regra.split("->");
if (r.length != 2) {
throw new Exception("Regra mal formatada");
}
State estadoAtual = getStateByName(r[0]);
if (estadoAtual == null) {
throw new Exception("Regra mal formatada - Estado invalido1:" + r[0]);
}
try {
for (String transicao : r[1].split("\\|")) {
State estadoDestino;
String letter = transicao.substring(0,1);
if(!checkLetter(letter)){
throw new Exception("Regra mal formata - Caracter invalido: " + transicao.substring(0,1));
}
if (transicao.substring(1).equals("")) {
estadoDestino = getStateByName("DEFAULT");
} else {
estadoDestino = getStateByName(transicao.substring(1));
if (estadoDestino == null) {
throw new Exception("Regra mal formatada - Estado invalido:" + transicao.substring(1));
}
}
this.rules.add(new Rule(letter, estadoAtual, estadoDestino));
}
} catch (RuntimeException e) {
throw new Exception("Regra mal formatada");
}
}
}
public ArrayList<Rule> getPossibleRules(State estado, String elemento) {
ArrayList<Rule> regras = new ArrayList<>();
for (Rule r : this.rules) {
if (r.getState() == estado && r.getElement().equals(elemento)) {
regras.add(r);
}
}
return regras;
}
public void setInicial(String inicial) throws Exception {
State estado = getStateByName(inicial);
if (estado == null) {
throw new Exception("Estado inicial invalido");
}
this.inicial = estado;
}
// ({A,B,C}; {a,b,c}; A -> aB | bA | cA | λ, B -> aB | bC | cA | λ, C -> aB | bA | λ; A)
public void readGrammar(String gramatica) throws Exception {
gramatica = gramatica.replaceAll("\\(|\\)|\\s+|\\n", ""); // Tira parentesis e espaços
if(gramatica.contains("=")){ // Tira o nome da gramatica do inicio
gramatica = gramatica.split("=")[1];
}
gramatica = gramatica.replaceAll("\\},", "\\};"); // Substitui a , por ;
String[] g = gramatica.split(";"); // Quebra partes da gramatica
if (g.length != 4) {
throw new Exception("Gramatica mal formatada");
}
setStates(g[0]);
setAlphabet(g[1]);
setRules(g[2]);
setInicial(g[3]);
}
public void printRules() {
for (Rule r : rules) {
System.out.println(r.toString());
}
}
boolean checkWord(String palavra) {
System.out.format("%-10s %-10s\n", "Palavra", "Estado");
System.out.format("%-10s %-10s\n", palavra, inicial);
return checkWord(palavra, inicial);
}
boolean checkWord(String palavra, State estadoAtual) {
System.out.format("%-10s %-10s\n", palavra, estadoAtual);
if (palavra.isEmpty() && estadoAtual.getIsFinal()) { // terminou a palavra
return true;
} else if (palavra.isEmpty()) {
return false;
}
ArrayList<Rule> possiveisRegras = getPossibleRules(estadoAtual, String.valueOf(palavra.charAt(0)));
for (Rule r : possiveisRegras) {
if (checkWord(palavra.substring(1), r.getDestiny())) {
return true;
}
}
return false;
}
}
Rule.java
package verificador;
public class Rule {
private String element;
private State state;
private State destiny;
public Rule(String element, State state, State destiny){
this.element = element;
this.state = state;
this.destiny = destiny;
if(element.equals("#") || element.equals("λ")){
this.state.setIsFinal(true);
}
}
public String getElement() {
return element;
}
public void setElement(String element) {
this.element = element;
}
public State getState() {
return state;
}
public void setState(State state) {
this.state = state;
}
public State getDestiny() {
return destiny;
}
public void setDestiny(State destiny) {
this.destiny = destiny;
}
@Override
public String toString(){
return state + " -> " + element + destiny;
}
}
Java state.
package verificador;
public class State {
private String name;
private boolean isFinal;
public State(String name){
this.name = name;
}
public State(String name, boolean isFinal){
this.name = name;
this.isFinal = isFinal;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean getIsFinal() {
return isFinal;
}
public void setIsFinal(boolean isFinal) {
this.isFinal = isFinal;
}
@Override
public String toString(){
if(name.equals("DEFAULT")){
return "";
}
return name;
}
}
Java checker. (Place where it takes care of it, responsible method setRules();
package verificador;
import java.util.ArrayList;
import java.util.Arrays;
public class Verificador {
private final ArrayList<State> states;
private final ArrayList<String> alphabet;
private final ArrayList<Rule> rules;
private State inicial;
public Verificador() {
states = new ArrayList<>();
alphabet = new ArrayList<>();
rules = new ArrayList<>();
}
public void setStates(String states) {
states = states.replaceAll("\\{|\\}", ""); // Tira {}
for (String e : states.split(",")) {
this.states.add(new State(e));
}
this.states.add(new State("DEFAULT", true));
}
public State getStateByName(String name) {
for (State e : states) {
if (e.getName().equals(name)) {
return e;
}
}
return null;
}
public void setAlphabet(String alphabet) {
alphabet = alphabet.replaceAll("\\{|\\}", ""); // Tira {}
this.alphabet.addAll(Arrays.asList(alphabet.split(",")));
this.alphabet.add("#");
this.alphabet.add("λ");
}
public boolean checkLetter(String letter) {
for (String l : alphabet) {
if (l.equals(letter)) {
return true;
}
}
return false;
}
public void setRules(String regras) throws Exception {
regras = regras.replaceAll("\\{|\\}", ""); // Tira {}
for (String regra : regras.split(",")) {
String[] r = regra.split("->");
if (r.length != 2) {
throw new Exception("Regra mal formatada");
}
State estadoAtual = getStateByName(r[0]);
if (estadoAtual == null) {
throw new Exception("Regra mal formatada - Estado invalido1:" + r[0]);
}
try {
for (String transicao : r[1].split("\\|")) {
State estadoDestino;
String letter = transicao.substring(0,1);
if(!checkLetter(letter)){
throw new Exception("Regra mal formata - Caracter invalido: " + transicao.substring(0,1));
}
if (transicao.substring(1).equals("")) {
estadoDestino = getStateByName("DEFAULT");
} else {
estadoDestino = getStateByName(transicao.substring(1));
if (estadoDestino == null) {
throw new Exception("Regra mal formatada - Estado invalido:" + transicao.substring(1));
}
}
this.rules.add(new Rule(letter, estadoAtual, estadoDestino));
}
} catch (RuntimeException e) {
throw new Exception("Regra mal formatada");
}
}
}
public ArrayList<Rule> getPossibleRules(State estado, String elemento) {
ArrayList<Rule> regras = new ArrayList<>();
for (Rule r : this.rules) {
if (r.getState() == estado && r.getElement().equals(elemento)) {
regras.add(r);
}
}
return regras;
}
public void setInicial(String inicial) throws Exception {
State estado = getStateByName(inicial);
if (estado == null) {
throw new Exception("Estado inicial invalido");
}
this.inicial = estado;
}
// ({A,B,C}; {a,b,c}; A -> aB | bA | cA | λ, B -> aB | bC | cA | λ, C -> aB | bA | λ; A)
public void readGrammar(String gramatica) throws Exception {
gramatica = gramatica.replaceAll("\\(|\\)|\\s+|\\n", ""); // Tira parentesis e espaços
if(gramatica.contains("=")){ // Tira o nome da gramatica do inicio
gramatica = gramatica.split("=")[1];
}
gramatica = gramatica.replaceAll("\\},", "\\};"); // Substitui a , por ;
String[] g = gramatica.split(";"); // Quebra partes da gramatica
if (g.length != 4) {
throw new Exception("Gramatica mal formatada");
}
setStates(g[0]);
setAlphabet(g[1]);
setRules(g[2]);
setInicial(g[3]);
}
public void printRules() {
for (Rule r : rules) {
System.out.println(r.toString());
}
}
boolean checkWord(String palavra) {
System.out.format("%-10s %-10s\n", "Palavra", "Estado");
System.out.format("%-10s %-10s\n", palavra, inicial);
return checkWord(palavra, inicial);
}
boolean checkWord(String palavra, State estadoAtual) {
System.out.format("%-10s %-10s\n", palavra, estadoAtual);
if (palavra.isEmpty() && estadoAtual.getIsFinal()) { // terminou a palavra
return true;
} else if (palavra.isEmpty()) {
return false;
}
ArrayList<Rule> possiveisRegras = getPossibleRules(estadoAtual, String.valueOf(palavra.charAt(0)));
for (Rule r : possiveisRegras) {
if (checkWord(palavra.substring(1), r.getDestiny())) {
return true;
}
}
return false;
}
}
I didn’t ask this question before ?
– Isac
I had already done for the same program, only it’s for a different action now :(
– Stan