我正在测试编译器的词法分析。
我在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并键入浏览,找到此文件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程序运行方式的基础知识。主要方法是什么,它的意义是什么。

或者,请阅读以下问答:

  • What does "Could not find or load main class" mean?
  • 09-30 00:15