📄 scanner.java
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package scanner;import symbols.*;import java.io.*;import java.util.*;import exceptions.*;/** * * @author Beeven */public class scanner { class Word extends Token{ Word(int t){ super(t);} String lexme=""; Word(String l, int t){super(t);lexme=l;} } char peek=' '; int lastTokenTag=0; Hashtable reservedWord = new Hashtable(); void reserve(Word w){reservedWord.put(w.lexme, w);} public scanner() { reserve(new Word("sin",Tag.SIN)); reserve(new Word("cos",Tag.COS)); reserve(new Word("max",Tag.MAX)); reserve(new Word("min",Tag.MIN)); reserve(new Word("true",Tag.TRUE)); reserve(new Word("false",Tag.FALSE)); } void readch() throws IOException { peek = (Character.toLowerCase((char)System.in.read())); } boolean readch(char c) throws IOException { readch(); if(peek != c) return false; peek = ' '; return true; } /** * Get token from the input stream. * @return The token get from the input stream * @throws java.io.IOException * @throws exceptions.LexicalException */ public Token scan() throws IOException,LexicalException { while(peek==' '){ readch(); } switch(peek) { case '+': lastTokenTag=Tag.ADD; readch(); return new Token(Tag.ADD); case '*': lastTokenTag=Tag.MULTIPLY; readch(); return new Token(Tag.MULTIPLY); case '/': lastTokenTag=Tag.DIVIDE; readch(); return new Token(Tag.DIVIDE); case '^': lastTokenTag=Tag.POWER; readch(); return new Token(Tag.POWER); case '(': lastTokenTag=Tag.LP; readch(); return new Token(Tag.LP); case ',': lastTokenTag=Tag.COMMA; readch(); return new Token(Tag.COMMA); case ')': lastTokenTag=Tag.RP; readch(); return new Token(Tag.RP); case '!': lastTokenTag=Tag.NOT; readch(); return new Token(Tag.NOT); case '&': lastTokenTag=Tag.AND; readch(); return new Token(Tag.AND); case '|': lastTokenTag=Tag.OR; readch(); return new Token(Tag.OR); case '?': lastTokenTag=Tag.QM; readch(); return new Token(Tag.QM); case ':': lastTokenTag=Tag.COLON; readch(); return new Token(Tag.COLON); case '=': lastTokenTag=Tag.EQ; readch(); return new Token(Tag.EQ); case '<': if(readch('=')) {lastTokenTag=Tag.LE;return new Token(Tag.LE); } else if(peek=='>'){lastTokenTag=Tag.NE; return new Token(Tag.NE);} else{lastTokenTag=Tag.LT; return new Token(Tag.LT);} case '>': if(readch('=')){lastTokenTag=Tag.GE; return new Token(Tag.GE);} else{lastTokenTag=Tag.GT; return new Token(Tag.GT);} case '-': readch(); if(lastTokenTag==Tag.NUM || lastTokenTag==Tag.RP) { lastTokenTag=Tag.MINUS;return new Token(Tag.MINUS); } else{lastTokenTag=Tag.NEG; return new Token(Tag.NEG);} case ((char)-1): lastTokenTag=Tag.DOLLAR; return new Token(Tag.DOLLAR); } if(Character.isDigit(peek)) { int value = 0; double dv=0, d=10; do{ value = 10*value+Character.digit(peek, 10); readch(); }while(Character.isDigit(peek)); dv=value; if(peek=='.') { while(true) { readch(); if(!Character.isDigit(peek)) break; dv=dv+Character.digit(peek, 10)/d; d*=10; } if(d==10) throw new IllegalDecimalException(); //'.'followed by 'e' } if(peek=='E' || peek=='e') { int power=0; boolean minus=false; readch(); if(peek=='-') {minus=true; readch();} else if(peek=='+'){readch();} if(!Character.isDigit(peek)) throw new IllegalDecimalException(); do{ power = 10*power+Character.digit(peek, 10); readch(); }while(Character.isDigit(peek)); if(minus) for(int i=0;i<power;i++) dv/=10; else for(int i=0;i<power;i++) dv*=10; } lastTokenTag=Tag.NUM; return new Num(dv); } if(Character.isLetter(peek)) { StringBuilder sb= new StringBuilder(); do{ sb.append(peek); readch(); }while(Character.isLetter(peek)); Word w=(Word)reservedWord.get(sb.toString()); if(w!=null){lastTokenTag=w.tag; return w;} else throw new IllegalIdentifierException(); } throw new IllegalSymbolException(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -