Ladies_Man

complab9 json v2

Jun 12th, 2016
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.25 KB | None | 0 0
  1. %option reentrant noyywrap bison-bridge bison-locations
  2. %option extra-type="struct Extra *"
  3.  
  4. %{
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include "lexer.h"
  9. #include "parser.tab.h"
  10.  
  11. #define YY_USER_ACTION                                             \
  12.     {                                                          \
  13.         int i;                                             \
  14.         struct Extra *extra = yyextra;                     \
  15.         if (! extra->continued) {                          \
  16.             yylloc->first_line = extra->cur_line;      \
  17.             yylloc->first_column = extra->cur_column;  \
  18.         }                                                  \
  19.         extra->continued = 0;                              \
  20.         for (i = 0; i < yyleng; i++)                       \
  21.         {                                                  \
  22.             if (yytext[i] == '\n')                     \
  23.             {                                          \
  24.                 extra->cur_line++;                 \
  25.                 extra->cur_column = 1;             \
  26.             }                                          \
  27.             else                                       \
  28.                 extra->cur_column++;               \
  29.             }                                          \
  30.                                                            \
  31.         yylloc->last_line = extra->cur_line;               \
  32.         yylloc->last_column = extra->cur_column;           \
  33.     }
  34.  
  35. void yyerror(YYLTYPE *loc, yyscan_t scanner, long env[26], char *msg)
  36. {
  37.     printf("Error (%d,%d): %s\n", loc->first_line, loc->first_column, msg);
  38. }
  39.  
  40. %}
  41.  
  42. QUOTE       ["]
  43. CHAR        [a-zA-Z0-9_\/\\.]
  44. CHARS       {CHAR}+
  45. STRING      {QUOTE}{QUOTE}|{QUOTE}{CHARS}{QUOTE}
  46. DIGIT       [0-9]
  47. DIGIT1TO9   [1-9]
  48. DIGITS      {DIGIT}+
  49. INT         {DIGIT}|{DIGIT1TO9}{DIGITS}|-{DIGIT}|-{DIGIT1TO9}{DIGITS}
  50. FRAC        [.]{DIGITS}
  51. E           [eE][+-]?
  52. EXP         [E]{DIGITS}
  53. NUMBER      {INT}|{INT}{FRAC}|{INT}{EXP}|{INT}{FRAC}{EXP}
  54.  
  55. %%
  56.  
  57. [ \n\t]+    
  58.  
  59. true        { return VAL_TRUE; }
  60.  
  61. false       { return VAL_FALSE; }
  62.  
  63. null        { return VAL_NULL; }
  64.  
  65. {STRING}    { yylval->string = strdup(yytext); return STRING; }
  66.  
  67. {NUMBER}    { yylval->string = strdup(yytext); return NUMBER; }
  68.  
  69. \{          { return OBJ_BEG; }
  70.  
  71. \}          { return OBJ_END; }
  72.  
  73. \[          { return ARR_BEG; }
  74.  
  75. \]          { return ARR_END; }
  76.  
  77. :           { return SYM_COLON; }
  78.  
  79. ,           { return SYM_COMMA; }
  80.  
  81. %%
  82.  
  83. void init_scanner(char *program, yyscan_t *scanner, struct Extra *extra)
  84. {
  85.    extra->continued = 0;
  86.    extra->cur_line = 1;
  87.    extra->cur_column = 1;
  88.    yylex_init(scanner);
  89.    yylex_init_extra(extra, scanner);
  90.    yy_scan_string(program, *scanner);
  91. }
  92.  
  93. void destroy_scanner(yyscan_t scanner)
  94. {
  95.    yylex_destroy(scanner);
  96. }
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104. %{
  105. #include <stdio.h>
  106. #include "lexer.h"
  107. %}
  108.  
  109. %define api.pure
  110. %locations
  111. %lex-param {yyscan_t scanner}
  112. %parse-param {yyscan_t scanner}
  113. %parse-param {long env[26]}
  114.  
  115. %union {
  116.     char *string;
  117.     long num;
  118. }
  119.  
  120. %token <string> NUMBER
  121. %token <string> STRING
  122. %token VAL_TRUE VAL_FALSE VAL_NULL
  123. %left OBJ_BEG OBJ_END ARR_BEG ARR_END
  124. %left SYM_COMMA
  125. %left SYM_COLON
  126. %start START
  127.  
  128. %type<string> OBJECT ARRAY OBJ_BEG OBJ_END ARR_BEG ARR_END MEMBERS PAIR ELEMENTS VALUE
  129.  
  130. %{
  131. int yylex(YYSTYPE *yylval_param, YYLTYPE *yylloc_param , yyscan_t scanner);
  132. void yyerror(YYLTYPE *yylloc, yyscan_t scanner, long env[26], char *msg);
  133. %}
  134.  
  135. %%
  136.  
  137. START:      VALUE                       { printf("START{%s}\n", $1); }
  138.     ;
  139.  
  140. OBJECT:     OBJ_BEG OBJ_END             { $$ = "{}\n"; }
  141.     |       OBJ_BEG MEMBERS OBJ_END     { printf("\nOBJECT{%s}\n", $2); }
  142.     ;
  143.  
  144. MEMBERS:    PAIR                        { $$ = $1; }
  145.     |       PAIR SYM_COMMA MEMBERS      { printf("%s, %s ", $1, $3); }
  146.     ;
  147.  
  148. PAIR:       STRING SYM_COLON VALUE      { printf("(%s: %s)", $1, $3); }
  149.     ;
  150.  
  151. ARRAY:      ARR_BEG ARR_END             { $$ = "[]\n"; }
  152.     |       ARR_BEG ELEMENTS ARR_END    { printf("[%s]", $2); }
  153.     ;
  154.  
  155. ELEMENTS:   VALUE                       { $$ = $1; }
  156.     |       VALUE SYM_COMMA ELEMENTS    { printf("%s, %s ", $1, $3); }
  157.     ;
  158.  
  159. VALUE:      STRING                      { $$ = yylval.string; }
  160.     |       NUMBER                      { $$ = yylval.string; }
  161.     |       OBJECT                      { $$ = $1; }
  162.     |       ARRAY                       { $$ = $1; }
  163.     |       VAL_TRUE                    { $$ = "true"; }
  164.     |       VAL_FALSE                   { $$ = "false"; }
  165.     |       VAL_NULL                    { $$ = "null"; }
  166.     ;
  167.  
  168. %%
  169.  
  170. #define INPUT                                                   \
  171.     "{                                                          \
  172.         \"version\": 2.0,                                       \
  173.         \"background\":                                         \
  174.         {                                                       \
  175.             \"scripts\": [\"jquery214.js\", \"background.js\"], \
  176.             \"persistent\": false                               \
  177.         },                                                      \
  178.         \"content_scripts\":                                    \
  179.         {                                                       \
  180.             \"matches\":{\"<all_urls>\"},                       \
  181.             \"js\":     [],                                     \
  182.             \"run_at\": \"document_end\"                        \
  183.         }                                                       \
  184.     }"
  185.  
  186. int main()
  187. {
  188.     yyscan_t scanner;
  189.     struct Extra extra;
  190.     long env[26];
  191.     init_scanner(INPUT, &scanner, &extra);
  192.     yyparse(scanner, env);
  193.     destroy_scanner(scanner);
  194.     return 0;
  195. }
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204. #ifndef _LEXER_H_INCLUDED
  205. #define _LEXER_H_INCLUDED
  206.  
  207. #ifndef YY_TYPEDEF_YY_SCANNER_T
  208. #define YY_TYPEDEF_YY_SCANNER_T
  209. typedef void *yyscan_t;
  210. #endif
  211.  
  212. struct Extra {
  213.     int continued;
  214.     int cur_line;
  215.     int cur_column;
  216. };
  217.  
  218. void init_scanner(char *program, yyscan_t *scanner, struct Extra *extra);
  219. void destroy_scanner(yyscan_t scanner);
  220.  
  221. #endif
Advertisement
Add Comment
Please, Sign In to add comment