Skip to content

Python3 parser generation incorrect by ANTLR 4.7.1 and problem in Parser.py #2745

@gerrie-myburgh

Description

@gerrie-myburgh

ANTLR 4.7.1 is generating incorrect Python3 code for the grammer :

var_defs
locals [ varlist = [] ]
   : 'var' var_def ( ',' var_def )* ';' {print($varlist)}
   ;

var_def
   : v=VARIABLE ':' var_type {$var_defs::varlist.append($v.text)} 
   ;

The code generated for the parser is :

...
self.state = 45
self.var_type()
getInvokingContext(1).varlist.append((None if localctx.v is None else localctx.v.text))
...

instead of :

...
self.state = 45
self.var_type()
self.getInvokingContext(1).varlist.append((None if localctx.v is None else localctx.v.text))
...

When the getInvokingContext function is called then the following error pops up in Parser.py: ctx.ruleIndex is not defined.

    def getInvokingContext(self, ruleIndex:int):
        ctx = self._ctx
        while ctx is not None:
>>>>>> if ctx.ruleIndex == ruleIndex:
                return ctx
            ctx = ctx.parentCtx
        return None

I had to change it to:

    def getInvokingContext(self, ruleIndex:int):
        ctx = self._ctx
        while ctx is not None:
>>>>>> if ctx.getRuleIndex() == ruleIndex:
                return ctx
            ctx = ctx.parentCtx
        return None

I am using Python 3.8.1, vs code ANTLR4 grammer plugin. with ANTLR 4.7.1

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions