编译原理实验——无符号数的词法分析程序(C++实现)

无符号数的词法分析程序

要求:
从键盘上输入一串字符(包括字母、数字等),最后以“;”结束,编写程
序识别出其中的无符号数。

无符号数文法规则可定义如下:
<无符号数>→<无符号实数>│<无符号整数>
<无符号实数>→<无符号整数>.<数字串>[E<比例因子>]│
<无符号整数>E<比例因子>
<比例因子>→<有符号整数>
<有符号整数>→[+│-]<无符号整数>
<无符号整数>→<数字串>
<数字串>→<数字>{<数字>}
<数字>→0 1 2 3… 9

运行结果如图:

#include <bits/stdc++.h>
 using namespace std;

 const int N  = 100;
 char ch[N];
 int cnt;
 void unsigned_number();
 int float_number(int b);
 int _E(int b);
 int main()
 {
	cnt = 0;
	cout<<"请输入字符串并以‘;’结束"<<endl; 
	for(;;)
	{
		cin>>ch[cnt];
		if(ch[cnt]==';') break;
		cnt++;
	}
	unsigned_number();

	return 0;
	
 }

 void unsigned_number()
 {
	bool isfirst = true;
	//hh表示识别到的无符号实数的第一个数,tt表述最后一个数
	int hh,tt;
	for(int i = 0; i <= cnt; i++)
	{
		if(isfirst)
		{
			if(isdigit(ch[i]))
			{isfirst = false;hh = i;} 
			else continue;
		}
		else
		{
			if(isdigit(ch[i])) continue;
			else if(ch[i]=='.') 
			{	
				//输出浮点数或整数或浮点数的科学计数表示
				tt = float_number(i);
				for(int j = hh; j < tt; j++) printf("%c",ch[j]);
				printf("\n");
				isfirst = true;
				i = tt;
			}
			else if(ch[i]=='E') 
			{	
				//输出整数或整数的科学计数表示
				tt = _E(i);
				for(int j = hh; j < tt; j++) printf("%c",ch[j]);
				printf("\n");
				isfirst = true;
				i = tt;
			}
			else
			{ 		
				//输出整数	
				tt = i; 
				for(int j = hh; j < tt; j++) printf("%c",ch[j]);
				printf("\n");
				isfirst = true;
				
			}
		}
	}
}

int float_number(int i)
{
	//设置flag来判断是不是小数点后第一个字符
	bool flag = true;
	int t;
	for(int j = i+1; j <= cnt; j++)
	{
		if(flag)
		{
			flag = false;
			//小数点'.'后第一个字符是数字则继续否则返回'.'的下标
			if(isdigit(ch[j])) continue;
			else{t = i; return t;} 
		}
		else
		{
			if(isdigit(ch[j])) continue;
			//遇到'E',通过调用函数_E()来得到要返回的下标
			else if(ch[j] == 'E') 
			{
				t = _E(j);
				return t;
			}
			else
			{
				t = j;
				return t;
			}
		}
		
	}
}

int _E(int i)
{
	//设置flag来判断是不是'E'后的第一个字符,设置flag2来判断是不是'E'后的第二个字符
	//通过这两个标志来判断字符E之后字符的合法性
	bool flag = true;
	bool flag2 = true;
	int t;
	for(int j = i + 1; j < cnt; j++)
	{
		if(flag)
		{
			flag = false;
			//字符'E'之后是'+'或'-'则继续 否则返回字符'E'的下标
			if(ch[j] == '+'||ch[j] == '-') continue;
			else
			{
				t = i;
				return t;
			}
		}
		else
		{
			if(flag2)
			{
				flag2 = false;
				//如果'E'后面的第二个字符是数字则继续,否则返回'E'的下标
				if(isdigit(ch[j])) continue;
				else 
				{
					t = i;
					return t;
				}
			}
			else
			{
				if(isdigit(ch[j])) continue;
				else
				{
					t = j;
					return j;
				}
				
			}
			
		}
		
		
	}
}

#include<iostream> #include<cctype> #include<cstring> #include<cmath> using namespace std; int w=0; //尾累加器 int p=0; //指累加器 int j=0; //十进制小器 int e=1; //用来记录十进制的符号,当指为正时为1,为负时为-1 int i=0; //用来标志元素位置 int d=0; //用来表示每个值型元素对应的值 const int N=40;//用来确定输入识别符的最大长度 char data[N];//存放输入的识别符 bool is_digit; //标志是否是字 string CJ1;//确定是整形还是实型 double CJ2;//记值 //函声明 void check(char c);//检查首字母是否是字的函 void deal_integer(char c);//处理识别符的整部分 void deal_point(char c);//用来处理小部分 void deal_index(char c);//用来处理指部分 void s_next();// 确定实型 void z_next();//确定整型 void last();// 计算 CJ2 void error();//程序中错误处理程序 void deal();//处理函主体 int main(){ //主函 cout<<"please input your data,and its maximum length is "<<N<<":"<<endl;//等待用户输入识别符 cin>>data; deal();//处理函主体 last();// 计算 CJ2 system("pause"); return 0; } void check(char c) //判断输入的首字母是否是字 { is_digit=isdigit(c); while(is_digit!=true){//输入的首字母不是字时 cout<<"\nError! Try again.."<<endl;//要求重新输入 cin>>data; check(data[0]); } } void deal_integer(char c){//处理识别符的整部分 d=(int)c-48; w=w*10+d; i++; if(isdigit(data[i])!=0)//下一个仍是值时,调用程序本身 deal_integer(data[i]); } void deal_point(char c){//用来处理小部分 int temp=i; if(isdigit(c)!=0)//是值字符时 deal_integer(c); else { error(); //错误处理程序 deal();//处理函主体 } j=i-temp;//记录十进制小 } void deal_index(char c){//用来处理指部分 if(c=='-') {e=-1;i++;}//是'-'号时 else {if(c=='+') i++;//是'+' 号时 else { if(isdigit(c)==false) //非值字符时 { error();//错误处理程序 deal();//处理函主体 } else { d=(int)c-48;//把输入字符转换为整型 goto pro2;} } } if(isdigit(data[i])!=0) pro1: d=(int)(data[i])-48; pro2: p=p*10+d; i++; if(isdigit(data[i])!=0)//是值字符时 goto pro1; else if(data[i]!='\0'){//非结束标志 error();//错误处理程序 deal();//处理函主体 } else s_next(); // 确定实型 } void s_next(){// 确定实型 i--;//退一个字符 CJ1="实型"; } void z_next(){//确定整型 i--;//退一个字符 CJ1="整型"; } void last(){// 计算 CJ2 CJ2=w*pow((double)10,e*p-j); cout<<CJ1<<": "<<CJ2<<endl;//输出 } void error(){//程序中错误处理程序 cout<<"\nError! Try again.."<<endl;//重新输入据 cin>>data; p=0;w=0;j=0; //所有全局变量重新初始化 e=1;i=0;d=0; //exit(0); } void deal(){ check(data[0]);//判断输入的首字母是否是字 deal_integer(data[i]);//处理识别符的整部分 if(data[i]=='.') { deal_point(data[++i]);//用来处理小部分 if(data[i]=='e'||data[i]=='E')//如果是e或E时 deal_index(data[++i]);//用来处理指部分 else if(data[i]!='\0') { error();//错误处理程序 deal();//处理函主体 } else s_next();// 确定实型 } else { if(data[i]=='e'||data[i]=='E')//如果是e或E时 { deal_index(data[++i]);//用来处理指部分 //CJ1="整型"; } else if(data[i]!='\0'){ //非结束标志 error();//错误处理程序 deal();//处理函主体 } else z_next();//确定整型 } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值