1
我必須爲C的一個子集做一個編譯器,我決定使用JFlex和Cup。但是,當我創建.jflex
文件時,它使用JFlex進行編譯,但輸出(.java文件)不會編譯。無法獲得JFlex輸出來編譯
目錄:
src:
Lexer.java
makefile
jflex.jar
cup.jar
tinyc.jflex
所以我執行的命令是在follwing:
jflex tinyc.jflex
javac -cp ".:cup.jar:jflex.jar" Lexer.java
我不斷收到以下錯誤:
Lexer.java:640: error: cannot find symbol
{ return new symbol(sym.NOT);
^
symbol: class symbol
location: class Lexer
Lexer.java:640: error: cannot find symbol
{ return new symbol(sym.NOT);
^
symbol: variable sym
location: class Lexer
Basicly它不知道的我想應該是上課。我試圖通過以下示例獲得基本版本,但我只是不管理。任何指針?
編輯:
我從JFlex的手動輸入的例子,並試圖編譯之一,但它給了我同樣的錯誤也是如此。所以我一定在做錯事。任何指針?
tinyc.jflex:
import java_cup.runtime.*;
import java.io.FileInputStream;
import java.io.InputStream;
%%
%class Lexer
%cup
%line
%column
%unicode
%{
StringBuffer string = new StringBuffer();
private Symbol symbol(int type) {
return new Symbol(type, yyline, yycolumn);
}
private Symbol symbol(int type, Object val) {
return new Symbol(type, yyline, yycolumn, val);
}
%}
digit = [0-9]
alpha = [a-zA-Z_]
alphanum = [A-Za-z0-9]
symbol = [_]
identifier = {alpha}+({alphanum}|{symbol})*
sl_comment = "//".*
ml_comment = "/*"((.*?)|[\n]*)*"*/"
commment = {sl_comment} | {ml_comment}
%%
"int" { return new symbol(sym.INTEGER);}
"char" { return new symbol(sym.CHAR);}
"return" { return new symbol(sym.RETURN);}
"if" { return new symbol(sym.IF); }
"else" { return new symbol(sym.ELSE); }
"while" { return new symbol(sym.WHILE); }
"do" { return new symbol(sym.DO); }
"length" { return new symbol(sym.LENGTH); }
"write" { return new symbol(sym.WRITE); }
"read" { return new symbol(sym.READ); }
"," { return new symbol(sym.COMMA); }
";" { return new symbol(sym.SEMICOLON); }
"+" { return new symbol(sym.ADD); }
"-" { return new symbol(sym.MIN); }
"*" { return new symbol(sym.MUL); }
"/" { return new symbol(sym.DIV); }
"(" { return new symbol(sym.LPAR); }
")" { return new symbol(sym.RPAR); }
"[" { return new symbol(sym.LRBACK); }
"]" { return new symbol(sym.RBACK); }
"{" { return new symbol(sym.LBRACE); }
"}" { return new symbol(sym.RBRACE); }
">" { return new symbol(sym.GREATER); }
"<" { return new symbol(sym.LESS); }
"!=" { return new symbol(sym.NEQ); }
"==" { return new symbol(sym.EQU); }
"!" { return new symbol(sym.NOT); }
"=" { return new symbol(sym.ASSIGN); }
{identifier} { return new symbol(sym.NAME, yytext()); }
{digit}+ { return new symbol(sym.NUMBER, new Integer(Integer.parseInt(yytext()))); }
{commment} { yyline += countLines(yytext()); }
[\n] { ++yyline; }
[\r\t\f\ ]+ { }
. { System.err.println("unexpected char " + yytext() + " !\n"); System.exit(0); }
哦,好的!感謝這:)這真的比Flex和Bison tbh更復雜。但我會管!非常感謝您的回覆! –