0

g4文法を使用してLexer + Parser(結合)を実行すると、次のようになります

No. of Tokens=9 行 1:1 不一致の入力 'module' を期待 '[' Verilog2001Visitor VisitR Visit Symbol=module Rule=74 range Line=[2] module kuku(a,b);

Buid はクリーンで、警告やエラーはありません。ベストプラクティスは何ですか

  1. ミスマッチ入力エラーの根本原因を見つける
  2. 誤ったルール範囲が使用されている理由を調べる

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);
        }
      }
     }
4

0 に答える 0