Ladies_Man

complab9 json v3

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