-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
686af62
commit 3e238a7
Showing
7 changed files
with
192 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |