zoukankan      html  css  js  c++  java
  • Flex 替换输入的字符串

    #include <iostream>
    #include "lexer.h"
    
    #define yylex aaslex
    #define yy_scan_string aas_scan_string
    #define yy_delete_buffer aas_delete_buffer
    extern char* yylex(std::string& res);
    
    int main()
    {
      std::string src = 
        "define(def1, 1 + 2)
    "
        "define(def2,eax,1)
    "
        "define(def3,0x22222)
    "
        "define(def4,01 def3 02)
    "
        "define(def5)
    "
    
        "def3:
    "
        "db 90 90 #90 def4 // db 指令 
    "
        "mov def2
    
    "
        "lea	eax [def1]
    "
        ;
    
      YY_BUFFER_STATE state = yy_scan_string(src.data());
      std::string res;
      yylex(res);
      yy_delete_buffer(state);
      std::cout << res << "
    ";
    }
    

    lexer.l:

    %option noyywrap noline
    %option prefix="aas"
    %option header-file="lexer.h"
    
    %{
    #include <iostream>
    #include <string>
    #include <map>
    
    #define YY_DECL char * yylex (std::string& res)
    #define O(v) res += v
    #define GET_DEFINE (defineMap.count(yytext) ? defineMap[yytext] : yytext)
    
    // save dafines
    std::string cur_define_key;
    std::string cur_define_val;
    std::map<std::string, std::string> defineMap;
    %}
    
    
    ID  (?i:[a-z][a-z0-9]*)
    
    %x define define_val
    %%
    
    <*>"//".* { /* eat comment */ }
    
    "define(" { BEGIN(define); }
    <define>{ID}  { cur_define_key = yytext;  }
    <define>","   { BEGIN(define_val); cur_define_val = ""; }
    <define_val>[ 	]+ { cur_define_val += yytext; }
    <define_val>{ID} { cur_define_val += GET_DEFINE;  }
    <define_val>[^)
    ] { cur_define_val += yytext; }
    <define_val>")"  { /* define end */ defineMap[cur_define_key] = cur_define_val; BEGIN(0); }
    <define>")"  { printf("Error: 必须要有一个参数
    "); BEGIN(0); }
    
    {ID} { O( GET_DEFINE );  }
    
     { O(yytext); }
    . { O(yytext); }
    
    <<EOF>> { 
      std::cout << "===================================" << "
    ";
      for (auto i = defineMap.begin(); i != defineMap.end(); i++)
      {
        printf("key:%s, value:%s
    ", i->first.data(), i->second.data());
      }
      std::cout << "===================================" << "
    ";
      defineMap.clear();
    
      return NULL; 
    }
    
    %%
    
    

    print:

    Error: 必须要有一个参数
    ===================================
    key:def1, value: 1 + 2
    key:def2, value:eax,1
    key:def3, value:0x22222
    key:def4, value:01 0x22222 02
    ===================================
    
    
    
    
    
    0x22222:
    db 90 90 #90 01 0x22222 02
    mov eax,1
    
    lea     eax [ 1 + 2]
    
  • 相关阅读:
    Educational Codeforces Round 99 (Rated for Div. 2) (FG咕咕)
    2020 ccpc 威海
    【dp每日一题】CF 543A. Writing Code
    【dp每日一题】CF543C. Remembering Strings
    【dp每日一题】CF545C. Woodcutters
    【dp每日一题】CF566F. Clique in the Divisibility Graph
    Codeforces Round #686 (Div. 3)
    【dp每日一题】CF 559C. Gerald and Giant Chess
    2019 ICPC Asia Yinchuan Regional
    Educational Codeforces Round 98 (Rated for Div. 2)
  • 原文地址:https://www.cnblogs.com/ajanuw/p/14848728.html
Copyright © 2011-2022 走看看