Ladies_Man

complab9 json v1

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