📄 compiler.cpp
字号:
#include "head.h"
WORD word[BUFLINE];//单词量不能大于256
stateStack _stack;//状态栈
int G_table[20][8];//文法矩阵
fourexp *fourexpHead;//生成四元式链的头指针
Vn vn_stack[STACKSIZE];//生成四元式时用的非终结符栈
int vn_stack_top = 0;//非终结符的栈顶
void add_fourexp(){//四元式前面的计算地址
int num = 0 ;
fourexp * i = fourexpHead ;
while( i ){
i->address = num ;
num++ ;
i = i->next;
}
i = fourexpHead;
while( i ){
if( i->isadd == true ){
i->temp = i->chain->address+1;
}
i = i->next;
}
}//地址计算
void pop_vn(int i){//出栈
vn_stack_top -= i;
}
void push_vn(Vn * vn_stack,Vn *s){//入栈
vn_stack[vn_stack_top] = *s;
vn_stack_top++;
}
void push(int n){//入栈
if(_stack.top==STACKSIZE) {cout<<"语法分析栈满!"<<endl;exit(0);}
_stack.stack[_stack.top]=n;
_stack.top++;
}
void stackinit(){//状态栈初始化
_stack.top = 0;
}
int pop(){//出栈
if(_stack.top==0){cout<<"语法分析栈空!"<<endl;exit(0);}
_stack.top--;
return _stack.stack[_stack.top];
}
int gettop(){//取栈顶元素
if(_stack.top==0){cout<<"语法分析栈空!"<<endl;exit(0);}
return _stack.stack[_stack.top-1];
}
int isEmpty(stateStack stack){//判断栈是否为空
if(stack.top==0) return 1;
else return 0;
}
int string_int(const char *str)//类型转换
{
int num, i;
num = *str - '0';
i = 1;
while (str[i] && isdigit(str[i]))
num = num * 10 + str[i++] - '0';
return num;
}
/*
char* int_string(int i){
int ran = i;
int isNeg = i<0?1:0; //检查是否为负数
if(isNeg) ran *= -1; //输入负数时取相反数
int bits = 0; //记录共有几位数字
int numBits[10]; //因为int的范围在-2147483648和2147483647之间,
//不超过十位,所以数组长度取10
do //把数字的各个位拆开
{
numBits[bits] = ran%10;
ran = (ran-numBits[bits])/10;
bits++;
}
while(ran!=0);
char* str = new char[bits+isNeg+1]; //str是返回字符串的指针
str[0] = '-'; //负数的时候第一位保存负号
str[bits+isNeg] = '\0'; //字符串的结尾
for(int ii=isNeg;ii<bits+isNeg;ii++) //转换成字符串保存到str
{
str[ii] = (char)(numBits[bits-(ii-isNeg)-1] + '0');
}
return str;
}
*/
int getLine(FILE* fp){ //获取文件行数
FILE* p = fp;
int line_Counter = 0;
if(!p) { cout<<"空文件!"<<endl; exit(0);}
while(!feof(p))
{
//cout<<(char)fgetc(p)<<"";
if((char)fgetc(p)=='\n') line_Counter++;
}
return line_Counter+1;
}
void error()
{
cout<<"词法错误,标志符不能以数字开头"<<endl;
exit(0);
}
void error1()
{
cout<<"语法错误,输入正确句子"<<endl;
exit(0);
}
int lex(char * buf)//词法分析程序
{
int i=0,j=0;
int n=0;
char tmp_name[10];
while(buf[i]!='\0')
{
if(buf[i]==' '||buf[i]=='\n')//滤空
i++;
else if(isalpha(buf[i])||buf[i]=='_')//标志符判断
{
tmp_name[j]=buf[i];
i++;
while(isalnum(buf[i]))
{
j++;
tmp_name[j]=buf[i];
i++;
}
memcpy(word[n].name,tmp_name,j+1);
word[n].name[j+1]='\0';
if(strcmp(word[n].name,"if")==0) { word[n].id=IF; strcpy(word[n].name,"if");}
else if(strcmp(word[n].name,"then")==0){ word[n].id=THEN; strcpy(word[n].name,"then");}
else if(strcmp(word[n].name,"else")==0){ word[n].id=ELSE; strcpy(word[n].name,"else");}
else if(strcmp(word[n].name,"while")==0){ word[n].id=WHILE; strcpy(word[n].name,"while");}
else if(strcmp(word[n].name,"do")==0){ word[n].id=DO; strcpy(word[n].name,"do");}
else if(strcmp(word[n].name,"begin")==0){ word[n].id=BEGIN; strcpy(word[n].name,"begin");}
else if(strcmp(word[n].name,"end")==0){ word[n].id=END; strcpy(word[n].name,"end");}
else if(strcmp(word[n].name,"true")==0) { word[n].id = TRUE; word[n].value = 1; strcpy(word[n].name,"true"); }
else if(strcmp(word[n].name,"false")==0){ word[n].id = FALSE;word[n].value = 0; strcpy(word[n].name,"false");}
else{ word[n].id=I; word[n].value =0;}//标识符
n++;
j=0;
}// endif
else if(isdigit(buf[i]))//数字判断
{
while(isdigit(buf[i])){
tmp_name[j]=buf[i];
i++;
j++;
}
if(isalpha(buf[i])) error();
memcpy(word[n].name,tmp_name,j+1);
word[n].name[j]='\0';
word[n].id = CONST;
word[n].value = string_int(word[n].name);
n++;
j=0;
}
else//运算符,界符判断
{
switch(buf[i])
{
case ':' ://赋值
if(buf[i+1] == '='){
word[n].id = EVALUATE;
// word[n].type = 1;
strcpy(word[n].name,":=");
n++;
}
else error();
break;
case ';':
word[n].id =SEMICOLON;
// word[n].type = 4;
strcpy(word[n].name,";");
n++;
break;
case '&':
word[n].id = AND;
// word[n].type = 1;
strcpy(word[n].name,"&");
n++;
break;
case '|':
word[n].id = OR;
// word[n].type = 1;
strcpy(word[n].name,"|");
n++;
break;
case '>':
word[n].id = GREAT;
// word[n].type = 1;
strcpy(word[n].name,">");
n++;
break;
case '<':
word[n].id =LOW ;
// word[n].type = 1;
strcpy(word[n].name,"<");
n++;
break;
case '=':
if(buf[i-1] != ':'){
word[n].id = EQUAL;
// word[n].type = 1;
strcpy(word[n].name,"=");
n++;
}
break;
case '+':
word[n].id = PLUS;
// word[n].type = 1;
strcpy(word[n].name,"+");
n++;
break;
case '*':
word[n].id = MULTIPLY;
// word[n].type = 1;
strcpy(word[n].name,"*");
n++;
break;
case '(':
word[n].id = LEFT;
// word[n].type = 4;
strcpy(word[n].name,"(");
n++;
break;
case ')':
word[n].id = RIGHT;
// word[n].type = 4;
strcpy(word[n].name,")");
n++;
break;
default :
if(buf[i] == '#') goto lv;
cout<<"有非法字符";
exit(0);
}
i++;
}
}
lv:
strcpy(word[n].name,"#");
// word[n].type = 4;
word[n].id=ACC;
return n;//单词符号个数
}//词法分析
void G_init()//初始化文法矩阵
{
for(int i=0;i<20;i++)
for(int j=0;j<8;j++)
G_table[i][j]=-1;
G_table[0][0]=27;G_table[0][1]=23; //初始化文法表
G_table[1][0]=23;G_table[1][1]=0;G_table[1][2]=25;G_table[1][3]=1;G_table[1][4]=23;G_table[1][5]=2;G_table[1][6]=23;
G_table[2][0]=23;G_table[2][1]=0;G_table[2][2]=25;G_table[2][3]=1;G_table[2][4]=23;
G_table[3][0]=23;G_table[3][1]=3;G_table[3][2]=25;G_table[3][3]=4;G_table[3][4]=23;
G_table[4][0]=23;G_table[4][1]=5;G_table[4][2]=24;G_table[4][3]=6;
G_table[5][0]=23;G_table[5][1]=7;G_table[5][2]=8;G_table[5][3]=26;
G_table[6][0]=24;G_table[6][1]=23;G_table[6][2]=9;G_table[6][3]=24;
G_table[7][0]=24;G_table[7][1]=23;
G_table[8][0]=25;G_table[8][1]=25;G_table[8][2]=10;G_table[8][3]=25;
G_table[9][0]=25;G_table[9][1]=25;G_table[9][2]=11;G_table[9][3]=25;
G_table[10][0]=25;G_table[10][1]=7;G_table[10][2]=12;G_table[10][3]=7;
G_table[11][0]=25;G_table[11][1]=7;G_table[11][2]=13;G_table[11][3]=7;
G_table[12][0]=25;G_table[12][1]=7;G_table[12][2]=14;G_table[12][3]=7;
G_table[13][0]=25;G_table[13][1]=15;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -