2012年(32)
分类: Python/Ruby
2012-03-07 18:25:34
代码下载: git clone git://git.code.sf.net/p/redy/code redy-code
这一章的内容有:
这一章大家会看到一个完整的大的状态机,用于识别Redy语言中所有的单词。
- /*merge parts*/
- extern struct state me_begin;
- extern struct state me_period;
a)状态LexicalBegin输入类型有下面这么27种:
- 符号@,下画线,以及字母 (TO_ID)
- 数字0 (D_0)
- 数字1到9 (D1_9)
- 双引号 (D_QUOTE)
- 空格和制表符 (WS)
- 换行符 (NewLine)
- 分号 (Semicolon)
- 19种运算符
- '(' ')' '[' ']' '.' ','
- '+' '-' '~' '*' '/' '%'
- '<' '>' '=' '!'
- '&' '^' '|'
- 除以上字符以外的所有字符 (Other)
用一个一维数组来映射状态LexicalBegin接受符号的输入类型:
- enum MERGE_INPUT_TYPE
- {
- ME_OTHER=0,
- ME_TO_ID,
- ME_D0,
- ME_D1_9,
- ME_QUOTE,
- ME_WS,
- ME_NEWLINE,
- ME_SEMICOLON,
- ME_COMMA,
- ME_PERIOD,
- ME_REVERSE,
- ME_L_RB,
- ME_R_RB,
- ME_L_SB,
- ME_R_SB,
- ME_EXCLAMATION,
- ME_AMPERSAND,
- ME_BAR,
- ME_CARET,
- ME_STAR,
- ME_PERCENT,
- ME_MINUS,
- ME_PLUS,
- ME_DIVIDE,
- ME_EQUAL,
- ME_GREATER,
- ME_LESS,
- ME_INPUT_NUM
- };
- char merge_input_map[ASCII_NUM]=
- {
- 0,0,0,0,0,0,0,0,0,5,6,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
- 5,15,4,0,0,20,16,0,11,11,19,22,8,21,9,23,2,3,3,3,3,3,3,3,3,3,0,7,26,24,25,0,
- 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,13,0,14,18,0,
- 0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,17,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,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
- };
后继状态为:
- struct state* me_begin_targets[]=
- {
- &lex_state_err, /*ME_OTHER*/
- &id_identifier, /*ME_TO_ID*/
- &nu_zero, /*ME_D0*/
- &nu_number, /*ME_D1_9*/
- &st_string, /*ME_QUOTE*/
- &ws_ws, /*ME_WS*/
- &sb_newline, /*ME_NEWLINE*/
- &sb_semicolon, /*ME_SEMICOLON*/
- &op_comma, /*ME_COMMA*/
- &me_period, /*ME_PERIOD*/
- &op_reverse, /*ME_REVERSE*/
- &op_l_rb, /*ME_L_RB*/
- &op_r_rb, /*ME_R_RB*/
- &op_l_sb, /*ME_L_SB*/
- &op_r_sb, /*ME_R_SB*/
- &op_not_equal_begin,/*ME_EXCLAMATION*/
- &op_bits_and, /*ME_AMPERSAND*/
- &op_bits_or, /*ME_BAR*/
- &op_bits_xor, /*ME_CARET*/
- &op_multiply, /*ME_STAR*/
- &op_mod, /*ME_PERCENT*/
- &op_minus, /*ME_MINUS*/
- &op_plus, /*ME_PLUS*/
- &op_divide, /*ME_DIVIDE*/
- &op_assign, /*ME_EQUAL*/
- &op_greater_than, /*ME_GREATER*/
- &op_less_than, /*ME_LESS*/
- };
状态LexicalBegin为:
- struct state me_begin=
- {
- "me_begin",
- TOKEN_UNKOWN,
- ME_INPUT_NUM,
- merge_input_map,
- 0,
- me_begin_targets,
- 0,
- };
b)状态lexicalPointLexicalPoint只在输入类型D0_9下转移到Float::FractionBegin,输入映射数组:
- char me_period_input_map[ASCII_NUM]=
- {
- 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,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,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
- };
后继状态为:
- struct state* me_period_targtes[]=
- {
- &lex_state_err,
- &ft_fraction,
- };
状态LexicalPoint:
- struct state me_period=
- {
- "me_period",
- TOKEN_DIVIDE,
- 2,
- me_period_input_map,
- 0,
- me_period_targtes,
- 1,
- };
到现在为止,已经把两个状态的状态链数据构造完成,七个小的状态机就被这两个状态链接了在一起,形成一个大的综合性的状态机,该状态机可以识别Redy语言中的所有单词。