编译原理:代码生成(pcode),C语言实现

编译原理:代码生成(pcode),C语言实现

部分参考代码及函数:
#include
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include
#include
#include
#include <bits/stdc++.h>
void error() {
printf(“ERROR!\n”);
}
using namespace std;
struct biao { //符号表
string name;
int type;//1是int 2是char 3是fun——int 4是fun——char 5是fun——void 6是数组int 7是数组char
int chang;//是常量。取1 ; 是变量 取0;
int value;//值;如果是char,储存ascii
int ceng;
int sz1;
int sz2;//数组
vectorsz;
bool operator<(const biao &b) const{
if(name!=b.name)
return name < b.name;
return ceng < b.ceng;
}
};
set a={};
set::iterator b,bb;
set::iterator duan[106] = {};
int shican[100][100]={};
int x();int yz();int clsm();void taichangle();void taichangle1();void jinsym(int q);void tiaoguobds();void tiaoguoswitch();void tiaoguo();void tiaoguofor();void rusym();void chuliszzkh1();void chuliszzkh2();void kkj();void chulishuzucl();void chulishuzu1();void chulishuzu2();int String2Int(char str);int lxbsf();int blsm();int csb();int fhyj();int yfhzhsdyyj();int tj();int yj();int bc();int qkb(int abi);int qs();int qkzyj(int abi);int zcsb();int xyj();int fuheyj();int yjl();void getsym();void stos1();void s1tos();void stos2();void s2tos();int cunbl();int cl();int wfhzs();int jfysf();int cfysf();int yfhzhsdy();int wfhzhsdy();int zhs();int zs();char Int2String(int num,char *str);int zfc();

enum symbol {
NOTHING, BSF, ZXCL, ZFCL, ZFC, CONST, INT, CHAR, VOID, MAIN, IF, ELSE,
SWITCH, CASE, DEFAULT, WHILE, FOR, SCANF, PRINTF, RETURN, PLUS, MINU,
MULT, DIV, LSS, LEQ, GRE, GEQ, EQL, NEQ, COLON, ASSIGN, SEMICN, COMMA,
LPARENT, RPARENT, LBRACK, RBRACK, LBRACE, RBRACE
} sym, sym1,sym2;
struct hans{
string name;
symbol symb;
int ll;
string hstoken;
string hslin;
}temp; //每次遇到有返回值函数定义。存
vectorhanshu;
struct dyhs{
symbol symm;
string t;
string l;
int jl;
};//调用语句的栈
vectorhszhan;
struct symzh{
symbol sym3;
char sym3token[106];
char sym3lin[106];
int ll;
}symzhan[106];
FILE* output = fopen(“pcoderesult.txt”, “wb”);
char token[106] = {}, sym1token[106] = {}, sym1lin[106] = {}, sym2token[106]={},sym2lin[106]={},vfun[1006][106] = {};
char lin[106] = {},me[10000006] = {},c;
int l = 0,foryj=0, llll=0,ji = 0, level = 1, sel,bdsz=0, lll = 0,aai=0,i=0,hs=0,switchjl=0,switchkai=0;//当不涉及函数的时候。hs=0;当有返回值函数调用语句,hs=1;无返回值函数调用语句,hs=2;调用完清0;
int szbds=-1,kai=0, shicanceng=-1,tji=0,symzhani=0,yfhzhszhi=0,forzhi=0,ru=0,printflx=0; //赋值语句的时候记录数组中括号,forzhi是对for语句的标点
int shuzu[10][2]={},voidhsreturn=0,switchzhi=0,shuzujl=0,abci=0,tjkai=0,tjjl=0,ifsz[100][2]={},ifszi=0,csi=0;//管理表达式右边预读的开关;记录abc的i的;条件语句的开关;if数组;参数的i
string me_handsome;
char ab[10005],abc[100][10005]={};
char linn[1006]= {},linl[1006]= {};
struct me_shuai {
int select;
int zhi;
char abi;
int you;
} aa[10006],xme[10006];
void addfhb(int flag) {
biao q;
string x;
x = token;
q.name = x;
q.ceng = level;
switch (flag) {
case 1:
q.chang = 1;
q.type = 1; //整形常量
break;
case 2:
q.chang = 1;
q.type = 2; //字符型常量
break;
case 3:
if (sel == 1)
q.type = 3; //函数int
else if (sel == 2)
q.type = 4;//函数char
break;
case 5:
if (sel == 1)
q.type = 1;
else if (sel == 2)
q.type = 2;
break;
case 6:
q.type = 5;
break;
case 7:
if (sel == 1)
q.type = 6;
else if (sel == 2)
q.type = 7;
break;
default:
break;
}
a.insert(q);
}
void addcs(){
biao t;
t.name = token;
t.ceng = level;
while ((b = a.find(t)) == a.end()) {
t.ceng–;
if (t.ceng==-1) {
break;
}
}
t=*b;
t.value=shican[shicanceng][csi++];
a.erase(*b);
a.insert(t);
}
int bldyjcsh() {
if (lxbsf()) {
if (sym == INT) sel = 1;
else sel = 2;
getsym();
if (sym != BSF) error();
strcpy(sym2token,token);
me_handsome=token;
ru++;
rusym();
getsym();
if (sym == ASSIGN) {
jinsym(–symzhani);
addfhb(5);
} else if (sym == LBRACK) {
jinsym(–symzhani);
addfhb(7);
} else error();
getsym();
if (sym == ASSIGN) {
cunbl();
getsym();
cl();
} else if (sym == LBRACK) {
getsym();
chulishuzu2();
wfhzs();
if (sym != RBRACK) error();
getsym();
if (sym != ASSIGN) {
if (sym == LBRACK) {//二维数组
getsym();
chulishuzu1();
wfhzs();
if (sym != RBRACK) error();
getsym();
if (sym != ASSIGN) error();
//cunbl();//数组的变量?
getsym();
if (sym != LBRACE) error();
getsym();
if (sym != LBRACE) error();
getsym();
chulishuzucl();
cl();
while (sym == COMMA) {
getsym();
chulishuzucl();
cl();
}
if (sym != RBRACE) error();
getsym();
while (sym == COMMA) {
getsym();
if (sym != LBRACE) error();
getsym();
chulishuzucl();
cl();
while (sym == COMMA) {
getsym();
chulishuzucl();
cl();
}
if (sym != RBRACE) error();
getsym();
}
if (sym != RBRACE) error();
getsym();
return 0;
} else error();
}
getsym();
if (sym != LBRACE) error();
getsym();
chulishuzucl();
cl();
while (sym == COMMA) {
getsym();
chulishuzucl();
cl();
}
if (sym != RBRACE) error();
getsym();
//
}
} else error();
return 0;
}
int bldywcsh() {
if (lxbsf()) {
if (sym == INT) sel = 1;
else sel = 2;
getsym();
if (sym != BSF) error();
me_handsome=token;
ru++;
rusym();
getsym();
if (sym == LBRACK) {
jinsym(–symzhani);
addfhb(7);
} else {
jinsym(–symzhani);
addfhb(5);
}
getsym();
if (sym == LBRACK) {
getsym();
chulishuzu2();
wfhzs();
if (sym != RBRACK) error();
getsym();
if (sym == LBRACK) {
getsym();
chulishuzu1();
wfhzs();
if (sym != RBRACK) error();
getsym();
}
kkj();
}
if (sym == COMMA) {
while (sym == COMMA) {
getsym();
if (sym != BSF) error();
me_handsome=token;
ru++;
rusym();
getsym();
if (sym == LBRACK) {
jinsym(–symzhani);
addfhb(7);
} else {
jinsym(–symzhani);
addfhb(5);
}
getsym();
if (sym == LBRACK) {
getsym();
chulishuzu2();
wfhzs();
if (sym != RBRACK) error();
getsym();
if (sym == LBRACK) {
getsym();
chulishuzu1();
wfhzs();
if (sym != RBRACK) error();
getsym();
}
kkj();
}
}
}
} else error();
return 0;
}
int bldy() {
int flag = 0;
if (sym != SEMICN) {
while (sym != ASSIGN) {
getsym();
if (sym == SEMICN) {
flag = 1;
break;
}
}
} else
flag = 1;
jinsym(–symzhani);
if (flag) bldywcsh();
else bldyjcsh();
return 0;
}
int blsm() {
int flag = 0;
while (lxbsf()) {
if (sym != INT && sym != CHAR)
break;
ru++;
rusym();
getsym();
getsym();
if ((sym != LBRACK) && (sym != COMMA) && (sym != SEMICN) && (sym != ASSIGN)) {
flag = 1;
break;
}
bldy();
if (sym != SEMICN) error();
getsym();
}
if (flag) {
jinsym(–symzhani);
return 0;
}
return 1;
}
int bds() {
if (jfysf()) {
getsym();
}
x();
while (jfysf()) {
getsym();
x();
}
return 0;
}
int bc() {
wfhzs();
return 0;
}
void chulishuzu1(){
biao z;
biao t;
t.name = me_handsome;
t.ceng = level;
while ((b = a.find(t)) == a.end()) {
t.ceng–;
if (t.ceng==-1) {
break;
}
}
z=*b;
z.sz1=String2Int(token);

a.erase(*b);
a.insert(z);

}
void chulishuzu2(){
biao z;
biao t;
t.name = me_handsome;
t.ceng = level;
while ((b = a.find(t)) == a.end()) {
t.ceng–;
if (t.ceng==-1) {
break;
}
}
z=*b;
z.sz2=String2Int(token);
z.sz1=0;
a.erase(*b);
a.insert(z);
}
int cx() {
if (sym == CONST) {
clsm();
}
if (lxbsf()) {
blsm();
}
while (true) {
if (sym == VOID) {
ru++;
rusym();
getsym();
if (sym == BSF) {
jinsym(–symzhani);
wfhzhsdy();
} else {
jinsym(–symzhani);
break;
}
} else {
yfhzhsdy();
}
}
zhs();
return 0;
}
void catToken() {
strcat(token, &c);
}
void catlin() {
strcat(lin, &c);
}
void clearToken() { //清空token数组
memset(token, ‘\0’, sizeof(token));
memset(lin, ‘\0’, sizeof(lin));
}
int cunbl(){
ru++;
rusym();
int abi=0;
while(true){
getsym();
if(symMINU)
abi=1;
else if(sel
1){
if(symZXCL){
biao t;
t.name = sym2token;
t.ceng = level;
while ((b = a.find(t)) == a.end()) {
t.ceng–;
if (t.ceng
-1) {
break;
}
}
biao z;

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值