当我使用 g4 语法运行 Lexer+Parser(组合)时,我得到
令牌数=9 行 1:1 不匹配的输入“模块”期望“[” Verilog2001Visitor VisitR 访问符号=模块规则=74 范围行=[2] 模块 kuku(a,b);
Buid 很干净,没有警告/错误。最佳做法是什么
- 找出不匹配输入错误的根本原因
- 找出为什么使用了不正确的规则范围
我使用 Antlrworks2、VS2010 Pro、Win XP SP3
这是我的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.Runtime.InteropServices;
using Antlr4.Runtime;
using Antlr4.Runtime.Tree;
using Antlr4.Runtime.Atn;
using Antlr4.Runtime.Dfa;
using Antlr4.Runtime.Misc;
namespace Antlr4
{
public class MyVisitor : Verilog2001BaseVisitor<object>
{
public static String[] ruleNames = Verilog2001Parser.ruleNames;
public void VisitR (Verilog2001Parser.RangeContext context, CommonTokenStream CTS)
{
Console.WriteLine("Verilog2001Visitor VisitR");
context
.children
.OfType<TerminalNodeImpl>()
.ToList()
.ForEach(child => Visit(child , context, CTS));
}
private void Visit(TerminalNodeImpl node , Verilog2001Parser.RangeContext rc,
CommonTokenStream cts)
{
try
{
int NumChildrens = rc.ChildCount;
int RuleIndex = rc.GetRuleIndex();
ICharStream Line = cts.TokenSource.InputStream;
int LineNum = cts.TokenSource.Line;
int TokPos = cts.TokenSource.Column;
String RuleName = ruleNames[RuleIndex];
Console.WriteLine(" Visit Symbol={0} Rule={1} {2} Line=[{3}] {4}
Pos={5}", node.Symbol.Text, RuleIndex, RuleName, LineNum, Line.ToString(),
TokPos);
}
catch (Exception ex)
{
Console.WriteLine("ERROR: " + ex);
}
}
}
class Program
{
private static void Main(string[] args)
{
(new Program()).Run();
}
public void Run()
{
try
{
Console.WriteLine("START");
RunParser();
Console.Write("DONE. Hit RETURN to exit: ");
}
catch (Exception ex)
{
Console.WriteLine("ERROR: " + ex);
Console.Write("Hit RETURN to exit: ");
}
Console.ReadLine();
}
private void RunParser()
{
AntlrInputStream inputStream = new AntlrInputStream(" module kuku(a,b);\n");
Verilog2001Lexer Verilog2001Lexer = new Verilog2001Lexer(inputStream);
//for (int i = 1; i < Verilog2001Lexer.tokenNames.Length; i++)
//{ Console.WriteLine(Verilog2001Lexer.tokenNames[i]); }
CommonTokenStream TokenStream = new CommonTokenStream(Verilog2001Lexer);
int nTokens=TokenStream.GetNumberOfOnChannelTokens();
Console.WriteLine("No. of Tokens=" + nTokens);
//for (int i = 1; i < nTokens; i++)
//{ Console.WriteLine("Token "+i+" = "+commonTokenStream.Lt(i)); }
Verilog2001Parser Verilog2001Parser = new Verilog2001Parser(TokenStream);
MyVisitor visitor = new MyVisitor();
Verilog2001Parser.RangeContext R;
R = Verilog2001Parser.range();
visitor.VisitR(R, TokenStream);
}
}
}