Advertisement
knkd

test.y

Jun 22nd, 2012
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.07 KB | None | 0 0
  1. %{
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. void yyerror(const char *str) {
  6.         fprintf(stderr,"ошибка: %s\n",str);
  7.         fflush(stdout);
  8. }
  9.  
  10. int yywrap() {
  11.         return 1;
  12. }
  13.  
  14. int indent_level;
  15. void indent() {
  16.         fprintf(stdout, "IL:%d\n", indent_level);
  17.         int i;
  18.         for (i = 0; i < indent_level*4; i++) {
  19.                 fprintf(stdout, " ");
  20.         }
  21. }
  22.  
  23. int main() {
  24.         indent_level = 0;
  25.         yyparse();
  26. }
  27.  
  28. %}
  29.  
  30. %token ASSIGMENT EOL OBRACKET CBRACKET
  31.  
  32. %union {
  33.         double _double;
  34.         int       _int;
  35.         char * _string;
  36.         int    _block;
  37. }
  38.  
  39. %token <_double>   FLOAT
  40. %token <_string>   IDENT
  41. %token <_string>   STRING
  42. %token    <_int>   INTEGER
  43. %token  <_block>   block
  44.  
  45. %%
  46.  
  47. statements
  48.         :
  49.         | statements statement
  50.         ;
  51.  
  52. oblock
  53.         :
  54.         OBRACKET {
  55.                 fprintf(stdout, "block:open\n");
  56.                 fflush(stdout);
  57.                 $<_block>$ = 1;
  58.         }
  59.         ;
  60.  
  61. cblock
  62.         :
  63.         CBRACKET {
  64.                 fprintf(stdout, "block:close\n");
  65.                 fflush(stdout);
  66.                 $<_block>$ = 0;
  67.         }
  68.         ;
  69.  
  70. BLOCK
  71.         :
  72.         oblock statements cblock
  73.         ;
  74.  
  75. value
  76.         :
  77.         STRING {
  78.                 fprintf(stdout, "value:%s\n", $1);
  79.                 fflush(stdout);
  80.                 $<_string>$ = $1;
  81.         }
  82.         |
  83.         INTEGER {
  84.                 fprintf(stdout, "value:%d\n", $1);
  85.                 fflush(stdout);
  86.                 $<_int>$    = $1;
  87.         }
  88.         |
  89.         FLOAT {
  90.                 fprintf(stdout, "value:%lf\n", $1);
  91.                 fflush(stdout);
  92.                 $<_double>$ = $1;
  93.         }
  94.         |
  95.         BLOCK {
  96.                 fprintf(stdout, "value:block\n");
  97.                 fflush(stdout);
  98.         }
  99.         ;
  100.  
  101. statement
  102.         :
  103.         IDENT ASSIGMENT value EOL {
  104.                 fprintf(stdout, "ident:%s\n", $1);
  105.                 fflush(stdout);
  106.         }
  107.         |
  108.         EOL {
  109.                 ;
  110.         }
  111.         ;
  112.  
  113. %%
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement