Antlr解析器不会生成所有预期的代码

时间:2012-12-04 15:42:38

标签: c# antlr antlr3

我在Tutorial for walking ANTLR ASTs in C#?中大致定义了一个语法:

grammar Test; 

options 
{
language = 'CSharp3'; 
output=AST; 
} 
public expr : mexpr (PLUS^ mexpr)* SEMI! 
; 
mexpr 
: atom (STAR^ atom)* 
; 
atom: INT 
; 
//class csharpTestLexer extends Lexer; 
WS : (' ' 
| '\t' 
| '\n' 
| '\r') 
{ $channel = Hidden; } 
; 
LPAREN: '(' 
; 
RPAREN: ')' 
; 
STAR: '*' 
; 
PLUS: '+' 
; 
SEMI: ';' 
; 
protected 
DIGIT 
: '0'..'9' 
; 
INT : (DIGIT)+ 
;

这是构建,但是我没有parser.expr_result类,我确实没有,parser.expr()返回AstParserRuleReturnScope我做错了什么?这是选择吗?工具命令行选项?其他什么?

1 个答案:

答案 0 :(得分:1)

ANTLR 3.3如此声明规则expr

    public TestParser.expr_return expr()

ANTLR 3.4声明如下:

    public AstParserRuleReturnScope<object, IToken> expr()

以下是TestParser.expr_return

的定义
    public class expr_return : ParserRuleReturnScope<IToken>, IAstRuleReturnScope<object>
    {
        private object _tree;
        public object Tree { get { return _tree; } set { _tree = value; } }
    }

AstParserRuleReturnScope类看起来等同于生成的expr_return类。