home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / prgramer / bison / lex.c < prev    next >
C/C++ Source or Header  |  1990-07-01  |  10KB  |  478 lines

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