分类:
2012-10-28 11:12:12
原文地址:Redy词法分析--字符串的识别 作者:NosicLin
代码下载: git clone git://git.code.sf.net/p/redy/code redy-code这一章内容有:
(二)字符串的识别用状态矩阵的方法识别字符串
在Redy字符串是以双引号开头,双引号结尾,中间可以是任意字符,但除'\'以外,'\'用于转义特殊的字符。
(1)BNF文法
- string ::='"'stringitem*'"'
- stringitem ::=
|'\'esc_item - esc_item ::=
(2)状态机
(3)状态矩阵
对于字符串来说,输入类型这么几种
- 引号 (quote)
- 换行符(newline)
- 反斜杠(backslash)
- 其它字符(other)
其中:
- esc_item包括:引号,换行符,反斜杠,其它字符。
- stringItem包括:其它字符。
状态矩阵为:
状态\输入
Other
Newline
Backslash
Quote
string_begin
String
String
string
string_esc
string_end
string_esc
String
String
String
String
string_end
4)程序数据:用一个一维数组来映射字符的输入类型:
- #define STRING_OTHER 0
- #define STRING_NEWLINE 1
- #define STRING_BACKSLASH 2
- #define STRING_QUITE 3
- char string_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,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,
- 0,0,0,0,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
- };
状态矩阵为:
- #define STRING_STATE_TYPES 4
- #define STRING_INPUT_TYPES 4
- #define STRING_STATE_BEGIN 0
- #define STRING_STATE_STRING 1
- #define STRING_STATE_ESC 2
- #define STRING_STATE_END 3
- #define STRING_STATE_ERR LEX_ERR
- /*Other | Newline | BackSlash | Quite */
- int string_automaton[STRING_STATE_TYPES*STRING_INPUT_TYPES]=
- {
- /*STRING_STATE_BEGIN*/
- STRING_STATE_ERR,STRING_STATE_ERR,STRING_STATE_ERR,STRING_STATE_STRING,
- /*STRING_STATE_STRING*/
- STRING_STATE_STRING,STRING_STATE_ERR,STRING_STATE_ESC,STRING_STATE_END,
- /*STRING_STATE_ESC*/
- STRING_STATE_STRING,STRING_STATE_STRING,STRING_STATE_STRING,STRING_STATE_STRING,
- /*STRING_STATE_END*/
- STRING_STATE_ERR,STRING_STATE_ERR,STRING_STATE_ERR,STRING_STATE_ERR
- };
状态信息:然后再把这些信息集合起来,交结我们的驱动程序来处理:
- char string_state[STRING_STATE_TYPES]={0,0,0,1};
- struct automaton am_string=
- {
- STRING_STATE_TYPES,
- STRING_INPUT_TYPES,
- string_type_map,
- STRING_STATE_BEGIN,
- string_automaton,
- string_state
- };
由于驱动程序和测试程序和变量识别是一样的,这里我就不贴出了(5)运行结果
返回文档首页所有的代码都可以在下载下的文件夹的tutorial/lexical/string里面找到