Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python parser generator for R grammar #2193

Closed
TDiazT opened this issue Jan 2, 2018 · 7 comments · Fixed by #2978
Closed

Python parser generator for R grammar #2193

TDiazT opened this issue Jan 2, 2018 · 7 comments · Fixed by #2978

Comments

@TDiazT
Copy link

TDiazT commented Jan 2, 2018

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

@ericvergnaud
Copy link
Contributor

ericvergnaud commented Jan 2, 2018 via email

@TDiazT
Copy link
Author

TDiazT commented Jan 3, 2018

  1. Thanks!
  2. Ohh okay, sounds reasonable :p
  3. Interesting... I'll check on that then (I have Python 3.5.0)

@kenahoo
Copy link

kenahoo commented Mar 1, 2018

I have the same error about from typing.io import TextIO can't find 'io' in 'typing.pyi'. I have to go into the generated parser file and change it to from typing import TextIO, then everything else goes as expected. I'm running Python 3.6.2.

@ericvergnaud
Copy link
Contributor

ericvergnaud commented Mar 1, 2018 via email

@kenahoo
Copy link

kenahoo commented Mar 2, 2018

@ericvergnaud - that can't be the case, I'm not using the R grammar, I'm writing my own grammar and using the Python3 output.

@ericvergnaud
Copy link
Contributor

Can you explore https://docs.python.org/3/library/typing.html
?
Seems to state that typing.io/TextIO is part of the runtime

@brizjin
Copy link

brizjin commented Nov 11, 2018

I have the same error: typing.io import TextIO can't find 'io' in 'typing.pyi'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
5 participants