用Java编写一个简单的词法扫描程序,识别以下词汇:
+
,*
Lexemes被空格分隔(空格,制表符,换行符)。任何不匹配的输入都是错误。
运行程序的示例如下:
% echo " 1* (27+x) " | java LexerTest
NUMBER [1]
TIMES [*]
LPAREN [(]
NUMBER [27]
PLUS [+]
ID [x]
RPAREN [)]
% echo "((1: + 877x)+var1*x)" | java LexerTest
LPAREN [(]
LPAREN [(]
NUMBER [1]
ERROR [Unknown char :]
PLUS [+]
NUMBER [877]
ID [x]
RPAREN [)]
PLUS [+]
ID [var1]
TIMES [*]
ID [x]
RPAREN [)]
这是我的代码......有人帮助我!
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class Lexer {
private Token object;
private char[] a= new char[100];
private int i=0;
private int j=0;
/**
*
* @param is
* @throws IOException
*/
public Lexer(InputStream is) throws IOException
{
try (BufferedReader input = new BufferedReader(new InputStreamReader(System.in))) {
int x;
char[] b= new char[1000];
while((x = input.read()) != -1){
b[j]=(char)x;
j++;
}
}
//a = char[j];
}
private Lexer() {
throw new UnsupportedOperationException("Not supported yet.");
}
public Token getToken() {
if(j== 0)
{return null;}
else if(Character.isLetter(a[i]))
{ object.id = a[i]+object.id;
i++;
while(Character.isLetter(a[i])||Character.isDigit(a[i]))
{
object.id = a[i]+object.id;
i++;
}
object.format=2;
return object;
}
else if(Character.isDigit(a[i]))
{object.id = a[i]+object.id;
i++;
while(Character.isDigit(a[i]))
{
object.id = a[i]+object.id;
i++;
}
object.format=1;
return object;
}
else{
switch (a[i]) {
case '(':
object.id = a[i]+object.id;
i++;
object.format=4;
return object;
case ')':
object.id = a[i]+object.id;
i++;
object.format=5;
return object;
case '+':
object.id = a[i]+object.id;
i++;
object.format=6;
return object;
case '*':
object.id = a[i]+object.id;
i++;
object.format=3;
return object;
case '\n':
object.id = a[i]+object.id;
i++;
object.format=8;
return object;
default:
object.id = a[i]+object.id;
i++;
object.format=7;
return object; }
}
}
public class Token{
private Token(String id,int format)
{
this.id = id;
this.format = format;
}
public String id;
public int format;
public String toString(){
switch (object.format) {
case '1':
return("NUMBER [" + id + "]");
case '2':
return("ID [" + id + "]");
case '4':
return("LPAREN [" + id + "]");
case '5':
return("RPAREN [" + id + "]");
case '6':
return("PLUS [" + id + "]");
case '3':
return("TIMES [" + id + "]");
case '8':
return null;
case '7'://default:
return("ERROR [Unknown char" + id + "]"); }
return null;
}
}
public static void main(String[] args) {
// Get tokens and display each on a line
Lexer lexer = new Lexer();
Lexer.Token token;
while ((token = lexer.getToken()) != null)
System.out.println(token.toString());
}
}