This repo contains code to mirror other repos. It also contains the code that is getting mirrored.
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

33 строки
955 B

  1. from .parsers.lalr_analysis import GrammarAnalyzer
  2. from .common import is_terminal
  3. from .parsers import lalr_parser, earley
  4. class LALR:
  5. def build_parser(self, rules, callback, start):
  6. ga = GrammarAnalyzer(rules, start)
  7. ga.analyze()
  8. return lalr_parser.Parser(ga, callback)
  9. class Earley:
  10. @staticmethod
  11. def _process_expansion(x):
  12. return [{'literal': s} if is_terminal(s) else s for s in x]
  13. def build_parser(self, rules, callback, start):
  14. rules = [{'name':n, 'symbols': self._process_expansion(x), 'postprocess':getattr(callback, a)} for n,x,a in rules]
  15. return EarleyParser(earley.Parser(rules, start))
  16. class EarleyParser:
  17. def __init__(self, parser):
  18. self.parser = parser
  19. def parse(self, text):
  20. res = self.parser.parse(text)
  21. assert len(res) ==1 , 'Ambiguious Parse! Not handled yet'
  22. return res[0]
  23. ENGINE_DICT = { 'lalr': LALR, 'earley': Earley }