无符号数的词法分析程序
要求:
从键盘上输入一串字符(包括字母、数字等),最后以“;”结束,编写程
序识别出其中的无符号数。
无符号数文法规则可定义如下:
<无符号数>→<无符号实数>│<无符号整数>
<无符号实数>→<无符号整数>.<数字串>[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;
}
}
}
}
}