鉴于语法
grammar T;
options
{
k=4;
language=CSharp3;
TokenLabelType=CommonToken;
output=AST;
ASTLabelType=CommonTree;
}
tokens
{
LPAREN = '(';
RPAREN = ')';
LBRACK = '{';
RBRACK = '}';
}
fragment
ID : ('a'..'z'|'A'..'Z'|'_')('a'..'z'|'A'..'Z'|'0'..'9'|'_')*;
WS : (' ' | '\t' | '\n' |'\r' )+ { $channel = Hidden; } ;
public program: CLASSDEF+ EOF! ;
CLASSDEF: 'class' ID LBRACK
RBRACK ;
这会产生一个词法分析器和一个解析器,我使用如下
using System;
using Antlr.Runtime;
using Antlr.Runtime.Tree;
namespace compiler
{
internal class Program2
{
public static void Main(string[]arg)
{
ANTLRStringStream Input = new ANTLRStringStream(@"class foo
{
}");
TLexer lex = new TLexer(Input);
Console.WriteLine("errors:" + lex.NumberOfSyntaxErrors);
CommonTokenStream tokens = new CommonTokenStream(lex);
TParser parser = new TParser(tokens);
var parsed = parser.program();
Console.WriteLine("errors: " + parser.NumberOfSyntaxErrors);
CommonTree tree = parsed.Tree;
Console.WriteLine("type:" + tree.Type);
Console.WriteLine("text:" + tree.Text);
Console.WriteLine("children:" +tree.ChildCount);
Console.WriteLine(tree.ToString());
Console.WriteLine(tree.ToStringTree());
Console.ReadKey();
}
}
}
运行此代码时,我得到 0 个 lex 错误和 1 个解析错误
结果
errors:0
errors: 1
type:0
text:{
}
children:0
<error: {
}>
<error: {
}>
问题!
我认为 ANTLR 应该提供智能错误消息,但我没有找出问题所在
我是否缺少改进错误消息的代码?