📄 sal.cpp
字号:
char *e1_place = new char[8];
memset(e1_place,0,8);
strcpy(e1_place,t());
//printf("%s",e1_place);
//sprintf(e1_place,"%c", t());
while (DataArray[RecursionIndex]->codeID ==5 || DataArray[RecursionIndex]->codeID==6){ //=='+' || '-'
char sign[2];
memset(sign,'\0',2);
strcpy(sign,DataArray[RecursionIndex++]->value);
char *e2_place=new char[8];
memset(e2_place,'\0',8);
strcpy(e2_place,t());
char *buff=new char[10];
memset(buff,'\0',10);
sprintf(buff,"#ADD_%d",FourGroupIndex++);
createFourGroup(sign,e1_place,e2_place,buff); //产生四元组
strcpy(e1_place,buff);
}
return e1_place;
}
//***************************************/
//函数名:GrammaAnalize */
//作 用:语法分析 */
//参 数:无 */
//反回值:正确返回-1,错误返回错误索引 */
//***************************************/
int GrammaAnalize(){
int index=0;
while(DataArray[index]->codeID != 0){ //!='.'
if(index>=(DA_index-1)) return index;
if(DataArray[index]->codeID == 1){ //==const
if(DA_index < index+5) return index;
while(DataArray[index]->codeID != 12){
if(DataArray[index+1]->codeID==15 && DataArray[index+2]->codeID==9 && DataArray[index+3]->codeID==16){
if(DataArray[index+4]->codeID == 11 || DataArray[index+4]->codeID == 12) { //==','||==';'
//记录四元组
createFourGroup(DataArray[index+2]->value,DataArray[index+3]->value,"",DataArray[index+1]->value);
index=index+4;
continue;
}else{
return index+4;
}
}else{
return index;
}
}
index++;
continue;
}
if(DataArray[index]->codeID==2){ //=="var"
if(DA_index < index+3) return DA_index-1;
while(DataArray[index]->codeID !=12){
if(DataArray[index+1]->codeID == 15){
if(DataArray[index+2]->codeID == 11 || DataArray[index+2]->codeID == 12){
createFourGroup("","","",DataArray[index+1]->value);
index=index+2;
continue;
}else{
return index+2;
}
}else{
return index+1;
}
}
index++;
continue;
}
if(DataArray[index]->codeID==3 || DataArray[index]->codeID==4){ //write or read
if(DA_index < index+5) return DA_index-1;
if(DataArray[index+1]->codeID==13 && DataArray[index+2]->codeID==15 && DataArray[index+3]->codeID==14 && DataArray[index+4]->codeID==12){
char nOper[2];
if(DataArray[index]->codeID==3){
nOper[0]='R';
}else{
nOper[0]='W';
}
nOper[1]='\0';
createFourGroup(nOper,"","",DataArray[index+2]->value);
index=index+5;
}else{
if(DataArray[index+4]->codeID!=12) return index+4;
return index+1;
}
continue;
}
if(DataArray[index]->codeID ==15){ //赋值语句
if(DataArray[index+1]->codeID !=10) return index+1;
if(DA_index < index+4) return DA_index-1;
if(DataArray[index+2]->codeID == 16 && DataArray[index+3]->codeID == 12){
createFourGroup(DataArray[index+1]->value,DataArray[index+2]->value,"",DataArray[index]->value);
index=index+4;
continue;
}else{
char Var_1[8];
strcpy(Var_1,DataArray[index++]->value);
char nSign[2];
strcpy(nSign,DataArray[index++]->value);
char Var_2[8];
//递归分析
RecursionIndex=index; //设置递归分析点
strcpy(Var_2,e());
if(RecursionError != -1){
return RecursionError;
}
index=RecursionIndex;
if(DataArray[index++]->codeID !=12){
return index;
}
createFourGroup(":=",Var_2,"",Var_1);
continue;
}
}
return index;
}
return -1;
}
//***************************************/
//函数名:MeaningAnalize */
//作 用:语义分析 */
//参 数:无 */
//反回值:正确返回0,错误返回1 */
//***************************************/
int MeaningAnalize(){
int nIndex,i;
for(nIndex=0; nIndex < FGA_index; nIndex++){
//检测是否重复定义标识符
if(strcmp(FourGroupArray[nIndex]->oper,"=")==0 || strcmp(FourGroupArray[nIndex]->oper,"")==0){
for(i=nIndex+1; i<FGA_index; i++){
if((strcmp(FourGroupArray[i]->oper,"")==0 || strcmp(FourGroupArray[i]->oper,"=")==0) && strcmp(FourGroupArray[i]->Add,FourGroupArray[nIndex]->Add)==0){
printf("Error:符识符%s 重定义\n",FourGroupArray[i]->Add);
return 1;
}
}
}
//检测是否引用未定义标识符
if(strcmp(FourGroupArray[nIndex]->oper,"") != 0 && strcmp(FourGroupArray[nIndex]->oper,"=")!=0 ){
if(isChar(FourGroupArray[nIndex]->var_01[0])){
int Error=1;
for(i=nIndex-1; i >= 0; i--){ //向上查找
if((strcmp(FourGroupArray[i]->oper,"")==0 || strcmp(FourGroupArray[i]->oper,"=")==0) && strcmp(FourGroupArray[i]->Add,FourGroupArray[nIndex]->var_01)==0){
Error=0;
}
}
if(Error !=0 ){
printf("Error:引用未定义变量%s.\n",FourGroupArray[nIndex]->var_01);
return 1;
}
}
if(isChar(FourGroupArray[nIndex]->var_02[0])){
int Error=1;
for(i=nIndex-1; i >= 0; i--){ //向上查找
if((strcmp(FourGroupArray[i]->oper,"")==0 || strcmp(FourGroupArray[i]->oper,"=")==0) && strcmp(FourGroupArray[i]->Add,FourGroupArray[nIndex]->var_02)==0){
Error=0;
}
}
if(Error !=0 ){
printf("Error:引用未定义变量%s.\n",FourGroupArray[nIndex]->var_02);
return 1;
}
}
if(isChar(FourGroupArray[nIndex]->Add[0])){
int Error=1;
for(i=nIndex-1; i >= 0; i--){ //向上查找
if((strcmp(FourGroupArray[i]->oper,"")==0 || strcmp(FourGroupArray[i]->oper,"=")==0) && strcmp(FourGroupArray[i]->Add,FourGroupArray[nIndex]->Add)==0){
Error=0;
}
}
if(Error !=0 ){
printf("Error:引用未定义变量%s.\n",FourGroupArray[nIndex]->Add);
return 1;
}
}
}
//检查是否除以一个O值
if(strcmp(FourGroupArray[nIndex]->oper,"/")==0 && strcmp(FourGroupArray[nIndex]->var_02,"0")==0){
printf("Error:运算符'/'后的值不能为%s.\n",FourGroupArray[nIndex]->var_02);
return 1;
}
//检查是否引用未初始化的变量
if(isChar(FourGroupArray[nIndex]->var_01[0]) || isChar(FourGroupArray[nIndex]->var_02[0])){
if(isChar(FourGroupArray[nIndex]->var_01[0])){ //向上找,查找是否已赋值
int Error=1;
for(i=nIndex-1; i >= 0; i--){ //向上查找
if((strcmp(FourGroupArray[i]->oper,"=")==0 || strcmp(FourGroupArray[i]->oper,":=")==0) && strcmp(FourGroupArray[i]->Add,FourGroupArray[nIndex]->var_01)==0){
Error=0;
}
}
if(Error !=0 ){
printf("Error:引用未初始化的变量%s.\n",FourGroupArray[nIndex]->var_01);
return 1;
}
}
if(isChar(FourGroupArray[nIndex]->var_02[0])){ //向上找,查找是否已赋值
int Error=1;
for(i=nIndex-1; i >= 0; i--){ //向上查找
if((strcmp(FourGroupArray[i]->oper,"=")==0 || strcmp(FourGroupArray[i]->oper,":=")==0) && strcmp(FourGroupArray[i]->Add,FourGroupArray[nIndex]->var_02)==0){
Error=0;
}
}
if(Error !=0 ){
printf("Error:引用未初始化的变量%s.\n",FourGroupArray[nIndex]->var_02);
return 1;
}
}
}
}
return 0;
}
int main( ){
//词法分析
while (syn!=0){
scaner(); //执行分析函数
if(syn == -1){
CodeEdit(); //发生错误
return 0;
}
//创建结点
node *newNode=(node*)malloc(sizeof(node));
newNode->codeID=syn;
memset(newNode->value,'\0',8);
strcpy(newNode->value,token);
strcpy(LastEffectiveCode,token);
newNode->row=WordRow;
newNode->pos=WordPos;
DataArray[DA_index++]=newNode;
};
OutPutDualGroup();
fclose(f_Input);
fclose(f_DualGroup);
//语法分析
int Error=GrammaAnalize();
if(Error!=-1){
printf("Error:row(%i),pos(%i) Error occur near the sign '%s' !\n",DataArray[Error]->row,DataArray[Error]->pos,DataArray[Error]->value);
system("notepad InputFile.txt");
return 0;
}
//语义分析
Error=MeaningAnalize();
if(Error!=0){
system("notepad InputFile.txt");
return 0;
}
//输出四元中间码
OutPutFourGroup();
printf("语义分析成功...\n");
fclose(f_FourGroup);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -