首页 > 其他分享 >编译原理实验

编译原理实验

时间:2022-10-09 17:45:09浏览次数:62  
标签:ch return int ++ Four 编译 实验 youxianj 原理

一、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

相关文章

  • 实验2:Open vSwitch虚拟交换机实践 与 实验3:OpenFlow协议分析实践
    一、实验目的1.能够对OpenvSwitch进行基本操作;2.能够通过命令行终端使用OVS命令操作OpenvSwitch交换机,管理流表;2.能够通过Mininet的Python代码运行OVS命令,控制网络拓扑......
  • SpringBoot启动配置原理
    https://cloud.tencent.com/developer/article/1802822?from=article.detail.1449134#SpringBoot启动配置原理几个重要的事件回调机制配置在META-INF/spring.factori......
  • 软件工程实验
    一、最大子数组//求某数组的最大子数组//最优方法,时间复杂度O(n)//和最大的子序列的第一个元素肯定是正数//因为元素有正有负,因此子序列的最大和一定大于0#include<std......
  • 纠错编码-海明码计算与校验原理
    简单介绍海明码是一种纠错编码,也就是发送海明码给接收端后,如果传输过程出错,接收端根据收到的码的特征,可以判断出是否出错,并且知道如何纠正出错的位(bit)。接下来介绍给出......
  • Python学习实验报告03——序列
    实验要求:完成课本实例部分及实战部分实验内容:Part1实例:实例01:创建一个文件命名为tips,导入日期时间类,定义一个包含七条励志文字的列表,获取当前星期作为索引输出每日一......
  • Cisco PBR 中的 next-hop 多种设置实验
    1:起因在配置PBR中设置next-hop的时候对后面的一些参数不明白,研究清楚,刚好在网上找到这个帖子:​​http://www.doczj.com/doc/f99682692.html​​万分感谢!!!!命令总结:1:setip......
  • 实验4:开源控制器实践——OpenDaylight
    一、实验目的能够独立完成OpenDaylight控制器的安装配置;能够使用Postman工具调用OpenDaylightAPI接口下发流表。二、实验环境Ubuntu20.04Desktopamd64三、实验......
  • 计算机算法设计与分析 实验题 及代码
    很舒服的题目,不难。科班的知识就是舒服。实验2:递归与分治实验目的熟悉递归算法的基本思想和基本步骤,熟练掌握递归公式的推导和定义方法,用递归算法解决实际问题。实验要......
  • 实验4:开源控制器实践——OpenDaylight
    基本要求利用Mininet平台搭建下图所示网络拓扑,并连接OpenDaylight控制器通过Postman工具调用OpenDaylight提供的API下发流表,实现拓扑内主机h1和h3网络中断10s进阶要求......
  • SpringBoot启动原理
    https://cloud.tencent.com/developer/article/1747423?from=article.detail.1449134背景1>大家都知道SpringBoot是通过main函数启动的,这里面跟踪代码到处都没有找到whi......