关于词法分析器的小程序

王朝java/jsp·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

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();

}

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航