Skip to content

Python3 runtime raises AttributeError in Parser.py #3052

@l215884529

Description

@l215884529

I met an AttributeError when trying codes in the 'Actions and Attributes' section.

I used the following grammar which is translated into a Python3 version from the sample grammar shown in that section.

grammar DynScope;
 
prog: block ;
 
block
	/* List of symbols defined within this block */
	locals [
        symbols = list()
	]
	: '{' decl* stat+ '}'
	// print out all symbols found in block
	// $block::symbols evaluates to a List as defined in scope
	{print("symbols=%s" % ($symbols))}
	;
 
/** Match a declaration and add identifier name to list of symbols */
decl: 'int' ID {$block::symbols.append($ID.text);} ';' ;
 
/** Match an assignment then test list of symbols to verify
 * that it contains the variable on the left side of the assignment.
 * Method contains() is List.contains() because $block::symbols
 * is a List.
 */
stat: ID '=' INT ';'
	{
if not $ID.text in $block::symbols:
    print("Undefined variable: %s" % ($ID.text))
	}
	| block
	;
 
ID : [a-z]+ ;
INT : [0-9]+ ;
WS : [ \t\r\n]+ -> skip ;

Executing the Python3 target generated from the above grammar leads to an AttributeError, saying that the Context object has no ruleIndex attribute.

Below is the last part of the traceback.

...
  File "/usr/local/lib/python3.9/site-packages/antlr4/Parser.py", line 456, in getInvokingContext
    if ctx.ruleIndex == ruleIndex:
AttributeError: 'DeclContext' object has no attribute 'ruleIndex'

There was a previous issue #2745 talking about this, however, it seems that this problem is still remaining unsolved.

Manually changing the line into below solves this problem.

if ctx.getRuleIndex() == ruleIndex:

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