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

全部博文(32)

文章存档

2012年(32)

分类: Python/Ruby

2012-03-02 11:42:30



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

(二)字符串的识别
在Redy字符串是以双引号开头,双引号结尾,中间可以是任意字符,但除'\'以外,'\'用于转义特殊的字符。
(1)BNF文法
  1. string ::='"'stringitem*'"'
  2. stringitem ::= |'\'esc_item
  3. esc_item ::=
(2)状态机
(3)状态矩阵
对于字符串来说,输入类型这么几种
  1. 引号 (quote)
  2. 换行符(newline)
  3. 反斜杠(backslash)
  4. 其它字符(other)
其中:
  1. esc_item包括:引号,换行符,反斜杠,其它字符。
  2. stringItem包括:其它字符。
状态矩阵为:

状态\输入

Other

Newline

Backslash

Quote

string_begin




String

String

string


string_esc

string_end

string_esc

String

String

String

String

string_end






4)程序数据:
用一个一维数组来映射字符的输入类型:

  1. #define STRING_OTHER 0
  2. #define STRING_NEWLINE 1
  3. #define STRING_BACKSLASH 2
  4. #define STRING_QUITE 3
  5. char string_type_map[ASSIC_NUM]=
  6. {
  7.     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,
  8.     0,0,3,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,
  9.     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,2,0,0,0,
  10.     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,
  11.     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,
  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. };

状态矩阵为:

  1. #define STRING_STATE_TYPES 4
  2. #define STRING_INPUT_TYPES 4

  3. #define STRING_STATE_BEGIN 0
  4. #define STRING_STATE_STRING 1
  5. #define STRING_STATE_ESC 2
  6. #define STRING_STATE_END 3

  7. #define STRING_STATE_ERR LEX_ERR

  8. /*Other | Newline | BackSlash | Quite */
  9. int string_automaton[STRING_STATE_TYPES*STRING_INPUT_TYPES]=
  10. {
  11.     /*STRING_STATE_BEGIN*/
  12.     STRING_STATE_ERR,STRING_STATE_ERR,STRING_STATE_ERR,STRING_STATE_STRING,
  13.     /*STRING_STATE_STRING*/
  14.     STRING_STATE_STRING,STRING_STATE_ERR,STRING_STATE_ESC,STRING_STATE_END,
  15.     /*STRING_STATE_ESC*/
  16.     STRING_STATE_STRING,STRING_STATE_STRING,STRING_STATE_STRING,STRING_STATE_STRING,
  17.     /*STRING_STATE_END*/
  18.     STRING_STATE_ERR,STRING_STATE_ERR,STRING_STATE_ERR,STRING_STATE_ERR
  19. };
状态信息:

  1. char string_state[STRING_STATE_TYPES]={0,0,0,1};
然后再把这些信息集合起来,交结我们的驱动程序来处理:

  1. struct automaton am_string=
  2. {
  3.     STRING_STATE_TYPES,
  4.     STRING_INPUT_TYPES,
  5.     string_type_map,
  6.     STRING_STATE_BEGIN,
  7.     string_automaton,
  8.     string_state
  9. };
由于驱动程序和测试程序和变量识别是一样的,这里我就不贴出了

(5)运行结果


所有的代码都可以在下载下的文件夹的tutorial/lexical/string里面找到


返回文档首页













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

我要去鸟巢2012-03-03 08:48:09

恩,不错不错