手机站
网通分站
电信主站
密 码:
用户名:
当前位置 : 主页>程序设计>Java技术>列表

关于词法分析器的小程序

来源:互联网 作者:west263.com 时间:2008-02-23
西部数码-全国虚拟主机10强!40余项虚拟主机管理功能,全国领先!双线多线虚拟主机南北访问畅通无阻!免费赠送企业邮局,.CN域名,自助建站480元起,免费试用7天,满意再付款! P4主机租用799元/月.月付免压金!

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]

文章整理:西部数码--专业提供域名注册虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!