lark icon indicating copy to clipboard operation
lark copied to clipboard

Parsing context-free grammar with smart interplay between lexer and parser

Open zuckerruebe opened this issue 3 years ago • 2 comments

I'd like to parse inputs like

equations 3
a=7
b=a*c
c=exp(-1)

which are generated by a context-sensitive grammar (because of the 3 giving the number of equations to follow).

Is there a way to parse such a grammar using an interplay between lexer and parser in Lark, preferably in a "fast" way, e.g. using LARL.

Early kind of works but is painfully - and understandibly - slow and needs to be painstakingly tweaked to most of the time yield the desired parse tree from the ambiguous grammar.

zuckerruebe avatar Oct 03 '22 14:10 zuckerruebe

Well, I'm not sure why the 3 matters here.. Just parse everything and verify the count after it's done.

But if you really need this much control, look into the interactive parser with Lark.parse_interactive().

erezsh avatar Oct 03 '22 15:10 erezsh

The example might not have been the best but there are instances where without knowing the number of ... (this extends to other "blocks" than equations) you cannot reliably determine where the next block starts.

zuckerruebe avatar Oct 03 '22 16:10 zuckerruebe