Пример #1
0
 @Override
 public Iterable<GSSNode> getGSSNodes() {
   return grammarGraph
       .getNonterminals()
       .stream()
       .flatMap(s -> StreamSupport.stream(s.getGSSNodes().spliterator(), false))
       .collect(Collectors.toList());
 }
Пример #2
0
 @Test
 public void testParser3() {
   GrammarGraph graph = GrammarGraph.from(grammar, input3, Configuration.DEFAULT);
   ParseResult result = Iguana.parse(input3, graph, startSymbol);
   assertTrue(result.isParseSuccess());
   assertEquals(getParseResult3(graph), result);
   assertEquals(getTree3(), result.asParseSuccess().getTree());
 }
Пример #3
0
 @Test
 public void testParser2() {
   GrammarGraph graph = GrammarGraph.from(grammar, input2);
   ParseResult result = Iguana.parse(input2, graph, startSymbol);
   assertTrue(result.isParseSuccess());
   assertEquals(getParseResult2(graph), result);
   assertEquals(getTree2(), result.asParseSuccess().getTree());
 }
Пример #4
0
  @Test
  public void test() {
    System.out.println(grammar);

    // Input input = Input.fromString("xawz");
    Input input = Input.fromString("xyaw");
    GrammarGraph graph = GrammarGraph.from(grammar, input, Configuration.DEFAULT);

    ParseResult result = Iguana.parse(input, graph, Nonterminal.withName("S"));
  }
Пример #5
0
  @Test
  public void test() {
    System.out.println(grammar);

    grammar = new EBNFToBNF().transform(grammar);
    System.out.println(grammar);

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

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

    Assert.assertTrue(result.isParseSuccess());

    // Visualization.generateGrammarGraph("/Users/anastasiaizmaylova/git/diguana/test/org/jgll/parser/datadependent/", graph);

    // Visualization.generateSPPFGraph("/Users/anastasiaizmaylova/git/diguana/test/org/jgll/parser/datadependent/",
    // 		result.asParseSuccess().getSPPFNode(), input);

    Assert.assertTrue(result.asParseSuccess().getStatistics().getCountAmbiguousNodes() == 0);
  }
Пример #6
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, false))
                    .build())
            // E ::= ('-') E  {UNDEFINED,1,RIGHT_REC} 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)
                    .setPrecedenceLevel(PrecedenceLevel.from(1, 1, 1, true, false, false, false))
                    .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, false))
                    .build())
            // E ::= E ('^') E  {LEFT,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(94).build()).build())
                            .build())
                    .addSymbol(Nonterminal.builder("E").build())
                    .setRecursion(Recursion.LEFT_RIGHT_REC)
                    .setAssociativity(Associativity.LEFT)
                    .setPrecedence(2)
                    .setPrecedenceLevel(PrecedenceLevel.from(2, 2, -1, false, false, true, false))
                    .build())
            // E ::= E ('*') E  {UNDEFINED,3,LEFT_RIGHT_REC} PREC(3,3)
            .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(3)
                    .setPrecedenceLevel(PrecedenceLevel.from(3, 3, 3, false, false, true, false))
                    .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, false))
                    .build())
            .build();
    // grammar = new EBNFToBNF().transform(grammar);
    System.out.println(grammar);

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

    Input input = Input.fromString("a*-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());
  }
Пример #7
0
 protected NonterminalGrammarSlot getStartSymbol(Nonterminal nonterminal) {
   return grammarGraph.getHead(nonterminal);
 }
Пример #8
0
  @Override
  public final ParseResult parse(
      Input input,
      GrammarGraph grammarGraph,
      Nonterminal nonterminal,
      Map<String, ? extends Object> map,
      boolean global) {
    this.grammarGraph = grammarGraph;
    this.input = input;

    grammarGraph.reset(input);
    resetParser();

    /** Data-dependent GLL parsing */
    this.ctx = new PersistentEvaluatorContext(input);

    if (global) map.forEach((k, v) -> ctx.declareGlobalVariable(k, v));

    NonterminalGrammarSlot startSymbol = getStartSymbol(nonterminal);

    if (startSymbol == null) {
      throw new RuntimeException("No nonterminal named " + nonterminal + " found");
    }

    NonterminalNode root;

    Environment env = null;

    if (!global && !map.isEmpty()) {
      Object[] arguments = new Object[map.size()];

      int i = 0;
      for (String parameter : nonterminal.getParameters()) arguments[i++] = map.get(parameter);

      startGSSNode = startSymbol.getGSSNode(0, new GSSNodeData<>(arguments));
      env = getEmptyEnvironment().declare(nonterminal.getParameters(), arguments);
    } else {
      startGSSNode = startSymbol.getGSSNode(0);
    }

    cu = startGSSNode;

    logger.log("Parsing %s:", input.getURI());

    long start = System.nanoTime();
    long startUserTime = BenchmarkUtil.getUserTime();
    long startSystemTime = BenchmarkUtil.getSystemTime();

    parse(startSymbol, env);

    root = startGSSNode.getNonterminalNode(input.length() - 1);

    ParseResult parseResult;

    long end = System.nanoTime();
    long endUserTime = BenchmarkUtil.getUserTime();
    long endSystemTime = BenchmarkUtil.getSystemTime();

    if (root == null) {
      parseResult = new ParseError(errorSlot, input, errorIndex, errorGSSNode);
      logger.log("Parse error:\n %s", parseResult);
    } else {
      ParseStatistics parseStatistics =
          ParseStatistics.builder()
              .setNanoTime(end - start)
              .setUserTime(endUserTime - startUserTime)
              .setSystemTime(endSystemTime - startSystemTime)
              .setMemoryUsed(BenchmarkUtil.getMemoryUsed())
              .setDescriptorsCount(descriptorsCount)
              .setGSSNodesCount(countGSSNodes + 1) // + start gss node
              .setGSSEdgesCount(countGSSEdges)
              .setNonterminalNodesCount(countNonterminalNodes)
              .setTerminalNodesCount(countTerminalNodes)
              .setIntermediateNodesCount(countIntemediateNodes)
              .setPackedNodesCount(countPackedNodes)
              .setAmbiguousNodesCount(countAmbiguousNodes)
              .build();

      parseResult = new ParseSuccess(root, parseStatistics, input);
      logger.log("Parsing finished successfully.");
      logger.log(parseStatistics.toString());
    }

    return parseResult;
  }
Пример #9
0
 private NonterminalNode expectedSPPF1(GrammarGraph registry) {
   TerminalNode node0 = createTerminalNode(registry.getSlot("a"), 0, 1, input1);
   NonterminalNode node1 =
       createNonterminalNode(registry.getSlot("E"), registry.getSlot("E ::= a ."), node0, input1);
   TerminalNode node2 = createTerminalNode(registry.getSlot("+"), 1, 2, input1);
   IntermediateNode node3 =
       createIntermediateNode(registry.getSlot("E ::= E + . E"), node1, node2);
   TerminalNode node4 = createTerminalNode(registry.getSlot("a"), 2, 3, input1);
   NonterminalNode node5 =
       createNonterminalNode(registry.getSlot("E"), registry.getSlot("E ::= a ."), node4, input1);
   IntermediateNode node6 =
       createIntermediateNode(registry.getSlot("E ::= E + E ."), node3, node5);
   NonterminalNode node7 =
       createNonterminalNode(
           registry.getSlot("E"), registry.getSlot("E ::= E + E ."), node6, input1);
   return node7;
 }
Пример #10
0
 private NonterminalNode expectedSPPF3(GrammarGraph registry) {
   TerminalNode node0 = createTerminalNode(registry.getSlot("a"), 0, 1, input3);
   NonterminalNode node1 =
       createNonterminalNode(registry.getSlot("E"), registry.getSlot("E ::= a ."), node0, input3);
   TerminalNode node2 = createTerminalNode(registry.getSlot("+"), 1, 2, input3);
   IntermediateNode node3 =
       createIntermediateNode(registry.getSlot("E ::= E + . E"), node1, node2);
   TerminalNode node4 = createTerminalNode(registry.getSlot("a"), 2, 3, input3);
   NonterminalNode node5 =
       createNonterminalNode(registry.getSlot("E"), registry.getSlot("E ::= a ."), node4, input3);
   TerminalNode node6 = createTerminalNode(registry.getSlot("*"), 3, 4, input3);
   IntermediateNode node7 =
       createIntermediateNode(registry.getSlot("E ::= E * . E"), node5, node6);
   TerminalNode node8 = createTerminalNode(registry.getSlot("a"), 4, 5, input3);
   NonterminalNode node9 =
       createNonterminalNode(registry.getSlot("E"), registry.getSlot("E ::= a ."), node8, input3);
   IntermediateNode node10 =
       createIntermediateNode(registry.getSlot("E ::= E * E ."), node7, node9);
   NonterminalNode node11 =
       createNonterminalNode(
           registry.getSlot("E"), registry.getSlot("E ::= E * E ."), node10, input3);
   TerminalNode node12 = createTerminalNode(registry.getSlot("+"), 5, 6, input3);
   IntermediateNode node13 =
       createIntermediateNode(registry.getSlot("E ::= E + . E"), node11, node12);
   TerminalNode node14 = createTerminalNode(registry.getSlot("a"), 6, 7, input3);
   NonterminalNode node15 =
       createNonterminalNode(registry.getSlot("E"), registry.getSlot("E ::= a ."), node14, input3);
   IntermediateNode node16 =
       createIntermediateNode(registry.getSlot("E ::= E + E ."), node13, node15);
   IntermediateNode node17 =
       createIntermediateNode(registry.getSlot("E ::= E + . E"), node9, node12);
   IntermediateNode node18 =
       createIntermediateNode(registry.getSlot("E ::= E + E ."), node17, node15);
   NonterminalNode node19 =
       createNonterminalNode(
           registry.getSlot("E"), registry.getSlot("E ::= E + E ."), node18, input3);
   IntermediateNode node20 =
       createIntermediateNode(registry.getSlot("E ::= E * E ."), node7, node19);
   NonterminalNode node21 =
       createNonterminalNode(
           registry.getSlot("E"), registry.getSlot("E ::= E + E ."), node16, input3);
   node21.addPackedNode(registry.getSlot("E ::= E * E ."), node20);
   TerminalNode node22 = createTerminalNode(registry.getSlot("*"), 7, 8, input3);
   IntermediateNode node23 =
       createIntermediateNode(registry.getSlot("E ::= E * . E"), node21, node22);
   TerminalNode node24 = createTerminalNode(registry.getSlot("a"), 8, 9, input3);
   NonterminalNode node25 =
       createNonterminalNode(registry.getSlot("E"), registry.getSlot("E ::= a ."), node24, input3);
   IntermediateNode node26 =
       createIntermediateNode(registry.getSlot("E ::= E * . E"), node15, node22);
   IntermediateNode node27 =
       createIntermediateNode(registry.getSlot("E ::= E * E ."), node26, node25);
   NonterminalNode node28 =
       createNonterminalNode(
           registry.getSlot("E"), registry.getSlot("E ::= E * E ."), node27, input3);
   IntermediateNode node29 =
       createIntermediateNode(registry.getSlot("E ::= E + E ."), node17, node28);
   IntermediateNode node30 =
       createIntermediateNode(registry.getSlot("E ::= E * . E"), node19, node22);
   IntermediateNode node31 =
       createIntermediateNode(registry.getSlot("E ::= E * E ."), node30, node25);
   NonterminalNode node32 =
       createNonterminalNode(
           registry.getSlot("E"), registry.getSlot("E ::= E + E ."), node29, input3);
   node32.addPackedNode(registry.getSlot("E ::= E * E ."), node31);
   IntermediateNode node33 =
       createIntermediateNode(registry.getSlot("E ::= E * E ."), node23, node25);
   node33.addPackedNode(registry.getSlot("E ::= E * E ."), node7, node32);
   IntermediateNode node34 =
       createIntermediateNode(registry.getSlot("E ::= E + E ."), node13, node28);
   NonterminalNode node35 =
       createNonterminalNode(
           registry.getSlot("E"), registry.getSlot("E ::= E * E ."), node33, input3);
   node35.addPackedNode(registry.getSlot("E ::= E + E ."), node34);
   IntermediateNode node36 =
       createIntermediateNode(registry.getSlot("E ::= E + E ."), node3, node11);
   IntermediateNode node37 =
       createIntermediateNode(registry.getSlot("E ::= E + E ."), node3, node5);
   NonterminalNode node38 =
       createNonterminalNode(
           registry.getSlot("E"), registry.getSlot("E ::= E + E ."), node37, input3);
   IntermediateNode node39 =
       createIntermediateNode(registry.getSlot("E ::= E * . E"), node38, node6);
   IntermediateNode node40 =
       createIntermediateNode(registry.getSlot("E ::= E * E ."), node39, node9);
   NonterminalNode node41 =
       createNonterminalNode(
           registry.getSlot("E"), registry.getSlot("E ::= E + E ."), node36, input3);
   node41.addPackedNode(registry.getSlot("E ::= E * E ."), node40);
   IntermediateNode node42 =
       createIntermediateNode(registry.getSlot("E ::= E + . E"), node41, node12);
   IntermediateNode node43 =
       createIntermediateNode(registry.getSlot("E ::= E + E ."), node3, node35);
   node43.addPackedNode(registry.getSlot("E ::= E + E ."), node42, node28);
   IntermediateNode node44 =
       createIntermediateNode(registry.getSlot("E ::= E + E ."), node3, node21);
   node44.addPackedNode(registry.getSlot("E ::= E + E ."), node42, node15);
   IntermediateNode node45 =
       createIntermediateNode(registry.getSlot("E ::= E * E ."), node39, node19);
   NonterminalNode node46 =
       createNonterminalNode(
           registry.getSlot("E"), registry.getSlot("E ::= E + E ."), node44, input3);
   node46.addPackedNode(registry.getSlot("E ::= E * E ."), node45);
   IntermediateNode node47 =
       createIntermediateNode(registry.getSlot("E ::= E * . E"), node46, node22);
   IntermediateNode node48 =
       createIntermediateNode(registry.getSlot("E ::= E * E ."), node47, node25);
   node48.addPackedNode(registry.getSlot("E ::= E * E ."), node39, node32);
   NonterminalNode node49 =
       createNonterminalNode(
           registry.getSlot("E"), registry.getSlot("E ::= E + E ."), node43, input3);
   node49.addPackedNode(registry.getSlot("E ::= E * E ."), node48);
   return node49;
 }
Пример #11
0
 private NonterminalNode expectedSPPF2(GrammarGraph registry) {
   TerminalNode node0 = createTerminalNode(registry.getSlot("a"), 0, 1, input2);
   NonterminalNode node1 =
       createNonterminalNode(registry.getSlot("E"), registry.getSlot("E ::= a ."), node0, input2);
   TerminalNode node2 = createTerminalNode(registry.getSlot("+"), 1, 2, input2);
   IntermediateNode node3 =
       createIntermediateNode(registry.getSlot("E ::= E + . E"), node1, node2);
   TerminalNode node4 = createTerminalNode(registry.getSlot("a"), 2, 3, input2);
   NonterminalNode node5 =
       createNonterminalNode(registry.getSlot("E"), registry.getSlot("E ::= a ."), node4, input2);
   TerminalNode node6 = createTerminalNode(registry.getSlot("*"), 3, 4, input2);
   IntermediateNode node7 =
       createIntermediateNode(registry.getSlot("E ::= E * . E"), node5, node6);
   TerminalNode node8 = createTerminalNode(registry.getSlot("a"), 4, 5, input2);
   NonterminalNode node9 =
       createNonterminalNode(registry.getSlot("E"), registry.getSlot("E ::= a ."), node8, input2);
   IntermediateNode node10 =
       createIntermediateNode(registry.getSlot("E ::= E * E ."), node7, node9);
   NonterminalNode node11 =
       createNonterminalNode(
           registry.getSlot("E"), registry.getSlot("E ::= E * E ."), node10, input2);
   IntermediateNode node12 =
       createIntermediateNode(registry.getSlot("E ::= E + E ."), node3, node11);
   IntermediateNode node13 =
       createIntermediateNode(registry.getSlot("E ::= E + E ."), node3, node5);
   NonterminalNode node14 =
       createNonterminalNode(
           registry.getSlot("E"), registry.getSlot("E ::= E + E ."), node13, input2);
   IntermediateNode node15 =
       createIntermediateNode(registry.getSlot("E ::= E * . E"), node14, node6);
   IntermediateNode node16 =
       createIntermediateNode(registry.getSlot("E ::= E * E ."), node15, node9);
   NonterminalNode node17 =
       createNonterminalNode(
           registry.getSlot("E"), registry.getSlot("E ::= E + E ."), node12, input2);
   node17.addPackedNode(registry.getSlot("E ::= E * E ."), node16);
   return node17;
 }