算符优先文法详解.doc
文本预览下载声明
编译原理实验代码:
对于任意给定的文法,判断其是否是算符优先文法。
代码如下:
#includeiostream#includestdlib.h#include fstream#define row 50#define col 50#define SIZE 50using namespace std;//两个重要结构体的定义//FIRSTVT表或LASTVT表中一个表项(A,a)结构体的初始化typedef struct{?char nonterm;??????? //非终结符?char term;???????? //终结符}StackElement;??
//存放(A,a)的栈的初始化typedef struct????????????? {?StackElement *top;?StackElement *bottom;?int stacksize;}stack;??//初始化(A,a)栈void InitStack(stack S){?? S.bottom = new StackElement[SIZE];?????????????? ?? if(!S.bottom)? ??? cout存储空间分配失败!endl;????????? ?? S.top = S.bottom;?? S.stacksize = SIZE;}??
//判断(A,a)栈是否为空bool ifEmpty(stack S){?if(S.top==S.bottom) return true;??? //如果栈为空,则返回true?else return false;?????????????? //否则不为空,返回false}???//插入栈顶(A,a)元素void Insert(stack S,StackElement e){?if(S.top-S.bottom=S.stacksize)? cout栈已满,无法插入!endl;?else?{?S.top-nonterm=e.nonterm;?S.top-term=e.term;?S.top++;?}}???//弹出栈顶(A,a)元素StackElement Pop(stack S){?StackElement e;?e.nonterm = \0;?e.term = \0;?if(S.top==S.bottom)?{? cout栈为空,无法进行删除操作!endl;??????? return e;?}?else?{?S.top--;??? e.nonterm=S.top-nonterm;?e.term=S.top-term;?return e;?}}???//终结符与非终结符的判断函数(布尔类型)bool TerminalJud(char c)???? {?if(c=Ac=Z) return false;?? //非终结符返回false??? //else if(c=ac=z) return false;?else return true;?????????????? //终结符返回true}??
//判断非终结符在first表中是否已存在bool ItemJud(char first[][col],int frist_len,char C){?for(int i=0;ifrist_len;i++)?{? if(first[i][0]==C)? ?? return true;???? //如果first表中已存在此非终结符,则返回true?}??? return false;}??
//读文件函数int readfile(char sen[][col]){???? char addr[50];???? cout请输入要读文件的地址(\\用\\\\表示):endl;???? cinaddr;???? ifstream fin;???? fin.open(addr,ios::in);???? if(!fin)???? {????? coutCannot open file!\nendl;???? }???? int i,mm,l,z,k,j;???? char D[100][100];???? for(i=0;!fin.eof();i++)???? {????? finD[i];????? coutD[i]endl;???? }???? k=0;???? //couti?? iiiiendl;???? //coutkendl;???? //for(j=0;ji;j++
显示全部