Skip to content

Change "$self" to "$parser" in "target agnostic grammars" instructionsΒ #3508

@kaby76

Description

@kaby76

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions