Browse Source

Clean up

tags/gm/2021-09-23T00Z/github.com--lark-parser-lark/0.6.6
Erez Shinan 6 years ago
parent
commit
934a997f83
3 changed files with 17 additions and 24 deletions
  1. +2
    -2
      lark/parsers/earley.py
  2. +10
    -13
      lark/parsers/earley_forest.py
  3. +5
    -9
      lark/parsers/xearley.py

+ 2
- 2
lark/parsers/earley.py View File

@@ -10,7 +10,7 @@ is better documented here:
http://www.bramvandersanden.com/post/2014/06/shared-packed-parse-forest/
"""

from collections import deque, defaultdict
from collections import deque

from ..visitors import Transformer_InPlace, v_args
from ..exceptions import ParseError, UnexpectedToken
@@ -305,7 +305,7 @@ class Parser:
assert False, 'Earley should not generate multiple start symbol items!'

# Perform our SPPF -> AST conversion using the right ForestVisitor.
return self.forest_tree_visitor.go(solutions[0])
return self.forest_tree_visitor.visit(solutions[0])


class ApplyCallbacks(Transformer_InPlace):


+ 10
- 13
lark/parsers/earley_forest.py View File

@@ -8,17 +8,14 @@ http://www.bramvandersanden.com/post/2014/06/shared-packed-parse-forest/
"""

from random import randint
from ..tree import Tree
from ..exceptions import ParseError
from ..lexer import Token
from ..utils import Str
from ..grammar import NonTerminal, Terminal, Symbol

from math import isinf
from collections import deque
from operator import attrgetter
from importlib import import_module

from ..tree import Tree
from ..exceptions import ParseError

class ForestNode(object):
pass

@@ -163,7 +160,7 @@ class ForestVisitor(object):
def visit_packed_node_in(self, node): pass
def visit_packed_node_out(self, node): pass

def go(self, root):
def visit(self, root):
self.result = None
# Visiting is a list of IDs of all symbol/intermediate nodes currently in
# the stack. It serves two purposes: to detect when we 'recurse' in and out
@@ -278,16 +275,16 @@ class ForestToTreeVisitor(ForestVisitor):
self.forest_sum_visitor = forest_sum_visitor
self.callbacks = callbacks

def go(self, root):
def visit(self, root):
self.output_stack = deque()
return super(ForestToTreeVisitor, self).go(root)
return super(ForestToTreeVisitor, self).visit(root)

def visit_token_node(self, node):
self.output_stack[-1].append(node)

def visit_symbol_node_in(self, node):
if self.forest_sum_visitor and node.is_ambiguous and isinf(node.priority):
self.forest_sum_visitor.go(node)
self.forest_sum_visitor.visit(node)
return next(iter(node.children))

def visit_packed_node_in(self, node):
@@ -331,7 +328,7 @@ class ForestToAmbiguousTreeVisitor(ForestToTreeVisitor):

def visit_symbol_node_in(self, node):
if self.forest_sum_visitor and node.is_ambiguous and isinf(node.priority):
self.forest_sum_visitor.go(node)
self.forest_sum_visitor.visit(node)
if not node.is_intermediate and node.is_ambiguous:
self.output_stack.append(Tree('_ambig', []))
return iter(node.children)
@@ -370,8 +367,8 @@ class ForestToPyDotVisitor(ForestVisitor):
self.pydot = import_module('pydot')
self.graph = self.pydot.Dot(graph_type='digraph', rankdir=rankdir)

def go(self, root, filename):
super(ForestToPyDotVisitor, self).go(root)
def visit(self, root, filename):
super(ForestToPyDotVisitor, self).visit(root)
self.graph.write_png(filename)

def visit_token_node(self, node):


+ 5
- 9
lark/parsers/xearley.py View File

@@ -13,18 +13,14 @@ Instead of running a lexer beforehand, or using a costy char-by-char method, thi
uses regular expressions by necessity, achieving high-performance while maintaining all of
Earley's power in parsing any CFG.
"""
# Author: Erez Shinan (2017)
# Email : erezshin@gmail.com

from collections import defaultdict, deque
from collections import defaultdict

from ..exceptions import ParseError, UnexpectedCharacters
from ..exceptions import UnexpectedCharacters
from ..lexer import Token
from .grammar_analysis import GrammarAnalyzer
from ..grammar import NonTerminal, Terminal
from .earley import ApplyCallbacks, Parser as BaseParser
from .earley_common import Item, TransitiveItem
from .earley_forest import ForestToTreeVisitor, ForestToAmbiguousTreeVisitor, ForestSumVisitor, ForestToPyDotVisitor, SymbolNode
from ..grammar import Terminal
from .earley import Parser as BaseParser
from .earley_forest import SymbolNode


class Parser(BaseParser):


Loading…
Cancel
Save