home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / gwm18a.zip / wool.yac < prev    next >
Text File  |  1995-07-03  |  3KB  |  168 lines

  1. %{
  2. /* Copyright 1989 GROUPE BULL -- See license conditions in file COPYRIGHT
  3.  * Copyright 1989 Massachusetts Institute of Technology
  4.  */
  5. /***************************\
  6. *                 *
  7. *  Yacc grammar for Wool   *
  8. *                 *
  9. \***************************/
  10.  
  11. #include <stdio.h>
  12. #include <ctype.h>
  13. #include "EXTERN.h"
  14. #include "wool.h"
  15. #include "wl_atom.h"
  16. #include "wl_coll.h"
  17. #include "wl_list.h"
  18. #include "INTERN.h"
  19. #include "yacc.h"
  20. #if defined SVR4
  21. #define SYSV
  22. #endif
  23. #ifdef SYSV
  24. #include <string.h>
  25. #else /* SYSV */
  26. #include <strings.h>
  27. #endif /* SYSV */
  28.  
  29. %}
  30.  
  31. %union{
  32.     WOOL_OBJECT wool_object;
  33.     WOOL_Collection wool_collection;
  34. }
  35.  
  36. %type <wool_object> s_expression main_s_exp 
  37. %type <wool_collection> list_of_s_expressions
  38.  
  39. %token  END_OF_FILE     /* MUST be the first token! */
  40. %token  STRING NON_CLOSED_STRING QUOTECHAR
  41. %token  NUMBER HEX_NUMBER LEFTPAR RIGHTPAR LEFTBRA RIGHTBRA
  42. %token  NAME 
  43.  
  44. %start main_s_exp 
  45.  
  46. %%
  47. main_s_exp    : s_expression
  48.             {$$ = wool_read_expr = $1;
  49.              YYACCEPT;}
  50.         | /* empty */ RIGHTPAR main_s_exp
  51.             {$$ = wool_read_expr = $2;
  52.              YYACCEPT;}
  53.         | /* empty */ END_OF_FILE
  54.             {$$ = wool_read_expr = NULL;
  55.              YYACCEPT;}
  56.         ;
  57.  
  58. s_expression    : LEFTPAR list_of_s_expressions RIGHTPAR
  59.             {$$ = (WOOL_OBJECT) WLList_make($2);}
  60.         | LEFTBRA list_of_s_expressions RIGHTBRA
  61.             {$$ = WLCollection_progn($2);}
  62.         | NAME
  63.             {$$ = (WOOL_OBJECT) wool_atom(yytext);}
  64.         | NUMBER
  65.             {$$ = (WOOL_OBJECT) 
  66.                WLNumber_make((Num) atoi(yytext));}
  67.         | HEX_NUMBER
  68.             {int num;
  69.              sscanf(yytext+2,"%x",&num);
  70.              $$ = (WOOL_OBJECT) WLNumber_make((Num) num);}
  71.         | STRING
  72.             {$$ = (WOOL_OBJECT)
  73.                 WLString_make(strip_string(yytext));}
  74.         | QUOTECHAR s_expression
  75.             {$$ = (WOOL_OBJECT) WLQuotedExpr_make($2);}
  76.         | NON_CLOSED_STRING
  77.            {$$ = NIL;
  78.             yyerror("Non closed string");}
  79.         | LEFTPAR list_of_s_expressions END_OF_FILE
  80.             {$$ = NIL;
  81.             yyerror("Lacking \")\" at the end of file!");}
  82.         | error 
  83.              {$$ = NIL;}
  84.         ;
  85.  
  86. list_of_s_expressions : list_of_s_expressions s_expression
  87.             {$$ = WLCollection_add($1, $2);}
  88.         | /* empty */
  89.             {$$ = WLCollection_make();}
  90.         ;
  91.  
  92.  
  93. %%
  94.  
  95. #include "lex.yy.c"    /* lexical created by lex */
  96.  
  97. yyerror(s)
  98. char *s;
  99. {
  100.     wool_error(SYNTAX_ERROR, s);
  101. }
  102.  
  103. /*
  104.  * Some routines to deal with strings:
  105.  */
  106.  
  107. /*
  108.  * strip_string strips strings from \, ", etc... 
  109.  * copies string raw_string to string stripped_string
  110.  */
  111.  
  112. static char *stripped_string;
  113. static int   stripped_string_limit;
  114.  
  115. char *strip_string(raw_string)
  116. char *raw_string;
  117. {
  118.     register char  *p, *q, c;
  119.     int num;
  120.  
  121.     if ((int) strlen(raw_string) > stripped_string_limit) {
  122.     stripped_string_limit = strlen(raw_string);
  123.     stripped_string = (char *)
  124.         Realloc(stripped_string, stripped_string_limit);
  125.     }
  126.     for (p = raw_string + 1, q = stripped_string; *p; p++, q++) {
  127.     switch (*p) {
  128.     case '"':
  129.         *q = '\0';
  130.         break;
  131.     case '\\':
  132.         switch (*(++p)) {
  133.         case '\n':
  134.         q--;
  135.         break;
  136.         case 'n':
  137.         *q = '\n';
  138.         break;
  139.         case 'r':
  140.         *q = '\r';
  141.         break;
  142.         case 't':
  143.         *q = '\t';
  144.         break;
  145.         case 'e':
  146.         *q = '\033';
  147.         break;
  148.         case 'x':
  149.         sscanf(++p,"%2x",&num);
  150.         *q = num;
  151.         p++;
  152.         break;
  153.         default:
  154.         if ((*p <= '9') && (*p >= '0')) {
  155.             *q = (char)
  156.             (*p - '0') * 64 + (*(++p) - '0') * 8 + *(++p) - '0';
  157.         } else {
  158.             *q = *p;
  159.         }
  160.         }
  161.         break;
  162.     default:
  163.         *q = *p;
  164.     }
  165.     }
  166.     return stripped_string;
  167. }
  168.