home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 018.lha / parser / parser.lex < prev    next >
Text File  |  1986-10-19  |  3KB  |  127 lines

  1. %{
  2. /*
  3.         Little Smalltalk lexical analyzer
  4. */
  5. # include <math.h>
  6.  
  7. # undef input
  8. # undef unput
  9.  
  10. double atof();
  11. int linenum = 1;
  12. %}
  13. %%
  14. [ \t]+                          {;}
  15. \n                              {linenum++;}
  16. \"                              {readcomment();}
  17. ":="                            {return(ASSIGN);}
  18. "<-"                            {return(ASSIGN);}
  19. Class                           {return(lexsave(CLASS));}
  20. self                            {yylval.p = selfvar;  return(PSEUDO);}
  21. selfProcess            {yylval.p = procvar;  return(PSEUDO);}
  22. super                           {yylval.p = supervar; return(PSEUDO);}
  23. nil                             {yylval.p = nilvar;   return(PSEUDO);}
  24. true                            {yylval.p = truevar;  return(PSEUDO);}
  25. false                           {yylval.p = falsevar; return(PSEUDO);}
  26. smalltalk                       {yylval.p = smallvar; return(PSEUDO);}
  27. \$.                             {yylval.i = yytext[1]; return(LITCHAR);}
  28. #                               {return(PS);}
  29. [0-9]+r-?[0-9A-Z]+(\.[0-9A-Z]+)?(e[-+]?[0-9]+)? {return(lexsave(LITFNUM));}
  30. [0-9]+                          {yylval.i = atoi(yytext); return(LITNUM);}
  31. [0-9]+(\.[0-9]+)?(e[-+]?[0-9]+)?   {return(lexsave(LITFNUM));}
  32. '[^']*'                         {char c; unput(c = input());
  33.                                  if (c == '\'') yymore();
  34.                                  else return(lexlstr());}
  35. [a-zA-Z0-9]+:?                  {return(varlex());}
  36. :[a-zA-Z0-9]+                   {return(slexsave(COLONVAR));}
  37. #[^ \t\n.()\[]+                 {return(slexsave(LITSYM));}
  38. "-"                             {return(lexsave(MINUS));}
  39. "("                             {return(LP);}
  40. ")"                             {return(RP);}
  41. "["                             {return(LB);}
  42. "]"                             {return(RB);}
  43. "."                             {return(PERIOD);}
  44. ^"|"                {return(lexsave(MBAR));}
  45. ^"!"                {return(lexsave(MBAR));}
  46. "|"                             {return(lexsave(BAR));}
  47. "!"                             {return(lexsave(BAR));}
  48. ";"                             {return(SEMI);}
  49. "^"                             {return(lexsave(UPARROW));}
  50. ">"                {return(lexsave(PE));}
  51. [^ \t\nA-Za-z0-9]               {return(lexsave(BINARY));}
  52. "<primitive"               {return(PRIMITIVE);}
  53. %%
  54. static int ocbuf = 0;
  55. static int pbbuf[400];
  56.  
  57. static int input()
  58. {    int c;
  59.  
  60.     if (ocbuf) {c = pbbuf[--ocbuf]; }
  61.     else {
  62.         c = getc(fp);
  63.         if (c == EOF) c = 0;
  64.         }
  65.     return(c);
  66. }
  67.  
  68. static unput(c)
  69. char c;
  70. {
  71.     if (c) pbbuf[ocbuf++] = c;
  72. }
  73.  
  74. # include <ctype.h>
  75.  
  76. static readcomment()
  77. {  char c;
  78.  
  79.    while ((c = input()) && c != '\"')
  80.     if (c == '\n') linenum++;
  81.    if (!c) yyerror("unterminated comment");
  82. }
  83.  
  84. char *walloc(s) char *s;
  85. {  char *p, *malloc();
  86.  
  87.    p = malloc((unsigned) (strlen(s) + 1));
  88.    if (p == (char *) 0) yyerror("out of variable string space");
  89.    strcpy(p, s);
  90.    return(p);
  91. }
  92.  
  93. static int slexsave(type)
  94. int type;
  95. {
  96.  
  97.     yylval.c = walloc(&yytext[1]);
  98.     if (yylval.c == 0) yerr("cannot create symbol %s", yytext);
  99.     return(type);
  100. }
  101.  
  102. static int lexsave(type)
  103. int type;
  104. {
  105.  
  106.     yylval.c = walloc(yytext);
  107.     if (yylval.c == 0) yerr("cannot create string %s", yytext);
  108.     return(type);
  109. }
  110.  
  111. static int varlex()
  112. {  
  113.  
  114.    lexsave(0);
  115.    if (yytext[yyleng-1] == ':') return(KEYWORD);
  116.    else if (islower(yytext[0])) return(LOWERCASEVAR);
  117.    else return(UPPERCASEVAR);
  118. }
  119.  
  120. static int lexlstr()
  121. {  char *p, *q;
  122.  
  123.    yylval.c = p = walloc(&yytext[1]);
  124.    *(p + yyleng -2) = '\0';
  125.    return(LITSTR);
  126. }
  127.