Skip to content

Python parser generator for R grammar #2193

@TDiazT

Description

@TDiazT

I wanted to use the R grammar (https://github.com/antlr/grammars-v4/tree/master/r), so I changed the actions (in RFilter.g4) to target Python but the generated parser has some errors which I had to change manually. I'm not sure if maybe I set something wrong in the actions or if it's something with the generation.

My Rfilter.g4 version (I omitted some sections which remained the same) :

/*

parser grammar RFilter;

options { tokenVocab=R; }

@members {
    self.curlies = 0
}

// TODO: MAKE THIS GET ONE COMMAND ONLY
stream : (elem|NL|';')* EOF ;

eat :   (NL {$NL.channel = Token.HIDDEN_CHANNEL})+ ;

elem: 
    [...]
    |   '{' eat? {self.curlies += 1} (elem|NL|';')* {self.curlies -= 1 } '}'
    [...]
    |   'else'
        {
tok = self._input.LT(-2)
if self.curlies > 0 and tok.type == self.NL:
   tok.channel = Token.HIDDEN_CHANNEL
    }
    ;

[...]

In particular:

  1. eat : (NL {$NL.channel = Token.HIDDEN_CHANNEL})+ ; becomes (0 if (localctx._NL is None else localctx._NL.channel) = Token.HIDDEN_CHANNEL, which I manually changed to localctx._NL.channel = Token.HIDDEN_CHANNEL.
  2. The action in the else rule adds extra indentation. I mean, currently as shown above it works, but otherwise if adding a bit more indentation (as I did when changing from the Java version to this one), then it adds more indentation than needed. Not sure if this is expected or not...
  3. There is an error in the importing of a module (it still works but thought I'd mention it). from typing.io import TextIO can't find 'io' in 'typing.pyi'.

Target: Python 3
Antlr: 4.7.1

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