AttributeError: 'MuParser' 对象没有属性 'startRule' [英] AttributeError: 'MuParser' object has no attribute 'startRule'

查看:25
本文介绍了AttributeError: 'MuParser' 对象没有属性 'startRule'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我从 SO 答案复制了这段代码:

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
 : .
 ;

我将代码编译成python:antlr4 -Dlanguage=Python3 Mu.g4

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

然后我尝试使用生成的 python 类运行 python 脚本:

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)

我这样称呼那个脚本:python3 script.py test.txt

然而输出是:AttributeError: 'MuParser' object has no attribute 'startRule'

我不明白为什么会发生这种情况,我所有的代码都是从教程中复制的.

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

推荐答案

正如评论中已经提到的,在您的情况下,startRule 应该替换为 parse.该方法对应语法中的如下解析器规则:

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
 ;

语法中的每条规则都会转换为您可以调用的方法/函数.您通常希望使用在末尾具有 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))

这篇关于AttributeError: 'MuParser' 对象没有属性 'startRule'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆