home *** CD-ROM | disk | FTP | other *** search
/ ftp.mactech.com 2010 / ftp.mactech.com.tar / ftp.mactech.com / online / source / c / compilers / Bison.sit.hqx / Bison / Source / lex.c < prev    next >
Text File  |  1992-08-21  |  10KB  |  519 lines

  1. /***********************************************************
  2.  *
  3.  * Macintosh/MPW version of GNU Bison 1.18
  4.  * Please read the README_MPW file for more information
  5.  *
  6.  ***********************************************************/
  7.  
  8. /* Token-reader for Bison's input parser,
  9.    Copyright (C) 1984, 1986, 1989 Free Software Foundation, Inc.
  10.  
  11. This file is part of Bison, the GNU Compiler Compiler.
  12.  
  13. Bison is free software; you can redistribute it and/or modify
  14. it under the terms of the GNU General Public License as published by
  15. the Free Software Foundation; either version 2, or (at your option)
  16. any later version.
  17.  
  18. Bison is distributed in the hope that it will be useful,
  19. but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21. GNU General Public License for more details.
  22.  
  23. You should have received a copy of the GNU General Public License
  24. along with Bison; see the file COPYING.  If not, write to
  25. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  26.  
  27.  
  28. /* 
  29.    lex() is the entry point.  It is called from reader.c.
  30.    It returns one of the token-type codes defined in lex.h.
  31.    When an identifier is seen, the code IDENTIFIER is returned
  32.    and the name is looked up in the symbol table using symtab.c;
  33.    symval is set to a pointer to the entry found.  */
  34.  
  35. #include <stdio.h>
  36. #include <ctype.h>
  37. #include "system.h"
  38. #include "files.h"
  39. #include "symtab.h"
  40. #include "lex.h"
  41. #include "new.h"
  42.  
  43. #ifdef macintosh
  44. #pragma segment scanner
  45. #endif
  46.  
  47.  
  48. extern int lineno;
  49. extern int translations;
  50.  
  51. int parse_percent_token();
  52.  
  53. extern void fatals();
  54. extern void fatal();
  55.  
  56. /* Buffer for storing the current token.  */
  57. char *token_buffer;
  58.  
  59. /* Allocated size of token_buffer, not including space for terminator.  */
  60. static int maxtoken;
  61.  
  62. bucket *symval;
  63. int numval;
  64.  
  65. static int unlexed;        /* these two describe a token to be reread */
  66. static bucket *unlexed_symval;    /* by the next call to lex */
  67.  
  68.  
  69. void
  70. init_lex()
  71. {
  72.   maxtoken = 100;
  73.   token_buffer = NEW2 (maxtoken + 1, char);
  74.   unlexed = -1;
  75. }
  76.  
  77.  
  78. static char *
  79. grow_token_buffer (p)
  80.      char *p;
  81. {
  82.   int offset = p - token_buffer;
  83.   maxtoken *= 2;
  84.   token_buffer = (char *) realloc(token_buffer, maxtoken + 1);
  85.   if (token_buffer == 0)
  86.     fatal("virtual memory exhausted");
  87.   return token_buffer + offset;
  88. }
  89.  
  90.  
  91. int
  92. skip_white_space()
  93. {
  94.   register int c;
  95.   register int inside;
  96.  
  97.   c = getc(finput);
  98.  
  99.   for (;;)
  100.     {
  101.       int cplus_comment;
  102.  
  103.       switch (c)
  104.     {
  105.     case '/':
  106.       c = getc(finput);
  107.       if (c != '*' && c != '/')
  108.         fatals("unexpected `/%c' found",c);
  109.       cplus_comment = (c == '/');
  110.  
  111.       c = getc(finput);
  112.  
  113.       inside = 1;
  114.       while (inside)
  115.         {
  116.           if (!cplus_comment && c == '*')
  117.         {
  118.           while (c == '*')
  119.             c = getc(finput);
  120.  
  121.           if (c == '/')
  122.             {
  123.               inside = 0;
  124.               c = getc(finput);
  125.             }
  126.         }
  127.           else if (c == '\n')
  128.         {
  129.           lineno++;
  130.           if (cplus_comment)
  131.             inside = 0;
  132.           else
  133.             c = getc(finput);
  134.         }
  135.           else if (c == EOF)
  136.         fatal("unterminated comment");
  137.           else
  138.         c = getc(finput);
  139.         }
  140.  
  141.       break;
  142.  
  143.     case '\n':
  144.       lineno++;
  145.  
  146.     case ' ':
  147.     case '\t':
  148.     case '\f':
  149.       c = getc(finput);
  150.       break;
  151.  
  152.     default:
  153.       return (c);
  154.     }
  155.     }
  156. }
  157.  
  158.  
  159. void
  160. unlex(token)
  161. int token;
  162. {
  163.   unlexed = token;
  164.   unlexed_symval = symval;
  165. }
  166.  
  167.  
  168.  
  169. int
  170. lex()
  171. {
  172.   register int c;
  173.   register char *p;
  174.  
  175.   if (unlexed >= 0)
  176.     {
  177.       symval = unlexed_symval;
  178.       c = unlexed;
  179.       unlexed = -1;
  180.       return (c);
  181.     }
  182.  
  183.   c = skip_white_space();
  184.  
  185.   switch (c)
  186.     {
  187.     case EOF:
  188.       return (ENDFILE);
  189.  
  190.     case 'A':  case 'B':  case 'C':  case 'D':  case 'E':
  191.     case 'F':  case 'G':  case 'H':  case 'I':  case 'J':
  192.     case 'K':  case 'L':  case 'M':  case 'N':  case 'O':
  193.     case 'P':  case 'Q':  case 'R':  case 'S':  case 'T':
  194.     case 'U':  case 'V':  case 'W':  case 'X':  case 'Y':
  195.     case 'Z':
  196.     case 'a':  case 'b':  case 'c':  case 'd':  case 'e':
  197.     case 'f':  case 'g':  case 'h':  case 'i':  case 'j':
  198.     case 'k':  case 'l':  case 'm':  case 'n':  case 'o':
  199.     case 'p':  case 'q':  case 'r':  case 's':  case 't':
  200.     case 'u':  case 'v':  case 'w':  case 'x':  case 'y':
  201.     case 'z':
  202.     case '.':  case '_':
  203.       p = token_buffer;
  204.       while (isalnum(c) || c == '_' || c == '.')
  205.     {
  206.       if (p == token_buffer + maxtoken)
  207.         p = grow_token_buffer(p);
  208.  
  209.       *p++ = c;
  210.       c = getc(finput);
  211.     }
  212.  
  213.       *p = 0;
  214.       ungetc(c, finput);
  215.       symval = getsym(token_buffer);
  216.       return (IDENTIFIER);
  217.  
  218.     case '0':  case '1':  case '2':  case '3':  case '4':
  219.     case '5':  case '6':  case '7':  case '8':  case '9':
  220.       {
  221.     numval = 0;
  222.  
  223.     while (isdigit(c))
  224.       {
  225.         numval = numval*10 + c - '0';
  226.         c = getc(finput);
  227.       }
  228.     ungetc(c, finput);
  229.     return (NUMBER);
  230.       }
  231.  
  232.     case '\'':
  233.       translations = -1;
  234.  
  235.       /* parse the literal token and compute character code in  code  */
  236.  
  237.       c = getc(finput);
  238.       {
  239.     register int code = 0;
  240.  
  241.     if (c == '\\')
  242.       {
  243.         c = getc(finput);
  244.  
  245.         if (c <= '7' && c >= '0')
  246.           {
  247.         while (c <= '7' && c >= '0')
  248.           {
  249.             code = (code * 8) + (c - '0');
  250.             c = getc(finput);
  251.             if (code >= 256 || code < 0)
  252.               fatals("malformatted literal token `\\%03o'", code);
  253.           }
  254.           }
  255.         else
  256.           {
  257.         if (c == 't')
  258.           code = '\t';
  259.         else if (c == 'n')
  260.           code = '\n';
  261.         else if (c == 'a')
  262.           code = '\007';
  263.         else if (c == 'r')
  264.           code = '\r';
  265.         else if (c == 'f')
  266.           code = '\f';
  267.         else if (c == 'b')
  268.           code = '\b';
  269.         else if (c == 'v')
  270.           code = 013;
  271.         else if (c == 'x')
  272.           {
  273.             c = getc(finput);
  274.             while ((c <= '9' && c >= '0')
  275.                || (c >= 'a' && c <= 'z')
  276.                || (c >= 'A' && c <= 'Z'))
  277.               {
  278.             code *= 16;
  279.             if (c <= '9' && c >= '0')
  280.               code += c - '0';
  281.             else if (c >= 'a' && c <= 'z')
  282.               code += c - 'a' + 10;
  283.             else if (c >= 'A' && c <= 'Z')
  284.               code += c - 'A' + 10;
  285.             if (code >= 256 || code<0)/* JF this said if(c>=128) */
  286.               fatals("malformatted literal token `\\x%x'",code);
  287.             c = getc(finput);
  288.               }
  289.             ungetc(c, finput);
  290.           }
  291.         else if (c == '\\')
  292.           code = '\\';
  293.         else if (c == '\'')
  294.           code = '\'';
  295.         else if (c == '\"')    /* JF this is a good idea */
  296.           code = '\"';
  297.         else
  298.           {
  299.             if (c >= 040 && c <= 0177)
  300.               fatals ("unknown escape sequence `\\%c'", c);
  301.             else
  302.               fatals ("unknown escape sequence: `\\' followed by char code 0x%x", c);
  303.           }
  304.  
  305.         c = getc(finput);
  306.           }
  307.       }
  308.     else
  309.       {
  310.         code = c;
  311.         c = getc(finput);
  312.       }
  313.     if (c != '\'')
  314.       fatal("multicharacter literal tokens not supported");
  315.  
  316.     /* now fill token_buffer with the canonical name for this character
  317.        as a literal token.  Do not use what the user typed,
  318.        so that '\012' and '\n' can be interchangeable.  */
  319.  
  320.     p = token_buffer;
  321.     *p++ = '\'';
  322.     if (code == '\\')
  323.       {
  324.         *p++ = '\\';
  325.         *p++ = '\\';
  326.       }
  327.     else if (code == '\'')
  328.       {
  329.         *p++ = '\\';
  330.         *p++ = '\'';
  331.       }
  332.     else if (code >= 040 && code != 0177)
  333.       *p++ = code;
  334.     else if (code == '\t')
  335.       {
  336.         *p++ = '\\';
  337.         *p++ = 't';
  338.       }
  339.     else if (code == '\n')
  340.       {
  341.         *p++ = '\\';
  342.         *p++ = 'n';
  343.       }
  344.     else if (code == '\r')
  345.       {
  346.         *p++ = '\\';
  347.         *p++ = 'r';
  348.       }
  349.     else if (code == '\v')
  350.       {
  351.         *p++ = '\\';
  352.         *p++ = 'v';
  353.       }
  354.     else if (code == '\b')
  355.       {
  356.         *p++ = '\\';
  357.         *p++ = 'b';
  358.       }
  359.     else if (code == '\f')
  360.       {
  361.         *p++ = '\\';
  362.         *p++ = 'f';
  363.       }
  364.         else
  365.       {
  366.         *p++ = code / 0100 + '0';
  367.         *p++ = ((code / 010) & 07) + '0';
  368.         *p++ = (code & 07) + '0';
  369.       }
  370.     *p++ = '\'';
  371.     *p = 0;
  372.     symval = getsym(token_buffer);
  373.     symval->class = STOKEN;
  374.     if (! symval->user_token_number)
  375.       symval->user_token_number = code;
  376.     return (IDENTIFIER);
  377.       }
  378.  
  379.     case ',':
  380.       return (COMMA);
  381.  
  382.     case ':':
  383.       return (COLON);
  384.  
  385.     case ';':
  386.       return (SEMICOLON);
  387.  
  388.     case '|':
  389.       return (BAR);
  390.  
  391.     case '{':
  392.       return (LEFT_CURLY);
  393.  
  394.     case '=':
  395.       do
  396.     {
  397.       c = getc(finput);
  398.       if (c == '\n') lineno++;
  399.     }
  400.       while(c==' ' || c=='\n' || c=='\t');
  401.  
  402.       if (c == '{')
  403.           return(LEFT_CURLY);
  404.       else
  405.     {
  406.       ungetc(c, finput);
  407.       return(ILLEGAL);
  408.     }
  409.  
  410.     case '<':
  411.       p = token_buffer;
  412.       c = getc(finput);
  413.       while (c != '>')
  414.     {
  415.       if (c == '\n' || c == EOF)
  416.         fatal("unterminated type name");
  417.  
  418.       if (p == token_buffer + maxtoken)
  419.         p = grow_token_buffer(p);
  420.  
  421.       *p++ = c;
  422.       c = getc(finput);
  423.     }
  424.       *p = 0;
  425.       return (TYPENAME);
  426.         
  427.  
  428.     case '%':
  429.       return (parse_percent_token());
  430.  
  431.     default:
  432.       return (ILLEGAL);
  433.     }
  434. }
  435.  
  436.  
  437. /* parse a token which starts with %.  Assumes the % has already been read and discarded.  */
  438.  
  439. int
  440. parse_percent_token ()
  441. {
  442.   register int c;
  443.   register char *p;
  444.  
  445.   p = token_buffer;
  446.   c = getc(finput);
  447.  
  448.   switch (c)
  449.     {
  450.     case '%':
  451.       return (TWO_PERCENTS);
  452.  
  453.     case '{':
  454.       return (PERCENT_LEFT_CURLY);
  455.  
  456.     case '<':
  457.       return (LEFT);
  458.  
  459.     case '>':
  460.       return (RIGHT);
  461.  
  462.     case '2':
  463.       return (NONASSOC);
  464.  
  465.     case '0':
  466.       return (TOKEN);
  467.  
  468.     case '=':
  469.       return (PREC);
  470.     }
  471.   if (!isalpha(c))
  472.     return (ILLEGAL);
  473.  
  474.   while (isalpha(c) || c == '_')
  475.     {
  476.       if (p == token_buffer + maxtoken)
  477.     p = grow_token_buffer(p);
  478.  
  479.       *p++ = c;
  480.       c = getc(finput);
  481.     }
  482.  
  483.   ungetc(c, finput);
  484.  
  485.   *p = 0;
  486.  
  487.   if (strcmp(token_buffer, "token") == 0
  488.       ||
  489.       strcmp(token_buffer, "term") == 0)
  490.     return (TOKEN);
  491.   else if (strcmp(token_buffer, "nterm") == 0)
  492.     return (NTERM);
  493.   else if (strcmp(token_buffer, "type") == 0)
  494.     return (TYPE);
  495.   else if (strcmp(token_buffer, "guard") == 0)
  496.     return (GUARD);
  497.   else if (strcmp(token_buffer, "union") == 0)
  498.     return (UNION);
  499.   else if (strcmp(token_buffer, "expect") == 0)
  500.     return (EXPECT);
  501.   else if (strcmp(token_buffer, "start") == 0)
  502.     return (START);
  503.   else if (strcmp(token_buffer, "left") == 0)
  504.     return (LEFT);
  505.   else if (strcmp(token_buffer, "right") == 0)
  506.     return (RIGHT);
  507.   else if (strcmp(token_buffer, "nonassoc") == 0
  508.        ||
  509.        strcmp(token_buffer, "binary") == 0)
  510.     return (NONASSOC);
  511.   else if (strcmp(token_buffer, "semantic_parser") == 0)
  512.     return (SEMANTIC_PARSER);
  513.   else if (strcmp(token_buffer, "pure_parser") == 0)
  514.     return (PURE_PARSER);
  515.   else if (strcmp(token_buffer, "prec") == 0)
  516.     return (PREC);
  517.   else return (ILLEGAL);
  518. }
  519.