Skip to content

Commit

Permalink
fix: up
Browse files Browse the repository at this point in the history
  • Loading branch information
MarsonShine committed May 28, 2024
1 parent 686af62 commit 3e238a7
Show file tree
Hide file tree
Showing 7 changed files with 192 additions and 0 deletions.
41 changes: 41 additions & 0 deletions dragonbook/src/Parser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import java.io.IOException;

/**
* Parser
*/
public class Parser {
static int lookahead;

public Parser() throws IOException {
lookahead = System.in.read();
}

void expr() throws IOException {
term();
while (true) {
if (lookahead == '+') {
match('+');
term();
System.out.print('+');
} else if(lookahead == '-') {
match('-');
term();
System.out.print('-');
} else {
return;
}
}
}

void term() throws IOException {
if (Character.isDigit(lookahead)) {
System.out.print((char) lookahead);
match(lookahead);
} else throw new Error("syntax error");
}

void match(int t) throws IOException {
if (lookahead == t) lookahead = System.in.read();
else throw new Error("syntax error");
}
}
12 changes: 12 additions & 0 deletions dragonbook/src/Postfix.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import java.io.IOException;

/*
* 将中缀表达式翻译为后缀表达
*/
public class Postfix {
public static void main(String[] args) throws IOException {
Parser parser = new Parser();
parser.expr();
System.out.println('\n');
}
}
93 changes: 93 additions & 0 deletions dragonbook/src/lexer/Lexer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package lexer;

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

/**
* 词法分析器
*/
public class Lexer {
public int line = 1;
private char peek = ' ';
private Hashtable words = new Hashtable<>();
void reserve(Word t) {
words.put(t.lexeme, t);
}
public Lexer() {
reserve(new Word(Tag.TRUE, "true"));
reserve(new Word(Tag.FALSE, "false"));
}

public Token scan() throws IOException {
for(;;peek = (char)System.in.read()) {
if (peek == ' ' || peek == '\t') {
continue;
}
else if (peek == '\n') {
line++;
}
else {
break;
}
}
// 处理注释
if (peek == '/') {
peek = (char)System.in.read();
if (peek == '/') {
do {
peek = (char)System.in.read();
} while (peek != '\n');
}
else if (peek == '*') {
// 处理多行注释
char prevPeek = ' ';
do {
prevPeek = peek;
peek = (char)System.in.read();
} while (prevPeek != '*' || peek != '/');
}
else {
throw new IOException("illegal comment");
}
}
// 处理关系运算符
if("<>=!".indexOf(peek) > -1) {
StringBuffer b = new StringBuffer();
b.append(peek);
peek = (char)System.in.read();
if (peek == '=') {
b.append(peek);
}
return new Rel(b.toString());
}
// 读取数位序列
if (Character.isDigit(peek)) {
int v = 0;
do {
v = 10 * v + Character.digit(peek, 10);
peek = (char)System.in.read();
} while (Character.isDigit(peek));
return new Num(v);
}
// 分析保留字和标识符
if(Character.isLetter(peek)) {
StringBuffer b = new StringBuffer();
do {
b.append(peek);
peek = (char)System.in.read();
} while (Character.isLetterOrDigit(peek));
String s = b.toString();
Word w = (Word)words.get(s);
if (w != null) {
return w;
}
w = new Word(Tag.ID, s);
words.put(s,w);
return w;
}
// 将当前字符作为一个词法单元返回
Token t = new Token(peek);
peek = ' ';
return t;
}
}
12 changes: 12 additions & 0 deletions dragonbook/src/lexer/Num.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package lexer;
/**
* 数字,常量
*/
public class Num extends Token {
public final int value;
public Num(int v) {
super(Tag.NUM);
value = v;
}

}
13 changes: 13 additions & 0 deletions dragonbook/src/lexer/Rel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package lexer;

/**
* 关系运算符
*/
public class Rel extends Token {
public final String lexeme;
public Rel(String s) {
super(Tag.REL);
lexeme = s;
}

}
9 changes: 9 additions & 0 deletions dragonbook/src/lexer/Tag.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package lexer;

public class Tag {
public final static int NUM = 256;
public final static int ID = 257;
public final static int TRUE = 258;
public final static int FALSE = 259;
public final static int REL = 260;
}
12 changes: 12 additions & 0 deletions dragonbook/src/lexer/Word.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package lexer;
/**
* 保留字、标识符
* 比如用于保留字 true 的对象可以初始化: <code>new Word("true", Token.TRUE)</code>
*/
public class Word extends Token {
public final String lexeme;
public Word(int t,String s) {
super(t);
this.lexeme = s;
}
}

0 comments on commit 3e238a7

Please sign in to comment.