Search notes:

Python library Lark: rules

Rules that start with a question mark

A rule that starts with a question mark is «inlined» if the rule expands to a single child.
The following example tries to demonstrate the difference between a «normal» rule and a rule that starts with a question mark:
import lark

grammar_without_question_mark = """
start:   rul   rul

rul:       /\w+/  /\w+/
   |  "("  /\w+/  ")"

%ignore " "
"""

# ---------------------


grammar_with_question_mark = """
start:   rul   rul

?rul:      /\w+/  /\w+/
   |  "("  /\w+/  ")"

%ignore " "
"""

# ---------------------

def parse(grammar):

    parser = lark.Lark(grammar)
    parsed = parser.parse('foo bar (baz)')

    for ch_1 in parsed.children:

        if   isinstance(ch_1, lark.Tree):
             print('Tree with name ' + str(ch_1.data))

             for ch_2 in ch_1.children:
                 print('   ' + ch_2.value)

        elif isinstance(ch_1, lark.Token):
             print('Token with value ' + str(ch_1.value))


parse(grammar_without_question_mark)

print('-' * 30)

parse(grammar_with_question_mark   )
Github repository about-Python, path: /libraries/Lark/rules/question-mark.py
When parsing the input foo bar (baz), the first grammar (where rul is without question mark), prints
Tree with name rul
   foo
   bar
Tree with name rul
   baz
The second grammar (using ?rul) prints
Tree with name rul
   foo
   bar
Token with value baz
More about the question mark in Larks documentation: Shaping the tree (from where this example is basically copied).

Index