📄 sxb.cpp
字号:
#include <iostream.h>
#include <string.h>
#include <stdio.h>
#include <cctype> //可使用isdigit()和isalpha()函数;
#include <fstream.h>
//保留字=1;标识符=2;常数=3;运算符=4;界符=5;
bool isAlphaDigit(char a){ //该函数判断是数字还是字母
if(isalpha(a))
return true;
else if(isdigit(a))
return true;
else
return false;
}
bool isword(char a[8]){ //该函数判断该字符串是不是关键字;
int m=0;
int t=0;
for(int i=0;i<11;i++) //用for循环记录该字符串的个数;
{if(isalpha(a[i]))
m++;
}
char *symconst="const";
char *symvar="var";
char *symprocedure="procedure";
char *symcall="call";
char *symend="end";
char *symif="if";
char *symthen="then";
char *symwhile="while";
char *symdo="do";
char *symread="read";
char *symwrite="write";
char *symbegin="begin";
if(t=*a-*symconst==0&&*a++&&*symconst++)
{if(m<5)
return false;
else return true;
}
if(t=*a-*symvar==0&&*a++&&*symvar++) //判断是否和关键字匹配;
{if(m<3)
return false;
else return true;
}
else if(t=*a-*symprocedure==0&&*a++&&*symprocedure++)
{if(m<9)
return false;
else
return true;
}
else if(t=*a-*symcall==0&&*a++&&*symcall++)
{if(m<4)
return false;
else
return true;
}
else if(t=*a-*symend==0&&*a++&&*symend++)
{if(m<3)
return false;
else return true;
}
else if(t=*a-*symif==0&&*a++&&*symif++)
{if(m<2)
return false;
else return true;
}
else if(t=*a-*symthen==0&&*a++&&*symthen++)
{if(m<4)
return false;
else return true;
}
else if(t=*a-*symwhile==0&&*a++&&*symwhile++)
{if(m<5)
return false;
else return true;
}
else if(t=*a-*symdo==0&&*a++&&*symdo++)
{if(m<2)
return false;
else return true;
}
else if(t=*a-*symread==0&&*a++&&*symread++)
{if(m<4)
return false;
else return true;
}
else if(t=*a-*symwrite==0&&*a++&&*symwrite++)
{if(m<5)
return false;
else return true;
}
else if(t=*a-*symbegin==0&&*a++&&*symbegin++)//判断是否和关键字匹配;
{if(m<5)
return false;
else return true;
}
else
return false;
}
getsym(){
char c;int k=0;char A[8]="";
c=cin.get();
if(c==' ') getsym(); //判断是否为空格;
else if(c!=' '&&c!='\t'&&c!='\n'&&c!=EOF) //判断是否为回车或者为table键或者为文件结束;
{
if(isalpha(c)) //是否为字母;是的话记录该字符
{A[0]=c;int k=0;
if(k<8){k=0;
while(isAlphaDigit(cin.peek()))
{ k=k+1; A[k]+=char(cin.get()); } //记录该字符串;
}
for(int i=0;i<8;i++)//输出该字符串
{ cout<<A[i];}
if(isword(A)) //判断是否为关键字;
cout<<':'<<"1 保留字"<<endl;
else
cout<<':'<<"2 标识符"<<endl;
}
else if(isdigit(c)){A[k]=c; //是否为数字,是的话记录;
if(k<8){k=0;
{ while(isdigit(cin.peek()))//查看下个字母是否为数字,是的话记录;
{ k=k+1; A[k]+=char(cin.get());}
{ while(isalpha(cin.peek())){cout<<"number+letter!"<<endl;break;}//如果是字母的话,输出“number+letter”,不继续记录;
}
}
}
for(int i=0;i<8;i++) //输出该常数,并输出类别;
{ cout<<A[i];}
cout<<':'<<"3 常数"<<endl;
}
else //其他的类型的判断;
switch(c){
case '+':
A[0]=c;
if(cin.peek()=='=')
A[1]=cin.get();
cout<<A<<':'<<" "<<"4 运算符"<<endl;
break;
case '-':
A[0]=c;
if(cin.peek()=='=')
A[1]=cin.get();
cout<<A<<':'<<" "<<"4 运算符"<<endl;
break;
case '*':
A[0]=c;
if(cin.peek()=='=')
A[1]=cin.get();
cout<<A<<':'<<" "<<"4 运算符"<<endl;
break;
case '/':
A[0]=c;
if(cin.peek()=='=')
A[1]=cin.get();
cout<<A<<':'<<" "<<"4 运算符"<<endl;
break;
case '!':
A[0]=c;
if(cin.peek()=='=')
A[1]=cin.get();
cout<<A<<':'<<" "<<"4 运算符"<<endl;
break;
case ':=':
cout<<A<<':'<<" "<<"4 运算符"<<endl;
break;
case '#':
cout<<A<<':'<<" "<<"4 运算符"<<endl;
break;
case '%':
A[0]=c;
if(cin.peek()=='=')
A[1]=cin.get();
cout<<A<<':'<<" "<<"4 运算符"<<endl;
break;
case '^':
cout<<A<<':'<<" "<<"4 运算符"<<endl;
break;
case '(':
cout<<A<<':'<<" "<<"4 运算符"<<endl;
break;
case ')':
cout<<A<<':'<<" "<<"4 运算符"<<endl;
break;
case '=':
cout<<A<<':'<<" "<<"4 运算符"<<endl;
break;
case '<':
A[0]=c;
if(cin.peek()=='=')
A[1]=cin.get();
cout<<A<<':'<<" "<<"4 运算符"<<endl;
break;
case '>':
A[0]=c;
if(cin.peek()=='=')
A[1]=cin.get();
cout<<A<<':'<<" "<<"4 运算符"<<endl;
break;
case '"':
cout<<c<<':'<<" "<<"5 界符"<<endl;
break;
case '`':
cout<<c<<':'<<" "<<"5 界符"<<endl;
break;
case ',':
cout<<c<<':'<<" "<<"5 界符"<<endl;
break;
case '.':
cout<<c<<':'<<" "<<"5 界符"<<endl;
break;
case ';':
cout<<c<<':'<<" "<<"5 界符"<<endl;
break;
default: cout<<c<<":"<<" "<<"It's a wrong sign!"<<endl;
break;
}
//return true;
}
}
void error(int n)
{
int i;
printf(" ");
for (i = 1; i <= cc - 1; i++)
printf(" ");
fprintf(outfile, " ");
fprintf(outfile, "^\n");
printf("^\n");
fprintf(outfile, "Error %3d: %s\n", n, err_msg[n]);
printf("Error %3d: %s\n", n, err_msg[n]);
err++;
} // error
void block(symset fsys)
{ int dx;
int cx0; // initial code index
mask* mk;
int block_dx;
int savedTx;
symset set1, set;
dx = 3;
block_dx = dx;
mk = (mask*) &table[tx];
mk->address = cx;
// gen(JMP, 0, 0);
if (level > MAXLEVEL)
{
error(32); // There are too many levels.
}
do
{
if (sym == SYM_CONST)
{ // constant declarations
getsym();
do
{
constdeclaration();
while (sym == SYM_COMMA)
{
getsym();
constdeclaration();
}
if (sym == SYM_SEMICOLON)
{
getsym();
}
else
{
error(5); // Missing ',' or ';'.
}
}
while (sym == SYM_IDENTIFIER);
} // if
if (sym == SYM_VAR)
{ // variable declarations
getsym();
do
{
void vardeclaration();
while (sym == SYM_COMMA)
{
getsym();
vardeclaration();
}
if (sym == SYM_SEMICOLON)
{
getsym();
}
else
{
error(5); // Missing ',' or ';'.
}
}
while (sym == SYM_IDENTIFIER);
block_dx = dx; // modified by yzhang 02-03-15
} // if
while (sym == SYM_PROCEDURE)
{ // procedure declarations
getsym();
if (sym == SYM_IDENTIFIER)
{
// enter(ID_PROCEDURE);
getsym();
}
else
{
error(4); // There must be an identifier to follow 'const', 'var', or 'procedure'.
}
if (sym == SYM_SEMICOLON)
{
getsym();
}
else
{
error(5); // Missing ',' or ';'.
}
level++;
savedTx = tx;
set1 = createset(SYM_SEMICOLON, SYM_NULL);
set = uniteset(set1, fsys);
block(set);
destroyset(set1);
destroyset(set);
tx = savedTx;
level--;
/* if (sym == SYM_SEMICOLON)
{
getsym();
set1 = createset(SYM_IDENTIFIER, SYM_PROCEDURE, SYM_NULL);
set = uniteset(statbegsys, set1);
// test(set, fsys, 6);
destroyset(set1);
destroyset(set);
}
else
{
error(5); // Missing ',' or ';'.
}
} // while
set1 = createset(SYM_IDENTIFIER, SYM_NULL);
set = uniteset(statbegsys, set1);
// test(set, declbegsys, 7);
destroyset(set1);
destroyset(set);
}
while (inset(sym, declbegsys));
code[mk->address].a = cx;
mk->address = cx;
cx0 = cx;
// gen(INT, 0, block_dx);
set1 = createset(SYM_SEMICOLON, SYM_END, SYM_NULL);
set = uniteset(set1, fsys);
// statement(set);
destroyset(set1);
destroyset(set);
// gen(OPR, 0, OPR_RET); // return
// test(fsys, phi, 8); // test for error: Follow the statement is an incorrect symbol.
//listcode(cx0, cx);
} // block*/
int position(char* id)
{
int i;
strcpy(table[0].name, id);
i = tx + 1;
while (strcmp(table[--i].name, id) != 0);
return i;
} // position
void vardeclaration(void)
{
if (sym == SYM_IDENTIFIER)
{
enter(ID_VARIABLE);
getsym();
}
else
{
error(4); // There must be an identifier to follow 'const', 'var', or 'procedure'.
}*/
} // vardeclaration
void main(){
// char c;
int k=0;char A[9]="";int cc=0;
//ken=" ";
cout<<"please write the program:"<<endl;
getsym();block();getsym();getsym();getsym();getsym();getsym();getsym();getsym();getsym();getsym();getsym();getsym();getsym();getsym();getsym();getsym();getsym();
//if(c!=' '&&c!='\t'&&c!='\n'&&c!=EOF)
getsym();getsym();getsym();getsym();getsym();getsym();getsym();getsym();getsym();getsym();
getsym(); getsym(); getsym(); getsym(); getsym(); getsym(); getsym(); getsym(); getsym(); getsym(); getsym();
getsym();getsym();getsym();getsym();getsym();getsym();getsym();getsym();getsym();getsym();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -