Exemple #1
0
  @Before
  public void init() {

    Nonterminal X = Nonterminal.withName("X");

    Nonterminal A = Nonterminal.withName("A");
    Nonterminal B = Nonterminal.withName("B");
    Nonterminal C = Nonterminal.withName("C");

    Rule r1 =
        Rule.withHead(X)
            .addSymbol(Nonterminal.builder(A).setLabel("a").build())
            .addSymbol(
                Star.builder(
                        Sequence.builder(
                                Code.code(
                                    Nonterminal.builder(B)
                                        .addPreCondition(predicate(equal(rExt("a"), lExt("b"))))
                                        .build(),
                                    stat(println(lExt("b")))),
                                Code.code(
                                    Nonterminal.builder(C).setLabel("b").build(),
                                    stat(println(var("b")))))
                            .build())
                    .setLabel("b")
                    .build())
            .build();

    Rule r2 = Rule.withHead(A).addSymbol(Terminal.from(Character.from('a'))).build();
    Rule r3 = Rule.withHead(B).addSymbol(Terminal.from(Character.from('b'))).build();
    Rule r4 = Rule.withHead(C).addSymbol(Terminal.from(Character.from('c'))).build();

    grammar = Grammar.builder().addRules(r1, r2, r3, r4).build();
  }
Exemple #2
0
  @Test
  public void test() {
    Grammar grammar =
        Grammar.builder()

            // $default$ ::=  {UNDEFINED,-1,NON_REC} PREC(1,1)
            .addRule(
                Rule.withHead(Nonterminal.builder("$default$").build())
                    .setLayoutStrategy(NO_LAYOUT)
                    .setRecursion(Recursion.NON_REC)
                    .setAssociativity(Associativity.UNDEFINED)
                    .setPrecedence(-1)
                    .setPrecedenceLevel(
                        PrecedenceLevel.from(
                            1,
                            1,
                            -1,
                            false,
                            false,
                            false,
                            new Integer[] {},
                            false,
                            new Integer[] {}))
                    .build())
            // E ::= E (^) E  {LEFT,1,LEFT_RIGHT_REC} LEFT(1,1,1) PREC(1,1)
            .addRule(
                Rule.withHead(Nonterminal.builder("E").build())
                    .addSymbol(Nonterminal.builder("E").build())
                    .addSymbol(
                        Terminal.builder(Sequence.builder(Character.builder(94).build()).build())
                            .build())
                    .addSymbol(Nonterminal.builder("E").build())
                    .setRecursion(Recursion.LEFT_RIGHT_REC)
                    .setAssociativity(Associativity.LEFT)
                    .setPrecedence(1)
                    .setAssociativityGroup(
                        new AssociativityGroup(
                            Associativity.LEFT,
                            PrecedenceLevel.from(
                                1,
                                1,
                                -1,
                                true,
                                false,
                                false,
                                new Integer[] {},
                                false,
                                new Integer[] {}),
                            1,
                            1,
                            1))
                    .setPrecedenceLevel(
                        PrecedenceLevel.from(
                            1,
                            1,
                            -1,
                            true,
                            false,
                            false,
                            new Integer[] {},
                            false,
                            new Integer[] {}))
                    .build())
            // E ::= (-) E  {UNDEFINED,1,RIGHT_REC} LEFT(1,1,1) PREC(1,1)
            .addRule(
                Rule.withHead(Nonterminal.builder("E").build())
                    .addSymbol(
                        Terminal.builder(Sequence.builder(Character.builder(45).build()).build())
                            .build())
                    .addSymbol(Nonterminal.builder("E").build())
                    .setRecursion(Recursion.RIGHT_REC)
                    .setAssociativity(Associativity.UNDEFINED)
                    .setPrecedence(1)
                    .setAssociativityGroup(
                        new AssociativityGroup(
                            Associativity.LEFT,
                            PrecedenceLevel.from(
                                1,
                                1,
                                -1,
                                true,
                                false,
                                false,
                                new Integer[] {},
                                false,
                                new Integer[] {}),
                            1,
                            1,
                            1))
                    .setPrecedenceLevel(
                        PrecedenceLevel.from(
                            1,
                            1,
                            -1,
                            true,
                            false,
                            false,
                            new Integer[] {},
                            false,
                            new Integer[] {}))
                    .build())
            // E ::= (+) E  {UNDEFINED,1,RIGHT_REC} LEFT(1,1,1) PREC(1,1)
            .addRule(
                Rule.withHead(Nonterminal.builder("E").build())
                    .addSymbol(
                        Terminal.builder(Sequence.builder(Character.builder(43).build()).build())
                            .build())
                    .addSymbol(Nonterminal.builder("E").build())
                    .setRecursion(Recursion.RIGHT_REC)
                    .setAssociativity(Associativity.UNDEFINED)
                    .setPrecedence(1)
                    .setAssociativityGroup(
                        new AssociativityGroup(
                            Associativity.LEFT,
                            PrecedenceLevel.from(
                                1,
                                1,
                                -1,
                                true,
                                false,
                                false,
                                new Integer[] {},
                                false,
                                new Integer[] {}),
                            1,
                            1,
                            1))
                    .setPrecedenceLevel(
                        PrecedenceLevel.from(
                            1,
                            1,
                            -1,
                            true,
                            false,
                            false,
                            new Integer[] {},
                            false,
                            new Integer[] {}))
                    .build())
            // E ::= (a)  {UNDEFINED,-1,NON_REC} PREC(1,1)
            .addRule(
                Rule.withHead(Nonterminal.builder("E").build())
                    .addSymbol(
                        Terminal.builder(Sequence.builder(Character.builder(97).build()).build())
                            .build())
                    .setRecursion(Recursion.NON_REC)
                    .setAssociativity(Associativity.UNDEFINED)
                    .setPrecedence(-1)
                    .setPrecedenceLevel(
                        PrecedenceLevel.from(
                            1,
                            1,
                            -1,
                            true,
                            false,
                            false,
                            new Integer[] {},
                            false,
                            new Integer[] {}))
                    .build())
            // E ::= E (*) E  {UNDEFINED,2,LEFT_RIGHT_REC} PREC(2,2)
            .addRule(
                Rule.withHead(Nonterminal.builder("E").build())
                    .addSymbol(Nonterminal.builder("E").build())
                    .addSymbol(
                        Terminal.builder(Sequence.builder(Character.builder(42).build()).build())
                            .build())
                    .addSymbol(Nonterminal.builder("E").build())
                    .setRecursion(Recursion.LEFT_RIGHT_REC)
                    .setAssociativity(Associativity.UNDEFINED)
                    .setPrecedence(2)
                    .setPrecedenceLevel(
                        PrecedenceLevel.from(
                            2,
                            2,
                            2,
                            false,
                            false,
                            true,
                            new Integer[] {1},
                            false,
                            new Integer[] {}))
                    .build())
            // S ::= E  {UNDEFINED,-1,NON_REC} PREC(1,1)
            .addRule(
                Rule.withHead(Nonterminal.builder("S").build())
                    .addSymbol(Nonterminal.builder("E").build())
                    .setRecursion(Recursion.NON_REC)
                    .setAssociativity(Associativity.UNDEFINED)
                    .setPrecedence(-1)
                    .setPrecedenceLevel(
                        PrecedenceLevel.from(
                            1,
                            1,
                            -1,
                            false,
                            false,
                            false,
                            new Integer[] {},
                            false,
                            new Integer[] {}))
                    .build())
            .build();

    grammar = new DesugarPrecedenceAndAssociativity().transform(grammar);
    System.out.println(grammar.toStringWithOrderByPrecedence());

    Input input = Input.fromString("a^-a");
    GrammarGraph graph = GrammarGraph.from(grammar, input, Configuration.DEFAULT);

    // Visualization.generateGrammarGraph("test/org/iguana/parser/datadependent/precedence/",
    // graph);

    ParseResult result = Iguana.parse(input, graph, Nonterminal.withName("S"));

    Assert.assertTrue(result.isParseSuccess());

    // Visualization.generateSPPFGraph("test/org/iguana/parser/datadependent/precedence/",
    //                   result.asParseSuccess().getSPPFNode(), input);

    Assert.assertEquals(0, result.asParseSuccess().getStatistics().getCountAmbiguousNodes());
  }