2.c
上传用户:infocom
上传日期:2021-09-11
资源大小:157k
文件大小:4k
- #include<stdio.h>
- #include<string.h>
- #define buf_s 2000 /*缓冲区长度*/
- #define word_s 50 /*最大单词长度*/
- /*单词种别*/
- char *symbol[20]={"ERROR","IDN","INT8","INT10","INT16",
- "ADD","SUB","MUL","DIV","MORE","LESS","EQUAL",
- "LEFT","RIGHT","SEM",
- "IF","ELSE","THEN","WHILE","DO"};
- /*关键字*/
- char *key[5]={"if","else","then","while","do"};
- /* 运算符、分隔符*/
- char op_s[10]="+-*/><=();";
- int scan(char*buf,char*word,int*symbol_number);
- int main(){
- char buffer[buf_s];/*初始化缓冲区*/
- char finish_word[word_s]="";/*初始化单词为空*/
- int symbol_num=0;/*初始化单词种别值为0(错误)*/
- gets(buffer);/*读取程序*/
- while(scan(buffer,finish_word,&symbol_num))/*当到达程序结束,scan函数返回0,结束词法分析*/
- printf("%stt%sn",symbol[symbol_num],finish_word);/*打印单词种类、属性*/
- return 0;
- }
- /*scan分词函数(缓冲区地址,存放分隔单词地址,单词列别值地址)*/
- int scan(char*buf,char*word,int*symbol_number){
- static int n=0;/*静态,记录当前缓冲区读取位置*/
- char ch;
- int word_n=0;/*单词尾部*/
- int key=0;/*关键字类别值的临时存放变量*/
- int op=0;/*运算符、分隔符类别值的临时存放变量*/
- ch=buf[n];/*取第一个字符*/
-
- while(ch==' '||ch=='n'){/*如果是换行符,或者空格,跳过,读取下一个字符*/
- n++;
- ch=buf[n];
- }
-
- if(ch=='0'){/*第一个字符为0,则为十进制0,十六进制,或者八进制*/
- word[word_n]=ch;/*ch-->word*/
- word_n++;
- n++; /*取下一个字符*/
- ch=buf[n];
- if(ch=='x'||ch=='X'){/*十六进制分支*/
- word[word_n]=ch;
- word_n++;
- n++;/*取下一个字符*/
- ch=buf[n];
- if(isdigit(ch)||(ch>='a'&&ch<='f')||(ch>='A'&&ch<='F')){
- word[word_n]=ch;
- word_n++;
- n++;
- ch=buf[n];
- while(isdigit(ch)||(ch>='a'&&ch<='f')||(ch>='A'&&ch<='F')){
- word[word_n]=ch;
- word_n++;
- n++;
- ch=buf[n];
- }
- word[word_n]=' ';
- *symbol_number=4;/*单词类别值设为4(十六进制)*/
- }
- else{/*当为“0x”+“非0-9,a-f,A-F”,出错*/
- word[word_n]=' ';
- *symbol_number=0;/*单词类别值设为0(出错)*/
- }
- }
- else if(ch>='0'&&ch<='7'){/*八进制分支*/
- word[word_n]=ch;
- word_n++;
- n++;
- ch=buf[n];
- while(ch>='0'&&ch<='7'){
- word[word_n]=ch;
- word_n++;
- n++;
- ch=buf[n];
- }
- word[word_n]=' ';
- *symbol_number=2;/*单词类别值设为2(八进制)*/
- }
- else{/*为十进制0*/
- word[word_n]=' ';
- *symbol_number=3;/*单词类别值设为3(十进制)*/
- }
- }
- else if(ch>='1'&&ch<='9'){/*十进制分支*/
- word[word_n]=ch;
- word_n++;
- n++;
- ch=buf[n];
- while(ch>='0'&&ch<='9'){
- word[word_n]=ch;
- word_n++;
- n++;
- ch=buf[n];
- }
- word[word_n]=' ';
- *symbol_number=3;/*单词类别值设为3(十进制)*/
- }
- else if(isalpha(ch)){/*标示符(IDN),或者关键字(key)分支*/
- word[word_n]=ch;
- word_n++;
- n++;
- ch=buf[n];
- while(isalpha(ch)||isdigit(ch)){
- word[word_n]=ch;
- word_n++;
- n++;
- ch=buf[n];
- }
- word[word_n]=' ';
- if(key=iskeyword(word))/*是否为关键字*/
- *symbol_number=key;/*单词类别值设为key(关键字类别值)*/
- else *symbol_number=1;/*单词类别值设为1(标识符)*/
-
-
- }
- else if(op=isop_s(ch)){//运算或分隔符分枝
- word[word_n]=ch;
- word_n++;
- word[word_n]=' ';
- n++;
- *symbol_number=op;
- }else if(ch==' '){/*结束符,退出scan程序*/
- return 0;
- }else{/*为非法字符*/
- word[word_n]=ch;
- word_n++;
- n++;
- word[word_n]=' ';
- *symbol_number=0;/*单词类别值设为0(非法字符)*/}
- return 1;
- }
- int isalpha(ch){//是否为字符
- if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))
- return 1;
- else
- return 0;}
- int isdigit(ch){//是否为数字
- if(ch>='0'&&ch<='9')
- return 1;
- else
- return 0;}
- int iskeyword(char*w){//关键字
- int i;
- for(i=0;i<5;i++){
- if(strcmp(w,key[i])==0)
- return 15+i;/*返回关键字类别值*/
- }
- return 0;
- }
- int isop_s(char m){//运算或分隔符
- int i;
- for(i=0;i<10;i++){
- if(m==op_s[i])
- return 5+i;/*返回运算或分隔符类别值*/
- }
- return 0;
- }