编译原理
实验报告
学生姓名:
学号:
学
专
院:
业:
辅导教师:
实验一 词法分析程序设计
一、实验目的:
1.了解词法分析过程,理解和掌握构编译系统的基本理论、编
译程序的基本结构。
2.掌握编译程序设计的基本方法和步骤,同时增强编写和调试
程序的能力。
二、实验要求:
编写一个词法分析器。
三、实验内容:
给定字母表{0,1,…9,a,b…z,A,B,…Z},定义其上的正规定义
式如下:
Digit →1|2|…|9
Num →0|Digit
Letter→a|b|…|z|A|B|…|Z
原语言中允许出现的词由如下正规式规定(本语言不区分字母大
小写,即认为大小写相同):
var|integer|longint|bool|If|Then|Else|While|Do|For
|Begin|End|and| or| +|-|*|/|:=|<|>|<>|>=|<=|= =|;
|:|(|)|,|Digit·Num*|Letter·(Letter|Num)*
四、实验结果:
五、源代码:
public void analyse() {
strToken = ""; // 置 strToken 为空串
FileUtil.clearFile();//清空文件
while (i < buffer.length()) {
getChar();
getBC();
if (isLetter(ch)) { // 如果 ch 为字母
while (isLetter(ch) || isDigit(ch)) {
concat();
getChar();
}
retract(); // 回调
if (isKeyWord(strToken)) {
writeFile(strToken,strToken);//strToken 为关键字
} else {
writeFile("id",strToken);//strToken 为标识符
}
strToken = "";
} else if (isDigit(ch)) {
while (isDigit(ch)) {//ch 为数字
concat();
getChar();
}
if(!isLetter(ch)){//不能数字+字母
retract(); // 回调
writeFile("digit",strToken); // 是整形
}else writeFile("error",strToken); // 非法
strToken = "";
} else if (isOperator(ch)) { //运算符
if(ch == '/'){
getChar();
if(ch == '*') {//为/*注释
while(true){
getChar();
if(ch == '*'){// 为多行注释结束
getChar();
if(ch == '/') {
getChar();
break;}
}
}
}
if(ch == '/'){//为//单行注释
while(ch != 9){
getChar();
}
}
retract();
}
switch (ch) {
case '+': writeFile("plus",ch+""); break;
case '-': writeFile("min",ch+""); break;
case '*': writeFile("mul",ch+""); break;
case '/': writeFile("div",ch+""); break;
case '>': writeFile("gt",ch+""); break;
case '<': writeFile("lt",ch+""); break;
case '=': writeFile("eq",ch+""); break;
case '&': writeFile("and",ch+""); break;
case '|': writeFile("or",ch+""); break;
case '~': writeFile("not",ch+""); break;
default: break;
}
} else if (isSeparators(ch)) { // 界符
writeFile("separators",ch+"");
} else writeFile("error",ch+"");
}
}
实验二 算符优先语法分析程序
一、实验目的:
1.了解词法分析过程,理解和掌握构编译系统的基本理论、编
译程序的基本结构。
2.掌握编译程序设计的基本方法和步骤,同时增强编写和调试
程序的能力。
二、实验要求:
编写一个词法分析器。
三、实验内容:
四、实验结果:
五、源代码:
private final char operators[] = { '+', '-', '*', '/', '=', '>', '<'};
// 界符数组
private final char separators[] = { ';',':','(',')',','};
//判断字母
public boolean isLetter(char ch) {
return Character.isLetter(ch);
}
//判数字
public boolean isDigit(char ch) {
return Character.isDigit(ch);
}
//判关键字
public boolean isKeyWord(String s) {
for (int i = 0; i < keyWords.length; i++) {
if (keyWords[i].equals(s))
return true;
}
return false;
}
//判运算符
public boolean isOperator(char ch) {
for (int i = 0; i < operators.length; i++) {
if (ch == operators[i])
return true;
}
return false;
}
//判分隔符
public boolean isSeparators(char ch) {
for (int i = 0; i < separators.length; i++) {
if (ch == separators[i])
return true;
}
return false;
}
//反射获得种别编码
public String getType(String args) {
//int type = -1;
String type ="无该类型";
Field[] fields = KeyTypes.class.getDeclaredFields();
for (Field field : fields) {
if (field.getName().equals(args)) {
try {
type = (String)field.get(new KeyTypes());
} catch (Exception e) {
e.printStackTrace();
}
}
}
return type;
}
}