#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>[ ]+ {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 ", "T_BEGIN");break; case T_NUMBER: printf("%s ", "T_NUMBER");break; case T_LOWER_CHAR: printf("%s ", "T_LOWER_CHAR");break; case T_UPPER_CHAR: printf("%s ", "T_UPPER_CHAR");break; case T_EXIT: printf("%s ", "T_EXIT");break; case T_UNKNOWN: printf("%s ", "T_UNKNOWN");break; case T_INPUT_ERROR: printf("%s ", "T_INPUT_ERROR");break; case T_END: printf("%s ", "T_END");break; } } int main(int argc, char* argv[]) { int token; BEGIN(INITIAL); // 全局初始化,需要放在scan调用之前 scanner_globals.yy_cursor = argv[1]; //将输入的第一个参数作为要解析的字符串 while(1) { if(*YYCURSOR == '