zoukankan      html  css  js  c++  java
  • 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>[ 
    	
    ]+ {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 == '') 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

  • 相关阅读:
    虚继承virtual public
    My first blog
    mybatis(一)SqlSessionFactory初始化
    dubbo
    设计模式
    基本算法
    redis
    spring cloud eureka
    spring boot
    spring MVC
  • 原文地址:https://www.cnblogs.com/xiaozong/p/6274448.html
Copyright © 2011-2022 走看看