编译技术上机实验题目
实验一
一、题目
编制C语言子集的词法分析程序 二、目的 通过设计、编制、调试一个具体的词法分析程序,加深对词法分析原理的理解,并掌握在对程序设计语言源程序进行扫描过程中将其分解为各类单词的词法分析方法。
三、要求
1.根据具体情况,由同学们自己选取C语言的一个适当大小的子集(可取一类典型单词,也可以尽可能使各种类型的单词都兼顾到),如课本表2.1;在上机前一定要制出相应的表。 2.程序功能
输入:字符串。
输出:二元式(种别编码,单词自身)构成的序列。
举例:
输入:a=$;# 输出:(6,a)
(12,=)
FOUND ERROR
(13,;)
#include <iostream>
#include <string>
using namespace std;
string key[6] = {"begin", "if", "then", "while", "do", "end"}; //关键字
boolisKey( string str, int&syn) //判断是否为关键字,若是传回相应关键码的种别名 {
inti;
for(i=0; i<6; i++)
{
if(str == key)
{
syn = i + 1;
return true;
}
}
return false;
}
boolisLetter(char c) //是否为字母
{
if((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'))
return true;
else
return false; }
boolisDigit(char c) //是否为数字
{
if(c >= '0' && c <= '9')
return true;
else
return false; }
void analyse(FILE *fileP)
{
int n;
char c;
string str = "";
while((c = fgetc(fileP)) != EOF)
{
if(c == ' ' || c == '\n' || c == '\t') continue;
else if(isDigit(c)) //数字
{
while(isDigit(c))
{
str += c;
c = fgetc(fileP);
}
fseek(fileP, -1, SEEK_CUR);
cout<< "(11, " <<str<< ")" <<endl;
str = "";
}
else if(isLetter(c)) //字母开头的
{
while(isDigit(c) || isLetter(c))
{
str += c;
c = fgetc(fileP);
}
fseek(fileP, -1, SEEK_CUR); if(isKey(str, n)) cout<< "(" << n << ", " <<str<< ")" <<endl; //关键码 else
cout<< "(10, " << "\'"<<str<< "\'" << ")" <<endl; //标志符 str = "";
}
else //操作符等
{
switch(c)
{
case '+':
cout<< "(13, +)" <<endl;
break;
case '-':
cout<< "(14, -)" <<endl;
break;
case '*':
cout<< "(15, *)" <<endl;
break;
case '/':
cout<< "(16, /)" <<endl;
break;
case ':':
{
if(c=fgetc(fileP) == '=')
cout<< "(18, :=)" <<endl;
else {
cout<< "(17, 编译技术上机实验题目
实验一
一、题目
编制C语言子集的词法分析程序
二、目的
通过设计、编制、调试一个具体的词法分析程序,加深对词法分析原理的理解,并掌握在对程序设计语言源程序进行扫描过程中将其分解为各类单词的词法分析方法。
三、要求
1.根据具体情况,由同学们自己选取C语言的一个适当大小的子集(可取一类典型单词,也可以尽可能使各种类型的单词都兼顾到),如课本表2.1;在上机前一定要制出相应的表。
2.程序功能
输入:字符串。
输出:二元式(种别编码,单词自身)构成的序列。
举例:
输入:a=$;#
输出:(6,a)
(12,=)
FOUND ERROR
(13,;)
#include <iostream>
#include <string>
using namespace std;
string key[6] = {"begin", "if", "then", "while", "do", "end"}; //关键字
bool isKey( string str, int &syn) //判断是否为关键字,若是传回相应关键码的种别名 {
int i;
for(i=0; i<6; i++)
{
if(str == key)
{
syn = i + 1;
return true;
}
}
return false; }
bool isLetter(char c) //是否为字母
{
if((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'))
return true;
else
return false; }
bool isDigit(char c) //是否为数字
{
if(c >= '0' && c <= '9')
return true;
else
return false; }
void analyse(FILE *fileP)
{
int n;
char c;
string str = "";
while((c = fgetc(fileP)) != EOF)
{
if(c == ' ' || c == '\n' || c == '\t') continue;
else if(isDigit(c)) //数字
{
while(isDigit(c))
{
str += c;
c = fgetc(fileP);
}
fseek(fileP, -1, SEEK_CUR);
cout << "(11, " << str << ")" << endl;
str = ""; } else if(isLetter(c)) //字母开头的
{
while(isDigit(c) || isLetter(c))
{
str += c;
c = fgetc(fileP);
}
fseek(fileP, -1, SEEK_CUR); if(isKey(str, n))
cout << "(" << n << ", " << str << ")" << endl; //关键码 else
cout << "(10, " << "\'"<< str << "\'" << ")" << endl; //标志符
str = "";
}
else //操作符等
{
switch(c)
{
case '+':
cout << "(13, +)" << endl;
break;
case '-':
cout << "(14, -)" << endl;
break;
case '*':
cout << "(15, *)" << endl;
break;
case '/':
cout << "(16, /)" << endl;
break;
case ':': {
if(c=fgetc(fileP) == '=')
cout << "(18, :=)" << endl; else
{
cout << "(17, " << endl;
fseek(fileP, -1, SEEK_CUR); }
break;
}
case '<':
{
c=fgetc(fileP);
if(c == '=')
cout << "(22, <=)" << endl; else if(c == '>')
cout << "(21, <>)" << endl; else
{
cout << "(20, <)" << endl; fseek(fileP, -1, SEEK_CUR); }
break;
}
case '>':
{
c=fgetc(fileP);
if(c == '=')
cout << "(24, >=)" << endl; else
{
cout << "(23, >)" << endl; fseek(fileP, -1, SEEK_CUR); }
break;
}
case '=':
cout << "(25, =)" << endl;
break;
case ';':
cout << "(26, ;)" << endl;
break;
case '(':
cout << "(27, ()" << endl;
break;
case ')':
cout << "(28, ))" << endl;
break;
case '#':
cout << "(0, #)" << endl;
break;
default:
cout<<"FOUND ERROR!"<<endl;
}
}
} } int main() {
FILE *fileP; fileP = fopen("test.txt", "r"); cout << "------词法分析如下------" << endl;
analyse(fileP);
return 0;
}
实验二
/*
一、题目
编制递归下降法的语法分析程序
二、目的
通过设计、编制、调试一个典型的语法分析程序,能识别由加+、乘*、括号()、操作数所组成的算术表达式,其文法如下:
E→TE'
E'→+TE'∣ε
T→FT'
T'→*FT'∣ε
F→(E)∣i
三、要求
1.程序功能(举例)
输入:#i1*(i2+i3)#
输出:SUCCESS
输入:# i1*( i2+i3#
输出:FOUND ERROR
*/
实验代码: #include <iostream>
#include <string> using namespace std; FILE *fileP; char a[100]; void Ee(); void T(); void Tt(); void F(); void E();
int i=0;
int j=0; int flag=0;
bool isLetter(char c) //是否为字母
{
if((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) return true;
else
return false;
}
bool isDigit(char c) //是否为数字
{
if(c >= '0' && c <= '9')
return true;
else
return false; }
void read()
{
if (fgetc(fileP)=='#')
{
char c=fgetc(fileP);
while(c!='#')
{
a[j++]=c;
c=fgetc(fileP);
}
}
}
void E() {
T();
Ee();
} void Ee() {
if(a=='+')
{
i++;
T();
Ee();
}
} void T() {
F();
Tt();
} void Tt() {
if(a=='*')
{
i++;
F();
Tt();
}
}
void F() {
char c=a;
if(isLetter(c))
{
while(isDigit(c) || isLetter(c))
{ i++;
c = a;
}
}
else if(isDigit(c))
{
while(isDigit(c))
{ i++;
c = a;
}
}
else if(c=='(')
{
i++;
E();
if(a==')')
{
}
else
flag=1;
}
else
flag=1;
}
void main() {
if((fileP = fopen("test.txt", "r"))==NULL) {
cout <<"请创建test.txt资源文件" <<endl; }
else {
read();
cout << "------test文件词法分析结果如下------" << endl; E();
if(flag==0)
cout<<"success"<<endl;
else
cout<<"faild"<<endl;
cout << "----------------------------" << endl;
}
if((fileP = fopen("test.txt", "r"))==NULL) {
cout <<"请创建test2.txt资源文件" <<endl;
}
else {
read();
cout << "------test2文件词法分析结果如下------" << endl;
E();
if(flag==0)
cout<<"success"<<endl;
else
cout<<"faild"<<endl;
cout << "----------------------------" << endl;
}
}
请自行新建test.txt" <<endl; fseek(fileP, -1, SEEK_CUR); }
break;
}
case '<':
{
c=fgetc(fileP);
if(c == '=')
cout<< "(22, <=)" <<endl; else if(c == '>')
cout<< "(21, <>)" <<endl; else
{
cout<< "(20, <)" <<endl; fseek(fileP, -1, SEEK_CUR); }
break;
}
case '>':
{
c=fgetc(fileP);
if(c == '=')
cout<< "(24, >=)" <<endl; else
{
cout<< "(23, >)" <<endl; fseek(fileP, -1, SEEK_CUR); }
break;
}
case '=':
cout<< "(25, =)" <<endl;
break;
case ';':
cout<< "(26, ;)" <<endl;
break;
case '(':
cout<< "(27, ()" <<endl;
break;
case ')':
cout<< "(28, ))" <<endl;
break;
case '#':
cout<< "(0, #)" <<endl;
break;
default:
cout<<"FOUND ERROR!"<<endl; }
}
} } int main() {
FILE *fileP; fileP = fopen("test.txt", "r"); cout<< "------词法分析如下------" <<endl;
analyse(fileP); return 0;
}
实验二
/*
一、题目
编制递归下降法的语法分析程序
二、目的 通过设计、编制、调试一个典型的语法分析程序,能识别由加+、乘*、括号()、操作数所组成的算术表达式,其文法如下:
E→TE' E'→+TE'∣ε
T→FT'
T'→*FT'∣ε
F→(E)∣i
三、要求
1.程序功能(举例)
输入:#i1*(i2+i3)#
输出:SUCCESS
输入:# i1*( i2+i3#
输出:FOUND ERROR
*/
实验代码:
#include <iostream>
#include <string>
using namespace std; FILE *fileP;
char a[100];
void Ee();
void T();
void Tt();
void F();
void E();
inti=0;
int j=0;
int flag=0;
boolisLetter(char c) //是否为字母
{
if((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'))
return true;
else
return false;
}
boolisDigit(char c) //是否为数字
{
if(c >= '0' && c <= '9') return true; else
return false; } void read() {
if (fgetc(fileP)=='#') {
char c=fgetc(fileP); while(c!='#') {
a[j++]=c;
c=fgetc(fileP); }
}
}
void E() {
T();
Ee();
} void Ee() {
if(a=='+')
{
i++;
T();
Ee();
}
} void T() {
F();
Tt();
}
void Tt()
{
if(a=='*')
{
i++;
F();
Tt();
}
} void F() {
char c=a;
if(isLetter(c))
{
while(isDigit(c) || isLetter(c)) { i++; c = a; }
}
else if(isDigit(c))
{
while(isDigit(c))
{ i++; c = a; }
}
else if(c=='(')
{
i++;
E();
if(a==')')
{
}
else
flag=1;
}
else
flag=1;
}
void main() {
if((fileP = fopen("test.txt", "r"))==NULL) {
cout<<"请创建test.txt资源文件" <<endl;
}
else {
read(); cout<< "------test文件词法分析结果如下------" <<endl;
E();
if(flag==0)
cout<<"success"<<endl;
else cout<<"faild"<<endl;
cout<< "----------------------------" <<endl;
}
if((fileP = fopen("test.txt", "r"))==NULL) {
cout<<"请创建test2.txt资源文件" <<endl;
}
else {
read(); cout<< "------test2文件词法分析结果如下------" <<endl;
E();
if(flag==0)
cout<<"success"<<endl;
else cout<<"faild"<<endl;
cout<< "----------------------------" <<endl;
}
}
请自行新建test.txt
ltlt编译原理gtgt上机实验报告编译原理上机实验报告一实验目的与要求目的在分析理解一个教学型编译程序如PL0的基础上对其词法分…
编译原理课程实验报告题目专业计算机指导教师签名华东理工大学信息学院计算机系20xx年4月10日一实验序号编译原理第一次实验二实验题…
编译原理词法分析一、实验目的设计、编制并调试一个词法分析程序,加深对词法分析原理的理解。二、实验要求2.1待分析的简单的词法(1)…
编译原理实验报告指导教师1一实验目的基本掌握计算机语言的词法分析程序的开发方法以及掌握计算机语言的语法分析程序设计与属性文法应用的…
编译原理实验报告班级姓名学号自我评定75实验一词法分析程序实现一实验目的与要求通过编写和调试一个词法分析程序掌握在对程序设计语言的…
北京联合大学信息学院“面向对象程序设计”课程上机实验报告题目:JAVA上机实验心得体会姓名(学号):专业:计算机科学与技术编制时间…
1.1WindowsXP的基本操作【思考与实践】1.将一个文件删除,然后清空回收站,该文件还可以恢复到原来位置吗?答:一般情况下不…
大学计算机基础课程实验报告手册学院年级专业姓名学号2220xx319xx20xx任课教师上机地点以上由学生填写实验教师签字西南大学…
交通与汽车工程学院实验报告课程名称课程代码学院直属系交通与汽车工程学院年级专业班学生姓名学号实验总成绩任课教师开课学院交通与汽车工…
指导教师纪良浩学院通信与信息工程专业通信类班级0101022学号20xx210722姓名曾小兵实验室S319实验题目实验一用选择法…