question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

LALR templates not expanding

See original GitHub issue

Describe the bug

Basically, templates don’t work as well on the LALR parser. I have to manually expand the template in order for my grammar to work on LALR. This kind of defeats the purpose of templates.

To Reproduce

Grammar “A”:

%import common.WORD
%import common.NEWLINE -> _NL
%ignore " "

start: _list{WORD}

_COMMA: "," _NL?
_list{x}: _sep{x, _COMMA} _COMMA?
_sep{x, sep}: x (sep x)*

Grammar “B” (this is equivalent to grammar A but I manually expanded one of the templates):

%import common.WORD
%import common.NEWLINE -> _NL
%ignore " "

start: _list{WORD}

_COMMA: "," _NL?
_list{x}:  x (_COMMA x)* _COMMA?

Problem

Earley can parse the following examples with either grammars

Hello, world
Hello, world,

LALR can only parse both inputs using grammar B (grammar A fails when trying to parse the second input)

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
erezshcommented, Apr 2, 2022

Each template is currently expanded into its own rule. Which is why you get a shift/reduce conflict.

Btw, I already opened an issue for this a year ago, but it didn’t get any attention: https://github.com/lark-parser/lark/issues/822

0reactions
erezshcommented, Apr 2, 2022

Templates generate trees where data is the template name. It’s very predictable.

Of course, you won’t see those starting with underscore, because they get inlined during parsing.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Resolving a shift/reduce conflict in an LALR parser
Yacc parsers are not particularly powerful and attempting a context-free parse may be asking too much of it. I suggest using some kind...
Read more >
Is LALR(1) or LL(k) parser better for C++ - Google Groups
The LALR(1) grammar does not deal with templates at all. My LL(2) grammar is weak in a number of ways (although I'm working...
Read more >
Example-Driven Error Reporting - Lark documentation
A demonstration of example-driven error reporting with the LALR parser (See ... label = 'Missing Opening' class JsonMissingClosing(JsonSyntaxError): label ...
Read more >
Elkhound: A fast, practical GLR parser generator
Abstract. The Generalized LR (GLR) parsing algorithm is attractive for use in parsing programming languages because it is asymptotically.
Read more >
cwbaker/lalr: Modern LALR(1) parser for C++ - GitHub
If the symbol and the production have the same precedence and the symbol explicitly has no associativity (i.e. it is listed in a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found