0917 实验一 词法分析程序
2015-09-21 20:25
274 查看
#include <stdio.h> #include <string.h> #include <iostream.h> #define BEGIN 1 #define IF 2 #define THEN 3 #define WHILE 4 #define DO 5 #define END 6 #define INT 11 #define LT 20 #define LE 21 #define EQ 24 #define NE 22 #define GT 12 #define GE 24 #define IS 18 #define PL 13 #define MI 14 #define MU 15 #define DI 16 #define SS 17 #define ID 10 #define AI 26 #define BI 27 #define HI 58 #define JI 0 char *keyword[8]={"begin","end","if","then","do","while"}; int i=0,j=0,k=0,t=0; char ch,dck[20]; char * chr_form[100]; char * int_form[100]; char form[1000]; int q=0,temp; void GetChar() { ch=form[k]; k++; } void getbc() { while(ch==' ') { GetChar(); } } void concat() { dck[i]=ch; i++; } bool isletter() { if(((ch<='z')&&(ch>='a'))||((ch<='Z')&&(ch>='A'))) return(1); else return(0); } bool isdigit() { if(((ch)<='9')&&((ch)>='0')) return (1); else return (0); } int reserve() { for(int q=0;q<5;q++) { if(strcmp(keyword[q],dck)==0) return q; if(q==4) return -1; } } void retract() { k--; ch=NULL; } char *insertld() { chr_form[j]=dck; j++; return chr_form[0]; } char * insertconst() { int_form[t]=dck; t++; return int_form[0]; } int code; void output(int a,char *p1,char *p2) { cout<<"\t种别码:"<<a<<"\t 单词值:"; while(p1<=p2) { printf("%s",*p1); p1++; } cout<<endl; } void analyze() { GetChar(); getbc(); if(isletter()) { while (isletter()||isdigit()) { concat(); GetChar(); } retract(); code=reserve(); switch(code) { case 0:cout<<"单词符号:" <<dck<<" 种别码为: "<<BEGIN<<endl;break; case 1:cout<<"单词符号:" <<dck<<" 种别码为: "<<END<<endl;break; case 2:cout<<"单词符号:" <<dck<<" 种别码为: "<<IF<<endl;break; case 3:cout<<"单词符号:" <<dck<<" 种别码为: "<<THEN<<endl;break; case 4:cout<<"单词符号:" <<dck<<" 种别码为: "<<DO<<endl;break; case 5:cout<<"单词符号:" <<dck<<" 种别码为: "<<WHILE<<endl;break; default: cout<<"单词符号:" <<dck<<" 种别码为: "<<ID<<endl;break; } } else { if( isdigit()) { while(isdigit()||ch=='.') { concat(); GetChar(); } retract(); cout<<"单词符号:"<<dck<<" 种别码为:"<<INT<<endl; } else { switch(ch) { case'+': cout<<"单词符号:+ 种别码为: "<<PL<<endl;break; case'-': cout<<"单词符号:- 种别码为: "<<MI<<endl;break; case'*': cout<<"单词符号:* 种别码为: "<<MU<<endl;break; case'/': cout<<"单词符号:/ 种别码为: "<<DI<<endl;break; case';': cout<<"单词符号:; 种别码为: "<<AI<<endl;break; case'(': cout<<"单词符号:( 种别码为: "<<BI<<endl;break; case')': cout<<"单词符号:) 种别码为: "<<HI<<endl;break; case'#': cout<<"单词符号:# 种别码为: "<<JI<<endl;break; case':':GetChar(); if(ch=='=') { cout<<"单词符号::= 种别码为: "<<IS<<endl;break; } else { retract(); cout<<"单词符号:: 种别码为: "<<SS<<endl;break; } case'=':cout<<"单词符号:= 种别码:"<<EQ<<endl; break; case'>':GetChar();switch(ch) { case'=':cout<<"单词符号:>= 种别码:"<<GE<<endl;break; default: retract; cout<<"单词符号:=> 种别码:"<<GT<<endl;break; } case'<':GetChar(); switch(ch) { case'=':cout<<"单词符号:<= 种别码:"<<LE<<endl;break; case'>':cout<<"单词符号:<> 种别码:"<<NE<<endl;break; default: retract(); cout<<"单词符号:< 种别码:"<<LT<<endl;break; } } } } while(k<q) { for(int p=0;p<50;p++) dck[p]='\0'; i=0; analyze(); } } void main () { printf("输入一段程序,以^结束:"); form[0]=cin.get(); for(q=1;form[q-1]!='!!';q++) { form[q]=cin.get(); if(form[q]=='^') { printf("所输入的程序段为:"); printf("\n"); cout.write(form,q); break; } } cout<<endl; analyze(); }
相关文章推荐
- 软链接/硬链接
- Linux环境变量的设置和查看方法 http://soft.chinabyte.com/os/169/11412169.shtml
- Java 垃圾回收机制 GC的算法和操作
- toCharArry()和chatAT()
- 机器学习规范的规则(两)核电规范与规则的偏好
- Java8揭秘(三)Default 方法
- 有关内存的思考?GetMemory()
- 我的第一篇文章-----------为了更好的明天。
- Android Message和obtainMessage的区别
- Launcher进程的启动
- c#左右socket连接超时控制方案
- 京东笔试题-小熊分苹果
- [LeetCode] 7.Reverse Integer
- ThinkPHP-数据库配置和模板调用
- Struts2环境的搭建
- POJ 1679 The Unique MST (次小生成树)
- std::vector的几种遍历方式比较
- ffmpeg解复用解码重采样
- malloc和new的区别
- 深入理解字符串常量