Search in sources :

Example 1 with GeneralSymbol

use of br.ufpe.cin.if688.parsing.analysis.GeneralSymbol in project if688.github.io by if688.

the class App method main.

public static void main(String[] args) throws NotLL1Exception, ErrorOnParseException {
    /*
		 * Gramática de exemplo:
		 *  A -> aB
		 * 	B -> cC
		 * 	C -> d
		 * 
		 */
    Nonterminal start = new Nonterminal("A");
    Nonterminal B = new Nonterminal("B");
    Nonterminal C = new Nonterminal("C");
    Terminal a = new Terminal("a");
    Terminal c = new Terminal("c");
    Terminal d = new Terminal("d");
    List<GeneralSymbol> prodA = new ArrayList<GeneralSymbol>();
    prodA.add(a);
    prodA.add(B);
    List<GeneralSymbol> prodB = new ArrayList<GeneralSymbol>();
    prodB.add(c);
    prodB.add(C);
    List<GeneralSymbol> prodC = new ArrayList<GeneralSymbol>();
    prodC.add(d);
    Production fpA = new Production(start, prodA);
    Production pB = new Production(B, prodB);
    Production pC = new Production(C, prodC);
    Collection<Production> col = new ArrayList<Production>();
    col.add(fpA);
    col.add(pB);
    col.add(pC);
    List<Terminal> example = new ArrayList<Terminal>();
    example.add(a);
    example.add(c);
    example.add(d);
    Grammar g = new Grammar(col, start);
    Map<Nonterminal, Set<GeneralSymbol>> first = SetGenerator.getFirst(g);
    Map<Nonterminal, Set<GeneralSymbol>> follow = SetGenerator.getFollow(g, first);
    Map<LL1Key, List<GeneralSymbol>> table = Table.createTable(g);
    Parser parser = ParserGenerator.createParser(g);
    ParseTree parseTree = ParserUtils.parseSequence(parser, example);
    System.out.println("Exemplo 1:\n" + "A -> aB\n" + "B -> cC\n" + "C -> d");
    System.out.println("Conjunto first: " + first.toString());
    System.out.println("Conjunto follow: " + follow.toString());
    System.out.println("Tabela de parsing: " + table.toString());
    System.out.println("Exemplo de parsing: " + parseTree.toString() + "\n");
}
Also used : Set(java.util.Set) ArrayList(java.util.ArrayList) Parser(br.ufpe.cin.if688.parser.Parser) LL1Key(br.ufpe.cin.if688.table.LL1Key) ArrayList(java.util.ArrayList) List(java.util.List) ParseTree(br.ufpe.cin.if688.parser.ParseTree)

Example 2 with GeneralSymbol

use of br.ufpe.cin.if688.parsing.analysis.GeneralSymbol in project if688.github.io by if688.

the class FollowSetTest method testFollowSetG1.

public void testFollowSetG1() {
    /*
		 * Gramática de exemplo:
		 *  A -> aB
		 * 	B -> cC
		 * 	C -> d
		 * 
		 */
    Nonterminal start = new Nonterminal("A");
    Nonterminal B = new Nonterminal("B");
    Nonterminal C = new Nonterminal("C");
    Terminal a = new Terminal("a");
    Terminal c = new Terminal("c");
    Terminal d = new Terminal("d");
    List<GeneralSymbol> prodA = new ArrayList<GeneralSymbol>();
    prodA.add(a);
    prodA.add(B);
    List<GeneralSymbol> prodB = new ArrayList<GeneralSymbol>();
    prodB.add(c);
    prodB.add(C);
    List<GeneralSymbol> prodC = new ArrayList<GeneralSymbol>();
    prodC.add(d);
    Production fpA = new Production(start, prodA);
    Production pB = new Production(B, prodB);
    Production pC = new Production(C, prodC);
    Collection<Production> col = new ArrayList<Production>();
    col.add(fpA);
    col.add(pB);
    col.add(pC);
    Grammar g = new Grammar(col, start);
    Map<Nonterminal, Set<GeneralSymbol>> first = SetGenerator.getFirst(g);
    Map<Nonterminal, Set<GeneralSymbol>> follow = SetGenerator.getFollow(g, first);
    Map<Nonterminal, Set<GeneralSymbol>> expected = new HashMap<Nonterminal, Set<GeneralSymbol>>();
    for (Nonterminal nt : g.getNonterminals()) {
        expected.put(nt, new HashSet<GeneralSymbol>());
    }
    expected.get(C).add(SpecialSymbol.EOF);
    expected.get(B).add(SpecialSymbol.EOF);
    expected.get(start).add(SpecialSymbol.EOF);
    assertEquals(expected, follow);
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) GeneralSymbol(br.ufpe.cin.if688.parsing.analysis.GeneralSymbol) Production(br.ufpe.cin.if688.parsing.grammar.Production) ArrayList(java.util.ArrayList) Grammar(br.ufpe.cin.if688.parsing.grammar.Grammar) Nonterminal(br.ufpe.cin.if688.parsing.grammar.Nonterminal) Terminal(br.ufpe.cin.if688.parsing.grammar.Terminal)

Example 3 with GeneralSymbol

use of br.ufpe.cin.if688.parsing.analysis.GeneralSymbol in project if688.github.io by if688.

the class FollowSetTest method testFollowSetG2.

public void testFollowSetG2() {
    /*
		 * Gramatica de exemplo
		 * S -> aABe
		 * A -> bK
		 * K -> bcK | ε
		 * B -> d
		*/
    Nonterminal S = new Nonterminal("S");
    Nonterminal A = new Nonterminal("A");
    Nonterminal B = new Nonterminal("B");
    Nonterminal K = new Nonterminal("K");
    Terminal a = new Terminal("a");
    Terminal b = new Terminal("b");
    Terminal c = new Terminal("c");
    Terminal d = new Terminal("d");
    Terminal e = new Terminal("e");
    List<GeneralSymbol> prodS = new ArrayList<GeneralSymbol>();
    prodS.add(a);
    prodS.add(A);
    prodS.add(B);
    prodS.add(e);
    List<GeneralSymbol> prodA = new ArrayList<GeneralSymbol>();
    prodA.add(b);
    prodA.add(K);
    List<GeneralSymbol> prodK1 = new ArrayList<GeneralSymbol>();
    prodK1.add(b);
    prodK1.add(c);
    prodK1.add(K);
    List<GeneralSymbol> prodK2 = new ArrayList<GeneralSymbol>();
    prodK2.add(SpecialSymbol.EPSILON);
    List<GeneralSymbol> prodB = new ArrayList<GeneralSymbol>();
    prodB.add(d);
    Production pS = new Production(S, prodS);
    Production pA = new Production(A, prodA);
    Production pK1 = new Production(K, prodK1);
    Production pK2 = new Production(K, prodK2);
    Production pB = new Production(B, prodB);
    Collection<Production> collection = new ArrayList<Production>();
    collection.add(pS);
    collection.add(pA);
    collection.add(pK1);
    collection.add(pK2);
    collection.add(pB);
    Grammar g = new Grammar(collection, S);
    Map<Nonterminal, Set<GeneralSymbol>> first = SetGenerator.getFirst(g);
    Map<Nonterminal, Set<GeneralSymbol>> follow = SetGenerator.getFollow(g, first);
    Map<Nonterminal, Set<GeneralSymbol>> expected = new HashMap<Nonterminal, Set<GeneralSymbol>>();
    for (Nonterminal nt : g.getNonterminals()) {
        expected.put(nt, new HashSet<GeneralSymbol>());
    }
    expected.get(K).add(d);
    expected.get(S).add(SpecialSymbol.EOF);
    expected.get(B).add(e);
    expected.get(A).add(d);
    assertEquals(expected, follow);
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) GeneralSymbol(br.ufpe.cin.if688.parsing.analysis.GeneralSymbol) Production(br.ufpe.cin.if688.parsing.grammar.Production) ArrayList(java.util.ArrayList) Grammar(br.ufpe.cin.if688.parsing.grammar.Grammar) Nonterminal(br.ufpe.cin.if688.parsing.grammar.Nonterminal) Terminal(br.ufpe.cin.if688.parsing.grammar.Terminal)

Example 4 with GeneralSymbol

use of br.ufpe.cin.if688.parsing.analysis.GeneralSymbol in project if688.github.io by if688.

the class FirstSetTest method testFirstSetG2.

public void testFirstSetG2() {
    /*
		 * Gramatica de exemplo
		 * S -> aABe
		 * A -> bK
		 * K -> bcK | ε
		 * B -> d
		*/
    Nonterminal S = new Nonterminal("S");
    Nonterminal A = new Nonterminal("A");
    Nonterminal B = new Nonterminal("B");
    Nonterminal K = new Nonterminal("K");
    Terminal a = new Terminal("a");
    Terminal b = new Terminal("b");
    Terminal c = new Terminal("c");
    Terminal d = new Terminal("d");
    Terminal e = new Terminal("e");
    List<GeneralSymbol> prodS = new ArrayList<GeneralSymbol>();
    prodS.add(a);
    prodS.add(A);
    prodS.add(B);
    prodS.add(e);
    List<GeneralSymbol> prodA = new ArrayList<GeneralSymbol>();
    prodA.add(b);
    prodA.add(K);
    List<GeneralSymbol> prodK1 = new ArrayList<GeneralSymbol>();
    prodK1.add(b);
    prodK1.add(c);
    prodK1.add(K);
    List<GeneralSymbol> prodK2 = new ArrayList<GeneralSymbol>();
    prodK2.add(SpecialSymbol.EPSILON);
    List<GeneralSymbol> prodB = new ArrayList<GeneralSymbol>();
    prodB.add(d);
    Production pS = new Production(S, prodS);
    Production pA = new Production(A, prodA);
    Production pK1 = new Production(K, prodK1);
    Production pK2 = new Production(K, prodK2);
    Production pB = new Production(B, prodB);
    Collection<Production> collection = new ArrayList<Production>();
    collection.add(pS);
    collection.add(pA);
    collection.add(pK1);
    collection.add(pK2);
    collection.add(pB);
    Grammar g = new Grammar(collection, S);
    Map<Nonterminal, Set<GeneralSymbol>> first = SetGenerator.getFirst(g);
    Map<Nonterminal, Set<GeneralSymbol>> expected = new HashMap<Nonterminal, Set<GeneralSymbol>>();
    for (Nonterminal nt : g.getNonterminals()) {
        expected.put(nt, new HashSet<GeneralSymbol>());
    }
    expected.get(K).add(SpecialSymbol.EPSILON);
    expected.get(K).add(b);
    expected.get(S).add(a);
    expected.get(B).add(d);
    expected.get(A).add(b);
    assertEquals(expected, first);
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) GeneralSymbol(br.ufpe.cin.if688.parsing.analysis.GeneralSymbol) Production(br.ufpe.cin.if688.parsing.grammar.Production) ArrayList(java.util.ArrayList) Grammar(br.ufpe.cin.if688.parsing.grammar.Grammar) Nonterminal(br.ufpe.cin.if688.parsing.grammar.Nonterminal) Terminal(br.ufpe.cin.if688.parsing.grammar.Terminal)

Example 5 with GeneralSymbol

use of br.ufpe.cin.if688.parsing.analysis.GeneralSymbol in project if688.github.io by if688.

the class FirstSetTest method testFirstSetG1.

public void testFirstSetG1() {
    /*
		 * Gramática de exemplo:
		 *  A -> aB
		 * 	B -> cC
		 * 	C -> d
		 * 
		 */
    Nonterminal start = new Nonterminal("A");
    Nonterminal B = new Nonterminal("B");
    Nonterminal C = new Nonterminal("C");
    Terminal a = new Terminal("a");
    Terminal c = new Terminal("c");
    Terminal d = new Terminal("d");
    List<GeneralSymbol> prodA = new ArrayList<GeneralSymbol>();
    prodA.add(a);
    prodA.add(B);
    List<GeneralSymbol> prodB = new ArrayList<GeneralSymbol>();
    prodB.add(c);
    prodB.add(C);
    List<GeneralSymbol> prodC = new ArrayList<GeneralSymbol>();
    prodC.add(d);
    Production fpA = new Production(start, prodA);
    Production pB = new Production(B, prodB);
    Production pC = new Production(C, prodC);
    Collection<Production> col = new ArrayList<Production>();
    col.add(fpA);
    col.add(pB);
    col.add(pC);
    Grammar g = new Grammar(col, start);
    Map<Nonterminal, Set<GeneralSymbol>> first = SetGenerator.getFirst(g);
    Map<Nonterminal, Set<GeneralSymbol>> expected = new HashMap<Nonterminal, Set<GeneralSymbol>>();
    for (Nonterminal nt : g.getNonterminals()) {
        expected.put(nt, new HashSet<GeneralSymbol>());
    }
    expected.get(C).add(d);
    expected.get(B).add(c);
    expected.get(start).add(a);
    assertEquals(expected, first);
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) GeneralSymbol(br.ufpe.cin.if688.parsing.analysis.GeneralSymbol) Production(br.ufpe.cin.if688.parsing.grammar.Production) ArrayList(java.util.ArrayList) Grammar(br.ufpe.cin.if688.parsing.grammar.Grammar) Nonterminal(br.ufpe.cin.if688.parsing.grammar.Nonterminal) Terminal(br.ufpe.cin.if688.parsing.grammar.Terminal)

Aggregations

ArrayList (java.util.ArrayList)7 GeneralSymbol (br.ufpe.cin.if688.parsing.analysis.GeneralSymbol)6 Grammar (br.ufpe.cin.if688.parsing.grammar.Grammar)6 Nonterminal (br.ufpe.cin.if688.parsing.grammar.Nonterminal)6 Production (br.ufpe.cin.if688.parsing.grammar.Production)6 Terminal (br.ufpe.cin.if688.parsing.grammar.Terminal)6 HashMap (java.util.HashMap)6 Set (java.util.Set)5 HashSet (java.util.HashSet)4 LL1Key (br.ufpe.cin.if688.table.LL1Key)3 List (java.util.List)3 ParseTree (br.ufpe.cin.if688.parser.ParseTree)1 Parser (br.ufpe.cin.if688.parser.Parser)1