设为首页 加入收藏

TOP

简单词法分析器实现
2015-11-21 00:59:02 来源: 作者: 【 】 浏览:2
Tags:简单 词法 分析器 实现
编写分析器有 两种方法,一种是通过DFA对单词进行识别,二是通过直接编写程序进行识别。本程序采用DFA对单词进行识别。
DFA的实现方法,大概思想和书上一致,在程序中,则是用二维数组代表状态转换矩阵,用一维数组表示终态。
一个词法编辑要实现的 功能主要包括以下几点:

能够识别标识符、关键字、数字和运算符,对注释进行过滤,同时还能识别出程序错误。

使用说明:

本程序的输入由当前目录下的in.txt文件读取输入,输出为一系列二元式
#include
  
   
#include
   
     #include
    
      //用指针而不是二维数组表示,这样就不用指定字符串长度,只是不能修改指针所指向字符串的内容 char *key[]={"int","char","float","double","void","const","for","if","else","then","while","switch","break","main","return"}; char buffer[20];//存储当前识别出的单词 char *identifier[50]; char *num[50]; int ident_num;//标志符数 int number;//数字数 int judgement_num(char c) { if (c >= '0' && c <= '9') return 0; else if (c == '.') return 1; return -1; } int judge(char c){ if(c=='_') return 2; else if(c>='0'&&c<='9') return 1; else if(c>='a'&&c<='z'||c>='A'&&c<='Z') return 0; return -1; } //next_i,next_j分别代表着状态转换表move的当前状态与接收到的字符 //width代表每一状态可能遇到的状态数,length代表终态集大小 int DFA(int begin,int move[],int width,int final[],int length,int(*judge)(char)){ int len=0,next_i,next_j,tmp; char next_char; memset(buffer,0,sizeof(buffer)); next_char=getchar(); next_i=begin; while((next_j=judge(next_char))!=-1){ tmp=next_i; next_i=move[next_i*width+next_j]; if(next_i==-1){ printf("move[%d][%d]has not next state\n",tmp,next_j); return -1; } buffer[len++]=next_char; next_char=getchar(); } ungetc(next_char,stdin); buffer[len]='\0'; for(int i=0;i
     
      '||c=='<'){ next_char=getchar(); if(next_char=='=') printf("(rlop,%c=)\n",c); else { printf("(rlop,%c)\n",c); ungetc(next_char,stdin); } } else if(c=='!'){ next_char=getchar(); if(next_char=='=') printf("(!=,_)\n"); else { printf("(not,_)\n"); ungetc(next_char,stdin); } } else if(c=='|'){ next_char=getchar(); if (next_char == '|') printf("(or,||)\n"); else { ungetc(next_char, stdin); } } else if(c=='&'){ next_char=getchar(); if (next_char == '&') printf("(and,&&)\n"); else { ungetc(next_char, stdin); } } else if(c=='='||c=='('||c==')'||c=='['||c==']'||c==';'||c==','||c=='{'||c=='}'){ printf("(%c,_)\n",c); } } void is_digit(){ int begin=0; int move[]={1,-1,1,2,2,-1}; int final[]={1,2}; int result=-1; result=DFA(begin,move,2,final,2,judgement_num); if(result==-1){ printf("digit DFA error\n"); exit(-1); } else if(result==0){ //printf("%s\n",buffer); for(int i=0;i
       
      
     
    
   
  
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇poj 3537Crosses and Crosses 博.. 下一篇合并流SequenceInputStream

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: