home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pccts1.zip / ADVTUT / TUT2.G < prev    next >
Text File  |  1993-04-01  |  3KB  |  127 lines

  1. #header <<#include "sym.h"
  2.           #include "charbuf.h"
  3.         >>
  4.  
  5. #token "[\t\ ]+"    << zzskip(); >>                /* Ignore White */
  6. #token "\n"         << zzline++; zzskip(); >>
  7. #token STRING "\"(~[\0-\0x1f\"\\]|(\\~[\0-\0x1f]))*\"" <<;>>
  8.  
  9. <<
  10. #define HashTableSize       999
  11. #define StringTableSize     5000
  12. #define GLOBAL              0
  13. #define PARAMETER           1
  14. #define LOCAL               2
  15.  
  16. static Sym *globals = NULL; /* global scope for symbols */
  17.  
  18. main()
  19. {
  20.     zzs_init(HashTableSize, StringTableSize);
  21.     ANTLR(p(), stdin);
  22. }
  23.  
  24.  
  25. pScope(p)
  26. Sym *p;
  27. {
  28.     for (; p!=NULL; p=p->scope)
  29.     {
  30.         printf("\tlevel %d | %-12s | %-15s\n",
  31.             p->level,
  32.             zztokens[p->token],
  33.             p->symbol);
  34.     }
  35. }
  36. >>
  37.  
  38. p       :   <<Sym *p;>>
  39.             ( func | "var" def[&globals, GLOBAL] ";" )*
  40.             <<p = zzs_rmscope(&globals);
  41.               printf("Globals:\n");
  42.               pScope(p);
  43.             >>
  44.             "@"
  45.         ;
  46.  
  47.  
  48. def[Sym **scope, int level]
  49.         :   <<Sym *var;>>
  50.             (   WORD
  51.                 <<zzs_scope($scope);
  52.                   var = zzs_newadd($1.text);
  53.                   var->level = $level;
  54.                   var->token = VAR;
  55.                 >>
  56.             |   VAR
  57.                 <<var = zzs_get($1.text);
  58.                   if ( $level != var->level )
  59.                   {
  60.                     zzs_scope($scope);
  61.                     var = zzs_newadd($1.text);
  62.                     var->level = $level;
  63.                     var->token = VAR;
  64.                   }
  65.                   else printf("redefined variable ignored: %s\n", $1.text);
  66.                 >>
  67.             )
  68.         ;
  69.  
  70.  
  71. func    :   <<Sym *locals=NULL, *var, *p;>>
  72.             WORD
  73.             <<zzs_scope(&globals);
  74.               var = zzs_newadd($1.text);
  75.               var->level = GLOBAL;
  76.               var->token = FUNC;
  77.             >>
  78.             "\(" { def[&locals, PARAMETER] } "\)"
  79.             "\{"
  80.                 ( "var" def[&locals, LOCAL] ";" )*
  81.                 ( statement )*
  82.             "\}"
  83.             <<p = zzs_rmscope(&locals);
  84.               printf("Locals for %s:\n", $1.text);
  85.               pScope(p);
  86.             >>
  87.         ;
  88.  
  89.  
  90. statement
  91.         :   expr ";"
  92.         |   "\{" ( statement )* "\}"
  93.         |   "if" "\(" expr "\)" statement {"else" statement}
  94.         |   "while" "\(" expr "\)" statement
  95.         |   "return" expr ";"
  96.         |   "print" expr ";"
  97.         ;
  98.  
  99.  
  100. expr    :   VAR "=" expr
  101.         |   expr1
  102.         ;
  103.  
  104. expr0   :   expr1 ( ("=="|"!=") expr1)*
  105.         ;
  106.  
  107. expr1   :   expr2 ( ("\+"|"\-") expr2)*
  108.         ;
  109.  
  110. expr2   :   expr3 ( ("\*"|"/") expr3)*
  111.         ;
  112.  
  113. expr3   :   {"\-"} expr4
  114.         ;
  115.  
  116. expr4   :   STRING
  117.         |   ( FUNC | WORD ) "\(" { expr } "\)"
  118.         |   "\(" expr "\)"
  119.         ;
  120.  
  121.  
  122. #token WORD "[a-zA-Z]+"
  123.     <<{
  124.         Sym *p = zzs_get(LATEXT(1));
  125.         if ( p != NULL ) NLA = p->token;
  126.     }>>
  127.