CSV 词法分析器找不到词法分析器类



我正在使用Netbeans,在制作ANTLR Lexer时出现以下错误:

C:\Users\Atahualpa\Documents\NetBeansProjects\antlr4CSV\src\antlr4csv\Antlr4CSV.java:26:错误:找不到符号 CSVLexer lexer = new CSVLexer(new ANTLRStringStream(source)); 符号:类 CSVLexer 位置: 类 Antlr4CSV

CSVLexer.java存在。见下图。

http://gyazo.com/084b85175c0cf8b6029bbd255e39d379

选中"编译并保存"会给出另一个错误。

我正在遵循的教程:http://bkiers.blogspot.nl/2011/03/2-introduction-to-antlr.html

AntlrCSV.java:

package antlr4csv;
import org.antlr.runtime.*;
public class Antlr4CSV {
  public static void main(String[] args) throws Exception {
    // the input source
    String source = 
        "value1,value2,"value3.1,"",value3.2"" + "n" + 
        ""linenbreak",Bbb,end";
    // create an instance of the lexer
    CSVLexer lexer = new CSVLexer(new ANTLRStringStream(source));
    // wrap a token-stream around the lexer
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    // when using ANTLR v3.3 or v3.4, un-comment the next line:
    //tokens.fill();
    // traverse the tokens and print them to see if the correct tokens are created
    int n = 1;
    for(Object o : tokens.getTokens()) {
      CommonToken token = (CommonToken)o;
      System.out.println("token(" + n + ") = " + token.getText().replace("n", "\n"));
      n++;
    }

CSVLexer.java:

// Generated from CSVLexer.g4 by ANTLR 4.4
import org.antlr.v4.runtime.Lexer;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.TokenStream;
import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.atn.*;
import org.antlr.v4.runtime.dfa.DFA;
import org.antlr.v4.runtime.misc.*;
import antlr4csv.Antlr4CSV;
@SuppressWarnings({"all", "warnings", "unchecked", "unused", "cast"})
public class CSVLexer extends Lexer {
    static { RuntimeMetaData.checkVersion("4.4", RuntimeMetaData.VERSION); }
    protected static final DFA[] _decisionToDFA;
    protected static final PredictionContextCache _sharedContextCache =
        new PredictionContextCache();
    public static final int
        Comma=1, LineBreak=2, SimpleValue=3, QuotedValue=4;
    public static String[] modeNames = {
        "DEFAULT_MODE"
    };
    public static final String[] tokenNames = {
        "'\u0000'", "'\u0001'", "'\u0002'", "'\u0003'", "'\u0004'"
    };
    public static final String[] ruleNames = {
        "Comma", "LineBreak", "SimpleValue", "QuotedValue"
    };

    public CSVLexer(CharStream input) {
        super(input);
        _interp = new LexerATNSimulator(this,_ATN,_decisionToDFA,_sharedContextCache);
    }
    @Override
    public String getGrammarFileName() { return "CSVLexer.g4"; }
    @Override
    public String[] getTokenNames() { return tokenNames; }
    @Override
    public String[] getRuleNames() { return ruleNames; }
    @Override
    public String getSerializedATN() { return _serializedATN; }
    @Override
    public String[] getModeNames() { return modeNames; }
    @Override
    public ATN getATN() { return _ATN; }
    public static final String _serializedATN =
        "3u0430ud6d1u8206uad2du4417uaef1u8d80uaadd26"b142t24"+
        "3t344t445t53232335317n33333346424n4r"+
        "416425353535357534n5f51653713535352263"+
        "35475t632462ff1717$$..32$$%233222253222"+
        "2732222t3222313322251632227233222t273"+
        "22213f7.22f43222r177172216r32221617322"+
        "21720322220217f2221632222224n2222322322"+
        "224253222252332222526322226b322227357$22"+
        "30317$2231347$223234n322333032223332322234"+
        "373222353332223536322236 322237353222 !7"+
        "$22!n322272162533352";
    public static final ATN _ATN =
        new ATNDeserializer().deserialize(_serializedATN.toCharArray());
    static {
        _decisionToDFA = new DFA[_ATN.getNumberOfDecisions()];
        for (int i = 0; i < _ATN.getNumberOfDecisions(); i++) {
            _decisionToDFA[i] = new DFA(_ATN.getDecisionState(i), i);
        }
    }
}

该教程适用于 v3,而您使用的是 v4。 ANTLRStringStream 在 v4 中不再可用。

试试这个:

CSVLexer lexer = new CSVLexer(new ANTLRInputStream("a,b,"c""c""));
CommonTokenStream tokens = new CommonTokenStream(lexer);
tokens.fill();
for (Token token : tokens.getTokens()) {
    if (token.getType() == Token.EOF) {
        break;
    }
    System.out.printf("%-12s --> %sn", CSVLexer.ruleNames[token.getType() - 1], token.getText());
}

这将打印:

简单值 --> a逗号 --> ,简单值 --> b逗号 --> ,引用值 --> "c"c"

相关内容

  • 没有找到相关文章

最新更新