关于词法分析器的小程序

类别:Java 点击:0 评论:0 推荐:

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

本文地址:http://com.8s8s.com/it/it11119.htm