📄 funcion.cpp
字号:
#include "StdAfx.h"
//////////////////////////////////////////////////////////////////////
// gets the order of the given word in the keyword array
int getKeyOrder(char* gword){
char *word=new char[strlen(gword)+1];
strcpy(word,gword);
_strlwr(word); // convert to low case
// same as the array keyword[]
int head=0, rear=keywordNum;
int i=0;
for(i=(head+rear)/2;head<rear;i=(head+rear)/2){
if( strcmp(word, keyword[i])<0 ) rear=i;
else if( strcmp(word, keyword[i])>0 ) head=i+1;
else return i;
}
return -1;
}
void getch(void)
{
while (cc == ll || 0 == ll) // ll: line lenghth cc: chatacter count
{
if (feof(infile))
{
printf("\nPROGRAM INCOMPLETE\n");
exit(1);
}
ll = cc = 0;
while ( !feof(infile)
&& ((ch = getc(infile)) != '\n'))
{
line[++ll] = ch;
} // while
line[++ll] = ' ';
++ln;
}
ch = line[++cc];
} // getch
//////////////////////////////////////////////////////////////////////
// gets a symbol from input stream.
void getsym(void)
{
static bool tightNumber=false;
char a[MAXNUMLEN + 1];
while (ch == ' '|| ch == '\t'){
getch(); //滤前导空格
//if(ch == '\t') cc+=3;
}
int i,k;
if (isalpha(ch)) //1.读标志符
{
cch=cc;
k = 0;
do{
if (k < MAXIDLEN) a[k++] = ch; // 舍弃大于 MAXIDLEN 部分的标识符 字符
getch();
} while (isalpha(ch) || isdigit(ch));
a[k] = 0;
strcpy(id, a);
i = getKeyOrder(id);
if (-1 != i){
sym = wsym[i]; // symbol is a reserved word
tightNumber=false;
}
else{
if(tightNumber){
cout<<"Illegal identifier \""<<a<<"\" in line "<<ln<<" column "<<cc-k
<<".Compile Stopped.\n";
fprintf(outfile,
"Illegal identifier \"%s\" in line %d column %d.Compile Stopped.\n",
a,ln, cc-k);
exit(1);
}
sym = SYM_IDENTIFIER; // symbol is an identifier
}
}
else if (isdigit(ch)) //2.读 数字
{
cch=cc;
k = num = 0;
sym = SYM_NUMBER;
do
{
num = num * 10 + ch - '0';
a[k++]=ch;
getch();
} while (isdigit(ch));
a[k]=0;
strcpy(id, a);
if( isalpha(ch) ) tightNumber=true;
else tightNumber=false;
if (k > MAXNUMLEN){
//error(25); // The number is too great.
cout<<"The number in line %d column %d."<<a<<"is too long!"<<endl;
fprintf(outfile, "The number in line %d column %d is too long.\nCompile Stopped.\n",
ln, cc-k);
exit(1);
}
}
else //3.读 操作符
{
cch=cc;
switch(ch){
case ':':
getch();
if (ch == '=')
{
sym = SYM_BECOMES; // :=
strcpy(id, ":=");
getch();
}
else
{
sym = SYM_NULL; // illegal? Yes,sir
}
break;
case '>':
getch();
if (ch == '=')
{
sym = SYM_GEQ; // >=
strcpy(id, ">=");
getch();
}
else
{
sym = SYM_GTR; // >
strcpy(id, ">");
}
break;
case '<':
getch();
if (ch == '=')
{
sym = SYM_LEQ; // <=
strcpy(id, "<=");
getch();
}
else if (ch == '>')
{
sym = SYM_NEQ; // <>
strcpy(id, "<>");
getch();
}
else
{
sym = SYM_LES; // <
strcpy(id, "<");
}
break;
case '+': // +
sym = SYM_PLUS;
id[0]='+';
id[1]=0;
getch();
break;
case '-': // -
sym = SYM_MINUS;
id[0]='-';
id[1]=0;
getch();
break;
case '*': // *
sym = SYM_TIMES;
id[0]='*';
id[1]=0;
getch();
break;
case '/': // /
sym = SYM_SLASH;
id[0]='/';
id[1]=0;
getch();
break;
case '=': // =
sym = SYM_EQU;
id[0]='=';
id[1]=0;
getch();
break;
case '(': // (
sym = SYM_LPAREN;
id[0]='(';
id[1]=0;
getch();
break;
case ')': // )
sym = SYM_RPAREN;
id[0]=')';
id[1]=0;
getch();
break;
case ',': // ,
sym = SYM_COMMA;
id[0] = ',';
id[1]=0;
getch();
break;
case ';': // ;
sym = SYM_SEMICOLON;
id[0] = ';';
id[1]=0;
getch();
break;
case '.': // .
sym = SYM_PERIOD;
id[0]='.';
id[1]=0;
// no getch()
break;
default: // other tokens
cout<<"Fatal Error: Unknown character:ACSII value "<<int(ch)<<" \""<<ch<<"\""<<endl;
fprintf(outfile, "Fatal Error: Unknown character. "__FILE__ " line %d.\n",__LINE__);
exit(1);
}
tightNumber=false;
}
} // getsym
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -