gpt4 book ai didi

java - 创建编译器时无法找到或加载主类 Scanner

转载 作者:太空宇宙 更新时间:2023-11-04 11:49:38 27 4
gpt4 key购买 nike

我正在测试编译器的词法分析。我在 JFLEX 中有文件 minijava.jflex 。我在 minijava.jflex 中有这段代码:

import java_cup.runtime.*;
%%
%public
%class Scanner
%implements sym
%unicode
%line
%column
%cup
%debug
%{
StringBuffer string = new StringBuffer();
private Symbol symbol(int type) {
return new Symbol(type, yyline, yycolumn);
}
private Symbol symbol(int type, Object value) {
return new Symbol(type, yyline, yycolumn, value);
}
%}
/* main character classes */
LineTerminator = \r|\n|\r\n
InputCharacter = [^\r\n]
WhiteSpace = {LineTerminator} | [ \t\f]
/* comments */
Comment = {TraditionalComment} | {EndOfLineComment} |
{DocumentationComment}
TraditionalComment = "/*" [^*] ~"*/" | "/*" "*"+ "/"
EndOfLineComment = "//" {InputCharacter}* {LineTerminator}?
DocumentationComment = "/*" "*"+ [^/*] ~"*/"
/* identifiers */
Identifier = [:jletter:][:jletterdigit:]*
/* integer literals */
DecIntegerLiteral = 0 | [1-9][0-9]*
/* floating point literals */
FloatLiteral = ({FLit1}|{FLit2}|{FLit3}) {Exponent}?
FLit1 = [0-9]+ \. [0-9]*
FLit2 = \. [0-9]+
FLit3 = [0-9]+
Exponent = [eE] [+-]? [0-9]+
/* string and character literals */
StringCharacter = [^\r\n\"\\]
SingleCharacter = [^\r\n\'\\]
%state STRING, CHARLITERAL
%%
<YYINITIAL> {
/* keywords */
"boolean" { return symbol(BOOLEAN); }
"char" { return symbol(CHAR); }
"class" { return symbol(CLASS); }
"else" { return symbol(ELSE); }
"extends" { return symbol(EXTENDS); }
"float" { return symbol(FLOAT); }
"int" { return symbol(INT); }
"new" { return symbol(NEW); }
"if" { return symbol(IF); }
"public" { return symbol(PUBLIC); }
"super" { return symbol(SUPER); }
"return" { return symbol(RETURN); }
"void" { return symbol(VOID); }
"while" { return symbol(WHILE); }
"this" { return symbol(THIS); }
/* boolean literals */
"true" { return symbol(BOOLEAN_LITERAL, new Boolean(true)); }
"false" { return symbol(BOOLEAN_LITERAL, new Boolean(false)); }
/* null literal */
"null" { return symbol(NULL_LITERAL); }
/* separators */
"(" { return symbol(LPAREN); }
")" { return symbol(RPAREN); }
"{" { return symbol(LBRACE); }
"}" { return symbol(RBRACE); }
"[" { return symbol(LBRACK); }
"]" { return symbol(RBRACK); }
";" { return symbol(SEMICOLON); }
"," { return symbol(COMMA); }
"." { return symbol(DOT); }
/* operators */
"=" { return symbol(EQ); }
">" { return symbol(GT); }
"<" { return symbol(LT); }
"!" { return symbol(NOT); }
"?" { return symbol(QUESTION); }
":" { return symbol(COLON); }
"==" { return symbol(EQEQ); }
"<=" { return symbol(LTEQ); }
">=" { return symbol(GTEQ); }
"!=" { return symbol(NOTEQ); }
"&&" { return symbol(ANDAND); }
"&" { return symbol(AT); }
"||" { return symbol(OROR); }
"+" { return symbol(PLUS); }
"-" { return symbol(MINUS); }
"*" { return symbol(MULT); }
"/" { return symbol(DIV); }
"%" { return symbol(MOD); }
/* string literal */
\" { yybegin(STRING); string.setLength(0); }
/* character literal */
\' { yybegin(CHARLITERAL); }
/* numeric literals */
{DecIntegerLiteral} { return symbol(INTEGER_LITERAL, new Integer(yytext())); }
{FloatLiteral} { return symbol(FLOATING_POINT_LITERAL, new Float(yytext().substring(0,yylength()))); }
/* comments */
{Comment} { /* ignore */ }
/* whitespace */
{WhiteSpace} { /* ignore */ }
/* identifiers */
{Identifier} { return symbol(IDENTIFIER, yytext()); }
}
<STRING> {
\" { yybegin(YYINITIAL); return symbol(STRING_LITERAL, string.toString()); }
{StringCharacter}+ { string.append( yytext() ); }
/* escape sequences */
"\\b" { string.append( '\b' ); }
"\\t" { string.append( '\t' ); }
"\\n" { string.append( '\n' ); }
"\\f" { string.append( '\f' ); }
"\\r" { string.append( '\r' ); }
"\\\"" { string.append( '\"' ); }
"\\'" { string.append( '\'' ); }
"\\\\" { string.append( '\\' ); }
/* error cases */
\\. { throw new RuntimeException("Illegal escape sequence \""+yytext()+"\""); }
{LineTerminator} { throw new RuntimeException("Unterminated string at end of line"); }
}
<CHARLITERAL> {
{SingleCharacter}\' { yybegin(YYINITIAL); return symbol(CHARACTER_LITERAL, new Character(yytext().charAt(0))); }
/* escape sequences */
"\\b"\' { yybegin(YYINITIAL); return symbol(CHARACTER_LITERAL, new Character('\b'));}
"\\t"\' { yybegin(YYINITIAL); return symbol(CHARACTER_LITERAL, new Character('\t'));}
"\\n"\' { yybegin(YYINITIAL); return symbol(CHARACTER_LITERAL, new Character('\n'));}
"\\f"\' { yybegin(YYINITIAL); return symbol(CHARACTER_LITERAL, new Character('\f'));}
"\\r"\' { yybegin(YYINITIAL); return symbol(CHARACTER_LITERAL, new Character('\r'));}
"\\\""\' { yybegin(YYINITIAL); return symbol(CHARACTER_LITERAL, new Character('\"'));}
"\\'"\' { yybegin(YYINITIAL); return symbol(CHARACTER_LITERAL, new Character('\''));}
"\\\\"\' { yybegin(YYINITIAL); return symbol(CHARACTER_LITERAL, new Character('\\')); }
/* error cases */
{LineTerminator} { throw new RuntimeException("Unterminated character literal at end of line"); }
}
/* error fallback */
.|\n { return symbol(ILLEGAL_CHARACTER, yytext());}
<<EOF>> { return symbol(EOF); }

然后我进入 jflex 并输入 browser 并找到这个文件 minijava.jflex。我从中生成代码,并在与 miniflex.java 相同的目录中创建文件 Scanner。我将此文件 Scanner.java 和 sym.java 放在 JFLEX 的 src 文件夹中。然后我编写这些命令:

javac Scanner.java
javac sym.java
java Scanner example.java

最后我发现这个错误:

Could not find or load main class Scanner

我在某处读到,我应该修改文件 sym.java 并为在 minijava.jflex 添加的每个终端插入一个常量,但我不知道如何应用该建议。

最佳答案

基本上,该文件将被转换为名为 Scanner 的类中的解析器代码。但生成的代码不会包含“main”方法。然而,您试图运行代码,就好像它在 Scanner 代码中具有 main 方法一样。

您需要编写一些将使用 Scanner 类的代码,并且该代码需要一个 main 方法以便您可以启动它。

返回讲义、Java 教科书、Java 教程等,并阅读有关 Java 程序如何运行的基础知识。主要方法是什么,意义是什么。

或者,阅读此问答:

关于java - 创建编译器时无法找到或加载主类 Scanner,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41976459/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com