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.

Trying to parse a soft keyword

See original GitHub issue

What is your question?

I’m currently making a Python transpiler, using lark/grammars/python.lark for parsing. The grammar is almost fine, but it lacks match…case support.

I’ve adapted grammar from official docs, but now I encounter a new problem

If you’re having trouble with your code or grammar Here’s a minimal example, with semicolons and braces added to drop indentation checks

from lark import Lark

grammar = """
%ignore /[\t \f\n]+/x
NAME: /[a-zA-Z_]\w*/
MATCH: "match"
CASE: "case"
NUMBER: ("+" | "-")? "0".."9"

expr: NAME | NUMBER

agmt: NAME "=" expr
match: MATCH expr "{" case+ "}"
case: CASE expr block

block: "{" code "}"
code: stmt+

stmt: (expr | match | agmt) ";"
"""

parser = Lark(grammar, start="code", parser="lalr")

code = """match = 1;
case = 2;
match N {
    case Z {
        5;
    } 
};"""

parser.parse(code)

What happens is “match” in first line treated as the beginning of match rule and fails to parse. Is there a way to resolve this while using LALR(1), not Earley?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:10 (10 by maintainers)

github_iconTop GitHub Comments

2reactions
MegaIngcommented, Oct 8, 2021

Not in general. There is a reason python3.10 does no longer use a lalr compatible parser/grammar. You can maybe get around this with a postlex that looks at the next token/symbol/end of the line for a colon l and change it according to that, but this is simply not context free.

1reaction
MegaIngcommented, Oct 9, 2021

@erezsh Yeah, you are correct it works.

@evtn #1016 should contain full support for parsing match statements.

Read more comments on GitHub >

github_iconTop Results From Across the Web

difference between soft parse and hard parse - Ask TOM
We want to be SOFT parsing at the system level (that means our applications as a whole make use of shared sql). That...
Read more >
Keyword Extraction: A Guide to Finding Keywords in Text
Keyword extraction is used to automatically pull out single keywords, or groups of two or more words to detect key phrases in unstructured...
Read more >
Oracle Performance tuning | Oracle hard parse vs soft parse
Oracle Performance tuning | Oracle hard parse vs soft ... tuning | Oracle hard parse vs soft parse https://sivakacademy.blogspot.com/p/o.
Read more >
How to parse contextual keywords in a programming language
How to parse contextual keywords in a programming language · Parsing partial directly in the grammar · Having special syntax for a verbatim ......
Read more >
A Guide To Parsing: Algorithms And Terminology
So they use them to try to parse everything, even the things they should not. ... example of context-sensitive elements are the so-called...
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