Chinaunix首页 | 论坛 | 博客
  • 博客访问: 327405
  • 博文数量: 32
  • 博客积分: 424
  • 博客等级: 准尉
  • 技术积分: 465
  • 用 户 组: 普通用户
  • 注册时间: 2012-03-02 10:23
文章分类

全部博文(32)

文章存档

2012年(32)

分类: Python/Ruby

2012-03-02 12:10:07


(一)简介
代码下载: git clone git://git.code.sf.net/p/redy/code redy-code
这一章内容有:
用状态矩阵的方法识别字符串

(二)注释的识别
在Redy中,注释是以符号'#'开头,后面可以接任意字符,直到这一行结束。
(1)状态机
 
(2)状态矩阵
对于注释来说,输入类型可以分为这么几种
  1. 符号'#'   (Pound)
  2. 换行符   (newline)
  3. 除以上字符的所有字符  (other)
其中 AnnoItem包括 Pound,other

状态矩阵为:

状态\输入

Other

Newlin

Pound

AnnotateBegin



Anotate

Anotate

Anotate

AnotateEnd

Anotate

AnotateEnd





其中开始状态为AnnotateBegin,终态为AnotateEnd

(3)程序数据
字符所属类型映射:
  1. enum ANNO_INPUT_TYPTE
  2. {
  3.     ANNO_OTHER=0,
  4.     ANNO_NEWLINE,
  5.     ANNO_POUND,
  6.     ANNO_INPUT_NUM,
  7. };

  8. char annotate_type_map[ASSIC_NUM]=
  9. {
  10.     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,
  11.     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,
  12.     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,
  13.     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,
  14.     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,
  15.     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,
  16.     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,
  17.     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

  18. };
状态矩阵:
  1. #define ANNO_STATE_NUM 3
  2. #define ANNO_BEGIN 0
  3. #define ANNO_ANNOTATE 1
  4. #define ANNO_END 2
  5. /* Other | Newlin | Pound */
  6. int annotate_automaton[ANNO_STATE_NUM*ANNO_INPUT_NUM]=
  7. {
  8.     LEX_ERR,LEX_ERR,ANNO_ANNOTATE,
  9.     ANNO_ANNOTATE,ANNO_END,ANNO_ANNOTATE,
  10.     LEX_ERR,LEX_ERR,LEX_ERR,
  11. };
状态信息:
  1. char anno_state[ANNO_STATE_NUM]={0,0,1};
然后再把这些信息集合起来,交结我们的驱动程序来处理,驱动程序与前面的介变量识别的一样,但是主函数有一点变化,因为要在输入的字符串包含换行符,所以不能用scanf函数,这里使用的时getline从标准输入中读取一行,主函数为:
  1. int main()
  2. {
  3.     char* buf;
  4.     int n;
  5.     char buf_copy[2048];
  6.     printf("input:__quit__ exit\n");
  7.     printf("input:");

  8.     getline(&buf,&n,stdin);
  9.     while(strcmp(buf,"__quit__")!=0)
  10.     {
  11.         int ret=driver(&am_anno,buf);
  12.         if(ret==-1)
  13.         {
  14.             printf("sorry,Not Anno\n");
  15.         }
  16.         else
  17.         {
  18.             memcpy(buf_copy,buf,ret+1);
  19.             buf_copy[ret+1]='\0';
  20.             printf("Recongise: %s\n",buf_copy);
  21.             if(ret+1==strlen(buf))
  22.             {
  23.                 printf("It's Anno\n");
  24.             }
  25.             else
  26.             {
  27.                 printf("It's Not Anno\n");
  28.             }
  29.         }
  30.         printf("\ninput:");
  31.         getline(&buf,&n,stdin);
  32.     }
  33.     return 0;
  34. }

(4)运行结果
关于注释识别的源程序,大家可以在tutorial/lexical/annotate下面找到



阅读(2482) | 评论(2) | 转发(1) |
给主人留下些什么吧!~~

NosicLin2012-03-03 09:23:51

我要去鸟巢: 这是编译原理吗?.....
恩,编译原理中词法识别的一部分

我要去鸟巢2012-03-03 08:43:58

这是编译原理吗?