⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 scan.java

📁 pl0编译器的java版本
💻 JAVA
字号:
// Things to think about adding:// 1. A new exception to return at end of file.// 2. A call back when a new line is read so //    a listing can be producedimport java.io.*;import java.util.*;public class Scan {    BufferedReader in;    String currLine;    HashMap keywords;    HashMap syms;    int posnInLine;     // position in the input line of the most recently                        // read character (the first is char 0).    LineRead callback;    Scan(BufferedReader br, LineRead lr) {    	in = br;    	callback = lr;    	currLine = "";        	keywords = new HashMap();    	keywords.put("begin", new Integer(Symbol.beginsym));    	keywords.put("call", new Integer(Symbol.callsym));    	keywords.put("const", new Integer(Symbol.constsym));    	keywords.put("do", new Integer(Symbol.dosym));    	keywords.put("end", new Integer(Symbol.endsym));    	keywords.put("if", new Integer(Symbol.ifsym));    	keywords.put("odd", new Integer(Symbol.oddsym));    	keywords.put("procedure", new Integer(Symbol.procsym));        keywords.put("read", new Integer(Symbol.readsym));    	keywords.put("then", new Integer(Symbol.thensym));    	keywords.put("var", new Integer(Symbol.varsym));    	keywords.put("while", new Integer(Symbol.whilesym));        keywords.put("write", new Integer(Symbol.writesym));        	syms = new HashMap();    	syms.put("+", new Integer(Symbol.plus));    	syms.put("-", new Integer(Symbol.minus));    	syms.put("*", new Integer(Symbol.times));    	syms.put("/", new Integer(Symbol.slash));    	syms.put("(", new Integer(Symbol.lparen));    	syms.put(")", new Integer(Symbol.rparen));    	syms.put("=", new Integer(Symbol.eql));    	syms.put(",", new Integer(Symbol.comma));    	syms.put(".", new Integer(Symbol.period));    	syms.put("#", new Integer(Symbol.neq));    	syms.put(";", new Integer(Symbol.semicolon));    }    String ch2str(char ch) {    	return new Character(ch).toString();    }    char getch() throws IOException {	    if (currLine.length() == 0) {	        currLine = in.readLine();	        if (currLine == null) {		        throw new IOException("Unexpected end of file\n");	        }	        if (callback != null) {		        callback.lineRead(currLine);	        }	        currLine = currLine + " ";	        posnInLine = -1; 	    }		    char ret = currLine.charAt(0);	    currLine = currLine.substring(1);	    posnInLine++;	    return ret;    }    void ungetch(char ch) {	    currLine = ch2str(ch) + currLine;	    posnInLine--;    }    Symbol getSym() throws IOException {	    char ch;    	do {	        ch = getch();	    } while (ch==' ' || ch==10 || ch==9);	    if (Character.isLetter(ch)) {  // Identifier or reserved word	        String id = ch2str(ch);	        for (;;) {		        ch = getch();		        if (!Character.isLetter(ch) && !Character.isDigit(ch)) {		            ungetch(ch);		            break;		        }		        id = id + ch2str(ch);	        }	        Integer i = (Integer) keywords.get(id);	        if (i == null) {		        // Not a keyword		        return new Symbol(Symbol.ident, id);	        }	        // A keyword!	        return new Symbol(i.intValue());	    }	    if (Character.isDigit(ch)) {   // A number	        String num = ch2str(ch);	        for (;;) {		        ch = getch();		        if (!Character.isDigit(ch)) {		            ungetch(ch);		            break;		        }		        num = num + ch2str(ch);	        }	    	        int i;	        try {		        i = Integer.parseInt(num);	        } catch (NumberFormatException nfe) {		        return new Symbol(Symbol.number, true, 0);	        }	        return new Symbol(Symbol.number, false, i);	    }	    if (ch == ':') {	        ch = getch();	        if (ch != '=') {		        return new Symbol(Symbol.nul);	        }	        return new Symbol(Symbol.becomes);	    } else {            if (ch == '<') {                ch = getch();                if (ch == '=') {                    return new Symbol(Symbol.leq);                    } else {                    ungetch(ch);                    return new Symbol(Symbol.lss);                }            } else {                if (ch == '>') {                    ch = getch();                    if (ch == '=') {                        return new Symbol(Symbol.geq);                            } else {                        ungetch(ch);                         return new Symbol(Symbol.gtr);                     }                }            }	    }		    Integer i = (Integer) syms.get(ch2str(ch));	    if (i == null) {	        return new Symbol(Symbol.nul);	    }	    return new Symbol(i.intValue());    }        public int linePosn() {	    return posnInLine;    }/*    public static void main(String args[]) {	    Scan s = new Scan(new BufferedReader(new InputStreamReader(System.in)), null);		    try {	        while (true) {		        Symbol sym = s.getSym();		        System.out.print(sym.symtype);		        if (sym.symtype == Symbol.ident) {		            System.out.print(" " + sym.id);		        } else if (sym.symtype == Symbol.number) {		            if (sym.overflow) {			            System.out.print(" overflow");		            } else {			            System.out.print(" " + sym.num);		            }		        }		        System.out.println();	        }	    } catch (IOException ioe) {	    }    }*/}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -