?? cifafenxiqi.doc
字號:
/*本程序用于詞法分析:從DOS界面輸入一行的C語句,并以#號結(jié)尾(注意必須以這個為結(jié)束標志,否則出錯),要求輸出對應(yīng)的單詞的種類碼和該類中的
內(nèi)碼。其中,關(guān)鍵字種類為1,分界符種類為2,算術(shù)運算符、關(guān)系運算符、常數(shù)、標識符分別為3、4、5、6。
問題:記得對給指針分配內(nèi)存,否則會出現(xiàn)異常!
作者:02級計算機4班:chenyiyin于2005年4月13日 Email:chengyy02@st.lzu.edu.cn
*/
#include<string.h>
#include<stdio.h>
#include<malloc.h>
char * key[8]={"BEGIN","DO","ELSE","END","IF","THEN","VAR","WHILE"};/*存放關(guān)鍵字*/
char * identify[20]; /*存放標識符*/
char * constant[20]; /*存放常數(shù)*/
int id=0; /*全局變量,用于表示標識符的個數(shù)*/
int cd=0; /*用于表示常數(shù)的個數(shù)*/
int Is_number(char ch) /*是否是數(shù)字*/
{
return (ch>='0'&&ch<='9');
}
int Is_alphe(char ch) /*是否是字母*/
{
return (ch>='a'&&ch<='z'||ch>='A'&&ch<='Z');
}
void Key_analyse(char * str) /*關(guān)鍵字和標識符的分析*/
{
int i=0;
int j=0;
for(;i<8;i++) /*如果和關(guān)鍵字數(shù)組中的一個字相同則表示它為關(guān)鍵字*/
{
if((strcmp(str,key[i]))==0) /*比較*/
{
j=1;
printf("%s [type,number]---[1,%d]\n",str,i); /*輸出種類和內(nèi)碼*/
}
}
if(j!=1) /*j=1則表示它已經(jīng)是關(guān)鍵字,否則就是標識符*/
{
int i=0;
for(;i<id;i++) /*和常數(shù)表中比較,若表中已經(jīng)存在這個常數(shù)*/
{ /*則不再添加到表中,否則加入表中。*/
if(strcmp(identify[i],str)==0)break;
}
printf("%s [type,number]---[6,%d]\n",str,i);
if(i==id)
{
identify[id]=(char*)malloc(sizeof(str));
strcpy(identify[id++],str);
}
}
}
void Constant_analyse(char *str) /*常數(shù)分析*/
{
int i=0;
for(;i<cd;i++)
{
if(strcmp(constant[i],str)==0) break; /*和標識符分析同理*/
}
printf("%s [type,number]---[5,%d]\n",str,i);
if(i==cd)
{
constant[cd]=(char*)malloc(sizeof(str));
strcpy(constant[cd++],str);
}
}
void Error_out(char * str)
{
printf("Error! %s\n",str);
}
void main()
{
char input[100]; /*存放輸入的字符串*/
char ch[10]; /*存放用于分析的單詞*/
int input_i=0;
int ch_i=0;
int m;
int n;
printf("Please input a file name to analyse:\n");
gets(input); /*輸入*/
k:
ch[ch_i]=input[input_i];
while(ch[ch_i]==' ') /*當是空格則繼續(xù)從input[]中取值*/
ch[ch_i]=input[++input_i];
if(Is_alphe(ch[ch_i])) /*當?shù)谝粋€為字母時,可以肯定它組成的單詞*/
{ /*不是關(guān)鍵字就是標識符。*/
while(Is_number(input[input_i+1])||Is_alphe(input[input_i+1]))
ch[++ch_i]=input[++input_i]; /*后面可以跟字母或數(shù)字*/
ch[++ch_i]='\0'; /*若不是字母或數(shù)字就表示這是個單詞,結(jié)束取數(shù)。*/
Key_analyse(ch); /*分析是關(guān)鍵字還是標識符。*/
}
else /*如果第一個不是字母,那么它組成的單詞*/
{ /*可能是常數(shù)或符號。*/
if(Is_number(ch[ch_i]))
{
while(Is_number(input[input_i+1])) /*取常數(shù)單詞。*/
ch[++ch_i]=input[++input_i];
if(Is_alphe(input[input_i+1]))
{
Error_out("It is not a number!");
return;
}
ch[++ch_i]='\0';
Constant_analyse(ch); /*常數(shù)分析*/
}
else /*符號分析*/
{
switch(ch[ch_i])
{
case ',' : printf("%c [type,number]---[2,0]\n",ch[ch_i]);break;
case ';' : printf("%c [type,number]---[2,1]\n",ch[ch_i]);break;
case '.' : printf("%c [type,number]---[2,2]\n",ch[ch_i]);break;
case ':' : if(input[input_i+1]=='=' )
printf(":= [type,number]---[2,3]\n");break;
case '(' : printf("%c [type,number]---[2,4]\n",ch[ch_i]);break;
case ')' : printf("%c [type,number]---[2,5]\n",ch[ch_i]);break;
case '+' : printf("%c [type,number]---[3,10]\n",ch[ch_i]);break;
case '-' : printf("%c [type,number]---[3,11]\n",ch[ch_i]);break;
case '*' : printf("%c [type,number]---[3,20]\n",ch[ch_i]);break;
case '/' : printf("%c [type,number]---[3,21]\n",ch[ch_i]);break;
case '<' : if(input[input_i+1]=='=')
printf("<= [type,number]---[4,01]\n",ch[ch_i]);
else if(input[input_i+1]=='>')
printf("<> [type,number]---[4,00]\n",ch[ch_i]);
else
printf("%c [type,number]---[4,05]\n",ch[ch_i]);break;
case '=' : printf("%c [type,number]---[4,02]\n",ch[ch_i]);break;
case '>' : if(input[input_i+1]=='=')
printf(">= [type,number]---[4,04]\n");
else
printf("%c [type,number]---[4,03]\n",ch[ch_i]);break;
default: printf("Error! You enter the wrong words!"); goto end; /*都不是,那肯定是錯誤輸入!*/
}
}
}
ch_i=0; /*設(shè)置為0,為ch[]接著取單詞作準備。*/
if(input[++input_i]!='#') goto k;
end: /*循環(huán)*/
for( m=0;m<cd;m++)
{
free(constant[m]);
}
for( n=0;n<id;n++)
{
free(identify[n]);
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -