Advertisement
Ladies_Man

flex

Apr 25th, 2016
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.76 KB | None | 0 0
  1. %option noyywrap bison-bridge bison-locations
  2.  
  3. %{
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7.  
  8. #define TAG_IDENT       1
  9. #define TAG_NUMBER      2
  10. #define TAG_STRING      3
  11. #define TAG_ERROR       4
  12.  
  13. char *tag_names[] = {
  14.     "END_OF_PROGRAM", "IDENT", "NUMBER", "STRING", "ERROR"
  15. };
  16.  
  17. struct Position {
  18.     int line, pos, index;
  19. };
  20.  
  21. void print_pos(struct Position *p) {
  22.     printf("(%d, %d)", p->line, p->pos);
  23. }
  24.  
  25. struct Fragment {
  26.     struct Position starting, following;
  27. };
  28.  
  29. typedef struct Fragment YYLTYPE;
  30.  
  31. void print_frag(struct Fragment *f) {
  32.     print_pos(&(f->starting));
  33.     printf(" - ");
  34.     print_pos(&(f->following));
  35. }
  36.  
  37. union Token {
  38.     char *ident;
  39. };
  40.  
  41. typedef union Token YYSTYPE;
  42.  
  43. int continued;
  44. struct Position cur;
  45.  
  46. # define YY_USER_ACTION                 \
  47. {                                       \
  48.     int i;                              \
  49.     if (!continued)                     \
  50.         yylloc->starting = cur;         \
  51.     continued = 0;                      \
  52.     for (i = 0; i < yyleng; i++) {      \
  53.         if ('\n' == yytext[i]) {        \
  54.             cur.line++;                 \
  55.             cur.pos = 1;                \
  56.         }                               \
  57.         else                            \
  58.             cur.pos++;                  \
  59.         cur.index++;                    \
  60.     }                                   \
  61.     yylloc->following = cur;            \
  62. }                                       \
  63.  
  64. void init_scanner(char *program) {
  65.     continued = 0;
  66.     cur.line = 1;
  67.     cur.pos = 1;
  68.     cur.index = 0;
  69.     yy_scan_string(program);
  70. }
  71.  
  72. void err(char *msg) {
  73.     printf("Error ");
  74.     print_pos(&cur);
  75.     printf(": %s\n", msg);
  76. }
  77.  
  78. %}
  79.  
  80. STRING      ["]([^"]|["]["]|[\\][\n])*["]
  81. NUMBER      [0-9]+|\$0[xX][a-fA-F0-9]+
  82. IDENT       [a-zA-Z]+[a-zA-Z0-9\$]*
  83.  
  84. %%
  85.  
  86. [\n\t ]+
  87.  
  88. {IDENT}     {
  89.                yylval->ident = yytext;
  90.                return TAG_IDENT;
  91.            }
  92.  
  93. {NUMBER}    {
  94.                yylval->ident = yytext;
  95.                return TAG_NUMBER;
  96.            }
  97.  
  98. {STRING}    {
  99.                yylval->ident = yytext;
  100.                return TAG_STRING;
  101.            }
  102.  
  103. <<EOF>>     return 0;
  104.  
  105. .           {
  106.                err("Unexpected character\n");
  107.                return TAG_ERROR;
  108.            }
  109.  
  110. %%
  111.  
  112. #define PROGRAM "1337 \"string with \"\" doubled quotes\"\" and \\ \n linebreak\" $0xFF Ident 3RR0R L33t"
  113.  
  114. int main()
  115. {
  116.     int tag;
  117.     YYSTYPE value;
  118.     YYLTYPE coords;
  119.  
  120.     init_scanner(PROGRAM);
  121.  
  122.     do {
  123.         tag = yylex(&value, &coords);
  124.         if (0 != tag) {
  125.           printf("%s ", tag_names[tag]);
  126.             print_frag(&coords);
  127.             printf(": %s\n", value.ident);
  128.         }
  129.     } while (0 != tag);
  130.  
  131.     return 0;
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement