Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java CSV Lexer找不到Lexer类_Java_Csv_Netbeans_Antlr4_Lexer - Fatal编程技术网

Java CSV Lexer找不到Lexer类

Java CSV Lexer找不到Lexer类,java,csv,netbeans,antlr4,lexer,Java,Csv,Netbeans,Antlr4,Lexer,我正在使用Netbeans,在生成ANTLR Lexer时出现以下错误: C:\Users\Atahualpa\Documents\NetBeansProjects\antlr4CSV\src\antlr4CSV\antlr4CSV.java:26:错误:找不到符号 CSVLexer lexer=新的CSVLexer(新的AntlStringStream(源)); 符号:CSVLexer类 位置:Antlr4CSV级 存在CSVLexer.java。请参见下图 检查Compile和Save会出

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

C:\Users\Atahualpa\Documents\NetBeansProjects\antlr4CSV\src\antlr4CSV\antlr4CSV.java:26:错误:找不到符号 CSVLexer lexer=新的CSVLexer(新的AntlStringStream(源)); 符号:CSVLexer类 位置:Antlr4CSV级

存在CSVLexer.java。请参见下图

检查Compile和Save会出现另一个错误

下面是我的教程:

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" + 
        "\"line\nbreak\",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 =
        "\3\u0430\ud6d1\u8206\uad2d\u4417\uaef1\u8d80\uaadd\2\6\"\b\1\4\2\t\2\4"+
        "\3\t\3\4\4\t\4\4\5\t\5\3\2\3\2\3\3\5\3\17\n\3\3\3\3\3\3\4\6\4\24\n\4\r"+
        "\4\16\4\25\3\5\3\5\3\5\3\5\7\5\34\n\5\f\5\16\5\37\13\5\3\5\3\5\2\2\6\3"+
        "\3\5\4\7\5\t\6\3\2\4\6\2\f\f\17\17$$..\3\2$$%\2\3\3\2\2\2\2\5\3\2\2\2"+
        "\2\7\3\2\2\2\2\t\3\2\2\2\3\13\3\2\2\2\5\16\3\2\2\2\7\23\3\2\2\2\t\27\3"+
        "\2\2\2\13\f\7.\2\2\f\4\3\2\2\2\r\17\7\17\2\2\16\r\3\2\2\2\16\17\3\2\2"+
        "\2\17\20\3\2\2\2\20\21\7\f\2\2\21\6\3\2\2\2\22\24\n\2\2\2\23\22\3\2\2"+
        "\2\24\25\3\2\2\2\25\23\3\2\2\2\25\26\3\2\2\2\26\b\3\2\2\2\27\35\7$\2\2"+
        "\30\31\7$\2\2\31\34\7$\2\2\32\34\n\3\2\2\33\30\3\2\2\2\33\32\3\2\2\2\34"+
        "\37\3\2\2\2\35\33\3\2\2\2\35\36\3\2\2\2\36 \3\2\2\2\37\35\3\2\2\2 !\7"+
        "$\2\2!\n\3\2\2\2\7\2\16\25\33\35\2";
    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);
        }
    }
}
//由ANTLR 4.4从CSVLexer.g4生成
导入org.antlr.v4.runtime.Lexer;
导入org.antlr.v4.runtime.CharStream;
导入org.antlr.v4.runtime.Token;
导入org.antlr.v4.runtime.TokenStream;
导入org.antlr.v4.runtime.*;
导入org.antlr.v4.runtime.atn.*;
导入org.antlr.v4.runtime.dfa.dfa;
导入org.antlr.v4.runtime.misc.*;
输入antlr4csv.antlr4csv;
@SuppressWarnings({“全部”、“警告”、“未选中”、“未使用”、“强制转换”})
公共类CSVLexer扩展Lexer{
静态{RuntimeMetaData.checkVersion(“4.4”,RuntimeMetaData.VERSION);}
受保护静态最终DFA[]\u决策TODFA;
受保护的静态最终预测ContextCache\u sharedContextCache=
新建PredictionContextCache();
公共静态最终整数
逗号=1,换行符=2,SimpleValue=3,QuotedValue=4;
公共静态字符串[]modeNames={
“默认_模式”
};
公共静态最终字符串[]标记名={
“'\\u0000'”、“\\u0001'”、“\\u0002'”、“\\u0003'”、“\\u0004'”
};
公共静态最终字符串[]规则名称={
“逗号”、“换行符”、“SimpleValue”、“QuotedValue”
};
公共CSVLexer(字符流输入){
超级(输入);
_interp=新的LexeranSimulator(此、\u ATN、\u decisionToDFA、\u sharedContextCache);
}
@凌驾
公共字符串getGrammarFileName(){返回“CSVLexer.g4”;}
@凌驾
公共字符串[]getTokenNames(){return tokenNames;}
@凌驾
公共字符串[]getRuleNames(){return ruleNames;}
@凌驾
公共字符串getSerializedATN(){return\u serializedATN;}
@凌驾
公共字符串[]getModeNames(){返回modeNames;}
@凌驾
公共ATN getATN(){return\u ATN;}
公共静态最终字符串\u序列化数据=
“\3\u0430\ud6d1\u8206\uad2d\u4417\uaef1\u8d80\uaadd\2\6\”\b\1\4\2\t\2\4”+
“\3\t\3\4\4\t\4\4\5\t\5\3\2\3\3\5\3\17\n\3\3\3\3\3\4\6\4\24\n\4\r”+
“\4\16\4\25\3\5\3\5\3\5\5\7\5\34\n\5\f\5\16\5\37\13\5\3\5\5\3\5\5\2\6\3”+
“\3\5\4\7\5\t\6\3\2\4\6\2\f\17\17$$..\3\2$$%\2\3\3\2\2\2\2\5\3\2\2\2”+
“\2\7\3\2\2\2\t\3\2\2\2\3\13\3\2\2\2\5\16\3\2\2\2\7\23\3\2\2\2\2\t\27\3”+
“\2\2\2\13\f\7。\2\2\f\4\3\2\2\2\r\17\7\17\2\2\16\r\3\2\2\2\16\17\3\2\2\2”+
“\2\17\20\3\2\2\2\20\21\7\f\2\2\21\6\3\2\2\2\22\24\n\2\2\2\2\23\22\3\2”+
“\2\24\25\3\2\2\25\23\3\2\2\2\25\26\3\2\2\2\26\b\3\2\2\2\2\27\35\7$\2\2”+
“\30\31\7$\2\2\31\34\7$\2\2\32\34\n\3\2\2\33\30\3\2\2\2\33\32\3\2\2\2\2\2\34”+
"\37\3\2\2\2\35\33\3\2\2\2\35\36\3\2\2\2\36 \3\2\2\2\37\35\3\2\2\2 !\7"+
“$\2\2!\n\3\2\2\2\7\2\16\25\33\35\2”;
公共静态最终ATN\u ATN=
新的ATNDeserializer().反序列化(_serializedATN.toCharArray());
静止的{
_decisionToDFA=新的DFA[_ATN.getNumberOfDecisions()];
对于(int i=0;i<_ATN.getNumberOfDecisions();i++){
_decisionToDFA[i]=新的DFA(_ATN.getDecisionState(i),i);
}
}
}

该教程适用于v3,您正在使用v4。
AntlStringStream
在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 --> %s\n", CSVLexer.ruleNames[token.getType() - 1], token.getText());
}
将打印:

SimpleValue --> a Comma --> , SimpleValue --> b Comma --> , QuotedValue --> "c""c" 简单值-->a 逗号-->, 简单值-->b 逗号-->,
QuotedValue-->“c”c“每次使用AntlInputStream时,我都会遇到相同的错误。@user3137368如果使用v4,您需要
AntlInputStream
,而不是
ANTLRStringStream
。但是我现在看到您的IDE找不到
CSVLexer
,它不是由您或您的IDE生成的,也不是添加到IDE的类路径中的。我发布的代码确实有效:我用v4.3测试了它。