-
Couldn't load subscription status.
- Fork 3.4k
Description
This is pretty critical.
The instructions for "Target agnostic grammars" are not correct. Following the third option, after adding field "self" to a base class, and using $self in an action, the Antlr tool will output "unknown attribute reference self in $self". In fact, there is no "$self" attribute. If one changes this to "self" (without the dollar sign), then the instructions would "work". But, as fate sometimes works, I tried something else.
If one changes $self to $parser, skip adding a field "self" to the base class altogether, "target agnostic" for a parser grammar will work for all but two targets! Note, this is only good for parser grammars. I don't think there is an equivalent "$lexer" for lexer actions, which really would be needed for actions in the lexer grammar. Or, really introduce "$self" for either lexer or parser actions.
The documentation for attributes really needs to include a discussion of "$parser". I don't know why it's missing because this attribute works.
Note, it's currently impossible to get a grammar with actions with a common syntax to work across all targets--at least for 4.9.3--but it is close using "$parser"--at least for a parser grammar. I would actually call the technique "not-quite target-agnostic grammars" because the common syntactic approach does not work for Cpp and PHP where there are include file issues and the non-conversion of "." to "->" for a pointer reference (see attached zip file for a proof of the technique working for CSharp, Dart, Go, Java, JavaScript, Python3). I have not tested "target agnostic grammars" for the old Antlr4cs port, which people shouldn't even be using at this point.
target-agnostic2.zip