a1105853719 2015-10-24 16:10 采纳率: 33.3%
浏览 2160

编译原理scanner的java代码

package lexer;

public class Token {

public final int tag;

public Token(int t) {
    tag = t;
}

public String toString() {
    return "" + (char) tag;
}

}

package lexer;

public class Tag {
public final static int AND = 256, BASIC = 257, BREAK = 258, DO = 259,
ELSE = 260, EQ = 261, FALSE = 262, GE = 263, ID = 264, IF = 265,
INDEX = 266, LE = 267, MINUS = 268, NE = 269, NUM = 270, OR = 271,
REAL = 272, TEMP = 273, TRUE = 274, WHILE = 275;
}

package lexer;

public class Real extends Token {
public final float value;

public Real(float v) {
    super(Tag.REAL);
    value = v;
}

public String toString() {
    return "" + value;
}

}

package lexer;

public class Num extends Token {

public final int value;

public Num(int v) {
    super(Tag.NUM);
    value = v;
}

public String toString() {
    return "" + value;
}

}

package lexer;

public class Word extends Token {
public String lexeme = "";

public Word(String s, int tag) {
    super(tag);
    lexeme = s;
}

public String toString() {
    return lexeme;
}

public static final Word and = new Word("&&", Tag.AND), or = new Word("||",
        Tag.OR), eq = new Word("==", Tag.EQ), ne = new Word("!=", Tag.NE),
        le = new Word("<=", Tag.LE), ge = new Word(">=", Tag.GE),
        minus = new Word("minus", Tag.MINUS), True = new Word("true",
                Tag.TRUE), False = new Word("false", Tag.FALSE),
        temp = new Word("t", Tag.TEMP);

}

package lexer;

import java.io.IOException;
import java.util.Hashtable;

public class Lexer {

public static int line = 1;
char peek = ' ';
Hashtable words = new Hashtable();

void reserve(Word w) {
    words.put(w.lexeme, w);
}

public Lexer() {
    reserve(new Word("if", Tag.IF));
    reserve(new Word("else", Tag.ELSE));
    reserve(new Word("while", Tag.WHILE));
    reserve(new Word("do", Tag.DO));
    reserve(new Word("break", Tag.BREAK));
    reserve(Word.True);
    reserve(Word.False);
}

public void readch() throws IOException {
    peek = (char) System.in.read();

}

boolean readch(char c) throws IOException {
    readch();
    if (peek != c) {
        return false;
    }
    peek = ' ';
    return true;
}

public Token scan() throws IOException {
    for (;; readch()) {
        if (peek == ' ' || peek == '\t')
            continue;
        else if (peek == '\n') {
            line += 1;
        } else {
            break;
        }
    }
    switch (peek) {
    case '&':
        if (readch('&'))
            return Word.and;
        else
            return new Token('&');
    case '|':
        if (readch('|'))
            return Word.or;
        else
            return new Token('|');
    case '=':
        if (readch('='))
            return Word.eq;
        else
            return new Token('=');
    case '!':
        if (readch('='))
            return Word.ne;
        else
            return new Token('!');
    case '<':
        if (readch('='))
            return Word.le;
        else
            return new Token('<');
    case '>':
        if (readch('='))
            return Word.ge;
        else
            return new Token('>');
    }
    if (Character.isDigit(peek)) {
        int v = 0;
        do {
            v = 10 * v + Character.digit(peek, 10);
            readch();
        } while (Character.isDigit(peek));
        if (peek != '.')
            return new Num(v);
        float x = v;
        float d = 10;
        for (;;) {
            readch();
            if (!Character.isDigit(peek))
                break;
            x = x + Character.digit(peek, 10) / d;
            d = d * 10;
        }
        return new Real(x);
    }
    if (Character.isLetter(peek)) {
        StringBuffer b = new StringBuffer();
        do {
            b.append(peek);
            readch();
        } while (Character.isLetterOrDigit(peek));
        String s = b.toString();
        Word w = (Word) words.get(s);
        if (w != null)
            return w;
        w = new Word(s, Tag.ID);
        words.put(s, w);
        return w;
    }
    Token tok = new Token(peek);
    peek = ' ';
    return tok;
}

public void out() {
    System.out.println(words.size());

}

public char getPeek() {
    return peek;
}

public void setPeek(char peek) {
    this.peek = peek;
}

}

package main;

import java.io.IOException;
import java.util.Hashtable;

import lexer.Lexer;
import lexer.Token;

public class Main {
public static void main(String[] args) throws IOException {
Lexer lexer = new Lexer();
char c;
do {
Token token=lexer.scan();
switch (token.tag) {
case 270:
case 272:
System.out.println("(NUM , "+token.toString()+")");
break;
case 264:
System.out.println("(ID , "+token.toString()+")");
break;
case 256:
case 257:
case 258:
case 259:
case 260:
case 265:
case 274:
case 275:
System.out.println("(KEY , "+token.toString()+")");
break;
case 13:
break;
default:
System.out.println("(SYM , "+token.toString()+")");
break;
}

    } while (lexer.getPeek()!='\n');
}

}

怎么把上面从控制台输入的代码改成读取txt文件输入

  • 写回答

1条回答 默认 最新

  • threenewbee 2015-10-24 22:10
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的