How to invert the positions

1

I'm doing a LFA (formal and automated languages) work, which recognizes GLUD languages only now I'd like to make it recognize GLUE languages, which would basically just change the position of the uppercase letter (target state).

PS: the program receives a grammar file.gr 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 stateDestination and current state.

The method that takes care of this is setRules, at first I just tried to change the values of the substring, to change the positions of the values it takes. Instead of (0,1) I changed it to (1.0) only though it still gives error ... n know more what to change to be able to invert the values ...

Running Program:

CurrentlywhenItrytouseGLUE

  

Main.java

packageverificador;importjava.util.ArrayList;importjava.util.Arrays;publicclassVerificador{privatefinalArrayList<State>states;privatefinalArrayList<String>alphabet;privatefinalArrayList<Rule>rules;privateStateinicial;publicVerificador(){states=newArrayList<>();alphabet=newArrayList<>();rules=newArrayList<>();}publicvoidsetStates(Stringstates){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;
    }
}
  

State.java

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;
    }

}
  

Verifier.java   (Location where you take care of that, 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;
    }

}
    
asked by anonymous 30.09.2017 / 18:11

0 answers