gpt4 book ai didi

java - 需要扫描仪类的帮助来创建 token

转载 作者:行者123 更新时间:2023-12-02 08:36:10 25 4
gpt4 key购买 nike

Errors im getting: cannot find symbol constructor method Token. but i do have a constructor in Token class

cannot find symbol variable tokenCode. i clearly use it alll over and i think i initialized it properly so whats wrong?

cannot find symbol variable scantest. i have that in same folder where all classes are in why wont it read it?

导入java.io.BufferedReader; 导入 java.io.FileReader; 导入java.io.*;

public class scanner implements CompilerConstants {
private char c;
private BufferedReader source;
public int token;

private String attr = "";
//private int val = '0';
private

public scanner(BufferedReader buffer) {
source = buffer;

getChar();
} //constructor of scanner


public void getChar()
{
c = (char)(source.read());
//do a read in
}


//lookup for finding identifiers
public boolean lookup(String word)
{
boolean check = false;
for(int i=0; i < RESERVEDWORD.length;i++)
if(word==(RESERVEDWORD[i]))
{
check = true;
}
return check;
}


//public boolean T(int tcc, String attt) //to return token
//{
// tokenCode = tcc;
// attribute = attt;
// return T;
// }


public Token nextToken() throws IOException
{

attr = "";

//need to save to do lookup see if its identifier or not
while(c!=EOFCHAR); //if not end of file then do
{
while (Character.isWhitespace(c))//remove white space, check whether is letter or digit
{
getChar();
}
if (Character.isLetter(c))
{
while(Character.isLetterOrDigit(c))
{
attr = attr + c;
getChar();
}

return new Token(lookup(attr), attr); //
}
else if (Character.isDigit(c)) {
while(Character.isDigit(c))
{
attr = attr + c;
getChar();
}
return new Token(NUMBER, attr);
}
else {
switch (c) {

case '<' : getChar();
if(c=='>')
{
getChar();
return new Token(NE, null);
}
else if (c=='=')
{
getChar();
return new Token(LE, null);
}
return new Token(LT, null);


case '>' : getChar();
if(c=='<')
{
getChar();
return new Token(NE, null);
}
else if (c=='=')
{
getChar();
return new Token(GE, null);
}
return new Token(GT, null);


case '=' : getChar();
return new Token(EQ, null);

case '|' : getChar();
return new Token(OR, null);

case '+' : getChar();
return new Token(PLUS, null);

case '-' : getChar();
return new Token(MINUS, null);

case '*' : getChar();
return new Token(TIMES, null);

case '/' : getChar();
return new Token(DIVIDE, null);

case '[' : getChar();
return new Token(LEFTSQ, null);

case ']' : getChar();
return new Token(RIGHTSQ, null);

case '(' : getChar();
return new Token(LEFTPAREN, null);

case ')' : getChar();
return new Token(RIGHTPAREN, null);

case ',' : getChar();
return new Token(COMMA, null);

case EOFCHAR : getChar();
return new Token(EOF, null);

}
} // switch

//return EOF.Token;
return Token(tokenCode, attr); //tokenAttribute

} // if
// return Token;
} // getToken


public static void main(String[] args)
{
BufferedReader source = new BufferedReader(new FileReader(scantest.echo));
}
}
Token class
public class Token implements CompilerConstants {
private int tokenCode;
private String attribute;

public Token(int tc, String att) //constructor
{
tokenCode = tc;
attribute = att;

}

public int getToken()//return tokencode
{
return tokenCode;
}
//return token attribute

public String tokenAttribute()
{
return attribute;
}

public String toString(){

String tokenString = tokenCode + " ";

switch (tokenCode) { //// relational expressions for metasymbols
case AND: return (tokenString + "/n AND");
case IDENTIFIER: return (tokenString + "/n IDENTIFIER" + attribute);
case OR: return (tokenString + "/n OR");
case NOT: return (tokenString + "/n NOT");
case ARRAY: return (tokenString + "/n ARRAY");
case BEGIN: return (tokenString + "/n BEGIN ");
case BOOLEAN: return (tokenString + "/n BOOLEAN ");
case DO: return (tokenString + "/n DO ");
case ELSE: return (tokenString + "/n ELSE");
case END: return (tokenString + "/n END");
case FOR: return (tokenString + "/n FOR");
case FROM: return (tokenString + "/n FROM");
case IF: return (tokenString + "/n IF");
case INTEGER: return (tokenString + "/n INTEGER");
case PROCEDURE: return (tokenString + "/n PROCEDURE");
case PROGRAM: return (tokenString + "/n PROGAM");
case READ: return (tokenString + "/n READ");
case START: return (tokenString + "/n START");
case THEN: return (tokenString + "/n THEN");
case TO: return (tokenString + "/n TO");
case TRUE: return (tokenString + "/n TRUE");
case WHILE: return (tokenString + "/n WHILE");
case WRITE: return (tokenString + "/n WRITE");
case WRITELN: return (tokenString + "/n WRITELN");
case NUMBER: return (tokenString + "/n NUMBER" + attribute);
case STRING: return (tokenString + "/n STRING" + attribute);
case LT: return (tokenString + "/n LT");
case LE: return (tokenString + "/n LE");
case GT: return (tokenString + "/n GT");
case GE: return (tokenString + "/n GE");
case EQ: return (tokenString + "/n EQ");
case NE: return (tokenString + "/n NE");
case PLUS: return (tokenString + "/n PLUS");
case MINUS: return (tokenString + "/n MINUS");
case TIMES: return (tokenString + "/n TIMES");
case DIVIDE: return (tokenString + "/n DIVIDE");
case LEFTSQ: return (tokenString + "/n LEFTSQ");
case RIGHTSQ: return (tokenString + "/n RIGHTSQ");
case LEFTPAREN: return (tokenString + "/n LEFTPAREN");
case COLONEQUAL: return (tokenString + "/n COLONEQUAL");
case COMMA: return (tokenString + "/n COMMA");
case EOF: return (tokenString + "/n EOF");


}
return tokenString;
}
}
CompilerConstants
public interface CompilerConstants {
public static final int AND = 1;
public static final int ARRAY = 2;
public static final int BEGIN = 3;
public static final int BOOLEAN = 4;
public static final int DO = 5;
public static final int ELSE = 6;
public static final int END = 7;
public static final int FALSE = 8;
public static final int FOR = 9;
public static final int FROM = 10;
public static final int IF = 11;
public static final int INTEGER = 12;
public static final int NOT = 13;
public static final int OR = 14;
public static final int PROCEDURE = 15;
public static final int PROGRAM = 16;
public static final int READ = 17;
public static final int START = 18;
public static final int THEN = 19;
public static final int TO = 20;
public static final int TRUE = 21;
public static final int WHILE = 22;
public static final int WRITE = 23;
public static final int WRITELN = 24;
public static final int IDENTIFIER = 30;
public static final int NUMBER = 31;
public static final int STRING = 32;
public static final int LT = 33;
public static final int LE = 34;
public static final int GT = 35;
public static final int GE = 36;
public static final int EQ = 37;
public static final int NE = 38;
public static final int PLUS = 39;
public static final int MINUS = 40;
public static final int TIMES = 41;
public static final int DIVIDE = 42;
public static final int LEFTSQ = 43;
public static final int RIGHTSQ = 44;
public static final int LEFTPAREN = 45;
public static final int RIGHTPAREN = 46;
public static final int COLONEQUAL = 47;
public static final int COMMA = 48;
public static final int EOF = 99;

public static final char EOFCHAR = (char)(-1);

public static final String[] RESERVEDWORD = {"","and","array","begin","boolean",
"do","else","end","false","for","from","if","integer","not","or",
"procedure","program","read","start","then","to","true","while",
"write","writeln"};

public static final boolean DEBUG = true;

} // interface CompilerConstants

最佳答案

该行有问题:

return new Token(lookup(attr), attr);

方法 lookup(attr) 返回一个 boolean 值,但 Token 构造函数需要一个 int 作为第一个参数。我可以看到:

public Token(int, String)

但不是:

public Token(boolean, String)

也许您将错误的参数传递给现有的 Token 构造函数。

<小时/>

现在,关于这一行:

return Token(tokenCode, attr);  //tokenAttribute

tokenCode 确实未定义。你在哪里初始化的?你在哪里使用它?

<小时/>

最后,关于:

new FileReader(scantest.echo));

很难回答,因为您没有提供类(class)来源。回声到底是什么?静态属性?

PS:您的类应以大写字母开头和/或使用驼峰式大小写表示法,例如 ScannerScanTest 而不是 scanner扫描测试。这是推荐的约定。

关于java - 需要扫描仪类的帮助来创建 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1695665/

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