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.

Extending Grammar Without Changing Grammar File

See original GitHub issue

Hi again,

You may recall that I’m using your Python 3 grammar (#57). I reached the point where I can transpile simple Python code into Haxe code (yay!)

The next problem I’m faced with is that Haxe contains certain constructs that don’t exist in Python (or in the grammar); for example, you need to specify the override keyword to override a method; and you can use the @ notation for attributes (Python does have something similar, but less expressive).

I’m still hoping you’ll decide to officially support and maintain the Python 3 grammar! In the meanwhile, what’s the best way of adding these extensions to the parser, without changing the built-in grammar? Right now, I’m abusing long strings as comments to store my own syntax, but this seems wrong. (I tried doing the same thing with regular comments, but couldn’t figure out how to modify the grammar to stop ignoring them and then get a generator to visit them.)

Is the right approach a two-step process (run it through two grammars, first mine then the Python one)? But if I do that, the Python stage will break because those tokens aren’t valid Python code (eg. override def function_name ...

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
erezshcommented, Apr 21, 2022

I think the easiest solution would be to have two Lark instances, once for each grammar. However, you can also use one instance, and choose between them by giving start to Lark.parse()

0reactions
chamini2commented, Apr 20, 2022

Hello!

I have a situation where we have two grammars, a normal one, and an extended version.

How could we achieve something like this for the extended:

%import base

%extend rule: /some_extension/

And the program will choose to use this grammar or the base depending on the situation?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Extend Grammars - Micro Focus
Although most of the time you would add new entries when you extend a grammar, you can sometimes choose to replace it entirely....
Read more >
extensible grammars - Camlp5
The extensible grammars are a system to build grammar entries which can be extended dynamically. A grammar entry is an abstract value internally...
Read more >
Developing Batfish - Extending a Grammar (Part 2) - NTC Blog
In this post I will be covering how to extend a grammar definition in detail, which includes updating the ANTLR files and adding...
Read more >
ANTLR Specification: Grammar Inheritance
Grammar inheritance is also an interesting way to change the behavior of an ... The subgrammar does not inherit actions outside of classes...
Read more >
Grammar: The language of languages (BNF, EBNF, ABNF and ...
Extended Backus-Naur form (EBNF) is a collection of extensions to Backus-Naur form. Not all of these are strictly a superset, as some change...
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