Ladies_Man

complab9 JSON Flex

Jun 12th, 2016
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.82 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. #define TAG_TRUE        5
  13. #define TAG_FALSE       6
  14. #define TAG_NULL        7
  15. #define TAG_OBJ_BEG     8
  16. #define TAG_OBJ_END     9
  17. #define TAG_ARR_BEG     10
  18. #define TAG_ARR_END     11
  19. #define TAG_SYM_COMMA     12
  20. #define TAG_SYM_COLON     13
  21.  
  22. char *tag_names[] = {
  23.     "END_OF_PROGRAM",
  24.     "IDENT", "NUMBER", "STRING", "ERROR",
  25.     "TRUE", "FALSE", "NULL",
  26.     "OBJ_BEG", "OBJ_END",
  27.     "ARR_BEG", "ARR_END",
  28.     "SYM_COMMA", "SYM_COLON"
  29. };
  30.  
  31. struct Position {
  32.     int line, pos, index;
  33. };
  34.  
  35. void print_pos(struct Position *p) {
  36.     printf("(%d, %d)", p->line, p->pos);
  37. }
  38.  
  39. struct Fragment {
  40.     struct Position starting, following;
  41. };
  42.  
  43. typedef struct Fragment YYLTYPE;
  44.  
  45. void print_frag(struct Fragment *f) {
  46.     print_pos(&(f->starting));
  47.     printf(" - ");
  48.     print_pos(&(f->following));
  49. }
  50.  
  51. union Token {
  52.     char *tokenName;
  53. };
  54.  
  55. typedef union Token YYSTYPE;
  56.  
  57. int continued;
  58. struct Position cur;
  59.  
  60. #define YY_USER_ACTION                  \
  61. {                                       \
  62.     int i;                              \
  63.     if (!continued)                     \
  64.         yylloc->starting = cur;         \
  65.     continued = 0;                      \
  66.     for (i = 0; i < yyleng; i++) {      \
  67.         if ('\n' == yytext[i]) {        \
  68.             cur.line++;                 \
  69.             cur.pos = 1;                \
  70.         }                               \
  71.         else                            \
  72.             cur.pos++;                  \
  73.         cur.index++;                    \
  74.     }                                   \
  75.     yylloc->following = cur;            \
  76. }                                       \
  77.  
  78. void init_scanner(char *program) {
  79.     continued = 0;
  80.     cur.line = 1;
  81.     cur.pos = 1;
  82.     cur.index = 0;
  83.     yy_scan_string(program);
  84. }
  85.  
  86. void err(char *msg) {
  87.     printf("Error ");
  88.     print_pos(&cur);
  89.     printf(": %s\n", msg);
  90. }
  91.  
  92. %}
  93.  
  94. QUOTE       ["]
  95. CHAR        [a-zA-Z0-9_\/\\.]
  96. CHARS       {CHAR}+
  97. STRING      {QUOTE}{QUOTE}|{QUOTE}{CHARS}{QUOTE}
  98. DIGIT       [0-9]
  99. DIGIT1TO9   [1-9]
  100. DIGITS      {DIGIT}+
  101. INT         {DIGIT}|{DIGIT1TO9}{DIGITS}|-{DIGIT}|-{DIGIT1TO9}{DIGITS}
  102. FRAC        [.]{DIGITS}
  103. E           [eE][+-]?
  104. EXP         [E]{DIGITS}
  105. NUMBER      {INT}|{INT}{FRAC}|{INT}{EXP}|{INT}{FRAC}{EXP}
  106.  
  107.  
  108. %%
  109.  
  110. [ \n\t]+    /*ignore ws*/
  111.  
  112. true        { yylval->tokenName = yytext; return TAG_TRUE; }
  113.  
  114. false       { yylval->tokenName = yytext; return TAG_FALSE; }
  115.  
  116. null        { yylval->tokenName = yytext; return TAG_NULL; }
  117.  
  118. {STRING}    { yylval->tokenName = yytext; return TAG_STRING; }
  119.  
  120. {NUMBER}    { yylval->tokenName = yytext; return TAG_NUMBER; }
  121.  
  122. \{          { yylval->tokenName = yytext; return TAG_OBJ_BEG; }
  123.  
  124. \}          { yylval->tokenName = yytext; return TAG_OBJ_END; }
  125.  
  126. \[          { yylval->tokenName = yytext; return TAG_ARR_BEG; }
  127.  
  128. \]          { yylval->tokenName = yytext; return TAG_ARR_END; }
  129.  
  130. :           { yylval->tokenName = yytext; return TAG_SYM_COLON; }
  131.  
  132. ,           { yylval->tokenName = yytext; return TAG_SYM_COMMA; }
  133.  
  134. <<EOF>>     return 0;
  135.  
  136. .           {
  137.               err("Unexpected character");
  138.               return TAG_ERROR;
  139.            }
  140.  
  141. %%
  142.  
  143. #define PROGRAM "{ \"scripts\": [\"jquery.js\", \"background.js\"], \"persistent\": false }"
  144.  
  145. int main()
  146. {
  147.     int tag;
  148.     YYSTYPE value;
  149.     YYLTYPE coords;
  150.  
  151.     init_scanner(PROGRAM);
  152.  
  153.     do {
  154.         tag = yylex(&value, &coords);
  155.         if (0 != tag && TAG_ERROR != tag) {
  156.           printf("%s ", tag_names[tag]);
  157.             print_frag(&coords);
  158.             printf(": %s\n", value.tokenName);
  159.         }
  160.     } while (0 != tag);
  161.  
  162.     return 0;
  163. }
Advertisement
Add Comment
Please, Sign In to add comment