lark icon indicating copy to clipboard operation
lark copied to clipboard

Lark allows both '?' and '_' on the same rule

Open chanicpanic opened this issue 3 years ago • 1 comments
trafficstars

Describe the bug

Lark allows ? and _ to be used on the same rule. As they both control the inlining of children, I would expect them to be mutually exclusive. In fact, including both of them on the same rule can cause an error during tree building.

To Reproduce

grammar = r"""
start: _a
!?_a: "A"
"""

p = Lark(grammar)
p.parse("A")
  File "/___/lark/lark.py", line 628, in parse
    return self.parser.parse(text, start=start, on_error=on_error)
  File "/___/lark/parser_frontends.py", line 96, in parse
    return self.parser.parse(stream, chosen_start, **kw)
  File "/___/lark/parsers/earley.py", line 292, in parse
    return transformer.transform(solutions[0])
  File "/___/lark/parsers/earley_forest.py", line 395, in transform
    self.visit(root)
  File "/___/lark/parsers/earley_forest.py", line 532, in visit
    super(ForestToParseTree, self).visit(root)
  File "/___/lark/parsers/earley_forest.py", line 336, in visit
    vpno(current)
  File "/___/lark/parsers/earley_forest.py", line 633, in visit_packed_node_out
    super(ForestToParseTree, self).visit_packed_node_out(node)
  File "/___/lark/parsers/earley_forest.py", line 445, in visit_packed_node_out
    self._visit_node_out_helper(node, self.transform_packed_node)
  File "/___/lark/parsers/earley_forest.py", line 433, in _visit_node_out_helper
    transformed = method(node, self.data[id(node)])
  File "/___/lark/parsers/earley_forest.py", line 617, in transform_packed_node
    return self._cache.setdefault(id(node), self._call_rule_func(node, children))
  File "/___/lark/parsers/earley_forest.py", line 563, in _call_rule_func
    return self.callbacks[node.rule](data)
  File "/___/lark/parse_tree_builder.py", line 146, in __call__
    filtered = children[i].children
AttributeError: 'Token' object has no attribute 'children'

chanicpanic avatar Aug 01 '22 17:08 chanicpanic

Good catch!

I guess we should just throw an error when they are used together.

erezsh avatar Aug 01 '22 21:08 erezsh

Thanks for reporting it!

erezsh avatar Oct 07 '22 18:10 erezsh