Are older Antlr capabilities implemented in Antlr4 #4354
-
Just ordered the 2nd edition of the definitive guide but just in case the book doesn't answer this question.
I'm assuming the grammar has a rule named include_filename as the example does, as well as a token includetree. This syntax to add a CommonTree (or supported types) to the current parse tree seems like a potentially quite powerful feature. Will be nice if Antrl4 has it. Can someone suggest other materials where the supported RHS of this construct (what's the -> even formally called?) is more thoroughly documented? MTIA. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 4 replies
-
Since I'm using the Python3 language target, I'll try to use the parser action to manipulate the RuleContext references (the parentCtx, the self._ctx.parentCtx.children[] & other fields) to insert the 'import ...;' parse tree. would be ideal if the CommonToken had the name field (& if FileStream ctor set it (since the InputStream has the unused name field... Maybe. |
Beta Was this translation helpful? Give feedback.
-
The |
Beta Was this translation helpful? Give feedback.
-
Turns out my use case has a slightly different wrinkle. SysML v2 imports don't actually map to a particular file(s) but as namespaces, they need to match 'known' namespaces - either seen inline (same notational blob(text stream)) or known to whatever is processing the SysML notational text (eg. the Eclipse modeling framework built with SysML v2 plugins, or the OMG's Jupyter Sysml kernel - these 2 load all recognized files so the content can be matched later). So the Python3 package will need to do the same, adding load, reload, etc. semantics to its API. But I might still use the parse tree constructs - fetch the 'right' files, parse them, extract out the namespace identifiers, keep the mapping of namespace to parse tree for each in a lookup table, retrieve the matching parse tree when the import is seen while applying any import semantics to the sub-tree clone being inserted. So the stuff I mentioned at 10am today mostly still applies. Just a matter of when & more manipulation. |
Beta Was this translation helpful? Give feedback.
The
-> ...
is called a tree rewrite. Antlr3 had tree rewriting, but Antlr4 does not, at least not in the Antlr4 grammar syntax. Yes, you could implement a tree rewrite with a parser listener. Or, you could use a tree listener with a token stream rewrite. Note, the Python3 target is very slow, so you may want to implement this with a compiled language target. For a command-line solution, use my Trash toolkit.