programming.rar

  • mona25903863
    了解作者
  • Visual C++
    开发工具
  • 3KB
    文件大小
  • rar
    文件格式
  • 0
    收藏次数
  • 1 积分
    下载积分
  • 22
    下载次数
  • 2010-04-15 20:35
    上传日期
编译原理课程设计,内含词法分析器和语法分析器。
programming.rar
  • word.cpp
    3KB
  • grammar.cpp
    9KB
内容介绍
#include <iostream.h> #include <string.h> #include <stdlib.h> #include <fstream.h> char name[50],name1[50],sym[50],symval[50]; int Nlable = 1,Ntemp = 1; ifstream fin; ofstream fout; void L(); char *B(char sym[]); char *E(char sym[]); void L1(char sym[]); char *F1(char sym[],char symval[],ifstream &fin); char *T(char sym[]); char *ItoS(int l); //整型转换为字符 typedef struct { char *next; char *truel; char *falsel; char *lexvel; char *place; char *code; }Node; int next(int &Nlable) { return Nlable; } void gencode1(char *op,char *arg1,char *arg2,ofstream &fout) { fout<<op<<'\t'<<arg1<<'\t'<<arg2<<endl; } void gencode(char *op,char *arg1,ofstream &fout) { fout<<op<<'\t'<<arg1<<endl; } char *newtemp(int &Ntemp) { char *a=ItoS(Ntemp); char b[20]="r"; Ntemp++; return strcat(b,a); } char *newlabel(int &Nlable) { char *a=ItoS(Nlable); char b[20]="L"; Nlable++; return strcat(b,a); } void erro(char str[]) { cout<<str<<endl; return; } void getnext(ifstream &fin,char sym[],char symval[]) { fin rel='nofollow' onclick='return false;'>>sym>>symval; } void s(char sym[],char symval[],ofstream &fout,ifstream &fin) { Node St,S1,S2,Bl; if(strcmp(sym,"{")==0) { getnext(fin,sym,symval); L(); if(strcmp(sym,"}")!=0) { erro("Miss } !"); exit(0); } else { getnext(fin,sym,symval); } } else if(strcmp(sym,"if")==0) { Bl.truel=new char[20]; St.next=new char[20]; Bl.falsel=new char[20]; S1.next=new char[20]; strcpy(Bl.truel,newlabel(Nlable)); strcpy(St.next,newlabel(Nlable)); strcpy(Bl.falsel,St.next); strcpy(S1.next,St.next); getnext(fin,sym,symval); if(strcmp(sym,"(")==0) { getnext(fin,sym,symval); Bl.place=B(sym); } if(strcmp(sym,")")!=0) { erro("Miss ')' !"); exit(0); } getnext(fin,sym,symval); if(strcmp(sym,"then")==0) { getnext(fin,sym,symval); gencode1("CMP",Bl.place,"#1",fout); gencode("CJ=",Bl.truel,fout); streamoff tell=fout.tellp(); gencode("GOTO",Bl.falsel,fout); gencode(Bl.truel,":",fout); s(sym,symval,fout,fin); if(strcmp(sym,"else")==0) { S2.next=new char[20]; strcpy(S2.next,St.next); strcpy(St.next,newlabel(Nlable)); streamoff position2=fout.tellp(); fout.seekp(tell,ios::beg); gencode("GOT0",Bl.falsel,fout); fout.seekp(position2,ios::beg); gencode("GOTO",St.next,fout); gencode(Bl.falsel,":",fout); getnext(fin,sym,symval); s(sym,symval,fout,fin); } } gencode(St.next,":",fout); } else if(strcmp(sym,"while")==0) { Bl.truel=new char[20]; strcpy(Bl.truel,newlabel(Nlable)); St.next=new char[20]; strcpy(St.next,newlabel(Nlable)); Bl.falsel=new char[20]; strcpy(Bl.falsel,St.next); S1.next=new char[20]; strcpy(S1.next,St.next); Bl.place=new char[20]; char *labble=new char[20]; getnext(fin,sym,symval); if(strcmp(sym,"(")==0) { getnext(fin,sym,symval); strcpy(labble,newlabel(Nlable)); gencode(labble,":",fout); strcpy(Bl.place,B(sym)); } if(strcmp(sym,")")!=0) { erro("Miss')'!"); exit(0); } getnext(fin,sym,symval); if(strcmp(sym,"do")==0) { getnext(fin,sym,symval); gencode1("CMP",Bl.place,"#1",fout); gencode("CJ=",Bl.truel,fout); gencode("GOTO",Bl.falsel,fout); gencode(Bl.truel,":",fout); s(sym,symval,fout,fin); gencode("GOTO",labble,fout); } gencode(St.next,":",fout); } else if(strcmp(sym,"ID")==0) { Node El; El.place=new char[20]; strcpy(El.place,symval); getnext(fin,sym,symval); if(strcmp(sym,"=")==0) { getnext(fin,sym,symval); gencode1("MOVE",E(sym),El.place,fout); } } } void L() { s(sym,symval,fout,fin); L1(sym); } char *T1(char sym[]) { Node Bl1,Bl2,Bl; Bl.place=new char[20]; Bl1.place=new char[20]; Bl2.place=new char[20]; strcpy(Bl.place,newtemp(Ntemp)); strcpy(Bl1.place,F1(sym,symval,fin)); gencode1("MOVE",Bl1.place,Bl.place,fout); while(strcmp(sym,"&")==0) { strcpy(Bl2.place,F1(sym,symval,fin)); gencode1("AND",Bl2.place,Bl.place,fout); } return Bl.place; } char *B(char sym[]) { Node Bl,Bl1,Bl2; Bl.place=new char[20]; Bl1.place=new char[20]; Bl2.place=new char[20]; strcpy(Bl.place,newtemp(Ntemp)); strcpy(Bl1.place,T1(sym)); gencode1("MOVE",Bl1.place,Bl.place,fout); while(strcmp(sym,"|")==0) { getnext(fin,sym,symval); Bl2.place=T1(sym); gencode1("OR",Bl2.place,Bl.place,fout); } return Bl.place; } char* E(char sym[]) { Node El,El1; El.place=new char[20]; El1.place=new char[20]; strcpy(El.place,newtemp(Ntemp)); strcpy(El1.place,T(sym)); gencode1("MOVE",El1.place,El.place,fout); while(strcmp(sym,"+")==0||strcmp(sym,"-")==0) { char str[20]; strcpy(str,sym); getnext(fin,sym,symval); strcpy(El1.place,T(sym)); if(strcmp(str,"+")==0) { gencode1("ADD",El1.place,El.place,fout); } else if(strcmp(str,"-")==0) { gencode1("SUB",El1.place,El.place,fout); } } return El.place; } void L1(char sym[]) { if(strcmp(sym,";")==0) { getnext(fin,sym,symval); L(); } else if(strcmp(sym,"}")!=0) { erro("Miss } !"); exit(0); } } char* F1(char sym[],char symval[],ifstream &fin) { Node Bl,El1,El2; Bl.place=new char[20]; El1.place=new char[20]; El2.place=new char[20]; strcpy(Bl.place,newtemp(Ntemp)); char *t=new char[20]; strcpy(t,newtemp(Ntemp)); if(strcmp(sym,"ID")!=0) { getnext(fin,sym,symval); strcpy(El1.place,E(sym)); if(strcmp(sym,"relop")==0) { getnext(fin,sym,symval); strcpy(El2.place,E(sym)); gencode1("MOVE",El1.place,Bl.place,fout); gencode1("MOVE",El1.place,t,fout); gencode1("CMP",t,El2.place,fout); char a[20]="CJ"; strcat(a,sym); gencode(a,"*+8",fout); gencode("GOTO","*+12",fout); gencode1("MOVE","#1",Bl.place,fout); gencode("GOTO","*+8",fout); gencode1("MOVE","#0",Bl.place,fout); } return Bl.place; } else { int len=strlen(sym)+strlen(symval)+3; getnext(fin,sym,symval); if(strcmp(sym,"+")==0||strcmp(sym,"-")==0||strcmp(sym,"*")==0||strcmp(sym,"/")==0||strcmp(sym,"relop")==0) { len=(strlen(sym)+strlen(symval)+1+len); fin.seekg(-len,ios::cur); getnext(fin,sym,symval); strcpy(El1.place,E(sym)); if(strcmp(sym,"relop")==0) { char *str=new char[20]; strcpy(str,symval); getnext(fin,sym,symval); strcpy(El2.place,E(sym)); gencode1("MOVE",El1.place,t,fout); gencode1("CMP",t,El2.place,fout); char a[10]="CJ"; strcat(a,str); gencode(a,"*+8",fout); gencode("GOTO","*+12",fout); gencode1("MOVE","#1",Bl.place,fout); gencode("GOTO","*+8",fout); gencode1("MOVE","#0",Bl.place,fout); } else { erro("Lack of factor !"); } } else { fin.seekg(-len,ios::cur); strcpy(Bl.place,symval); } return Bl.place; } } char *F(char sym[],char symval[]) { Node Fl; Fl.place=new char[20]; if(strcmp(sym,"(")==0) { getnext(fin,sym,symval); strcpy(Fl.place,E(sym)); if(strcmp(sym,")")!=0) { erro("Miss ')'!"); exit(0); } else { getnext(fin,sym,symval); } return Fl.place; } else if(strcmp(sym,"NUM")==0||strcmp(sym,"ID")==0) { Fl.place=new char[strlen(symval)+1]; strcpy(Fl.place,symval); getnext(fin,sym,symval); return Fl.place; } else { erro("Incomplete define!"); exit(0); } } char* T(char sym[]) { Node Fl,Fl1; Fl.place=new char[20]; strcpy(Fl.place,newtemp(Ntemp)); Fl1.place=new char[20]; strcpy(Fl1.place,F(sym,symval)); gencode1("MOVE",Fl1.place,Fl.place,fout); while(strcmp(sym,"*")==0||strcmp(sym,"/")
评论
    相关推荐
    • SLR语法分析器.zip
      使用C语言实现了SLR语法分析,需要的输入文件和代码都在压缩包中
    • Java语法分析器
      Java语法分析器Java语法分析器Java语法分析器Java语法分析器Java语法分析器Java语法分析器Java语法分析器
    • 编译原理 词法语法分析器
      语法分析器: 1 要求使用LL(1)文法 算符优先文法 LR 1 文法三种分析方法之一进行语法分析 2 输入上下文无关文法 输出语法分析程序 3 输入待检测的Token串 输出检测结果 同时可以处理分析中的异常和错误 ">编译...
    • boson:语法分析器生成器
      Boson - 语法分析器生成器 Boson是一个语法分析器生成器(也能生成词法分析器)。采用自有的Boson脚本(Boson Script)来定义语法和词法, Boson根据输入的脚本内容和命令参数生成相应的语法分析器及词法分析器的...
    • 语法分析器
      用C++实现的递归下降语法分析器 欢迎需要的朋友下载
    • 语法分析器
      语法分析器,编译原理,GUI图形界面,MFC
    • 语法分析器
      Java版的简单语法分析器,用Excel作为输入,控制台输出。
    • 语法分析器
      编译原理(第3版)C++实现的语法分析器
    • 词法分析器 语法分析器
      用C#编写的词法分析器和语法分析器,功能基本实现了。实验课上老师让交的成果。不会骗人的。
    • 语法分析器.zip
      c++实现语法分析器,编译原理课成课成实践,