宗炳煌-PHP程序猿

欢迎各位朋友前来,如果我的文章中有不对的地方,还望指正;如需转载,请注明原处

re2c实例

#include <stdio.h>
#include "demo_def.h"

#define T_BEGIN 0
#define T_NUMBER 1
#define T_LOWER_CHAR 2
#define T_UPPER_CHAR 3
#define T_EXIT 4
#define T_UNKNOWN 5
#define T_INPUT_ERROR 6
#define T_END 7
#define T_WHITESPACE 8

typedef struct _scanner {
  char *yy_cursor;
  int yy_state;
  char *yy_limit;
  char *yy_marker;
} Scanner;

Scanner scanner_globals;
 
#define YYCTYPE char
#define YYFILL(n) 
#define STATE(name)  yyc##name
#define BEGIN(state) YYSETCONDITION(STATE(state))
#define LANG_SCNG(v) (scanner_globals.v)
#define SCNG    LANG_SCNG
 
#define YYGETCONDITION()  SCNG(yy_state)
#define YYSETCONDITION(s) SCNG(yy_state) = s
#define YYCURSOR  SCNG(yy_cursor)
#define YYLIMIT   SCNG(yy_limit)
#define YYMARKER  SCNG(yy_marker)
 
int scan(){
    /*!re2c
 
      <INITIAL>"<?php" {BEGIN(ST_IN_SCRIPTING); return T_BEGIN;}
      <ST_IN_SCRIPTING>[0-9]+ {return T_NUMBER;}
      <ST_IN_SCRIPTING>[ \n\t\r]+ {return T_WHITESPACE;}
      <ST_IN_SCRIPTING>"exit" { return T_EXIT; }
      <ST_IN_SCRIPTING>[a-z]+ {return T_LOWER_CHAR;}
      <ST_IN_SCRIPTING>[A-Z]+ {return T_UPPER_CHAR;}
      <ST_IN_SCRIPTING>"?>" {return T_END;}
 
      <ST_IN_SCRIPTING>[^] {return T_UNKNOWN;}
      <*>[^] {return T_INPUT_ERROR;}
     */
}
 
void print_token(int token) {
    switch (token) {
        case T_BEGIN: printf("%s\n", "T_BEGIN");break;
        case T_NUMBER: printf("%s\n", "T_NUMBER");break;
        case T_LOWER_CHAR: printf("%s\n", "T_LOWER_CHAR");break;
        case T_UPPER_CHAR: printf("%s\n", "T_UPPER_CHAR");break;
        case T_EXIT: printf("%s\n", "T_EXIT");break;
        case T_UNKNOWN: printf("%s\n", "T_UNKNOWN");break;
        case T_INPUT_ERROR: printf("%s\n", "T_INPUT_ERROR");break;
        case T_END: printf("%s\n", "T_END");break;
    }
}
 
int main(int argc, char* argv[])
{
    int token;
    BEGIN(INITIAL); //  全局初始化,需要放在scan调用之前
    scanner_globals.yy_cursor = argv[1];    //将输入的第一个参数作为要解析的字符串
 
    while(1) {
        if(*YYCURSOR == '\0') break;
        token = scan();
        print_token(token);
    }
 
    return 0;
}

 

编译,运行,结果

[root@bhu les5]# re2c -o demo.c -c -t demo_def.h demo.l

[root@bhu les5]# gcc -o demo demo.c -g

[root@bhu les5]# ./demo "<?php 3ee344SDEFeee@#?>"

T_BEGIN

T_NUMBER

T_LOWER_CHAR

T_NUMBER

T_UPPER_CHAR

T_LOWER_CHAR

T_UNKNOWN

T_UNKNOWN

T_END

posted @ 2017-01-11 17:13  宗炳煌  阅读(...)  评论(...编辑  收藏