[] WHILE loop compiler theory of translation program design and implementation (recursive descent, the output quaternion type) (lexical assignment statement analysis, semantic analysis)

Disclaimer: This article is a blogger original article. Reproduced please contact bloggers authorization. Bloggers public micro-channel number] [Department of Knowing and Doing the campus. https://blog.csdn.net/cxh_1231/article/details/86482413

Note : This article documents WHUT- of Computer Science - compiler theory of curriculum curricular practice

Project Download: https://download.csdn.net/download/cxh_1231/10916735

Paper come Zhongjue, practice is essential to be aware of the matter!

0, author:

Compiler theory courses, a total of two experiments once and curricular practices. The first two experiments are:

  • Assignment of lexical analysis program design and implementation
  • Semantic analysis program design and implementation of an assignment statement

The practice of curricular content is: the WHILE loop of the translation program design and implementation (recursive descent, the output quaternion type)

So I will take practice as the goal, to do from the very beginning in accordance with the content of practice, first of all to achieve lexical analysis and syntax analysis, semantic analysis, intermediate results output four yuan. The whole process at one go. It is also possible to refer to the content as the previous experiment.

 

1, practice content:

Curricular practice mission statement

Student Name:              Professional class:                

Instructor:   Lin        workplace: Computer Science and Technology  

Topic: WHILE loop of the translation program design and implementation (recursive descent, the output quaternion type)

Initial conditions:

  • Theory: grasp the compilation theory, techniques, methods, master the use of a high-level computer language.

  • Practice: Computer Lab provides computer and software environment. If you own a computer you can be designed on it.

The main tasks required to complete: (including the specific requirements of practice workloads and technical requirements, as well as report writing, etc.)

  • Written grammar and attribute grammar that match a given grammar analysis methods.
  • Intermediate code to complete the requirements of the subject four yuan described.
  • Write a given grammar analysis method of thought, syntax and semantic analysis to complete the programming.
  • After the preparation of a good analysis program, designed to use a number of cases, by machine tests and analysis programs designed.
  • Design requirements written report format in Annex. Practice report curricular content of the text should include:
  1. System Description 1 (described problem domain);

  2. 2 and attribute grammar description grammar;

  3. 3 and syntax analysis method described in Design of parsing tables;

  4. 4 shows the intermediate code in the form of a given design subject description and the intermediate code sequence;

  5. 5 outline design build system;

  6. 6 detailed description of the algorithm (flow chart, or pseudo code);

  7. 7 software testing methods and test results;

  8. 8 evaluation design, characteristics, lack of harvest and experience and so on.

 

2, run shot:

 

3, part of the code:

Only part of the code, by reference.

The following code will not run! You can not run! You can not run!

Code can run: https://download.csdn.net/download/cxh_1231/10916735

/*
			编译原理课内实践
	While循环语句的翻译程序设计与实现
		递归下降法	输出四元式
*/

#include <iostream>
#include <string>
#include <fstream>
#include <stack>
#include <queue>

using namespace std;
#define ERROR 0;
#define OK 1;

void anasyProgram(string program);
void listWord();
void anasyWord();
//保存拆开后的单个符号
struct Word{
	string oneword;
	string type;
};
Word word[1000];
//程序单词个数;
int num = 0;
char suanshi[100];
int suanshinum = 0;

//读入源程序
void inputProgram() {
	//首先将整个txt文件读入到program字符串里
	ifstream in("program.txt", ios::in);
	istreambuf_iterator<char> beg(in), end;
	string program(beg, end);//或者string st;st.assign(beg,end);
	in.close();

	anasyProgram(program);
}

/********词法分析阶段**********/

//判端是界符:返回真
bool isDelimiter(char ch) {
	if (ch == '(' || ch == ')' || ch == ';' || ch == '{' || ch == '}')
		return true;
	else 
		return false;
}
//判断是字母:返回真
bool isLetter(char ch) {
	if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) 
		return true;
	else 
		return false;
}
//判断是数字:返回真
bool isNumber(char ch) {
	if(ch >= '0' && ch <= '9')
		return true;
	else
		return false;
}
//判断是运算符:返回真
bool isOperator(char ch) {
	if (ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '>' || ch == '<' || ch == '=' || ch == '!' || ch=='|' || ch=='&')
		return true;
	else return false;
}

//词法分析:分析程序,将每个单词拆分,放在Word结构体数组中
void anasyProgram(string program) {
	string temp;

	//测试:输出整个程序
	cout << "源程序:\n" << program << endl << endl;
	
	//测试:输出程序字母符号个数
	//cout << program.size() << endl;

	//进行拆分
	for (int i = 0; i < program.size(); i++) {

		//遇到空格,临时字符串清空
		if (program[i] == ' ') {
			temp = "";
			//cout << "已清空temp!" << endl;
			continue;
		}

		/*界符分析:{、}、(、)、;  */
		else if (temp.size() == 0 && isDelimiter(program[i])) {

			//自行完成

			continue;
		}

		/*标识符分析(也有可能是关键字):首位必须为字母*/
		else if (temp.size() == 0 && isLetter(program[i])) {

			//自行完成

		}

		/*运算符分析:+、-、 *、/、+=、-=、*=、/=、>、<、=、==、!=、>=、<=、||、&& */  /*暂时忽略++  --  */
		else if (temp.size() == 0 && isOperator(program[i])) {

			//自行完成

		}

		/*数字分析:首位为数字,则必须为数字,否则错误!*/
		else if (temp.size() == 0 && isNumber(program[i])) {

			//自行完成

			continue;
		}
	}

}

void listWord() {
	for (int i = 0; i < num; i++) {
		cout << word[i].type << "\t:" << word[i].oneword << endl;
	}
}

/*********语法分析阶段***********/
void isDelimiter(int i);
void isOperator(int i);
void isNumber(int i);

//标识符
void isIdentifier(int i) {
	if (word[i+1].type == "界符") {
		isDelimiter(i + 1);
	}
	else if (word[i+1].type == "运算符") {
		isOperator(i + 1);
	}
	else {
		cout << word[i].type << word[i].oneword << "后边接" << word[i + 1].type <<",语法错误!"<< endl;
	}

}

//运算符
void isOperator(int i) {
	if (word[i + 1].type == "标识符") {
		isIdentifier(i + 1);
	}
	else if (word[i+1].type=="常数") {
		isNumber(i + 1);
	}
	else if (word[i+1].oneword=="++" || word[i + 1].oneword == "--") {

	}
	else {
		cout << word[i].type << word[i].oneword << "后边接" << word[i + 1].type << ",语法错误!" << endl;
	}
}

//界符
void isDelimiter(int i) {

}

void isNumber(int i) {

}

/********使用文法解决*******/
void do_S();						//S→while(A){B}
void do_A();						//A→CDC
void do_D(string &tempD);			//D→优先关系
void do_B();						//B→i=C;
void do_C(string &tempC);			//C→ EG
void do_E(string &tempE);			//E→ FH
void do_G(queue<string> &tempG);	//G→ +EG | -EG | 空
void do_F(string &tempF);			//F→ (C) | i | n
void do_H(queue<string> &tempH);	//H→ *FH | /FH | 空

//四元式:OP为操作符,result=arg1+arg2;
struct Siyuanshi {
	string op;
	string arg1;
	string arg2;
	string result;
};
Siyuanshi siyuanshi[100];
int siyuanshiNum = 0;//四元式个数

int nowword = 0;
int anasyWordResult=OK;
int newtemp = 1;
int JumpType = 0;


void do_S(){
	//自行完成
}

void do_A(){
	//自行完成
}

void do_D(string &tempD){
	//自行完成
	   
}

void do_B(){
	//自行完成
}

void do_C(string &tempC){
	//自行完成
}

void do_E(string &tempE){	
	//自行完成
}

void do_G(queue<string> &tempG){
	//自行完成
}

void do_F(string & tempF){
	//自行完成
}

void do_H(queue<string> &tempH){
	//自行完成
}

//语法分析:分析单词,将单词之间的正误进行判断整理,提示输出
void anasyWord(){
	do_S();
}

/*输出四元式*/
void printSiyuanshi() {
	//自行完成
}

int main() {

	inputProgram();

	/*测试*/
	//for (int i = 0; i < suanshinum; i++) {
	//	cout << suanshi[i];
	//}

	//采用递归下降法语义分析
	anasyWord();

	if (anasyWordResult == 1) {
		cout << "\n语法分析正确!" << endl ;
		listWord();
		printSiyuanshi();
	}
	
	return 0;
}

 

Guess you like

Origin blog.csdn.net/cxh_1231/article/details/86482413