编译原理上机实验报告

编译技术上机实验题目

实验一

一、题目

编制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

相关推荐