AttributeError: 'MuParser' object has no attribute 'startRule'

断了今生、忘了曾经 提交于 2021-02-11 06:44:08

问题


So I copied this code from an SO answer:

grammar Mu;

parse
 : block EOF
 ;

block
 : stat*
 ;

stat
 : assignment
 | if_stat
 | while_stat
 | log
 | OTHER {System.err.println("unknown char: " + $OTHER.text);}
 ;

assignment
 : ID ASSIGN expr SCOL
 ;

if_stat
 : IF condition_block (ELSE IF condition_block)* (ELSE stat_block)?
 ;

condition_block
 : expr stat_block
 ;

stat_block
 : OBRACE block CBRACE
 | stat
 ;

while_stat
 : WHILE expr stat_block
 ;

log
 : LOG expr SCOL
 ;

expr
 : expr POW<assoc=right> expr           #powExpr
 | MINUS expr                           #unaryMinusExpr
 | NOT expr                             #notExpr
 | expr op=(MULT | DIV | MOD) expr      #multiplicationExpr
 | expr op=(PLUS | MINUS) expr          #additiveExpr
 | expr op=(LTEQ | GTEQ | LT | GT) expr #relationalExpr
 | expr op=(EQ | NEQ) expr              #equalityExpr
 | expr AND expr                        #andExpr
 | expr OR expr                         #orExpr
 | atom                                 #atomExpr
 ;

atom
 : OPAR expr CPAR #parExpr
 | (INT | FLOAT)  #numberAtom
 | (TRUE | FALSE) #booleanAtom
 | ID             #idAtom
 | STRING         #stringAtom
 | NIL            #nilAtom
 ;

OR : '||';
AND : '&&';
EQ : '==';
NEQ : '!=';
GT : '>';
LT : '<';
GTEQ : '>=';
LTEQ : '<=';
PLUS : '+';
MINUS : '-';
MULT : '*';
DIV : '/';
MOD : '%';
POW : '^';
NOT : '!';

SCOL : ';';
ASSIGN : '=';
OPAR : '(';
CPAR : ')';
OBRACE : '{';
CBRACE : '}';

TRUE : 'true';
FALSE : 'false';
NIL : 'nil';
IF : 'if';
ELSE : 'else';
WHILE : 'while';
LOG : 'log';

ID
 : [a-zA-Z_] [a-zA-Z_0-9]*
 ;

INT
 : [0-9]+
 ;

FLOAT
 : [0-9]+ '.' [0-9]*
 | '.' [0-9]+
 ;

STRING
 : '"' (~["\r\n] | '""')* '"'
 ;

COMMENT
 : '#' ~[\r\n]* -> skip
 ;

SPACE
 : [ \t\r\n] -> skip
 ;

OTHER
 : .
 ;

I compiled the code into python by doing: antlr4 -Dlanguage=Python3 Mu.g4

And then I tried running a python script using the generated python classes:

import sys
from antlr4 import *
from MuLexer import MuLexer
from MuParser import MuParser

def main(argv):
    input_stream = FileStream(argv[1])
    lexer = MuLexer(input_stream)
    stream = CommonTokenStream(lexer)
    parser = MuParser(stream)
    tree = parser.startRule()

if __name__ == '__main__':
    main(sys.argv)

I called that script like this: python3 script.py test.txt

The output however is: AttributeError: 'MuParser' object has no attribute 'startRule'

I don;t understand why that occurs, all the code I have is copied from tutorials.


回答1:


As already mentioned in the comments, startRule should be replaced with parse in your case. This method corresponds to the following parser rule in the grammar:

parse
 : block EOF
 ;

Every rule in the grammar translates to a method/function you can call. You usually want to use the rule that has an EOF (end of file) token at the end: that way you force the parser to consume all tokens from the input stream.

And to print a string representation of your parse tree, add this:

print(tree.toStringTree(recog=parser))


来源:https://stackoverflow.com/questions/60161501/attributeerror-muparser-object-has-no-attribute-startrule

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!