分类:
2012-10-28 11:12:01
原文地址:Redy词法分析--注释的识别 作者:NosicLin
代码下载: git clone git://git.code.sf.net/p/redy/code redy-code这一章内容有:
(二)注释的识别用状态矩阵的方法识别字符串
在Redy中,注释是以符号'#'开头,后面可以接任意字符,直到这一行结束。(1)状态机(2)状态矩阵
对于注释来说,输入类型可以分为这么几种
- 符号'#' (Pound)
- 换行符 (newline)
- 除以上字符的所有字符 (other)
(3)程序数据其中 AnnoItem包括 Pound,other状态矩阵为:
状态\输入
Other
Newlin
Pound
AnnotateBegin
Anotate
Anotate
Anotate
AnotateEnd
Anotate
AnotateEnd
其中开始状态为AnnotateBegin,终态为AnotateEnd字符所属类型映射:
- enum ANNO_INPUT_TYPTE
- {
- ANNO_OTHER=0,
- ANNO_NEWLINE,
- ANNO_POUND,
- ANNO_INPUT_NUM,
- };
- char annotate_type_map[ASSIC_NUM]=
- {
- 0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
- };
状态矩阵:
- #define ANNO_STATE_NUM 3
- #define ANNO_BEGIN 0
- #define ANNO_ANNOTATE 1
- #define ANNO_END 2
- /* Other | Newlin | Pound */
- int annotate_automaton[ANNO_STATE_NUM*ANNO_INPUT_NUM]=
- {
- LEX_ERR,LEX_ERR,ANNO_ANNOTATE,
- ANNO_ANNOTATE,ANNO_END,ANNO_ANNOTATE,
- LEX_ERR,LEX_ERR,LEX_ERR,
- };
状态信息:
- char anno_state[ANNO_STATE_NUM]={0,0,1};
然后再把这些信息集合起来,交结我们的驱动程序来处理,驱动程序与前面的介变量识别的一样,但是主函数有一点变化,因为要在输入的字符串包含换行符,所以不能用scanf函数,这里使用的时getline从标准输入中读取一行,主函数为:
- int main()
- {
- char* buf;
- int n;
- char buf_copy[2048];
- printf("input:__quit__ exit\n");
- printf("input:");
- getline(&buf,&n,stdin);
- while(strcmp(buf,"__quit__")!=0)
- {
- int ret=driver(&am_anno,buf);
- if(ret==-1)
- {
- printf("sorry,Not Anno\n");
- }
- else
- {
- memcpy(buf_copy,buf,ret+1);
- buf_copy[ret+1]='\0';
- printf("Recongise: %s\n",buf_copy);
- if(ret+1==strlen(buf))
- {
- printf("It's Anno\n");
- }
- else
- {
- printf("It's Not Anno\n");
- }
- }
- printf("\ninput:");
- getline(&buf,&n,stdin);
- }
- return 0;
- }
(4)运行结果关于注释识别的源程序,大家可以在tutorial/lexical/annotate下面找到