def parse(self, lcls, head, tokens, output): grammar = ParseRuleSet() for rule_name, rule in sorted(lcls.items(), reverse=True): if isinstance(rule, ParseRule): grammar.add(rule) logging_builder = LoggingBuilder(lcls) parse(grammar, head, tokens).apply(logging_builder) print(logging_builder.get_text()) logging_builder.compare(output, self)
# This is the example is explained in more detail in usage.rst from axaxaxas import ParseRule, ParseRuleSet, Terminal as T, NonTerminal as NT grammar = ParseRuleSet() grammar.add(ParseRule("sentence", [NT("noun"), NT("verb"), NT("noun")])) grammar.add(ParseRule("noun", [T("man")])) grammar.add(ParseRule("noun", [T("dog")])) grammar.add(ParseRule("verb", [T("bites")])) ### from axaxaxas import parse parse_forest = parse(grammar, "sentence", "man bites dog".split()) print(parse_forest.single()) ### grammar.add(ParseRule("relative", [T("step", optional=True), T("sister")])) grammar.add(ParseRule("relative", [T("great", star=True), T("grandfather")])) print(parse(grammar, "relative", "sister".split()).single()) print(parse(grammar, "relative", "step sister".split()).single()) print(parse(grammar, "relative", "grandfather".split()).single()) print(parse(grammar, "relative", "great great grandfather".split()).single()) ### grammar.add(ParseRule("described relative", [NT("adjective", star=True), NT("relative")])) grammar.add(ParseRule("adjective", [T("awesome")])) grammar.add(ParseRule("adjective", [T("great")]))
def setUp(self): self.p = ParseRuleSet()
# This is the example is explained in more detail in usage.rst from axaxaxas import ParseRule, ParseRuleSet, Terminal as T, NonTerminal as NT grammar = ParseRuleSet() grammar.add(ParseRule("sentence", [NT("noun"), NT("verb"), NT("noun")])) grammar.add(ParseRule("noun", [T("man")])) grammar.add(ParseRule("noun", [T("dog")])) grammar.add(ParseRule("verb", [T("bites")])) ### from axaxaxas import parse parse_forest = parse(grammar, "sentence", "man bites dog".split()) print(parse_forest.single()) ### grammar.add(ParseRule("relative", [T("step", optional=True), T("sister")])) grammar.add(ParseRule("relative", [T("great", star=True), T("grandfather")])) print(parse(grammar, "relative", "sister".split()).single()) print(parse(grammar, "relative", "step sister".split()).single()) print(parse(grammar, "relative", "grandfather".split()).single()) print(parse(grammar, "relative", "great great grandfather".split()).single()) ### grammar.add( ParseRule("described relative", [NT("adjective", star=True),