制度下降算法c语言

复制代码
#include<stdio.h>
#include<string.h>
int location=0;			//遍历字符串的当前位置
char arr[20]="id+id*id#";
void error();		//错误提示函数
/*
每一个非终结符都构造一个函数
*/
void E(char t);			
void Ep(char t);
void T(char t);
void Tp(char t);
void F(char t);

void E(char t){
    if(t=='('){
        printf("E->TE'");
        printf("\n");
        T(arr[location]);
        Ep(arr[location]);
    }else if(t=='i'){
        printf("E->TE'");
        printf("\n");
        T(arr[location]);
        Ep(arr[location]);
    }
    else{
        error();
        location++;
        E(arr[location]);
    }
}
void T(char t){
    if(t=='i'){
        printf("T->FT'");
        printf("\n");
        F(arr[location]);
        Tp(arr[location]);
    }else if(t=='('){
        printf("T->FT'");
        printf("\n");
        F(arr[location]);
        Tp(arr[location]);
    }else{
        error();
        location++;
        T(arr[location]);
    }
}
void Ep(char t){
    if(t=='+'){
        printf("E'->+TE'");
        printf("\n");
        location++;
        T(arr[location]);
        Ep(arr[location]);
    }else if(t==')'){
        printf("E'->&");
        printf("\n");
    }else if(t=='#'){
    }
    else{
        error();
        if(t=='i')
            location+=2;
        else
            location++;
        Ep(arr[location]);
    }
}
void Tp(char t){
    switch(t){
    case '+':
        printf("T'->&");
        printf("\n");
        break;
    case '*':
        printf("T'->*FT'");
        printf("\n");
        location++;
        F(arr[location]);
        Tp(arr[location]);
        break;
    case ')':
        printf("T'->&");
        printf("\n");
        break;
    case '#':
        printf("T'->&");
        printf("\n");
        break;
    default:
        error();
        if(t=='i')
            location+=2;
        else
            location++;
        Tp(arr[location]);
        break;
    }
}
void F(char t){
    if(t=='i'){
        printf("F->id");
        printf("\n");
        location+=2;
    }else if(t=='('){
        printf("F->(E)");
        printf("\n");
    }else{
        error();
        location++;
        F(arr[location]);
    }
}
void error(){
    printf("有一个错误,略过当前词法记号");
    printf("\n");
}
int main(){
    E(arr[location]);
}

运行结果:

相关推荐
wuminyu1 小时前
Kafka中sendfile与mmap实现机制解析
java·linux·c语言·jvm·c++
GreenTea2 小时前
vLLM 与 SGLang KV Cache 底层实现机制深度调研报告
前端·后端·算法
Blockbuater_drug2 小时前
FreeSASA: 开源 SASA 计算库,pip 一条命令替代 NACCESS
c语言·开源·sasa·freesasa·溶剂可及表面积·naccess·python api
Bode_20022 小时前
多资源规划(含生产调度、库存管理及产能规划)的创新优化算法
算法·调度
LaughingZhu3 小时前
Product Hunt 每日热榜 | 2026-09-12
人工智能·深度学习·神经网络·搜索引擎·百度
Logic1013 小时前
C语言/数据结构位运算题解:异或XOR找出蛋糕订单中的“VIP独特口味“——只出现一次的数字
c语言·数据结构·数组·位运算·时间复杂度·算法题·异或性质
土司大王3 小时前
LeetCode hot100——35.搜索插入位置:Java 二分模板、左闭右开区间与插入点分析
java·算法·leetcode
微三云生态系统架构师-彭丹3 小时前
远方好物S2B2C系统架构:一级分销与保证金托管的合规技术实现
人工智能·算法
土司大王3 小时前
LeetCode hot100——34.在排序数组中查找元素的第一个和最后一个位置:Java 二分模板、边界分析
java·算法·leetcode
Logic1014 小时前
C语言/数据结构位运算题解:异或XOR找出飞行棋中的“独特颜色棋子“——只出现一次的数字
c语言·数据结构·数组·位运算·时间复杂度·算法题·异或性质