调试语法的最佳实践(不匹配的输入)

时间:2013-09-24 10:40:41

标签: c# visual-studio-2010 windows-xp antlrworks2

当我使用g4语法运行Lexer + Parser(合并)时,我得到了

  

没有。令牌= 9行1:1不匹配输入'模块'期待'['   Verilog2001访问者访问次数访问符号=模块规则= 74范围行= [2]   模块kuku(a,b);

Buid很干净,没有警告/错误。

的最佳做法是什么?
  1. 找出输入错误不匹配的根本原因
  2. 找出使用错误规则范围的原因
  3. 我使用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);
            }
          }
         }
    

0 个答案:

没有答案