编译原理实验Sicily--简单优先分析程序

Description

 输入开始符号,非终结符,终结符,产生式
输出简单优先分析过程


以简单优先算术表达式G[E]: 为例
E→E+T | E-T | A
A→T
T→F*T | F/T | F
F→(B) | D
B→E
D→x | y | z

Input

 输入开始符号;
非终结符个数,非终结符,空格符分隔;
终结符个数,终结符,空格符分隔;
产生式的个数,各产生式的序号,产生式的左边和右边符号,空格符分隔;
简单优先分析表方阵的行列数,简单优先分析表,空格符分隔;(0错误;1小于;2等于;3大于)
输入分析字符串,#结束

Output

 输出分析过程;
用“ $ ”分隔,左边表示栈底到栈顶符号;右边表示尚未分析的字符串。

Sample InputCopy

E
6  E A T F B D
9 + -  * / ( ) x y z 
13
1  E E+T
2  E E-T
3  E A
4  A T
5  T F*T
6  T F/T
7  T F
8  F (B)
9  F D
10 B E
11 D x
12 D y
13 D z
15
  0  0  0  0  0  0  2  2  0  0  0  3  0  0  0
  0  0  0  0  0  0  3  3  0  0  0  3  0  0  0
  0  0  0  0  0  0  3  3  0  0  0  3  0  0  0
  0  0  0  0  0  0  3  3  2  2  0  3  0  0  0
  0  0  0  0  0  0  0  0  0  0  0  2  0  0  0
  0  0  0  0  0  0  3  3  3  3  0  3  0  0  0
  0  0  2  1  0  1  0  0  0  0  1  0  1  1  1
  0  0  2  1  0  1  0  0  0  0  1  0  1  1  1
  0  0  2  1  0  1  0  0  0  0  1  0  1  1  1
  0  0  2  1  0  1  0  0  0  0  1  0  1  1  1
  1  1  1  1  2  1  0  0  0  0  1  0  1  1  1
  0  0  0  0  0  0  3  3  3  3  0  3  0  0  0
  0  0  0  0  0  0  3  3  3  3  0  3  0  0  0
  0  0  0  0  0  0  3  3  3  3  0  3  0  0  0
  0  0  0  0  0  0  3  3  3  3  0  3  0  0  0
(x+(y-x*z)*(y+x*z))+x/z#

Sample OutputCopy

# & (x+(y-x*z)*(y+x*z))+x/z#
#( & x+(y-x*z)*(y+x*z))+x/z#
#(x & +(y-x*z)*(y+x*z))+x/z#
#(D & +(y-x*z)*(y+x*z))+x/z#
#(F & +(y-x*z)*(y+x*z))+x/z#
#(T & +(y-x*z)*(y+x*z))+x/z#
#(A & +(y-x*z)*(y+x*z))+x/z#
#(E & +(y-x*z)*(y+x*z))+x/z#
#(E+ & (y-x*z)*(y+x*z))+x/z#
#(E+( & y-x*z)*(y+x*z))+x/z#
#(E+(y & -x*z)*(y+x*z))+x/z#
#(E+(D & -x*z)*(y+x*z))+x/z#
#(E+(F & -x*z)*(y+x*z))+x/z#
#(E+(T & -x*z)*(y+x*z))+x/z#
#(E+(A & -x*z)*(y+x*z))+x/z#
#(E+(E & -x*z)*(y+x*z))+x/z#
#(E+(E- & x*z)*(y+x*z))+x/z#
#(E+(E-x & *z)*(y+x*z))+x/z#
#(E+(E-D & *z)*(y+x*z))+x/z#
#(E+(E-F & *z)*(y+x*z))+x/z#
#(E+(E-F* & z)*(y+x*z))+x/z#
#(E+(E-F*z & )*(y+x*z))+x/z#
#(E+(E-F*D & )*(y+x*z))+x/z#
#(E+(E-F*F & )*(y+x*z))+x/z#
#(E+(E-F*T & )*(y+x*z))+x/z#
#(E+(E-T & )*(y+x*z))+x/z#
#(E+(E & )*(y+x*z))+x/z#
#(E+(B & )*(y+x*z))+x/z#
#(E+(B) & *(y+x*z))+x/z#
#(E+F & *(y+x*z))+x/z#
#(E+F* & (y+x*z))+x/z#
#(E+F*( & y+x*z))+x/z#
#(E+F*(y & +x*z))+x/z#
#(E+F*(D & +x*z))+x/z#
#(E+F*(F & +x*z))+x/z#
#(E+F*(T & +x*z))+x/z#
#(E+F*(A & +x*z))+x/z#
#(E+F*(E & +x*z))+x/z#
#(E+F*(E+ & x*z))+x/z#
#(E+F*(E+x & *z))+x/z#
#(E+F*(E+D & *z))+x/z#
#(E+F*(E+F & *z))+x/z#
#(E+F*(E+F* & z))+x/z#
#(E+F*(E+F*z & ))+x/z#
#(E+F*(E+F*D & ))+x/z#
#(E+F*(E+F*F & ))+x/z#
#(E+F*(E+F*T & ))+x/z#
#(E+F*(E+T & ))+x/z#
#(E+F*(E & ))+x/z#
#(E+F*(B & ))+x/z#
#(E+F*(B) & )+x/z#
#(E+F*F & )+x/z#
#(E+F*T & )+x/z#
#(E+T & )+x/z#
#(E & )+x/z#
#(B & )+x/z#
#(B) & +x/z#
#F & +x/z#
#T & +x/z#
#A & +x/z#
#E & +x/z#
#E+ & x/z#
#E+x & /z#
#E+D & /z#
#E+F & /z#
#E+F/ & z#
#E+F/z & #
#E+F/D & #
#E+F/F & #
#E+F/T & #
#E+T & #
#E & # 

程序:

#include <iostream>
#include <map>
#include <string>
using namespace std;
int main()
{
	map<string,char> map1;
	map<char,int> map2;
	char start;
	cin>>start;
	int n,m,t;
	cin>>n;
	for(int i = 0; i < n; i++)
	{
		char ch;
		cin>>ch;
		map2[ch] = i;
	}
	cin>>m;
	for(int i = 0; i < m; i++)
	{
		char ch;
		cin>>ch;
		map2[ch] = i + n;
	}
	cin>>t;
	for(int i = 0; i < t; i++)
	{
		int index;
		char ch;
		string chan;
		cin>>index>>ch>>chan;
		map1[chan] = ch;
	}
	int matrix[101][101] = {0};
	int nn;
	cin>>nn;
	for(int i = 0; i < nn; i++)
	{
		for(int j = 0; j < nn; j++)
		{
			cin>>matrix[i][j];
		}
	}
	string sen;
	cin>>sen;
	sen.erase(sen.size()-1,1);
	cout<<"# & "<<sen<<"#"<<endl;
	string zuo = "";
	while(sen.size() != 0)
	{
		if(zuo.size() == 0)
		{
			zuo += sen[0];
			sen.erase(0,1);
			cout<<"#"<<zuo<<" & "<<sen<<"#"<<endl;
		}
		else
		{
			int a = map2[zuo[zuo.size()-1]];
			int b = map2[sen[0]];
			if(matrix[a][b] == 1 || matrix[a][b] == 2)
			{
				zuo += sen[0];
				sen.erase(0,1);
				cout<<"#"<<zuo<<" & "<<sen<<"#"<<endl;
			}
			else if(matrix[a][b] == 3)
			{
				while(matrix[a][b] == 3)
				{
					for(int i = 0; i < zuo.size(); i++)
					{
						string temp = zuo.substr(i,zuo.size()-i);
						if(map1[temp])
						{
							zuo.erase(i,zuo.size()-i);
							zuo += map1[temp];
							a = map2[zuo[zuo.size()-1]];
							b = map2[sen[0]];
							cout<<"#"<<zuo<<" & "<<sen<<"#"<<endl;
							break;
						}
					}
				}
			}
		}
	}
	while(zuo.size() != 1)
	{
		for(int i = 0; i < zuo.size(); i++)
		{
			string temp = zuo.substr(i,zuo.size()-i);
			if(map1[temp])
		    {
				zuo.erase(i,zuo.size()-i);
				zuo += map1[temp];
				cout<<"#"<<zuo<<" & "<<sen<<"#"<<endl;
				break;
			}
		}
	}
	return 0;	
}

 

摘 要 编译程序的工作过程通常是词法分析、语法分析、语义分析、代码生成、代码优化。编译程序的这些过程的执行先后就构成了编译程序的逻辑结构,但是这些逻辑结构不一定是按照某一个固定顺序的,也有可能是按照平行或者互锁的方式执行的。 本次课程设计是就简单优先文法原理来做一个简单优先分析器。简单优先分析文法的基本思想是对一个文法按一定的原则求出该文法所有符号包括终结符和非终结符之间的优先关系,按照这种关系确定规约过程中的句柄,它的规约过程实际上是规范规约。在算法实现过程中,主要使用visual C++进行编程。 关键字 简单优先关系 大于关系 小于关系 等于关系 Abstract Compiler of the process is usually lexical analysis, grammar analysis, semantic analysis, code generation, code optimization. Compiler of the implementation of these processes have posed a compiler on the logical structure, but these may not necessarily be in accordance with the logical structure of a fixed order, may also be in accordance with the parallel or interlocking approach to the implementation of the. This course is designed on the simple principle of priority to do a simpegrammar priority Analyzer。Simple grammar priority of the basic idea of a grammar obtained by certain principles of the grammar, including all the symbos at the end and at the end of the non-priority, according to this relationship in the process of determining the statute handler, it is actually the process of regulating the Statute of the Statute. In the algorithm process, the main use of visual C programming. Key Words Simple priority relations Over relations Less than relations Equal relations 目 录 摘要…………………………………………………………………………………………2 一、概述……………………………………………………………………………………4 (一)实验环境与开发工具 ………………………………………………………‥4 (二)简单优先文法简介 ……………………………………………………………4 (三)基本概念 ………………………………………………………………………4 (四)优先关系 ………………………………………………………………………4 二、需求分析 ……………………………………………………………………………5 (一)总体功能需求 …………………………………………………………………5 (二)系统的主要功能函数 …………………………………………………………5 (三)系统的设计思想 ………………………………………………………………6 三、详细设计 ……………………………………………………………………………7 (一)等于关系模块 …………………………………………………………………7 (二)小于关系模块 …………………………………………………………………8 (三)大于关系模块 …………………………………………………………………10 (四)句子识别模块 …………………………………………………………………12 四、测试结果………………………………………………………………………………16 五、小结……………………………………………………………………………………19 六、参考文献 ……………………………………………………………………………20 附录 源程序 ………………
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值