首页 > 其他分享 >编译原理 语法分析器

编译原理 语法分析器

时间:2023-08-21 12:37:11浏览次数:34  
标签:case 10 ch 语法分析 char 编译 state 原理 return


时间:2014-6-9 作者:顾博君

 

/*
 0. Z->S
 1. S->while(E)S
 2. S->E;
 3. S->;
 4. E->i=E
 5. E->T
 6. T->T+P
 7. T->P
 8. P->P*F
 9. P->F
10. F->i
11. F->(E)
12. F->num

--------------------------------------
 |      |
--------------------------------------
 |
--------------------------------------
*/

#include "stdio.h"
#include "conio.h"
#define MAX 140

typedef struct
{
    char left;
    int  length;
} production;

void create_production(production p[13])
{
    p[0].left='Z';//Z->S
    p[0].length=1;
    p[1].left='S';//S->while(E)S
    p[1].length=5;
    p[2].left='S';//S->E;
    p[2].length=2;
    p[3].left='S';//S->;
    p[3].length=1;
    p[4].left='E';//E->i=E
    p[4].length=3;
    p[5].left='E';//E->T
    p[5].length=1;
    p[6].left='T';//T->T+P
    p[6].length=3;
    p[7].left='T';//T->P
    p[7].length=1;
    p[8].left='P';//P->P*F
    p[8].length=3;
    p[9].left='P';//P->F
    p[9].length=1;
    p[10].left='F';//F->i
    p[10].length=1;
    p[11].left='F';//F->(E)
    p[11].length=3;
    p[12].left='F';//F->num
    p[12].length=1;
}
//状态栈
typedef struct
{
    int data[MAX];
    int top;
} state_stack;
//符号栈
typedef struct
{
    char data[MAX];
    int top;
} char_stack;

void init_state_stack(state_stack *st)
{
    st->top=0;
}

void init_char_stack(char_stack *st)
{
    st->top=0;
}

void state_push(state_stack *st,int s)
{
    if(st->top==MAX-1)
    {
        printf("\nstate stack is full!");
        getch();
        exit(0);
    }
    st->data[st->top]=s;
    st->top++;
}

void char_push(char_stack *st,char ch)
{
    if(st->top==MAX-1)
    {
        printf("\nchar stack is full!");
        getch();
        exit(0);
    }
    st->data[st->top]=ch;
    st->top++;
}

void state_pop(state_stack *st)
{
    if(st->top==0)
    {
        printf("\nstack is empty!");
        getch();
        exit(0);
    }
    st->top--;
}

void char_pop(char_stack *st)
{
    if(st->top==0)
    {
        printf("\nstack is empty!");
        getch();
        exit(0);
    }
    st->top--;
}

int read_state(state_stack *st)
{
    return st->data[st->top-1];
}

char read_char(char_stack *st)
{
    return st->data[st->top-1];
}

void error()
{
    printf("\nerror!");
    getch();
    exit(0);
}

int vn_to_int(char ch)
{
    switch(ch)
    {
    case 'S':
        return 0;
    case 'E':
        return 1;
    case 'T':
        return 2;
    case 'P':
        return 3;
    case 'F':
        return 4;
    }
}

int vt_to_int2(char ch)
{
    switch(ch)
    {
    case 4://while
        return 0;
    case 29://[
    case 31://(
    case 33://{
        return 1;
    case 30://]
    case 32://)
    case 34://|
        return 2;
    case 35://;
        return 3;
    case 11://标识符
        return 4;
    case 28://=
        return 5;
    case 13://'+'
    case 14://'-' 
        return 6;
    case 15://'*'
    case 16://'/'
        return 7;
    case 12://num
        return 8;
    case 38://'#'
        return 9;
    default:
        printf("\ncharacter is false!");
        getch();
        exit(0);
    }
    return -1;
}

int vt_to_int(int ch)
{
    switch(ch)
    {
    case 'w'://while
        return 0;
    case '(':
        return 1;
    case ')':
        return 2;
    case ';':
        return 3;
    case 'i':
        return 4;
    case '=':
        return 5;
    case '+':
        return 6;
    case '*':
        return 7;
    case '1'://num
        return 8;
    case '#':
        return 9;
    default:
        printf("\ncharacter is false!");
        getch();
        exit(0);
    }
    return -1;
}
void SLR_driver(state_stack *state,char_stack *input,char action1[25][10],
                int action2[25][10],int SLR_goto[25][5], production p[13] )
{
    int s,k,l,m;
    char c,ch;
    s=read_state(state);
    ch=read_char(input);
    printf("s=%d,ch=%c",s,ch);
    while(1)
    {
        k=vt_to_int(ch);
        c=action1[s][k];
        switch(c)
        {
        case 's':
            state_push(state,action2[s][k]);
            char_pop(input);
            break;
        case 'r':
            m=action2[s][k];
            for(l=p[m].length; l>0; l--)
                state_pop(state);
            state_push(state,SLR_goto[read_state(state)][vn_to_int(p[m].left)]);
            break;
        case 'a':
            printf("\naccept!");
            getch();
            exit(0);
        default:
            error();
        }

        s=read_state(state);
        ch=read_char(input);
        printf("\ns=%d,ch=%c",s,ch);
    }
}



void SLR_driver2(state_stack *state,state_stack *input,char action1[25][10],
                int action2[25][10],int SLR_goto[25][5], production p[13] )
{
    int s,k,l,m,ch;
    char c;
    s=read_state(state);
    ch=read_state(input);
    printf("s=%d,ch=%d",s,ch);
    while(1)
    {
        k=vt_to_int2(ch);
        c=action1[s][k];
        switch(c)
        {
        case 's':
            state_push(state,action2[s][k]);
            state_pop(input);
            break;
        case 'r':
            m=action2[s][k];
            for(l=p[m].length; l>0; l--)
                state_pop(state);
            state_push(state,SLR_goto[read_state(state)][vn_to_int(p[m].left)]);
            break;
        case 'a':
            printf("\naccept!");
            getch();
            exit(0);
        default:
            error();
        }

        s=read_state(state);
        ch=read_state(input);
        printf("\ns=%d,ch=%d",s,ch);
    }
}

main()
{
    char c,ch[MAX];
    int i=0,j;
    state_stack state,Input,t;
    char_stack input;
    char action1[25][10]=
    {
        {'s','s','0','s','s','0','0','0','s','0'},//0
        {'0','0','0','0','0','0','0','0','0','a'},//1
        {'0','s','0','0','0','0','0','0','0','0'},//2
        {'0','0','0','s','0','0','0','0','0','0'},//3
        {'0','0','0','0','0','0','0','0','0','r'},//4
        {'0','0','r','r','0','s','r','r','0','0'},//5
        {'0','0','r','r','0','0','s','0','0','0'},//6
        {'0','0','r','r','0','0','r','s','0','0'},//7
        {'0','0','r','r','0','0','r','r','0','0'},//8
        {'0','s','0','0','s','0','0','0','s','0'},//9
        {'0','0','r','r','0','0','r','r','0','0'},//10
        {'0','s','0','0','s','0','0','0','s','0'},//11
        {'0','0','0','0','0','0','0','0','0','r'},//12
        {'0','s','0','0','s','0','0','0','s','0'},//13
        {'0','s','0','0','s','0','0','0','s','0'},//14
        {'0','s','0','0','s','0','0','0','s','0'},//15
        {'0','0','s','0','0','0','0','0','0','0'},//16
        {'0','0','s','0','0','0','0','0','0','0'},//17
        {'0','0','r','r','0','0','0','0','0','0'},//18
        {'0','0','r','r','0','0','r','s','0','0'},//19
        {'0','0','r','r','0','0','r','r','0','0'},//20
        {'0','0','r','r','0','0','r','r','0','0'},//21
        {'0','0','r','r','0','0','r','r','0','0'},//22
        {'s','s','0','s','s','0','0','0','s','0'},//23
        {'0','0','0','0','0','0','0','0','0','r'}//24
    };
    int action2[25][10]=
    {
        { 2, 9,-1, 4, 5,-1,-1,-1,10,-1},//0
        {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},//1
        {-1,11,-1,-1,-1,-1,-1,-1,-1,-1},//2
        {-1,-1,-1,12,-1,-1,-1,-1,-1,-1},//3
        {-1,-1,-1,-1,-1,-1,-1,-1,-1, 3},//4
        {-1,-1,10,10,-1,13,10,10,-1,-1},//5
        {-1,-1, 5, 5,-1,-1,14,-1,-1,-1},//6
        {-1,-1, 7, 7,-1,-1, 7,15,-1,-1},//7
        {-1,-1, 9, 9,-1,-1, 9, 9,-1,-1},//8
        {-1, 9,-1,-1, 5,-1,-1,-1,10,-1},//9
        {-1,-1,12,12,-1,-1,12,12,-1,-1},//10
        {-1, 9,-1,-1, 5,-1,-1,-1,10,-1},//11
        {-1,-1,-1,-1,-1,-1,-1,-1,-1, 2},//12
        {-1, 9,-1,-1, 5,-1,-1,-1,10,-1},//13
        {-1, 9,-1,-1,20,-1,-1,-1,10,-1},//14
        {-1, 9,-1,-1,20,-1,-1,-1,10,-1},//15
        {-1,-1,22,-1,-1,-1,-1,-1,-1,-1},//16
        {-1,-1,23,-1,-1,-1,-1,-1,-1,-1},//17
        {-1,-1, 4, 4,-1,-1,-1,-1,-1,-1},//18
        {-1,-1, 6, 6,-1,-1, 6,15,-1,-1},//19
        {-1,-1,10,10,-1,-1,10,10,-1,-1},//20
        {-1,-1, 8, 8,-1,-1, 8, 8,-1,-1},//21
        {-1,-1,11,11,-1,-1,11,11,-1,-1},//22
        { 2, 9,-1, 4, 5,-1,-1,-1,10,-1},//23
        {-1,-1,-1,-1,-1,-1,-1,-1,-1, 1}//24
    };
    int SLR_goto[25][5]=
    {
        { 1, 3, 6, 7, 8},//0
        {-1,-1,-1,-1,-1},//1
        {-1,-1,-1,-1,-1},//2
        {-1,-1,-1,-1,-1},//3
        {-1,-1,-1,-1,-1},//4
        {-1,-1,-1,-1,-1},//5
        {-1,-1,-1,-1,-1},//6
        {-1,-1,-1,-1,-1},//7
        {-1,-1,-1,-1,-1},//8
        {-1,16, 6, 7, 8},//9
        {-1,-1,-1,-1,-1},//10
        {-1,17, 6, 7, 8},//11
        {-1,-1,-1,-1,-1},//12
        {-1,18, 6, 7, 8},//13
        {-1,-1,-1,19, 8},//14
        {-1,-1,-1,-1,21},//15
        {-1,-1,-1,-1,-1},//16
        {-1,-1,-1,-1,-1},//17
        {-1,-1,-1,-1,-1},//18
        {-1,-1,-1,-1,-1},//19
        {-1,-1,-1,-1,-1},//20
        {-1,-1,-1,-1,-1},//21
        {-1,-1,-1,-1,-1},//22
        {24, 3, 6, 7, 8},//23
        {-1,-1,-1,-1,-1}//24
    };
    production p[13];

    create_production(p);
    init_char_stack(&input);
    init_state_stack(&state);
    init_state_stack(&Input);
	init_state_stack(&t);
#if 1
    FILE *stream;
    char str[1000];
    int numread,a,b;
    if ((stream = fopen("TokenL.txt","r")) != NULL)
    {
        fscanf(stream,"TokenList:");
            while(fscanf(stream,"\n %d 编码:%d \t字符:%s",&a,&b,str)!=EOF){
                state_push(&t,b);
                //printf("%d\n",read_state(&Input));
            }
        fclose(stream);
        //printf("%s\n***********************\n",str);
        state_push(&state,0);
        while(t.top){
        	if(read_state(&t)!=37)//\n
        		state_push(&Input,read_state(&t));
        	state_pop(&t);
        }
    SLR_driver2(&state,&Input,action1,action2,SLR_goto,p);
    }
    else printf("\n*********Wrong***********\n");
#elif 2
    printf("\ninput a string:");
    scanf("%c",&c);
    while(c!='#')
    {
        ch[i]=c;
        i++;
        scanf("%c",&c);
    }

    ch[i]='#';
    for(j=i; j>=0; j--) printf("%c",ch[j]);
    printf("\n");
    for(j=i; j>=0; j--)
        char_push(&input,ch[j]);
    state_push(&state,0);
    SLR_driver(&state,&input,action1,action2,SLR_goto,p);
#endif
}

 

标签:case,10,ch,语法分析,char,编译,state,原理,return
From: https://blog.51cto.com/u_10101161/7173520

相关文章

  • python刷小红书流量(小眼睛笔记访问量),metrics_report接口,原理及代码,以及x-s签名验证202
    一、什么是小眼睛笔记访问量 如下图所示,为笔记访问量。二、小眼睛笔记访问量接口1、urlhttps://edith.xiaohongshu.com/api/sns/web/v1/note/metrics_report2、payloaddata={"note_id":note_id,"note_type":note_type,"report_type":1,......
  • 《深入浅出Java虚拟机 — JVM原理与实战》带你攻克技术盲区,夯实底层基础 —— 吃透cla
    前言介绍了解Java代码如何编译成字节码并在JVM上执行是非常重要的。这种理解可以帮助我们理解程序执行时发生的情况,确保语言特性符合逻辑,并在进行讨论时能够全面考虑各种因素和副作用。本文将深入探讨Java代码编译成字节码并在JVM上执行的过程。如果您对JVM的内部结构和字节码执行......
  • 高速信号处理处理卡设计原理图:501-基于TMS320C6670的软件无线电核心板
    基于TMS320C6670的软件无线电核心板一、板卡概述     北京太速科技自主研发的TMS320C6670核心板,采用TI KeyStone系列的四核定点/浮点DSP TMS320C6670作主处理器。板卡引出处理器的全部信号引脚,便于客户二次开发,降低了硬件的开发难度和时间成本。板卡满足工......
  • 深入研究高性能数据库连接池的实现原理与优化策略
    在现代的后端应用开发中,数据库连接池是提高性能和可伸缩性的关键组件之一。本文将深入探讨数据库连接池的实现原理,涵盖Java和Python示例,并介绍一些常见的连接池优化策略。数据库连接池的作用数据库连接池是一种维护和管理数据库连接的技术,它通过预先创建一组数据库连接,并将这些连接......
  • 深入理解数据库索引优化策略与原理
    在后端开发领域,数据库索引是优化查询性能的关键因素之一。本文将深入探讨数据库索引的优化策略和原理,重点关注Java与Python开发环境中的实际应用,同时结合Nginx与Elasticsearch等技术,为读者提供深奥的干货内容。1.索引概述与原理数据库索引是一种用于加速数据检索操作的数据结构。......
  • x86_64/aarch64架构下ffpyplayer源码编译
    问题来源:某鱼上挂着pytorch的aarch64架构下的源码编译,遇到某网友提出的要在aarch64架构下的ubuntu上ffpyplayer源码编译,于是有了本文。  =================================================   1.下载源码ffpyplayer源码下载地址:https://github.com/matham/ffpypla......
  • ffpyplayer源码编译报错:ffpyplayer/tools.pyx:182:28: Cannot assign type 'void (*)(
    编译ffpyplayer报错,具体错误如标题。  报错信息:ffpyplayer/tools.pyx:182:28:Cannotassigntype'void(*)(void*,int,constchar*,va_list)except*nogil'to'void(*)(void*,int,constchar*,va_list)noexceptnogil'  解决方法:pipinstallblos......
  • Linux驱动编译方法
    编译内核为什么编译驱动前要编译内核?编译驱动的内核要和开发板上的内核一致。因为开发板出厂时预烧录了一个内核,但自己在ubuntu编译是使用的是自己的内核,二者不一致时会导致导入驱动模块时出现问题(如内核污染提示)。内核编译的步骤下面记录内核编译步骤是对应IMX6ULLPRO开......
  • 【深度学习 | CNN】“深入解析卷积神经网络与反卷积:从生活案例到原理的全面指南” (从
    ......
  • POP3协议的历史及其工作原理
    POP3,全名为“PostOfficeProtocol-Version3”,即“邮局协议版本3”。是TCP/IP协议族中的一员,由RFC1939定义。POP3的具体历史可以追溯到1984年,由J.K.Reynolds带领的团队研发出了POP3协议的前身,POP1和POP2。到了1998年,POP3成为Internet标准,并持续发展和改进。虽然POP4曾被提出,但......