关于词法分析器的小程序

2008-02-23 10:08:28来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

class ParserException extends Exception
{
public ParserException(String message)
{
super(message);
}
}

class Token
{
public final static int INVALID = -1;
public final static int LEFTPARENTHESIS = 0;
public final static int RIGHTPARENTHESIS = 1;
public final static int ADD = 2;
public final static int SUB = 3;
public final static int MUL = 4;
public final static int DIV = 5;
public final static int NUM = 6;

private String content;
private int type;

public Token(String content, int type)
{
this.content = content;
this.type = type;
}

public String getContent()
{
return content;
}

public double getDoubleValue()
{
return Double.parseDouble(content);
}

public int getType()
{
return type;
}
}

public class Lex
{
private String buffer;
private int colNum = 0;
private char curChar;

public Lex(String input)
{
this.buffer = input;
curChar = getChar();
}

private char getChar()
{
char ch = '#';
while(buffer!=null && colNum<buffer.length())
{
ch = buffer.charAt(colNum);
colNum ;
break;
}

return ch;
}

private void skiPBlank()
{
while(curChar == ' ')
curChar = getChar();

}

public Token getToken() throws ParserException
{
Token tk = null;
if(curChar == ' ')
skipBlank();

switch(curChar)
{
case '(':
tk = new Token("(",Token.LEFTPARENTHESIS);
curChar = getChar();
break;
case ')':
tk = new Token(")",Token.RIGHTPARENTHESIS);
curChar = getChar();
break;
case ' ':
tk = new Token(" ",Token.ADD);
curChar = getChar();
break;
case '-':
tk = new Token("-",Token.SUB);
curChar = getChar();
break;
case '*':
tk = new Token("*",Token.MUL);
curChar = getChar();
break;
case '/':
tk = new Token("/",Token.DIV);
curChar = getChar();
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case '.':
tk = parseNumber();
break;
case '#':
case '=':
tk = null;
break;
default:
tk = new Token("Invalid character",Token.INVALID);
curChar = getChar();
break;
}

return tk;
}

private Token parseNumber() throws ParserException
{
int dotNum = 0;
boolean key = true;
StringBuffer buf = new StringBuffer();
buf.append(curChar);
if(curChar == '.') dotNum ;

while(key)
{
curChar = getChar();
switch(curChar)
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
buf.append(curChar);
continue;
case '.':
dotNum ;
if(dotNum > 1)
throw new ParserException("the string inputed error at column:" colNum);
buf.append('.');
continue;
default:
key = false;
continue;
}
}
return new Token(buf.toString(),Token.NUM);
}

public static void main(String args[]) {
try {
Lex lex = new Lex(args[0]);
while(true) {
Token tk = lex.getToken();
if(tk == null) {
break;
}
else
System.out.println(tk.getContent());
}
}
catch(Exception e) {
e.printStackTrace();
}
}
}

上一篇: 关于javabean的一些文档
下一篇: 编程范式[paradigm]

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:Java调用系统执行程序(OS Command)

下一篇:[ant]非常简单的ant文件