一、NFA到DFA
#include <stdio.h>
#include <fstream>
#include <cstring>
#include <set>
#include <malloc.h>
#include <math.h>
#include <iostream>
#define JIEDIANMAX 50//结点最大数量
using namespace std;
typedef char JIEDIANZHI;//结点为字符类型
char BUFFER[512];//读文件内容时,程序缓冲数组
int NUMBER,CHANGDU;//NUMBER,记录有穷字母表元素的个数,CHANGDU,记录States数组的长度
typedef struct ZHANGTTUB//状态图的边信息
{ int adjvex,quanzhong;//quanzhong,边所对应的权值
struct ZHANGTTUB *next;
}ZHANGTTUB;
typedef struct JIEDIANLX//图的结点类型定义
{ JIEDIANZHI data;
ZHANGTTUB *next;
}JIEDIANLX,adjlist[JIEDIANMAX];
typedef struct ZHANGTTU//图的定义
{ adjlist a;
int vexnum, arcnum;
}ZHANGTTU;
typedef struct ZHUANGT//状态的定义
{ set<int> Set;
char name;
}ZHUANGT;
ZHUANGT States[JIEDIANMAX];
int ZHUANHUAN(ZHANGTTU g, char p)//图中结点转化为对应下标
{ int i;
for (i = 0; i<g.vexnum; i++)
{if (p == g.a[i].data)
return i; }
return -1;
}
void CHUANGJIAN(ZHANGTTU &g, FILE *fpr)
{ int line = 0;
while (!feof(fpr))
{ fgets(BUFFER, 512, fpr);
if (strlen(BUFFER)>1)//获取图结点个数
{ int i = 0;
while (BUFFER[i] == ' ')
{ i++; }
g.a[line].data = BUFFER[i];
g.a[line].next = NULL;
line++;
}
}
g.vexnum = line;
rewind(fpr);//将文件指针返回到开头
line = 0;
ZHANGTTUB *s;
while (!feof(fpr))//添加文件中边的信息并构造图
{ int quanzhong = 0;//边的权值,都从0开始
fgets(BUFFER, 512, fpr);
if (strlen(BUFFER)>1)
{ for (int i = 0; i<strlen(BUFFER) - 1; i++)
{ if (BUFFER[i] == '{')
{ quanzhong++;
if (NUMBER<quanzhong)
NUMBER = quanzhong;
i++;
if (BUFFER[i] == 'N')
i = i + 4;
while (BUFFER[i] != '}')
{if (BUFFER[i] != ',')
{int x = ZHUANHUAN(g, BUFFER[i]);
s = (ZHANGTTUB *)malloc(sizeof(ZHANGTTUB));
s->adjvex = x;
s->quanzhong = quanzhong;
s->next = g.a[line].next;
g.a[line].next = s;}
i++;
}
}
}
line++;
}
}
}
ZHUANGT move(ZHUANGT s, int n, ZHANGTTU g)//求某种状态识别某些字母后的状态集合
{ ZHUANGT temp;
ZHANGTTUB *m;
set<int>::iterator itr;//迭代器
for (itr = s.Set.begin(); itr != s.Set.end(); itr++)//遍历当前状态中集合元素
{ int i = *itr;
m = g.a[i].next;
while (m)
{ if (m->quanzhong == n)
{ temp.Set.insert(m->adjvex); }
m = m->next; }
}
return temp;
}
int BIJIAOZT(ZHUANGT m, ZHUANGT n)//两个状态集合内容是否相等
{ int flag = 1;
if (m.Set.size() != n.Set.size())
{ flag = 0;
return flag; }
set<int>::iterator itrm;
set<int>::iterator itrn;
for (itrm = m.Set.begin(), itrn = n.Set.begin(); itrm != m.Set.end(); itrm++, itrn++)
{ int m = *itrm;
int n = *itrn;
if (m != n)
{ flag = 0; break; }
}
return flag;
}
void XUNZHAOGAIMING(ZHUANGT &s)//目前状态是否存于States数组中,存在则改名;不存在,则改名并加入States数组
{ int flag = 0;
if (CHANGDU == 0)
{ States[0] = s;
States[0].name = 'A';
CHANGDU++; }
else
{ for (int i = 0; i<CHANGDU; i++)
{ if (BIJIAOZT(States[i], s) == 1)//存在,改名
{ s.name = States[i].name;
flag = 1;
break; }
}
if (flag == 0)//不存在,改名并加入States数组
{ s.name = States[CHANGDU - 1].name + 1;
States[CHANGDU] = s;
CHANGDU++; }
}
}
void ZHUANHUANCW(ZHANGTTU g, FILE *fpw)
{ ZHUANGT s, temp;
s.Set.insert(0);
s.name = 'A';
XUNZHAOGAIMING(s);
for (int i = 0; i<CHANGDU; i++)
{ cout << "{";
fprintf(fpw, "{");
int t = 0;
set<int>::iterator itr;//迭代器
for (itr = States[i].Set.begin(); itr != States[i].Set.end(); itr++)//遍历当前s状态中集合元素
{ t++;
int i = *itr;
if (t == 2)
{ cout <<","<<g.a[i].data << ",";
fprintf(fpw, ",%c,", g.a[i].data); }
else if (t == 1)
{ cout << g.a[i].data <<"";
fprintf(fpw, "%c", g.a[i].data); }
else
{ cout << g.a[i].data << ",";
fprintf(fpw, "%c,", g.a[i].data); }
}
cout << "}";
fprintf(fpw, "}");
cout << States[i].name;
fprintf(fpw, "%c", States[i].name);
for (int j = 1; j <= NUMBER; j++)
{ temp = move(States[i], j, g);
XUNZHAOGAIMING(temp);
cout << temp.name;
fprintf(fpw, "%c", temp.name); }
cout << endl;
fprintf(fpw, "\n");
}
}
int main()
{ ZHANGTTU g;
FILE *fpr = fopen("test.txt", "r");
FILE *fpw = fopen("output.txt", "w");
CHUANGJIAN(g, fpr);
ZHUANHUANCW(g, fpw);
return 0;
}
二、词法分析器
#include<stdio.h>
#include<string.h>
#include <stdlib.h>
#define M 100
#define N 1000
int E(char*p, int n, int *youxianj);//优先级
int F(char*p, int n, int *youxianj)
{
if (n == -1)
{
return n;
}
char ch = p[n];
if (ch >= 'A'&&ch <= 'z')
{
youxianj[n] = 0; n++; return n;
}
else if (ch == '(' || ch == ')')
{
if (ch == '(')
{
youxianj[n] = 3; n++;
}
n = E(p, n, youxianj);
ch = p[n];
if (ch == ')')
{
youxianj[n] = 3; n++;
}
return n;
}
else
{
printf("语法错误!\n");
printf("出错位置:%d", n); n = -1; return n;
}
}
int T0(char*p, int n, int *youxianj)
{
if (n == -1)
{
return n;
}
char ch = p[n];
if (ch == '*')
{
youxianj[n] = 2;
n++;
n = F(p, n, youxianj);
n = T0(p, n, youxianj);
}
else if (ch == '/')
{
youxianj[n] = 2;
n++;
n = F(p, n, youxianj);
n = T0(p, n, youxianj);
}
else
{
return n;
}
}
int T(char*p, int n, int *youxianj)
{
if (n == -1)
{
return n;
}
char ch;
ch = p[n];
n = F(p, n, youxianj);
n = T0(p, n, youxianj);
return n;
}
int E0(char*p, int n, int *youxianj)
{
if (n == -1)
{
return n;
}
char ch = p[n];
if (ch == '+')
{
youxianj[n] = 1;
n++;
n = T(p, n, youxianj);
n = E0(p, n, youxianj);
}
else if (ch == '-')
{
youxianj[n] = 1;
n++;
n = T(p, n, youxianj);
n = E0(p, n, youxianj);
}
else
{
return n;
}
}
int E(char*p, int n, int *youxianj)//优先级
{
if (n == -1)
{
return n;
}
n = T(p, n, youxianj);
n = E0(p, n, youxianj);
return n;
}
int main()
{
char *p, c, *str, ch[M];
int n = 3, num = 0, i = 0, j = 1, youxianj[M];//优先级
youxianj[0] = youxianj[1] = youxianj[2] = 0;
p = NULL;
str = NULL;
str = (char *)malloc(N*sizeof(char));
scanf_s("%s", &ch);
num = strlen(ch);
p = ch;
n = E(p, n, youxianj);//优先级第一次使用
if (n == num)
{
int i = 0, j, q, b, d, t = 1, best = 0;//best为最高优先级等级,t用于记录当前序号
char Four[M];
best = youxianj[0];
int newbest = 0;
for (i; i<n; i++)
{
Four[i] = p[i];
if (best<youxianj[i])
best = youxianj[i];
}
int k = best + 1;
for (i = 0; i<k; i++)
{
if (newbest != 0)
{
best = newbest; newbest = 0;
}
if (best == 0) break;
for (j = 3; j<n; j++)
{
b = d = 0;
if (youxianj[j] == 3)
{
for (q = j - 1; q >= 0; q--)
{
if (youxianj[q] == 3)
{
b = 1; break;
}
}
int a = q + 1;
if (b == 1)
{
while (a>q&&a<j)
{
if (youxianj[a] != 0)
{
d = 1; break;
}
a++;
}
if (d == 0)
{
youxianj[q] = youxianj[j] = 0;
Four[q] = Four[j] = Four[j - 1];
if (j == n - 1)
{
if (newbest < youxianj[q - 1]) newbest = youxianj[q - 1];
}
else
{
if (newbest = youxianj[j + 1]) newbest = youxianj[j + 1];
}
}
}
}
if (youxianj[j] == best)
{
if (youxianj[j - 1] == 0 && youxianj[j + 1] == 0)
{
printf("T%d:=", t);
if (Four[j - 1] >= 0 && Four[j - 1] <= 9)
{
printf("T%d", Four[j - 1]);
}
else
{
printf("%c", Four[j - 1]);
}
if (Four[j] >= 0 && Four[j] <= 9)
{
printf("T%d", Four[j]);
}
else
{
printf("%c", Four[j]);
}
if (Four[j + 1] >= 0 && Four[j + 1] <= 9)
{
printf("T%d", Four[j + 1]);
}
else
{
printf("%c", Four[j + 1]);
}
printf(" (");
if (Four[j] >= 0 && Four[j] <= 9)
{
printf("T%d,", Four[j]);
}
else
{
printf("%c,", Four[j]);
}
if (Four[j - 1] >= 0 && Four[j - 1] <= 9)
{
printf("T%d,", Four[j - 1]);
}
else
{
printf("%c,", Four[j - 1]);
}
if (Four[j + 1] >= 0 && Four[j + 1] <= 9)
{
printf("T%d,", Four[j + 1]);
}
else
{
printf("%c,", Four[j + 1]);
}
printf("T%d)\n", t);
youxianj[j - 1] = youxianj[j] = youxianj[j + 1] = 0;
Four[j - 1] = Four[j] = Four[j + 1] = t;
int r = j - 1;
while ((Four[r] > 0 && Four[r] <= 9))
{
Four[r] = t; r--;
}
r = j - 1;
while ((Four[r] > 0 && Four[r] <= 9))
{
Four[r] = t; r++;
}//两个while用于将相连的已处理的字符赋值成相同的数字
t++;
}
}
}
best--;
}
printf("x:=T%d (:=,T%d,-,x)", t - 1, t - 1);
}
else
{
return 0;
}
return 0;
}
标签:ch,return,int,++,Four,编译,实验,youxianj,原理 From: https://www.cnblogs.com/xjlnl/p/16773019.html