Compiler_词法分析_表驱动法

本文出自:http://blog.csdn.net/svitter
DFA:


使用了表驱动法;


构造的表如下:

表驱动

num.E+/-other
016---
1125--
22-3--
3---4--
45----
55----
62----
7









 


代码如下:
//============================================================================
// Name        : compliler.cpp
// Author      : Vit
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <cstdio>
#include <string.h>
#include <stdlib.h>

using namespace std;

int State[8][5];

//set final accept
const bool Acsta[7] =
{ 0, 1, 1, 0, 0, 1, 0, };

//JudgeNum
int JudgeNum(char &ch)
{
	if (ch >= '0' && ch <= '9')
		return 0;
	else if (ch == '.')
		return 1;
	else if (ch == 'E')
		return 2;
	else if (ch == '+' || ch == '-')
		return 3;
	else
		return 4;
}
//init the table
void init()
{
	//set error state
	for (int i = 0; i < 8; i++)
		for (int j = 0; j < 5; j++)
		{
			State[i][j] = 7;
		}

	//set table
	State[0][0] = 1;
	State[0][1] = 6;
	State[1][0] = 1;
	State[1][1] = 2;
	State[1][2] = 5;
	State[2][0] = 2;
	State[2][2] = 3;
	State[3][3] = 4;
	State[4][0] = 5;
	State[5][0] = 5;
	State[6][0] = 2;
}

//利用函数调用来读
char* Judge(char *str)
{
	int i, j; //work point

	//var
	int len = strlen(str); //计算串长度
	char *t = new char[2000]; //返回串
	memset(t, '\0', 2000);
	int cur; //字符下标
	char ch; //字符
	int state; //状态
	int beg; //开始
	int endd; //结束
	bool change;
	//start
	for (i = 0; i < len; i++)
	{
		beg = cur = i;
		ch = str[i];
		state = 0;
		endd = beg;
		change = false;
		while (state != 7)
		{
			state = State[state][JudgeNum(ch)];
			if (Acsta[state])
			{
				endd = cur; //记录最后一次符合状态的下标
				change = true;
			}
			ch = str[++cur];
			if (change)
			{
				if ((endd - beg) >= strlen(t))
				{
					for (j = beg; j <= endd; j++)
					{
						t[j - beg] = str[j];
					}
				}
				change = false;
			}
		}
	}
	return t;
}

int main(void)
{
	char *t;
	char str[2000];
	init();
	freopen("test", "r", stdin);
	while (~scanf("%s", str))
	{
		t = Judge(str);
		printf("%s\n", t);
	}
	return 0;
}


  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
#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();//确定整型 } }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值