Ladies_Man

#COMPLR Lab9 lexer.l lexer.h

Jun 29th, 2016
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.94 KB | None | 0 0
  1. JSON. слабый форматтер
  2.  
  3.  
  4. lexer.l
  5.  
  6. %option reentrant noyywrap bison-bridge bison-locations
  7. %option extra-type="struct Extra *"
  8.  
  9. %{
  10.  
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include "lexer.h"
  15. #include "parser.tab.h"
  16.  
  17. #define FLOAT_TYPENAME "float"
  18. #define INT_TYPENAME "int"
  19.  
  20. #define YY_USER_ACTION                                      \
  21.     {                                                       \
  22.         int i;                                              \
  23.         struct Extra *extra = yyextra;                      \
  24.         if (! extra->continued) {                           \
  25.             yylloc->first_line = extra->cur_line;           \
  26.             yylloc->first_column = extra->cur_column;       \
  27.         }                                                   \
  28.         extra->continued = 0;                               \
  29.         for (i = 0; i < yyleng; i++)                        \
  30.         {                                                   \
  31.             if (yytext[i] == '\n')                          \
  32.             {                                               \
  33.                 extra->cur_line++;                          \
  34.                 extra->cur_column = 1;                      \
  35.             }                                               \
  36.             else                                            \
  37.                 extra->cur_column++;                        \
  38.             }                                               \
  39.                                                             \
  40.         yylloc->last_line = extra->cur_line;                \
  41.         yylloc->last_column = extra->cur_column;            \
  42.     }
  43.  
  44. void yyerror(YYLTYPE *loc, yyscan_t scanner, char *msg)
  45. {
  46.     printf("Error (%d,%d): %s\n", loc->first_line, loc->first_column, msg);
  47. }
  48.  
  49. %}
  50.  
  51. HEX_DIGIT       [0-9a-f]
  52. UNICODE         \\u{HEX_DIGIT}{HEX_DIGIT}{HEX_DIGIT}{HEX_DIGIT}
  53. UNESCAPED       [ -!#-\[\]-~]
  54. ESCAPED         [\n]|[\t]
  55.  
  56. CHAR            {UNICODE}|{UNESCAPED}|{ESCAPED}
  57. STRING          ["]["]|["]{CHAR}+["]
  58.  
  59. DIGIT           [0-9]
  60. DIGIT1TO9       [1-9]
  61. DIGITS          {DIGIT}+
  62. INT             {DIGIT}|{DIGIT1TO9}{DIGITS}|-{DIGIT}|-{DIGIT1TO9}{DIGITS}
  63. FRAC            [.]{DIGITS}
  64. EXP             [eE][+-]?{DIGITS}
  65. NUMBER          {INT}|{INT}{FRAC}|{INT}{EXP}|{INT}{FRAC}{EXP}
  66.  
  67. %%
  68.  
  69. [ ]+
  70.  
  71. true        { return ValTrue; }
  72. false       { return ValFalse; }
  73. null        { return ValNull; }
  74.  
  75. {STRING}    {  
  76.                 yylval->string = strdup(yytext);
  77.                 return String;
  78.             }
  79.  
  80. {NUMBER}    {
  81.                 if (strchr(yytext, '.')) {
  82.                     yylval->number.typeName = FLOAT_TYPENAME;
  83.                     yylval->number.numData.numFloat = atof(yytext);
  84.                 } else {
  85.                     yylval->number.typeName = INT_TYPENAME;
  86.                     yylval->number.numData.numInt = atoi(yytext);
  87.                 }
  88.                 return Number;
  89.             }
  90.  
  91. \{          { return ObjBeg; }
  92. \}          { return ObjEnd; }
  93.  
  94. \[          { return ArrBeg; }
  95. \]          { return ArrEnd; }
  96.  
  97. :           { return SymColon; }
  98. ,           { return SymComma; }
  99.  
  100. \n          { return SymLBreak; }
  101. \t          { return SymTab; }
  102.  
  103. %%
  104.  
  105. void init_scanner(char *program, yyscan_t *scanner, struct Extra *extra)
  106. {
  107.     extra->continued = 0;
  108.     extra->cur_line = 1;
  109.     extra->cur_column = 1;
  110.     yylex_init(scanner);
  111.     yylex_init_extra(extra, scanner);
  112.     yy_scan_string(program, *scanner);
  113. }
  114.  
  115. void destroy_scanner(yyscan_t scanner)
  116. {
  117.     yylex_destroy(scanner);
  118. }
  119.  
  120.  
  121.  
  122.  
  123.  
  124. lexer.h
  125.  
  126. #ifndef _LEXER_H_INCLUDED
  127. #define _LEXER_H_INCLUDED
  128.  
  129. #ifndef YY_TYPEDEF_YY_SCANNER_T
  130. #define YY_TYPEDEF_YY_SCANNER_T
  131. typedef void *yyscan_t;
  132. #endif
  133.  
  134. struct Extra {
  135.     int continued;
  136.     int cur_line;
  137.     int cur_column;
  138. };
  139.  
  140. void init_scanner(char *program, yyscan_t *scanner, struct Extra *extra);
  141. void destroy_scanner(yyscan_t scanner);
  142.  
  143. #endif
Advertisement
Add Comment
Please, Sign In to add comment