刚刚学习编译原理,这是第二章的代码的改写,源码
http://www.blogjava.net/Files/zhaochengming/compiler%20section%202%20parse.rar
1.KeyWord.java 关键字对象类
 package parse;
package parse;


 public class KeyWord
public class KeyWord  {
{
 private String lexptr;
    private String lexptr;
 private int token;
    private int token;

 public KeyWord(String lexptr, int token)
    public KeyWord(String lexptr, int token)  {
{
 this.lexptr = lexptr;
        this.lexptr = lexptr;
 this.token = token;
        this.token = token;
 }
    }

 public String getLexptr()
    public String getLexptr()  {
{
 return lexptr;
        return lexptr;
 }
    }

 public void setLexptr(String lexptr)
    public void setLexptr(String lexptr)  {
{
 this.lexptr = lexptr;
        this.lexptr = lexptr;
 }
    }

 public int getToken()
    public int getToken()  {
{
 return token;
        return token;
 }
    }

 public void setToken(int token)
    public void setToken(int token)  {
{
 this.token = token;
        this.token = token;
 }
    }
 }
}
2.SymTable.java 符号表对象类
 package parse;
package parse;

 import java.util.ArrayList;
import java.util.ArrayList;
 import java.util.List;
import java.util.List;


 /** *//**
/** *//**
 * 符号表
 * 符号表
 * @author Administrator
 * @author Administrator
 *
 *
 */
 */

 public class SymTable
public class SymTable  {
{
 private static List<KeyWord> SYMTABLE = new ArrayList<KeyWord>();
    private static List<KeyWord> SYMTABLE = new ArrayList<KeyWord>();
 // 符号表最大容量
    // 符号表最大容量
 private static final int SYMMAX = 999;
    private static final int SYMMAX = 999;
 // 字符串最大长度
    // 字符串最大长度
 public static final int STRMAX = 100;
    public static final int STRMAX = 100;

 private static final KeyWord KEYWORDS[] =
    private static final KeyWord KEYWORDS[] =  {
{
 new KeyWord(null,0),
        new KeyWord(null,0),
 new KeyWord("DIV", Token.DIV),
        new KeyWord("DIV", Token.DIV),
 new KeyWord("ADD", Token.ADD),
        new KeyWord("ADD", Token.ADD),
 new KeyWord("SUB", Token.SUB),
        new KeyWord("SUB", Token.SUB),
 new KeyWord("MOD", Token.MOD),
        new KeyWord("MOD", Token.MOD),
 new KeyWord("MUL", Token.MUL),
        new KeyWord("MUL", Token.MUL),
 };
    };
 
    
 // 初始化symtable
    // 初始化symtable

 static
    static  {
{

 for(KeyWord keyword : KEYWORDS)
        for(KeyWord keyword : KEYWORDS)  {
{
 insert(keyword.getLexptr(), keyword.getToken());
            insert(keyword.getLexptr(), keyword.getToken());
 }
        }
 }
    }
 
    

 /** *//**
    /** *//**
 * 在符号表中插入新的符号
     * 在符号表中插入新的符号
 * @param lexptr
     * @param lexptr
 * @param token
     * @param token
 * @return
     * @return
 */
     */

 public static int insert(String lexptr, int token)
    public static int insert(String lexptr, int token)  {
{
 int lexptrlen = 0;
        int lexptrlen = 0;
 if(lexptr != null) lexptrlen = lexptr.length();
        if(lexptr != null) lexptrlen = lexptr.length();

 if(SYMTABLE.size() >= SYMMAX)
        if(SYMTABLE.size() >= SYMMAX)  {
{
 throw new IllegalStateException("符号表满。");
            throw new IllegalStateException("符号表满。");
 }
        }

 if(lexptrlen >= STRMAX)
        if(lexptrlen >= STRMAX)  {
{
 throw new IllegalStateException("lexptr超过最大长度。");
            throw new IllegalStateException("lexptr超过最大长度。");
 }
        }
 SYMTABLE.add(new KeyWord(lexptr, token));
        SYMTABLE.add(new KeyWord(lexptr, token));
 return SYMTABLE.size() - 1;
        return SYMTABLE.size() - 1;
 }
    }
 
    

 /** *//**
    /** *//**
 * 返回lexptr在符号表中的下标
     * 返回lexptr在符号表中的下标
 * @param lexptr
     * @param lexptr
 * @return
     * @return
 */
     */

 public static int lookup(String lexptr)
    public static int lookup(String lexptr)  {
{

 for (int i = 1; i < SYMTABLE.size(); i++)
        for (int i = 1; i < SYMTABLE.size(); i++)  {
{

 if(SYMTABLE.get(i).getLexptr().equals(lexptr))
            if(SYMTABLE.get(i).getLexptr().equals(lexptr))  {
{
 return i;
                return i;
 }
            }
 }
        }
 return 0;
        return 0;
 }
    }
 
    

 public static KeyWord getKeyWord(int index)
    public static KeyWord getKeyWord(int index)  {
{
 return SYMTABLE.get(index);
        return SYMTABLE.get(index);
 }
    }
 
    
 }
}

3.Lex.java 词法分析对象类
 package parse;
package parse;



 public class Lex
public class Lex  {
{
 private int last = -1;
    private int last = -1;
 private int pos = 0;            // 当前位置
    private int pos = 0;            // 当前位置
 private ParseReader pr;
    private ParseReader pr;
 private String buf;
    private String buf;
 private int tokenVal;
    private int tokenVal;
 private Parser parser;
    private Parser parser;
 
    

 public Lex(ParseReader pr, Parser parse)
    public Lex(ParseReader pr, Parser parse)  {
{
 this.pr = pr;
        this.pr = pr;
 this.parser = parse;
        this.parser = parse;
 }
    }
 
    

 public int lexan()
    public int lexan()  {
{
 int t;
        int t;

 while (true)
        while (true)  {
{
 t = getChar();
            t = getChar();
 if(t == ' ' || t == '\r');
            if(t == ' ' || t == '\r');

 else if(isDigit(t))
            else if(isDigit(t))  {
{
 tokenVal = 0;
                tokenVal = 0;

 while(isDigit(t))
                while(isDigit(t))  {
{
 tokenVal = tokenVal* 10 + (t - '0');
                    tokenVal = tokenVal* 10 + (t - '0');
 t = getChar();
                    t = getChar();
 }
                }
 ungetc(t);
                ungetc(t);
 return Token.NUM;
                return Token.NUM;

 } else if (isAlpha(t))
            } else if (isAlpha(t))  {
{
 int p = -1, b = 0;
                int p = -1, b = 0;
 StringBuilder sb = new StringBuilder();
                StringBuilder sb = new StringBuilder();

 while (isDigit(t) || isAlpha(t))
                while (isDigit(t) || isAlpha(t))  {
{
 sb.append((char)t);
                    sb.append((char)t);
 t = getChar();
                    t = getChar();
 b++;
                    b++;
 }
                }

 if(b >= SymTable.STRMAX)
                if(b >= SymTable.STRMAX)  {
{
 parser.error("词法分析错误");
                    parser.error("词法分析错误");
 }
                }

 if(t != Token.EOF)
                if(t != Token.EOF)  {
{
 ungetc(t);
                    ungetc(t);
 }
                }
 p = SymTable.lookup(sb.toString());
                p = SymTable.lookup(sb.toString());

 if(p == 0)
                if(p == 0)  {
{
 p = SymTable.insert(sb.toString(), Token.ID);
                    p = SymTable.insert(sb.toString(), Token.ID);
 }
                }
 tokenVal = p;
                tokenVal = p;
 return SymTable.getKeyWord(tokenVal).getToken();
                return SymTable.getKeyWord(tokenVal).getToken();
 
                

 } else if (t == Token.EOF)
            } else if (t == Token.EOF)  {
{
 return Token.DONE;
                return Token.DONE;

 } else
            } else  {
{
 tokenVal = Token.NULL;
                tokenVal = Token.NULL;
 return t;
                return t;
 }
            }
 }
        }
 }
    }
 
    

 public int getTokenVal()
    public int getTokenVal()  {
{
 return tokenVal;
        return tokenVal;
 }
    }
 
    

 public int getChar()
    public int getChar()  {
{

 if(last < 0)
        if(last < 0)  {
{

 if(buf != null && pos < buf.length())
            if(buf != null && pos < buf.length())  {
{
 return buf.charAt(pos++);
                return buf.charAt(pos++);

 } else
            } else  {
{

 if(pr.isEof())
                if(pr.isEof())  {
{
 return Token.EOF;
                    return Token.EOF;

 } else
                } else  {
{
 buf = pr.readLine();
                    buf = pr.readLine();
 pos = 0;
                    pos = 0;
 return getChar();
                    return getChar();
 }
                }
 }
            }

 } else
        } else  {
{
 int c = last;
            int c = last;
 last = -1;
            last = -1;
 return c;
            return c;
 }
        }
 }
    }
 
    

 public void ungetc(int c)
    public void ungetc(int c)  {
{
 last = c;
        last = c;
 }
    }
 
    

 public boolean isDigit(int t)
    public boolean isDigit(int t)  {
{
 return t >= '0' && t <= '9';
        return t >= '0' && t <= '9';
 }
    }    

 public boolean isAlpha (int t)
    public boolean isAlpha (int t)  {
{
 return ((t >= 'A' && t <= 'Z') || (t >= 'a' && t <= 'z'));
        return ((t >= 'A' && t <= 'Z') || (t >= 'a' && t <= 'z'));
 }
    }    
 
    

 public int getLineNo()
    public int getLineNo()  {
{
 return pr.getLineNo();
        return pr.getLineNo();
 }
    }
 
    

 public int getPos()
    public int getPos()  {
{
 return pos;
        return pos;
 }
    }
 
    
 
    
 
    
 }
}

4.Parser.java 语法分析对象类
 package parse;
package parse;

 import java.io.FileNotFoundException;
import java.io.FileNotFoundException;


 public class Parser
public class Parser  {
{
 private StringBuilder temp = new StringBuilder();
    private StringBuilder temp = new StringBuilder();
 private Lex lex;
    private Lex lex;
 int lookahead;
    int lookahead;


 public Parser(ParseReader pr)
    public Parser(ParseReader pr)  {
{
 lex = new Lex(pr,this);
        lex = new Lex(pr,this);
 }
    }
 
    

 public void parse()
    public void parse()  {
{
 lookahead = lex.lexan();
        lookahead = lex.lexan();

 while (lookahead != Token.DONE)
        while (lookahead != Token.DONE)  {
{
 expr(); match(';');
            expr(); match(';');
 }
        }
 out("后缀表达式为: "+temp.toString());
        out("后缀表达式为: "+temp.toString());
 }
    }
 
    

 private void expr()
    private void expr()  {
{
 int t;
        int t;
 term();
        term();

 while(true)
        while(true)  {
{

 switch(lookahead)
            switch(lookahead)  {
{
 case '+':
            case '+':
 case '-':
            case '-':
 case Token.ADD:
            case Token.ADD:
 case Token.SUB:
            case Token.SUB:
 t = lookahead;
                t = lookahead;
 match(lookahead); term(); emit(t, Token.NULL);
                match(lookahead); term(); emit(t, Token.NULL);
 continue;
                continue;
 default:
            default:
 return;
                return;
 }
            }
 }
        }
 }
    }
 
    

 private void term()
    private void term()  {
{
 int t;
        int t;
 factor();
        factor();

 while(true)
        while(true) {
{

 switch(lookahead)
            switch(lookahead)  {
{
 case '*':
            case '*':
 case '/':
            case '/':
 case '%':
            case '%':
 case Token.MOD:
            case Token.MOD:
 case Token.DIV:
            case Token.DIV:
 case Token.MUL:
            case Token.MUL:
 t = lookahead;
                t = lookahead;
 match(lookahead); factor(); emit(t, Token.NULL);
                match(lookahead); factor(); emit(t, Token.NULL);
 continue;
                continue;
 default:
            default:
 return;
                return;
 }
            }
 }
        }
 }
    }
 
    

 private void factor()
    private void factor()  {
{

 switch(lookahead)
        switch(lookahead)  {
{
 case '(':
        case '(':
 match('('); expr(); match(')'); break;
            match('('); expr(); match(')'); break;
 case Token.NUM:
        case Token.NUM:
 emit(Token.NUM, lex.getTokenVal()); match(Token.NUM); break;
            emit(Token.NUM, lex.getTokenVal()); match(Token.NUM); break;
 case Token.ID:
        case Token.ID:
 emit(Token.ID, lex.getTokenVal()); match(Token.ID); break;
            emit(Token.ID, lex.getTokenVal()); match(Token.ID); break;
 default:
        default:
 error("语法分析错误");
            error("语法分析错误");
 }
        }
 }
    }
 
    

 private void match(int t)
    private void match(int t)  {
{

 if(lookahead == t)
        if(lookahead == t)  {
{
 lookahead = lex.lexan();
            lookahead = lex.lexan();
 } else error("语法错误");
        } else error("语法错误");
 }
    }

 
    

 public void emit(int t, int token)
    public void emit(int t, int token)  {
{

 switch(t)
        switch(t)  {
{
 case '+':
        case '+':
 case '-':
        case '-':
 case '*':
        case '*':
 case '%':
        case '%':
 case '/':
        case '/':
 temp.append(" ").append((char)t).append(" ");
            temp.append(" ").append((char)t).append(" ");
 out(String.valueOf((char)t));break;
            out(String.valueOf((char)t));break;
 case Token.DIV:
        case Token.DIV:
 temp.append(" DIV ");
            temp.append(" DIV ");
 out("DIV");break;
            out("DIV");break;
 case Token.MOD:
        case Token.MOD:
 temp.append(" MOD ");
            temp.append(" MOD ");
 out("MOD");break;
            out("MOD");break;
 case Token.MUL:
        case Token.MUL:
 temp.append(" MUL ");
            temp.append(" MUL ");
 out("MUL");break;
            out("MUL");break;
 case Token.ADD:
        case Token.ADD:
 temp.append(" ADD ");
            temp.append(" ADD ");
 out("ADD");break;
            out("ADD");break;
 case Token.SUB:
        case Token.SUB:
 temp.append(" SUB ");
            temp.append(" SUB ");
 out(" SUB ");break;
            out(" SUB ");break;
 case Token.ID:
        case Token.ID:
 temp.append(SymTable.getKeyWord(token).getLexptr());
            temp.append(SymTable.getKeyWord(token).getLexptr());
 out(SymTable.getKeyWord(token).getLexptr());
            out(SymTable.getKeyWord(token).getLexptr());
 break;
            break;
 default:
        default:
 temp.append(" ").append(token).append(" ");
            temp.append(" ").append(token).append(" ");
 out("token " + Token.name(t) + ", tokenval "+token);
            out("token " + Token.name(t) + ", tokenval "+token);
 }
        }
 }
    }


 public void out(String msg)
    public void out(String msg)  {
{
 System.out.println(msg);
        System.out.println(msg);
 }
    }
 
    

 public static void main(String args[])
    public static void main(String args[])  {
{
 Parser pr;
        Parser pr;

 try
        try  {
{
 pr = new Parser(new ParseReaderRAF("c:/a.txt"));
            pr = new Parser(new ParseReaderRAF("c:/a.txt"));
 pr.parse();
            pr.parse();
 
            

 } catch (FileNotFoundException e)
        } catch (FileNotFoundException e)  {
{
 e.printStackTrace();
            e.printStackTrace();
 }
        }
 }
    }
 
    

 public void error(String msg)
    public void error(String msg)  {
{
 System.err.println("ERROR:[" + msg + "] LineNumber : " + lex.getLineNo() +", position :" + lex.getPos());
        System.err.println("ERROR:[" + msg + "] LineNumber : " + lex.getLineNo() +", position :" + lex.getPos());
 System.exit(0);
        System.exit(0);
 }
    }
 }
}

5.ParseReader 词法分析读字符流接口
 package parse;
package parse;


 public interface ParseReader
public interface ParseReader  {
{
 public String readLine();
    public String readLine();
 public boolean isEof();
    public boolean isEof();
 public int getLineNo();
    public int getLineNo();
 }
}
6.ParseReaderRAF 词法分析读字符流RandomAccessFile对象
 package parse;
package parse;

 import java.io.File;
import java.io.File;
 import java.io.FileNotFoundException;
import java.io.FileNotFoundException;
 import java.io.IOException;
import java.io.IOException;
 import java.io.RandomAccessFile;
import java.io.RandomAccessFile;


 public class ParseReaderRAF implements ParseReader
public class ParseReaderRAF implements ParseReader {
{
 private RandomAccessFile raf;
    private RandomAccessFile raf;
 private int lineno = 0;
    private int lineno = 0;
 private String buf;
    private String buf;

 public ParseReaderRAF(String filepath) throws FileNotFoundException
    public ParseReaderRAF(String filepath) throws FileNotFoundException  {
{
 this.raf = new RandomAccessFile(new File(filepath), "r");
        this.raf = new RandomAccessFile(new File(filepath), "r");
 }
    }


 public int getLineNo()
    public int getLineNo()  {
{
 return lineno;
        return lineno;
 }
    }


 public boolean isEof()
    public boolean isEof()  {
{

 try
        try  {
{
 if(raf.getFilePointer() >= raf.length())
            if(raf.getFilePointer() >= raf.length())
 return true;
                return true;

 } catch (IOException e)
        } catch (IOException e)  {
{
 e.printStackTrace();
            e.printStackTrace();
 }
        }
 return false;
        return false;
 }
    }


 public String readLine()
    public String readLine()  {
{

 try
        try  {
{
 buf = raf.readLine();
            buf = raf.readLine();
 //System.out.println(buf);
            //System.out.println(buf);
 lineno++;
            lineno++;

 } catch (Exception e)
        } catch (Exception e)  {
{
 e.printStackTrace();
            e.printStackTrace();
 }
        }
 return buf;
        return buf;
 }
    }

 }
}

</script>