资源简介
北京工业大学 高老师的编译原理随堂实验代码
代码片段和文件信息
/* 编译原理上机实验 实验一 词法分析程序
* 姓名:_王华慈___
* 学号:_09070626_
* 班级:__090706__
* 2012年_5_月_31_日
*/
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PushbackInputStream;
public class gemmar {
/**
* @param args
*/
final static int IDN = 1;
final static int INT10 = 2;
final static int INT8 = 3;
final static int INT16 = 4;
final static int ADD = 5;
final static int SUB = 6;
final static int MUL = 7;
final static int DIV = 8;
final static int GT = 9;
final static int LT = 10;
final static int EQ = 11;
final static int LP = 12;
final static int RP = 13;
final static int SEMI = 14;
final static int DO = 100;
final static int ELSE = 101;
final static int IF = 102;
final static int THEN = 103;
final static int WHILE = 104;
// 常量声明
private static gemmar g = null;
private static PushbackInputStream f = null;
static int lookhead;
static String[] keyword = { “do“ “else“ “if“ “then“ “while“ };
int i = 0;
StringBuffer str = new StringBuffer();
StringBuffer stre = new StringBuffer();
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
String[] filea = args;
File file = new File(filea[0]);
InputStream in = new FileInputStream(file);
f = new PushbackInputStream(in);
g = new gemmar();
lookhead = g.scan(f);
System.out.println(“S“);
g.statement();
}
public void statement() {
g.stre.append(“ “);
if (lookhead == IDN) {
System.out.print(g.stre.toString() + “id:“);
match(IDN);
System.out.print(g.stre.toString() + “=“);
match(EQ);
System.out.println(g.stre.toString() + “E“);
exp();
} else if (keyword[lookhead - 100].equals(“if“)) {
System.out.print(g.stre.toString());
match(IF);
System.out.println(g.stre.toString() + “C“);
g.stre.append(“ “);
System.out.println(g.stre.toString() + “E“);
exp();
g.stre.delete(0 3);
if (lookhead == LT) {
System.out.print(g.stre.toString() + “<“);
match(LT);
} else if (lookhead == GT) {
System.out.print(g.stre.toString() + “>“);
match(GT);
} else if (lookhead == EQ) {
System.out.print(g.stre.toString() + “=“);
match(EQ);
} else
error();
System.out.println(g.stre.toString() + “E“);
exp();
g.stre.delete(0 3);
g.stre.delete(0 3);
System.out.print(g.stre.toString());
match(THEN);
System.out.println(g.stre.toString() + “S“);
statement();
g.stre.delete(0 3);
} else if (keyword[lookhead - 100].equals(“while“)) {
System.out.print(g.stre.toString() + “while“);
match(WHILE);
System.out.println(g.stre.toString() + “C“);
g.stre.append(“ “);
System.out.println(g.stre.toString() + “E“);
exp();
g.stre.delete(0 3);
if (lookhead == LT) {
System.out.print(g.stre.toString() + “<“);
match(LT);
} else if (lookhead == GT)
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 301 2012-05-14 13:45 translation\.classpath
文件 387 2012-05-14 13:45 translation\.project
文件 598 2012-05-14 13:45 translation\.settings\org.eclipse.jdt.core.prefs
文件 385 2012-05-15 14:24 translation\bin\C_Attr.class
文件 349 2012-05-15 14:24 translation\bin\E_Attr.class
文件 349 2012-05-15 14:24 translation\bin\F_Attr.class
文件 385 2012-05-15 14:24 translation\bin\S_Attr.class
文件 9521 2012-05-15 14:24 translation\bin\translate.class
文件 349 2012-05-15 14:24 translation\bin\T_Attr.class
文件 10794 2012-05-31 15:30 translation\src\translate.java
文件 301 2012-03-18 20:37 lexical\.classpath
文件 383 2012-03-18 20:37 lexical\.project
文件 598 2012-03-18 20:37 lexical\.settings\org.eclipse.jdt.core.prefs
文件 5636 2012-05-15 15:41 lexical\bin\lexical.class
文件 6507 2012-05-31 15:29 lexical\src\lexical.java
文件 301 2012-03-29 15:24 gammar\.classpath
文件 382 2012-03-29 15:24 gammar\.project
文件 598 2012-03-29 15:24 gammar\.settings\org.eclipse.jdt.core.prefs
文件 8127 2012-05-15 14:35 gammar\bin\gemmar.class
文件 9175 2012-05-31 15:28 gammar\src\gemmar.java
目录 0 2012-05-15 15:41 translation\.settings
目录 0 2012-05-15 15:41 translation\bin
目录 0 2012-05-15 15:41 translation\src
目录 0 2012-05-15 15:41 lexical\.settings
目录 0 2012-05-15 15:41 lexical\bin
目录 0 2012-05-15 15:41 lexical\src
目录 0 2012-05-15 15:42 gammar\.settings
目录 0 2012-05-15 15:42 gammar\bin
目录 0 2012-05-15 15:42 gammar\src
目录 0 2012-05-15 15:41 translation
............此处省略5个文件信息
评论
共有 条评论