- 浏览: 13590 次
- 性别:
- 来自: 苏州
最新评论
-
2010kk2010:
02a
01*
02b
01+
02c
01*
02a
01# ...
编译原理LL1语法分析算法的实现(token转四元式) -
2010kk2010:
02a
01*
02b
01+
02c
01*
02a
01# ...
编译原理LL1语法分析算法的实现(token转四元式)
#include <stdio.h>
#include <stack>
#include <vector>
#include <iostream>
#include <fstream>
#include <malloc.h>
using namespace std;
//token结构
struct token {
int code; //token的类别,code为1则是符号,为2则是数字
char value; //token的值
};
typedef struct token tokens;
vector<tokens> tokenBuffer; //用于存储token的缓冲区
stack<tokens> tokenBuffers;
//产生式结构
struct formula {
int id; //产生式编号
char left; //产生式左部
char right[256]; //产生式右部
int r_length; //产生式右部长度
};
typedef struct formula formulas;
formulas formulaBuffer[11]; //用于存储产生式的缓冲区
//四元式结构
struct expression {
char symbol; //操作符
char num1; //第一个操作数
char num2; //第二个操作数
char result; //结果变量
};
typedef struct expression expressions;
vector<expressions> expressionBuffer; //用于存储四元式的缓冲区
int expressionCount = 0; //四元式个数
//分析表中每一项的结构
struct analysisTable {
char currentState; //分析栈的栈顶符号
char currentToken; //当前字符
int expressionNum; //对应的产生式编号
};
typedef struct analysisTable analysisTables;
vector<analysisTables> analysisTableBuffer; //LL1分析表的缓冲区
stack<char> analysisStack; //分析栈
stack<char> sematicStack; //语义栈
//初始化LL1分析表
void initialAnalysisTableBuffer() {
analysisTables* temp1a = new analysisTable;
analysisTables* temp1b = new analysisTable;
analysisTables* temp1c = new analysisTable;
analysisTables* temp2 = new analysisTable;
analysisTables* temp3 = new analysisTable;
analysisTables* temp4 = new analysisTable;
analysisTables* temp5 = new analysisTable;
analysisTables* temp6 = new analysisTable;
analysisTables* temp7a = new analysisTable;
analysisTables* temp7b = new analysisTable;
analysisTables* temp7c = new analysisTable;
analysisTables* temp8 = new analysisTable;
analysisTables* temp9 = new analysisTable;
analysisTables* temp10 = new analysisTable;
analysisTables* temp11 = new analysisTable;
analysisTables* temp12 = new analysisTable;
analysisTables* temp13 = new analysisTable;
analysisTables* temp14 = new analysisTable;
analysisTables* temp15a = new analysisTable;
analysisTables* temp15b = new analysisTable;
analysisTables* temp15c = new analysisTable;
analysisTables* temp16 = new analysisTable;
temp1a->expressionNum = 1;
temp1a->currentState = 'E';
temp1a->currentToken = 'a';
temp1b->expressionNum = 1;
temp1b->currentState = 'E';
temp1b->currentToken = 'b';
temp1c->expressionNum = 1;
temp1c->currentState = 'E';
temp1c->currentToken = 'c';
temp2->expressionNum = 1;
temp2->currentState = 'E';
temp2->currentToken = '(';
temp3->expressionNum = 2;
temp3->currentState = 'L';
temp3->currentToken = '+';
temp4->expressionNum = 3;
temp4->currentState = 'L';
temp4->currentToken = '-';
temp5->expressionNum = 4;
temp5->currentState = 'L';
temp5->currentToken = ')';
temp6->expressionNum = 4;
temp6->currentState = 'L';
temp6->currentToken = '#';
temp7a->expressionNum = 5;
temp7a->currentState = 'T';
temp7a->currentToken = 'a';
temp7b->expressionNum = 5;
temp7b->currentState = 'T';
temp7b->currentToken = 'b';
temp7c->expressionNum = 5;
temp7c->currentState = 'T';
temp7c->currentToken = 'c';
temp8->expressionNum = 5;
temp8->currentState = 'T';
temp8->currentToken = '(';
temp9->expressionNum = 8;
temp9->currentState = 'M';
temp9->currentToken = '+';
temp10->expressionNum = 8;
temp10->currentState = 'M';
temp10->currentToken = '-';
temp11->expressionNum = 6;
temp11->currentState = 'M';
temp11->currentToken = '*';
temp12->expressionNum = 7;
temp12->currentState = 'M';
temp12->currentToken = '/';
temp13->expressionNum = 8;
temp13->currentState = 'M';
temp13->currentToken = ')';
temp14->expressionNum = 8;
temp14->currentState = 'M';
temp14->currentToken = '#';
temp15a->expressionNum = 9;
temp15a->currentState = 'F';
temp15a->currentToken = 'a';
temp15b->expressionNum = 9;
temp15b->currentState = 'F';
temp15b->currentToken = 'b';
temp15c->expressionNum = 9;
temp15c->currentState = 'F';
temp15c->currentToken = 'c';
temp16->expressionNum = 10;
temp16->currentState = 'F';
temp16->currentToken = '(';
analysisTableBuffer.push_back(*temp1a);
analysisTableBuffer.push_back(*temp1b);
analysisTableBuffer.push_back(*temp1c);
analysisTableBuffer.push_back(*temp2);
analysisTableBuffer.push_back(*temp3);
analysisTableBuffer.push_back(*temp4);
analysisTableBuffer.push_back(*temp5);
analysisTableBuffer.push_back(*temp6);
analysisTableBuffer.push_back(*temp7a);
analysisTableBuffer.push_back(*temp7b);
analysisTableBuffer.push_back(*temp7c);
analysisTableBuffer.push_back(*temp8);
analysisTableBuffer.push_back(*temp9);
analysisTableBuffer.push_back(*temp10);
analysisTableBuffer.push_back(*temp11);
analysisTableBuffer.push_back(*temp12);
analysisTableBuffer.push_back(*temp13);
analysisTableBuffer.push_back(*temp14);
analysisTableBuffer.push_back(*temp15a);
analysisTableBuffer.push_back(*temp15b);
analysisTableBuffer.push_back(*temp15c);
analysisTableBuffer.push_back(*temp16);
}
//由于本次实验主要是考察语法分析和语义分析,所以为了节省时间不采用查表的方式获取token,而是直接初始化token的值。
//初始化token缓冲区
void initialTokenBuffer() {
ifstream fin("tokens.txt");
char temp[10][5];
int i = 0;
while (!fin.eof()) {
fin.getline(temp[i], 4);
i++;
}
fin.close();
do {
i--;
tokens *token_temp = new tokens();
token_temp->code = atoi(&temp[i][0]);
token_temp->value = temp[i][2];
tokenBuffer.push_back(*token_temp);
tokenBuffers.push(*token_temp);
} while (i != 0);
// tokens* t1 = new tokens();
// tokens* t2 = new tokens();
// tokens* t3 = new tokens();
// tokens* t4 = new tokens();
// tokens* t5 = new tokens();
// tokens* t6 = new tokens();
// t1->code = 2;
// t1->value = 'a';
// t2->code = 1;
// t2->value = '+';
// t3->code = 2;
// t3->value = 'b';
// t4->code = 1;
// t4->value = '*';
// t5->code = 2;
// t5->value = 'c';
// t6->code = 1;
// t6->value = '#';
// tokenBuffer.push_back(*t1);
// tokenBuffer.push_back(*t2);
// tokenBuffer.push_back(*t3);
// tokenBuffer.push_back(*t4);
// tokenBuffer.push_back(*t5);
// tokenBuffer.push_back(*t6);
//
// tokenBuffers.push(*t6);
// tokenBuffers.push(*t5);
// tokenBuffers.push(*t4);
// tokenBuffers.push(*t3);
// tokenBuffers.push(*t2);
// tokenBuffers.push(*t1);
}
//初始化产生式缓冲区
void initialFormulaBuffer() {
formulas *fTemp1 = new formula;
formulas *fTemp2 = new formula;
formulas *fTemp3 = new formula;
formulas *fTemp4 = new formula;
formulas *fTemp5 = new formula;
formulas *fTemp6 = new formula;
formulas *fTemp7 = new formula;
formulas *fTemp8 = new formula;
formulas *fTemp9 = new formula;
formulas *fTemp10 = new formula;
fTemp1->id = 1;
fTemp1->left = 'E';
fTemp1->r_length = 2;
fTemp1->right[0] = 'T';
fTemp1->right[1] = 'L';
fTemp1->right[2] = '\0';
fTemp2->id = 2;
fTemp2->left = 'L';
fTemp2->r_length = 4;
fTemp2->right[0] = '+';
fTemp2->right[1] = 'T';
fTemp2->right[2] = 'Y';
fTemp2->right[3] = 'L';
fTemp2->right[4] = '\0';
fTemp3->id = 3;
fTemp3->left = 'L';
fTemp3->r_length = 4;
fTemp3->right[0] = '-';
fTemp3->right[1] = 'T';
fTemp3->right[2] = 'U';
fTemp3->right[3] = 'L';
fTemp3->right[4] = '\0';
fTemp4->id = 4;
fTemp4->left = 'L';
fTemp4->r_length = 0;
//fTemp->right[0] = "N";
fTemp5->id = 5;
fTemp5->left = 'T';
fTemp5->r_length = 2;
fTemp5->right[0] = 'F';
fTemp5->right[1] = 'M';
fTemp6->id = 6;
fTemp6->left = 'M';
fTemp6->r_length = 4;
fTemp6->right[0] = '*';
fTemp6->right[1] = 'F';
fTemp6->right[2] = 'I';
fTemp6->right[3] = 'M';
fTemp7->id = 7;
fTemp7->left = 'M';
fTemp7->r_length = 4;
fTemp7->right[0] = '/';
fTemp7->right[1] = 'F';
fTemp7->right[2] = 'O';
fTemp7->right[3] = 'M';
fTemp8->id = 8;
fTemp8->left = 'M';
fTemp8->r_length = 0;
fTemp9->id = 9;
fTemp9->left = 'F';
fTemp9->r_length = 2;
fTemp9->right[0] = 'i';
fTemp9->right[1] = 'P';
fTemp10->id = 10;
fTemp10->left = 'F';
fTemp10->r_length = 3;
fTemp10->right[0] = '(';
fTemp10->right[1] = 'E';
fTemp10->right[2] = ')';
formulaBuffer[0] = *fTemp1;
formulaBuffer[1] = *fTemp2;
formulaBuffer[2] = *fTemp3;
formulaBuffer[3] = *fTemp4;
formulaBuffer[4] = *fTemp5;
formulaBuffer[5] = *fTemp6;
formulaBuffer[6] = *fTemp7;
formulaBuffer[7] = *fTemp8;
formulaBuffer[8] = *fTemp9;
formulaBuffer[9] = *fTemp10;
}
//入语义栈操作
void accept(tokens temp) {
if (temp.code == 1) {
cout << temp.value << "匹配成功" << endl;
return;
}
cout << temp.value << " 匹配成功" << endl;
cout << temp.value << " 进入语义栈" << endl;
sematicStack.push(temp.value);
}
//产生四元式并添加到四元式缓冲区中
void produceExpression(char temp) {
expressions *eTemp = new expressions;
eTemp->num2 = sematicStack.top();
sematicStack.pop();
eTemp->num1 = sematicStack.top();
sematicStack.pop();
eTemp->symbol = temp;
eTemp->result = (char) ((int) 'u' + expressionCount);
sematicStack.push(eTemp->result);
expressionBuffer.push_back(*eTemp);
expressionCount++;
}
//输出四元式
void printExpression() {
for (vector<expressions>::iterator i = expressionBuffer.begin(); i != expressionBuffer.end(); i++) {
cout << "四元式:" << i->symbol << " " << (char) (i->num1) << " " << (char) (i->num2) << " " << i->result << endl;
}
}
//输出读取到的token
void printTokens() {
for (vector<tokens>::iterator i = tokenBuffer.begin(); i != tokenBuffer.end(); i++) {
cout << "token:" << i->code << " " << i->value << endl;
}
}
//查找分析表将相应产生式压入分析栈
bool searchAnalysiTable(char cState, tokens cToken) {
formulas *fTemp = new formula;
//查分析表,获取对应的产生式编号
int e_num;
vector<analysisTables>::iterator i;
for (i = analysisTableBuffer.begin(); i != analysisTableBuffer.end(); i++) {
if (i->currentState == cState && i->currentToken == cToken.value) {
e_num = i->expressionNum;
break;
}
}
if (i == analysisTableBuffer.end()) {
cout << "parse error" << endl;
return false;
}
cout << "产生式编号:" << e_num << endl;
//查找产生式缓冲区获得对应产生式
fTemp = &formulaBuffer[e_num - 1];
cout << analysisStack.top() << "出栈" << endl;
analysisStack.pop();
//将产生式右部逆序压栈
if (e_num == 9)
fTemp->right[0] = cToken.value;
int j = fTemp->r_length;
for (int i = 0; i < fTemp->r_length; i++) {
cout << fTemp->right[j - 1] << "进入分析栈" << endl;
analysisStack.push(fTemp->right[j - 1]);
j--;
}
return true;
}
int main(int argc, char *argv[]) {
initialAnalysisTableBuffer();
initialTokenBuffer();
initialFormulaBuffer();
printTokens();
analysisStack.push('#');
analysisStack.push('E');
bool b_temp;
while (!analysisStack.empty()) {
tokens currentToken = tokenBuffers.top();
if (currentToken.value == analysisStack.top()) {
accept(currentToken);
tokenBuffers.pop();
cout << analysisStack.top() << "出栈" << endl;
analysisStack.pop();
continue;
}
cout << "current state:" << analysisStack.top() << endl;
cout << "current token:" << currentToken.value << endl;
switch (analysisStack.top()) {
case 'Y':
produceExpression('+');
analysisStack.pop();
break;
case 'U':
produceExpression('-');
analysisStack.pop();
break;
case 'I':
produceExpression('*');
analysisStack.pop();
break;
case 'O':
produceExpression('/');
analysisStack.pop();
break;
case 'P':
analysisStack.pop();
cout << "P出栈" << endl;
break;
case '#':
analysisStack.pop();
break;
case 'E':
case 'F':
case 'L':
case 'M':
case 'N':
case 'T':
b_temp = searchAnalysiTable(analysisStack.top(), currentToken);
break;
default:
cout << "error" << endl;
getchar();
}
if (!b_temp)
return 0;
}
printExpression();
return 0;
}
评论
01*
02b
01+
02c
01*
02a
01#
只有算术表达式的翻译,没有控制语句的翻译...
01*
02b
01+
02c
01*
02a
01#
只有算术表达式的翻译,没有控制语句的翻译...
相关推荐
编译原理 LL1语法分析 湖南大学
自己实现的编译原理的LL1语法分析器,是自己的实验作业,用Vs2017实现,可以直接运行,代码注释丰富,希望与大家交流学习!欢迎大家下载!
自己实现的编译原理的LL1语法分析器,是自己的实验作业,用Vs2017实现,可以直接运行
使用MFC实现编译原理LL1语法分析器(含消除左递归)使用MFC实现编译原理LL1语法分析器(含消除左递归)
编译原理 LL1算法LL1算法编译原理 LL1算法LL1算法编译原理 LL1算法LL1算法编译原理 LL1算法LL1算法
编译 语法分析 LL(1) 编译 语法分析 LL(1) 编译 语法分析 LL(1) 编译 语法分析 LL(1)
编译原理LL1语法分析试验 包含、目的、内容、要求的具体步骤
编译原理 从词法分析器到语法分析器的实现,词法分析器以有穷状态机实现,而语法分析器主要使用LL1算法实现,中间使用了大量的图论算法。
编译原理 LL1语法分析器: 用JAVA写的一个简单语法分析器; 输入一个表达式,输出表达式判断的结果。
自定义一个文法集,输入文法产生式,计算文法的FIRST,FOLLOW和SELECT集合, 利用SELECT集合构造预测分析表,接着用预测分析程序,栈 和预测分析表对输入串进行分析,给出分析过程。
LL1语法分析,编译原理实验,非常好的程序
安徽大学 LL1 语法分析 first follow select 规约 预测分析表 清华大学 编译原理
预测分析法(LL (1)方法)的基本思想是:从文法开始符S 出发,从左到右扫描源程序,每次通过向前查看 1 个字符,选择合适的产生式,...本资源中含有研究性学习专题 3_LL(1)语法分析设计原理与实现中的源代码和测试样例
编译原理LL1分析法编译原理LL1分析法编译原理LL1分析法编译原理LL1分析法
构造预测分析表,并利用分析表和一个栈来实现对上述程序设计语言的分析程序。 3.分析法的功能是利用 LL(1)控制程序根据显示栈栈顶内容、向前看符号以及 LL(1)分析表,对输入符号串自上而下的分析过程。
编译原理课设 LL1语法分析器,注释掉的一部分代码是可以扩展的部分
语言为C++,使用了set,map容器,输入格式:S -> Aa | g | e,支持多‘|’ 符号,采用文件输入
编译原理LL1文法实现,使用D盘下TXT文档输入字符串例子
我编译原理实验的时候的语法分析程序,要求是输出产生式。内有实验指导书