Lark: How to handle empty input?

Created on 7 Feb 2019  路  2Comments  路  Source: lark-parser/lark

I've got a small query language where the empty string makes sense as a query. I'm handling it as a special case outside of Lark, and I'd like to be able to get rid of that special case. I've tried things like:

?start: the_actual_grammar
| "" -> op_empty

and:

?start: the_actual_grammar
| / */ -> op_empty

But it fails with "Dynamic Earley doesn't allow zero-width regexps" which pretty much explains the situation.

Any ideas how to work around this?

Most helpful comment

Why not use an empty rule?

>>> p = Lark('!start: "a" | ')
>>> p.parse('a')
Tree(start, [Token(A, 'a')])
>>> p.parse('')
Tree(start, [])

All 2 comments

Why not use an empty rule?

>>> p = Lark('!start: "a" | ')
>>> p.parse('a')
Tree(start, [Token(A, 'a')])
>>> p.parse('')
Tree(start, [])

Thanks, didn't realize that was possible!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

evandrocoan picture evandrocoan  路  6Comments

FrancescoManfredi picture FrancescoManfredi  路  7Comments

ius picture ius  路  4Comments

kootenpv picture kootenpv  路  4Comments

supposedly picture supposedly  路  5Comments