【编译原理】自下而上分析法 C++

对于文法
E  E + T | E – T | T
T  T * F | T / F | F
F  (E) | i
使用自下而上分析法的一种来进行构造算法
目前学过的自下而上分析法有
1、算符优先分析法(需要先来判断文法是否为算符优先文法)
2、LR(0)分析法
3、SLR(1)分析法
该程序的功能为,给定输入,程序按照先后顺序将使用的产生式输出。
如,输入25.6 * 14.5 + 2(首先经过词法分析,将其转化为 i * i + i),将在规约过程中使用到的产生式依次输出出来。

#include "stdafx.h"
#include <stdio.h>  
#include <malloc.h>  
#include <string>  
struct stack                                 
 {       
   stack *top;       
   char value;       
 };       
 char pop(stack *pst)                        
 {   char e;       
    if(pst->top==pst)      
     {    
     printf("The stack is null.");       
     return 0;    
    }    
    else      
   {       
     e=pst->top->value;       
     pst->top--;       
   return e;       
    }      
 }       
 void push(stack *pst,char e)               
 {       
     pst->top++;       
     pst->top->value=e;       
 }       
 void printstack(stack *pst)                 
{    stack *printtemp=pst;       
     while(printtemp<=(pst->top))       
      {       
          printf("%c",printtemp->value);       
          printtemp++;        
      }         
 }      
 void printstring(stack *pst)               
{     
    stack *printtemp=(pst->top);       
     while(printtemp>=pst)       
      {       
          printf("%c",printtemp->value);       
          printtemp--;        
      }         
 }      
 void printSLR(int &number,stack *status,stack *grammar,stack *string)  
 {     
     printf("%d      ",number);  
     printstack(status);printf("            ");  
     printstack(grammar);printf("            ");  
     printstring(string);printf("           ");  
     number++;  
 }  
int main()  
{     
    stack *string=(stack *)malloc(40);  
    string->top=string;  
    string->top->value='#';  
    stack 
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值