home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / fchek284.zip / forlex.c < prev    next >
C/C++ Source or Header  |  1995-06-02  |  89KB  |  3,369 lines

  1. /* forlex.c:
  2.  
  3.     Tokenizing routines for Fortran program checker.
  4.  
  5.     Copyright (C) 1993 by Robert K. Moniot.
  6.     This program is free software.  Permission is granted to
  7.     modify it and/or redistribute it, retaining this notice.
  8.     No guarantees accompany this software.
  9.  
  10.  
  11.  Part I. yylex()  -- gives tokens to the parser.
  12.  Part II. advance() -- bottom-level scanning of input stream.
  13.  
  14. */
  15.  
  16.     /* Declarations shared by all modules */
  17.  
  18. #include <stdio.h>
  19. #include <ctype.h>
  20. #include <string.h>
  21.  
  22. #include "ftnchek.h"
  23. #define FORLEX
  24. #include "symtab.h"
  25. #include "tokdefs.h"
  26.  
  27. /* lexdefs.h:
  28.         Macros and shared info for lexical analysis routines
  29. */
  30.  
  31. #define LEX_SHARED PRIVATE
  32.  
  33. #define EOL     '\n'    /* Character for end of line, not of statement */
  34.  
  35. extern YYSTYPE yylval;      /* Lexical value for Yacc */
  36.  
  37.  
  38.     /* Since EOS is special, need special macros for it */
  39. #define makeupper(C) (((C) != EOS && islower((int)(C)))? toupper((int)(C)):(C))
  40. #define iswhitespace(C) ( (C) != EOS && isspace((int)(C)) )
  41. #define isadigit(C)     ( (C) != EOS && isdigit((int)(C)) )
  42. #define isaletter(C)    ( (C) != EOS && isalpha((int)(C)) )
  43. #define ishex(C) ((C) != EOS && (isdigit((int)(C)) ||\
  44.             (toupper((int)(C))>='A' && toupper((int)(C))<='F') ))
  45.  
  46.     /* Define isidletter to allow underscore and/or dollar sign.
  47.        Nonstandardness is handled later. */
  48. #define isidletter(C)    ( (C) != EOS && ( isalpha((int)(C)) || \
  49.                        (C)=='_' || (C)=='$' ) )
  50.  
  51.         /* lead-in to a string: standard is ' but allow " too*/
  52. #ifdef ALLOW_QUOTEMARKS
  53. #define isaquote(C) ((C) == '\'' || (C) == '"')
  54. #else
  55. #define isaquote(C) ((C) == '\'')
  56. #endif
  57.  
  58. #define BCD(C) ((C)-'0')    /* Binary value of digit */
  59. #define HEX(C) (isdigit(C)?BCD(C):(makeupper(C)-'A'+10)) /* Hex value */
  60.  
  61.                 /* Blank-insensitive advance */
  62. #define bi_advance()    do {advance();} while(iswhitespace(curr_char))
  63.  
  64. LEX_SHARED int
  65.     inside_string,        /* TRUE when reading a string or hollerith */
  66.     inside_hollerith,    /* TRUE when reading a hollerith */
  67.     quote_char,        /* string delimiter: ' or "  */
  68.     WHILE_expected,        /* DO seen and WHILE is coming up */
  69.     contin_count,        /* Number of continuation lines of stmt */
  70.     prev_char,        /* shared between forlex.c and advance.c */
  71.     curr_char,        /* Current input character */
  72.     next_char;        /* Lookahead character */
  73.  
  74. #ifdef ALLOW_UNIX_CPP
  75. LEX_SHARED char
  76.     *next_filename;
  77. LEX_SHARED int
  78.     cpp_handled;
  79. #endif
  80.  
  81. extern int complex_const_allowed,    /* shared flags operated by fortran.y */
  82.        inside_format,
  83.        integer_context;
  84. extern int stmt_sequence_no;    /* shared with fortran.y */
  85.  
  86.         /* String management routines located in symtab.c */
  87. SYM_SHARED char
  88.    *new_global_string(),
  89.    *new_src_text_alloc(),
  90.    *new_src_text();
  91.  
  92. #ifdef VMS_INCLUDE        /* shared routines used by include section */
  93.  char *add_ext();
  94.  int has_extension();
  95. #endif
  96.  
  97.         /* Declare shared lexical routines */
  98. LEX_SHARED
  99. void advance();
  100. LEX_SHARED
  101. int is_keyword(), looking_at_cplx(), looking_at_keywd(), looking_at_relop();
  102.  
  103. LEX_SHARED
  104.  char src_text_buf[MAX_SRC_TEXT];
  105. LEX_SHARED
  106.  src_text_len;
  107.  
  108. #ifdef DEBUG_INCLUDE
  109. LEX_SHARED
  110. int debug_include=FALSE;
  111. #endif
  112.  
  113. /*
  114.  
  115. Part I. yylex()
  116.  
  117.    Shared functions defined:
  118.     yylex()            Returns next token.  Called from yyparse().
  119.     implied_id_token(t,s)    Creates token for blank common declaration.
  120.  
  121. Note: compilation options LEX_STORE_STRINGS and LEX_STORE_HOLLERITHS:
  122.   Define the macro name LEX_STORE_STRINGS to build a version of ftnchek that
  123.   stores string constants, and LEX_STORE_HOLLERITHS to store hollerith
  124.   constants.  Now that INCLUDE statements are supported, strings must
  125.   be stored.  Holleriths are not used, so they need not be stored.
  126. */
  127. #define LEX_STORE_STRINGS
  128.  
  129. #ifdef DEVELOPMENT        /* For maintaining the program */
  130. #define LEX_STORE_HOLLERITHS
  131. #define DEBUG_FORLEX
  132. #endif
  133.  
  134. #include <math.h>
  135.  
  136.  
  137.  
  138.     /* The following macro says whether a given character is legal,
  139.      * i.e. one of the stream control chars or a valid ANSI Fortran
  140.      * character.  Lower case letters are considered legal too.
  141.      * Nondigits in columns 1-6 (except EOF,EOS) are illegal.
  142.      * Hopefully this works for EBCDIC too.
  143.      */
  144. #define islegal(C) ( ((C) == EOF) || ((C) == EOS) || \
  145.     ( (col_num >= 6 || isdigit(C)) && \
  146.      (toascii((int)(C)) >= toascii(' ') && \
  147.       toascii((int)(C)) <= toascii('z') && \
  148.       legal_chars[toascii((int)(C))-toascii(' ')] == (C))) )
  149.  
  150.         /* Array has x where ASCII character is not valid.
  151.            This defn is not standard f77, since it includes
  152.            supported extensions: $ and _ in variable names,
  153.            <> in variable formats, and " in strings.
  154.          */
  155. PRIVATE char legal_chars[]=
  156. " x\"x$xx'()*+,-./0123456789:x<=>xx\
  157. ABCDEFGHIJKLMNOPQRSTUVWXYZxxxx_xabcdefghijklmnopqrstuvwxyz";
  158.  
  159. #if 0
  160.         /* Routines to alter the default status of characters,
  161.            to support various extensions to f77. Not used now.*/
  162. void
  163. make_legal_char(s)
  164.      char *s;            /* List of legal chars */
  165. {
  166.   int i;
  167.   while( *s != '\0' ) {
  168.     i = toascii((int)(*s));
  169.     if(i >= toascii(' ') && i <= toascii('z')) {
  170.       legal_chars[i-' '] = *s;
  171.     }
  172.     s++;
  173.   }
  174. }
  175.  
  176. void
  177. make_illegal_char(s)
  178.      char *s;            /* List of illegal chars */
  179. {
  180.   int i;
  181.   while( *s != '\0' ) {
  182.     i = toascii((int)(*s));
  183.     if(i >= toascii(' ') && i <= toascii('z')) {
  184.     legal_chars[i-toascii(' ')] = ( (*s != 'x')? 'x': 'X');
  185.     }
  186.     s++;
  187.   }
  188. }
  189. #endif
  190.  
  191.         /* local functions defined */
  192. PRIVATE void
  193. #ifdef ALLOW_UNIX_CPP
  194.     get_cpp_directive(),
  195. #endif
  196.     get_dot(), get_dotted_keyword(), get_edit_descriptor(), get_hollerith(),
  197.     get_identifier(), get_illegal_token(), get_label(),
  198.     get_letter(), get_number(), get_punctuation(),
  199.     get_simple_punctuation(), get_string(),
  200. #ifdef ALLOW_TYPELESS_CONSTANTS
  201. get_binary_const(),
  202. #endif
  203.     get_complex_const();
  204.  
  205. PRIVATE void
  206.     closeup();
  207.  
  208.  
  209.         /*  Gets next token for Yacc.  Return value is token.class,
  210.          *  and a copy of the token is stored in yylval.
  211.          */
  212. int
  213. yylex()
  214. {
  215.     Token token;
  216.  
  217.         /* Initialize token fields to scratch. */
  218. #if 0
  219. #ifndef TOK_type
  220.     token.TOK_type = 0;
  221. #endif
  222.     token.subclass = 0;
  223. #ifndef TOK_flags
  224.     token.TOK_flags = 0;
  225. #endif
  226.     token.value.integer = 0;
  227.     token.src_text = (char *) NULL;
  228. #else
  229. #if defined(__STDC__) || defined(VAXC)
  230.     (void)memset(&token,0,sizeof(token));
  231. #else
  232.     bzero((char *)&token,sizeof(token));
  233. #endif
  234. #endif
  235.     src_text_len = 0;
  236.  
  237.     if(curr_char == EOF) {
  238.     token.class = EOF;
  239.     token.line_num = line_num;
  240.     token.col_num = col_num;
  241.     }
  242.     else /* not EOF */ {
  243.  
  244.  
  245.         /* Skip leading spaces, and give error message if non-ANSI
  246.          * characters are found.
  247.          */
  248.  
  249.     while(iswhitespace(curr_char) || (! islegal(curr_char))  ) {
  250.       if(!iswhitespace(curr_char)) {
  251. #ifdef ALLOW_UNIX_CPP
  252.         if(curr_char == '#' && col_num == 1) {
  253.            get_cpp_directive();    /* turn # line into EOS */
  254.            break;
  255.         }
  256.         else
  257. #endif
  258.         yyerror("Illegal character");
  259.       }
  260.       advance();
  261.     }
  262.  
  263.     token.line_num = line_num;
  264.     token.col_num = col_num;
  265.  
  266.     if(inside_format) {    /* Handle format stuff here to avoid trouble */
  267.       get_edit_descriptor(&token);
  268.     }
  269.     else if(isadigit(curr_char)) {
  270.         if(col_num < 6)
  271.             get_label(&token);      /* Stmt label */
  272.         else
  273.             get_number(&token);     /* Numeric or hollerith const */
  274.     }
  275.     else if(isidletter(curr_char)) {
  276.         if(implicit_letter_flag)
  277.             get_letter(&token);    /* letter in IMPLICIT list */
  278.         else
  279.             get_identifier(&token); /* Identifier or keyword */
  280.     }
  281.     else if(isaquote(curr_char)) {
  282.             get_string(&token);    /* Quoted string */
  283.     }
  284.     else if(curr_char == '.') {
  285.             get_dot(&token);     /* '.' lead-in */
  286.     }
  287.     else {
  288.             get_punctuation(&token);  /* Punctuation character or EOS */
  289.     }
  290.     }/*end not EOF*/
  291.  
  292.     if(token.class == EOS) {
  293.     implicit_flag=FALSE;    /* in case of errors, reset flags */
  294.     implicit_letter_flag = FALSE;
  295.     }
  296.  
  297.  
  298.     prev_token_class = token.class;
  299.  
  300.     yylval = token;
  301.     return token.class;
  302.  
  303. } /* yylex */
  304.  
  305.  
  306.     /* Fills argument with token for an identifer, as if an identifer
  307.      * with name given by string s had been lexed.  This will
  308.      * be called by parser when blank common declaration is seen,
  309.      * and when a main prog without program statement is found,
  310.      * and when an unnamed block data statement is found,
  311.      * so processing of named and unnamed cases can be handled uniformly.
  312.     */
  313. void
  314. implied_id_token(t,s)
  315.     Token *t;
  316.     char *s;
  317. {
  318.     int h;
  319.     unsigned long hnum;
  320.  
  321.     hnum = hash(s);
  322.     while( h=hnum%HASHSZ, hashtab[h].name != NULL &&
  323.         strcmp(hashtab[h].name,s) != 0)
  324.             hnum = rehash(hnum);
  325.     if(hashtab[h].name == NULL) {    /* not seen before */
  326.         hashtab[h].name = s;
  327.         hashtab[h].loc_symtab = NULL;
  328.         hashtab[h].glob_symtab = NULL;
  329.         hashtab[h].com_loc_symtab = NULL;
  330.         hashtab[h].com_glob_symtab = NULL;
  331.     }
  332.     t->class = tok_identifier;
  333.     t->value.integer = h;
  334.     t->src_text = new_src_text("",0);
  335. } /* implied_id_token */
  336.  
  337. #ifdef ALLOW_UNIX_CPP
  338.         /* This does not create a token but just performs the
  339.            actions needed when a cpp directive is seen.  It
  340.            advances curr_char to the EOS.  The setting of
  341.            filename is delayed to this point because it is not
  342.            stored in tokens but is external, so changing it
  343.            must wait till the previous statement is fully
  344.            parsed and any error messages printed and arg or
  345.            com list headers completed.
  346.          */
  347. PRIVATE void
  348. get_cpp_directive()
  349. {
  350.   if(next_filename != (char *)NULL) {
  351.     current_filename = next_filename;
  352.     if(incdepth == 0)
  353.       top_filename = next_filename;
  354.   }
  355.   do {            /* Skip to end of directive.  It will become an EOS */
  356.     advance();
  357.   } while( curr_char != EOS);
  358.  
  359.   if(f77_standard || !allow_unix_cpp || !cpp_handled) {
  360.     nonstandard(line_num,col_num);
  361.     msg_tail(": preprocessor directive");
  362.     if(!cpp_handled)
  363.       msg_tail("(not processed)");
  364.   }
  365. }/*get_cpp_directive*/
  366. #endif
  367.  
  368. PRIVATE void
  369. get_dot(token)
  370.     Token *token;
  371. {
  372.     if(src_text_len < MAX_SRC_TEXT)
  373.       src_text_buf[src_text_len++] = curr_char;
  374.  
  375.     closeup();        /* Advance till nonspace char in next_char */
  376.  
  377.     if(isadigit(next_char))
  378.         get_number(token);        /* Numeric const */
  379.     else if(isaletter(next_char))
  380.         get_dotted_keyword(token);    /* .EQ. etc. */
  381.     else
  382.         get_simple_punctuation(token);    /* "." out of place */
  383. }
  384.  
  385.  
  386. PRIVATE struct {
  387.     char *name;
  388.     int class,subclass;
  389.  } dotted_keywords[]={
  390.             {".EQ.",tok_relop,relop_EQ},
  391.             {".NE.",tok_relop,relop_NE},
  392.             {".LE.",tok_relop,relop_LE},
  393.             {".LT.",tok_relop,relop_LT},
  394.             {".GE.",tok_relop,relop_GE},
  395.             {".GT.",tok_relop,relop_GT},
  396.             {".AND.",tok_AND,0},
  397.             {".OR.",tok_OR,0},
  398.             {".NOT.",tok_NOT,0},
  399.             {".FALSE.",tok_logical_const,FALSE},
  400.             {".TRUE.",tok_logical_const,TRUE},
  401.             {".EQV.",tok_EQV,0},
  402.             {".NEQV.",tok_NEQV,0},
  403.             {NULL,0,0}
  404.             };
  405.  
  406.  
  407. PRIVATE void
  408. get_dotted_keyword(token)
  409.     Token *token;
  410. {
  411.     int i=0,
  412.         has_embedded_space,    /* Spaces inside keyword */
  413.         space_seen_lately;    /* Flag for catching embedded space */
  414.     initial_flag = FALSE;
  415.                 /* Watch for embedded space, but not
  416.                    between dots and letters of keyword.
  417.                    I.e.  ". eq ." is OK, but not ".e q." */
  418.     has_embedded_space = FALSE;
  419.     space_seen_lately = FALSE;
  420.  
  421.     bi_advance();      /* gobble the initial '.' */
  422.  
  423.     while(isaletter(curr_char)) {
  424.  
  425.        if(src_text_len < MAX_SRC_TEXT)
  426.          src_text_buf[src_text_len++] = (char)makeupper(curr_char);
  427.  
  428.       if(space_seen_lately)
  429.         has_embedded_space = TRUE;
  430.  
  431.        bi_advance();
  432.  
  433.        space_seen_lately = iswhitespace(prev_char);
  434.     }
  435.  
  436.     if(src_text_len < MAX_SRC_TEXT)
  437.       src_text_buf[src_text_len++] = '.'; /* make it complete */
  438.  
  439.     if(curr_char != '.') {
  440.         yyerror("Badly formed logical/relational operator or constant");
  441.     }
  442.     else {
  443.         advance();      /* gobble the final '.' */
  444.     }
  445.     if(pretty_flag && has_embedded_space) {
  446.           ugly_code(token->line_num,token->col_num,
  447.             "keyword has embedded space");
  448.     }
  449.  
  450.     for(i=0; dotted_keywords[i].name != NULL; i++) {
  451.       if(strncmp(src_text_buf+1, /* only compare the significant parts */
  452.              dotted_keywords[i].name+1,
  453.              src_text_len-2) == 0) {
  454.         token->class = dotted_keywords[i].class;
  455.         token->subclass = dotted_keywords[i].subclass;
  456.         token->value.string = token->src_text = dotted_keywords[i].name;
  457. #ifdef DEBUG_FORLEX
  458.             if(debug_lexer)
  459.                (void)fprintf(list_fd,"\nDotted keyword:\t\t%s",
  460.                         token->src_text);
  461. #endif
  462.             return;
  463.         }
  464.     }
  465.             /* Match not found: signal an error */
  466.     yyerror("Unknown logical/relational operator or constant");
  467.     get_illegal_token(token);
  468.  
  469. } /* get_dotted_keyword */
  470.  
  471. PRIVATE void
  472. get_edit_descriptor(token)
  473.     Token *token;
  474. {
  475.     int c;
  476.     long repeat_spec;
  477.  
  478.     if(isadigit(curr_char)) {    /* Digit: repeat spec or holl or kP or nX */
  479.       repeat_spec = 0;
  480.       do {
  481.     if(src_text_len < MAX_SRC_TEXT)
  482.       src_text_buf[src_text_len++] = curr_char;
  483.     repeat_spec = repeat_spec*10L + (long)BCD(curr_char);
  484.     if( makeupper(next_char) == 'H' )
  485.       inside_hollerith = TRUE;/* get ready for hollerith*/
  486.     bi_advance();
  487.       } while(isadigit(curr_char));
  488.  
  489.       if( makeupper(curr_char) == 'H' ) {
  490.                 /* nH... pass off to hollerith routine */
  491.     get_hollerith(token, (int)repeat_spec);
  492.     return;
  493.       }
  494.       else {
  495.                 /* Otherwise it is a repeat spec or the
  496.                    numeric part of kP or nX which we treat
  497.                    as repeat specs too */
  498.     token->class = tok_integer_const;
  499.     token->value.integer = repeat_spec;
  500.     token->src_text = new_src_text(src_text_buf,src_text_len);
  501. #ifdef DEBUG_FORLEX
  502. if(debug_lexer)
  503. (void)fprintf(list_fd,"\nInteger const:\t\t%d (from %s)",
  504.           repeat_spec,
  505.               token->src_text);
  506. #endif
  507.       }
  508.     }/* end if digit */
  509.  
  510.     else if(isaletter(curr_char)) {
  511.       c = makeupper(curr_char);
  512.       if(src_text_len < MAX_SRC_TEXT)
  513.     src_text_buf[src_text_len++] = c;
  514.       bi_advance();
  515.       switch(c) {
  516.  
  517.     case 'P':        /* P of kP  k seen previously */
  518.       if(prev_token_class != tok_integer_const) {
  519.         if(f77_standard){
  520.           nonstandard(token->line_num,token->col_num);
  521.           msg_tail(": P must follow a number");
  522.         }
  523.       }
  524.       break;
  525.  
  526.     case 'X':        /* X or nX */
  527.       break;
  528.  
  529.     case 'S':        /* S or SP or SS */
  530.       c = makeupper(curr_char);
  531.       if(c == 'S' || c == 'P') {
  532.         if(src_text_len < MAX_SRC_TEXT)
  533.           src_text_buf[src_text_len++] = c;
  534.         bi_advance();
  535.       }
  536.       break;
  537.  
  538.     case 'B':        /* BN or BZ */
  539.       c = makeupper(curr_char);
  540.       if(c == 'N' || c == 'Z') {
  541.         if(src_text_len < MAX_SRC_TEXT)
  542.           src_text_buf[src_text_len++] = c;
  543.         bi_advance();
  544.       }
  545.       else {
  546.         if(f77_standard){
  547.           nonstandard(token->line_num,token->col_num);
  548.           msg_tail(": N or Z expected after B");
  549.         }
  550.       }
  551.       break;
  552.  
  553.     case 'T':        /* Tc or TLc or TRc */
  554.       c = makeupper(curr_char);
  555.       if(c == 'L' || c == 'R') {
  556.         if(src_text_len < MAX_SRC_TEXT)
  557.           src_text_buf[src_text_len++] = c;
  558.         bi_advance();
  559.       }
  560.       goto get_w_d;
  561.                 /* Iw, Ew.c and similar forms */
  562.     case 'A':    case 'D':    case 'E':
  563.     case 'F':    case 'G':    case 'L':
  564.     case 'I':
  565. get_w_d:                /* Get the w field if any */
  566.       while( isadigit(curr_char) ){
  567.         if(src_text_len < MAX_SRC_TEXT)
  568.           src_text_buf[src_text_len++] = curr_char;
  569.         bi_advance();
  570.       }
  571.             /* Include any dot followed by number (e.g. F10.5)
  572.             */
  573.       if( curr_char == '.' ) {
  574.         do {
  575.           if(src_text_len < MAX_SRC_TEXT)
  576.         src_text_buf[src_text_len++] = curr_char;
  577.           bi_advance();
  578.         } while( isadigit(curr_char) );
  579.       }
  580.       break;
  581.  
  582.     default:
  583.       if(f77_standard) {
  584.         nonstandard(token->line_num,token->col_num);
  585.         msg_tail(": edit descriptor");
  586.         src_text_buf[src_text_len++] = '\0';
  587.         msg_tail(src_text_buf);
  588.       }
  589.       goto get_w_d;
  590.       }/*end switch*/
  591.  
  592.       token->class = tok_edit_descriptor;
  593.       token->value.string = NULL;
  594.       token->src_text = new_src_text(src_text_buf,src_text_len);
  595.  
  596. #ifdef DEBUG_FORLEX
  597. if(debug_lexer)
  598. (void)fprintf(list_fd,"\nEdit descriptor:\t%s",token->src_text);
  599. #endif
  600.     }/*end else if isaletter*/
  601.  
  602.             /* Apostrophe or quote mark means a string. */
  603.     else if( isaquote(curr_char) ) {
  604.       get_string(token);
  605.     }
  606.                 /* Otherwise it is mere punctuation. Handle
  607.                    it here ourself to avoid complications. */
  608.     else {
  609.       src_text_buf[src_text_len++] = curr_char;
  610.       get_simple_punctuation(token);
  611.     }
  612. }
  613.  
  614. PRIVATE void
  615. get_hollerith(token,n)  /* Gets string of form nHaaaa */
  616.     Token *token;
  617.     int n;
  618. {
  619.     int i, last_col_num, last_line_num;
  620.  
  621.         /* strsize = length of only the string being defined
  622.            fullsize = length of whole hollerith const, which includes
  623.            length spec already stored in src_text_buf plus the
  624.            H plus the text plus final nul. */
  625.     int strsize=n,
  626.         leadin=src_text_len+1,
  627.         fullsize=leadin+strsize+1;
  628.     char *s;
  629.  
  630.     initial_flag = FALSE;
  631.  
  632.     s = new_src_text_alloc(fullsize);
  633.  
  634.     for(i=0; i<src_text_len; i++) /* Copy the leadin already saved */
  635.       s[i] = src_text_buf[i];
  636.     s[i++] = 'H';        /* store the 'H' */
  637.  
  638.     if(n==1)
  639.       inside_hollerith=FALSE;/* turn off flag ahead of next_char */
  640.     advance();/* Gobble the 'H' */
  641.  
  642.     last_col_num = col_num;
  643.     last_line_num = line_num;
  644.  
  645.     for(i=0; i<n; i++) {
  646.       while(curr_char == EOL) {
  647.             /* Treat short line as if extended with blanks */
  648.         int col;
  649.         for(col=last_col_num; i<n && col<max_stmt_col; i++,col++) {
  650.         s[leadin+i] = ' ';
  651.         }
  652.         last_col_num = col_num;
  653.         advance();
  654.       }
  655.       if(i==n) break;
  656.  
  657.       if(curr_char == EOS || curr_char == EOF) {
  658.         int col;
  659.         for(col=last_col_num; i<n && col<max_stmt_col; i++,col++) {
  660.           if(i < strsize)
  661.         s[leadin+i] = ' ';
  662.         }
  663.         if(i < n) {        /* If it did not fill up */
  664.           syntax_error((unsigned)last_line_num,(unsigned)last_col_num,
  665.                "Hollerith constant ends prematurely");
  666.           strsize=i;
  667.         }
  668.         break;
  669.       }
  670.       else {
  671.         s[leadin+i] = curr_char;
  672.         last_col_num = col_num;
  673.         last_line_num = line_num;
  674.         if(i==n-2)/* turn flag off ahead of next_char*/
  675.           inside_hollerith = FALSE;
  676.         advance();
  677.       }
  678.     }
  679.  
  680.     if(strsize > 0)
  681.       s[leadin+strsize] = '\0';
  682.  
  683.     inside_hollerith = FALSE;
  684.     token->class = tok_hollerith;
  685.     token->value.string = s + leadin;
  686.     token->size = n;
  687.     token->src_text = s;
  688. #ifdef DEBUG_FORLEX
  689.     if(debug_lexer)
  690.         (void)fprintf(list_fd,"\nHollerith:\t\t%s (from %s)",
  691.                   token->value.string,
  692.                   token->src_text);
  693. #endif
  694.  
  695. } /* get_hollerith */
  696.  
  697. #include "keywords.h"
  698.  
  699.     /* get_identifier reads a string of characters satisfying
  700.        isidletter.  As they are read and as long as they are
  701.        alphabetic, it looks for a match to a keyword, and
  702.        whenever one is found, checks with is_keyword to see
  703.        if the context is right.  If so, it returns the keyword.
  704.        Otherwise it keeps going and eventually returns the id.
  705.      */
  706. PRIVATE void
  707. get_identifier(token)
  708.     Token *token;
  709. {
  710.     int c,        /* Uppercase version of current letter */
  711.         preceding_c,/* Char preceding latest id */
  712.         has_embedded_space,    /* Spaces inside keyword or id */
  713.         space_seen_lately,    /* Flag for catching embedded space */
  714.         lo,hi,    /* Indices in keyword table where match may be */
  715.         klen,    /* Length of id read so far (after keyword test) */
  716.         keywd_class;/* Class number returned by is_keyword */
  717.     int possible_keyword;
  718.  
  719.     token->class = tok_identifier;
  720.     keywd_class = FALSE;
  721.  
  722.     klen = 0;
  723.     lo = 0;
  724.     hi = NUM_KEYWORDS-1;
  725.  
  726.     /* Define shorthand for the keyword letter under study */
  727. #define KN(i) keywords[i].name
  728. #define KL(i) keywords[i].name[klen]
  729.  
  730.     possible_keyword = TRUE;
  731.     preceding_c = prev_char;
  732.     has_embedded_space = FALSE;
  733.     space_seen_lately = FALSE;
  734.  
  735.             /* This loop gets  letter [letter|digit]* forms */
  736.     while(isidletter(curr_char) || isadigit(curr_char)) {
  737.       c = makeupper(curr_char); /* Get the next char of id */
  738.       if(src_text_len < MAX_SRC_TEXT)
  739.         src_text_buf[src_text_len++] = (int)makeupper(curr_char);
  740.  
  741.       if(space_seen_lately)
  742.         has_embedded_space = TRUE;
  743.  
  744.       bi_advance();        /* Pull in the next character */
  745.  
  746.       space_seen_lately = iswhitespace(prev_char);
  747.  
  748.                 /* As long as it may yet be a keyword,
  749.                    keep track of whether to invoke is_keyword.
  750.                  */
  751.       if(possible_keyword) {
  752.  
  753.         if(!isaletter(c)    /* If not alphabetic, cannot be keyword */
  754.            || klen >= sizeof(keywords[0].name)-1) /* or overlength */
  755.         {
  756. #ifdef DEBUG_IS_KEYWORD
  757. if(debug_lexer && getenv("BISECTION")) {
  758. src_text_buf[src_text_len] = '\0';
  759. (void)fprintf(list_fd,"\n%s not a keyword because",src_text_buf);
  760. if(!isaletter(c))
  761.   (void)fprintf(list_fd," non-letter at %c",c);
  762. if(klen >= sizeof(keywords[0].name)-1)
  763.   (void)fprintf(list_fd,"length %d >= max %d",klen,sizeof(keywords[0].name)-1);
  764. }
  765. #endif
  766.           possible_keyword = FALSE;
  767.         }
  768.         else {
  769.           int mid;
  770. #ifdef DEBUG_IS_KEYWORD
  771. if(debug_lexer && getenv("BISECTION")) {
  772. (void)fprintf(list_fd,"\nklen=%d c=%c",klen,c);
  773. (void)fprintf(list_fd,"\nBisecting [lo,hi]=[%d,%d] \"%s\"..\"%s\"",
  774.        lo,hi,KN(lo),KN(hi));
  775. }
  776. #endif
  777.                 /* Bisect lo .. hi looking for match
  778.                    on characters found so far. */
  779.           while(lo <= hi) {
  780.         mid = (lo + hi)/2;
  781.         if( KL(mid) < c ) {    /* No match in lower half */
  782.           lo = mid+1;
  783.         }
  784.         else if( KL(mid) > c ) {/* No match in upper half */
  785.           hi = mid-1;
  786.         }
  787.         else {        /* Match at midpoint: Bisect each
  788.                    half to find the new subinterval. */
  789.           int midlo=mid, midhi=mid;
  790.                 /* Bisect lo .. mid */
  791.           while( lo < midlo-1 &&  KL(lo) != c) {
  792.             mid = (lo + midlo)/2;
  793.             if(  KL(mid) < c ) {
  794.               lo = mid+1;
  795.             }
  796.             else {    /* equal */
  797.               midlo = mid;
  798.             }
  799.           }
  800.           if( KL(lo) != c )
  801.             lo = midlo;
  802.                 /* Bisect mid .. hi */
  803.           while( midhi < hi-1 && KL(hi) != c ) {
  804.             mid = (midhi + hi)/2;
  805.             if( KL(mid) > c ) {
  806.               hi = mid-1;
  807.             }
  808.             else {    /* equal */
  809.               midhi = mid;
  810.             }
  811.           }
  812.           if( KL(hi) != c )
  813.             hi = midhi;
  814.  
  815.           break;    /* After bisecting each half, we are done */
  816.         }        /* end else KL(mid) == c */
  817.           }            /* end while(lo <= hi) */
  818.  
  819.           klen++;        /* Now increment the length */
  820.  
  821. #ifdef DEBUG_IS_KEYWORD
  822. if(debug_lexer && getenv("BISECTION")) {
  823. (void)fprintf(list_fd,"\nNew [lo,hi]=[%d,%d] \"%s\"..\"%s\"",
  824.        lo,hi,KN(lo),KN(hi));
  825. }
  826. #endif
  827.             /* If range is null, a match has been ruled out. */
  828.           if(lo > hi) {
  829. #ifdef DEBUG_IS_KEYWORD
  830. if(debug_lexer && getenv("BISECTION")) {
  831. src_text_buf[src_text_len] = '\0';
  832. (void)fprintf(list_fd,"\nKeyword ruled out for %s at length %d since lo %d > hi %d",
  833.        src_text_buf,klen,lo,hi);
  834. }
  835. #endif
  836.         possible_keyword = FALSE;
  837.           }
  838.             /* If length of first keyword in range is equal
  839.                to the new length, then we have a match at
  840.                this point.  Check it out with is_keyword.
  841.              */
  842.           else if(KN(lo)[klen] == '\0') {
  843.         if( (keywd_class = is_keyword(lo)) != FALSE) {
  844.           token->class = keywd_class;    /* It's a keyword */
  845.           token->value.string = NULL;
  846.           token->src_text = KN(lo);
  847.           break;    /* Quit the input loop */
  848.         }
  849.         else if(lo == hi) {    /* Match is unique and ruled out */
  850.           possible_keyword = FALSE;
  851.         }
  852.           }
  853.         }/* end else isaletter(c) */
  854.       }/* end if(possible_keyword) */
  855.     }/* end while(isidletter || isadigit) */
  856.  
  857.     if(keywd_class == FALSE) {        /* it is an identifier */
  858.  
  859.                 /* Identifier: find its hashtable entry or
  860.                    create a new entry.    */
  861.             int h;
  862.             Lsymtab *symt;
  863. #ifdef ALLOW_TYPELESS_CONSTANTS
  864.                 /* Watch out for const like X'nnn' */
  865.             if(src_text_len == 1 && isaquote(curr_char)) {
  866.                 /* Read the string, append the trailing quote
  867.                    then invoke routine to interpret it. */
  868.               get_string(token);
  869. #ifndef LEX_RAWSTRINGS
  870.               if(src_text_len < MAX_SRC_TEXT)
  871.             src_text_buf[src_text_len++] = quote_char;
  872. #endif
  873.               get_binary_const(token,src_text_buf[0]);
  874.               return;
  875.             }
  876. #endif
  877.  
  878.             if(src_text_len < MAX_SRC_TEXT)
  879.               src_text_buf[src_text_len] = '\0';
  880.             token->value.integer = h = hash_lookup(src_text_buf);
  881.             token->src_text = hashtab[h].name;
  882.                 /* If it is an array give it a special token
  883.                    class, so that arrays can be distinguished
  884.                    from functions in the grammar. */
  885.             if((symt=hashtab[h].loc_symtab) != NULL
  886.                && symt->array_var) {
  887.               token->class = tok_array_identifier;
  888.  
  889.       }
  890.     }
  891.                 /* Check identifiers for being juxtaposed
  892.                    to keywords or having internal space.
  893.                    Keywords are immune to warning since
  894.                    want to allow both GOTO and GO TO, etc.
  895.                  */
  896.  
  897.     if(pretty_flag &&
  898.        (token->class==tok_identifier || token->class==tok_array_identifier)
  899.        && ( isidletter(preceding_c) || isadigit(preceding_c)
  900.            || has_embedded_space ) ) {
  901.  
  902.           ugly_code(token->line_num,token->col_num,"identifier");
  903.           msg_tail(hashtab[token->value.integer].name);
  904. #if 0    /* Keywords immune for now */
  905.           ugly_code(token->line_num,token->col_num,"keyword");
  906.           msg_tail(keywords[keytab_index[keywd_class-keytab_offset]].name);
  907. #endif
  908.       if(has_embedded_space)
  909.         msg_tail("has embedded space");
  910.       else
  911.         msg_tail("not clearly separated from context");
  912.     }
  913.  
  914. #ifdef DEBUG_FORLEX
  915.     if(debug_lexer){
  916.         switch(token->class) {
  917.         case tok_identifier:
  918.             (void)fprintf(list_fd,"\nIdentifier:\t\t%s",
  919.                       token->src_text);
  920.             break;
  921.         case tok_array_identifier:
  922.             (void)fprintf(list_fd,"\nArray_identifier:\t%s",
  923.                       token->src_text);
  924.             break;
  925.         default:
  926.             (void)fprintf(list_fd,"\nKeyword:\t\ttok_%s",
  927.                       token->src_text);
  928.             break;
  929.         }
  930.     }
  931. #endif
  932. } /* get_identifier */
  933.  
  934. /*  iskeyword:
  935.     Determines (to the best of its current ability) whether a given
  936.     identifier is a keyword or not.  Hopefully now no keywords are
  937.     reserved.
  938.  
  939.     Method uses context from start of statement up to and including
  940.     the character following the putative keyword to eliminate as
  941.     many cases as possible.  Any non-IK keywords (those that need not
  942.     be in the initial series of keywords of statement) have special
  943.     code to handle them.  Any IK's that are always the second word of a
  944.     pair are accepted if the predecessor was just seen.  The rest are
  945.     handed off to looking_at_keywd which tries to see if
  946.     it is an assignment statement.
  947.  
  948.     Note that some rules that could be used if F77 Standard were
  949.     adhered to strictly are not used here.  The idea is to allow
  950.     extensions, and leave catching syntax errors in the parser.
  951.     For example, specification-statement keywords are not excluded
  952.     after the first executable statement has been seen.  The status
  953.     of a variable as declared array or character type is not consulted
  954.     in ruling out an assignment statement if following parentheses
  955.     are present.  Etc.
  956. */
  957.  
  958.  
  959.         /* Macro to test if all the specified bits are set */
  960. #define MATCH(CONTEXT) ((keywords[i].context & (CONTEXT)) == (CONTEXT))
  961.  
  962.  
  963. LEX_SHARED int
  964. is_keyword(i)
  965.      int i;            /* Index in keywords table */
  966. {
  967.   int ans = FALSE;
  968.   int putative_keyword_class;    /* Class of the supposed keyword */
  969.  
  970.   while(iswhitespace(curr_char))          /* Move to lookahead char */
  971.     advance();
  972.  
  973. #ifdef DEBUG_IS_KEYWORD
  974.   if(debug_lexer){
  975.     (void)fprintf(list_fd,
  976.         "\nkeyword %s: initialflag=%d implicitflag=%d ",
  977.         keywords[i].name,initial_flag,implicit_flag);
  978.     (void)fprintf(list_fd,
  979.         "context=%o, next char=%c %o",keywords[i].context,
  980.                         curr_char,curr_char);
  981.   }
  982. #endif
  983.  
  984.   putative_keyword_class = keywords[i].class;
  985.  
  986.   if( !initial_flag && MATCH(IK) ) {
  987.             /* Dispose of keywords which can only occur in initial
  988.                part of statement, if found elsewhere. */
  989.     ans = FALSE;
  990.   }
  991.  
  992. #if 0 /* This does not work: curr_stmt_class not cleared beforehand */
  993.   else if(curr_stmt_class == tok_IF && MATCH(NI)) {
  994.             /* Dispose of keywords which cannot occur in stmt
  995.                field of logical IF if that is where we are.
  996.              */
  997.     ans = FALSE;
  998.   }
  999. #endif
  1000.  
  1001.   else if(MATCH(NA) && isalpha(curr_char)) {
  1002.             /* Dispose of keywords which cannot be followed
  1003.                by alphabetic character if that is so.
  1004.              */
  1005.     ans = FALSE;
  1006.   }
  1007.  
  1008.   else if(putative_keyword_class == tok_TO) {/* A non-IK case */
  1009.                 /* TO always follows the word GO or
  1010.                    is followed by a variable
  1011.                    name (in ASSIGN statement).
  1012.                  */
  1013. #ifdef SPLIT_KEYWORDS
  1014.  
  1015. #define in_assign_stmt (curr_stmt_class == tok_ASSIGN)
  1016.     ans = (prev_token_class == (in_assign_stmt?
  1017.                   tok_integer_const:
  1018.                   tok_GO));
  1019. #else
  1020.     ans = ( curr_stmt_class == tok_ASSIGN
  1021.        && prev_token_class == tok_integer_const);
  1022. #endif
  1023.   }
  1024.   else if(putative_keyword_class == tok_FUNCTION /* A non-IK case */
  1025.     && (stmt_sequence_no != 0 /* not the first statement of module */
  1026.  
  1027.     || !(initial_flag  /* if not initial can only be preceded by type */
  1028.          || is_a_type_token(curr_stmt_class)) )) {
  1029.     ans = FALSE; /* otherwise it will be handled correctly by looking_at */
  1030.   }
  1031.   else if(putative_keyword_class == tok_WHILE) { /* A non-IK case */
  1032.     ans = WHILE_expected; /* Only occurs in DO label [,] WHILE */
  1033.     WHILE_expected = FALSE;
  1034.   }
  1035.         /* Remaining cases are IK in initial part */
  1036.  
  1037.             /*   Eliminate those which can never be followed
  1038.                  by '(' or '=' if that is what we have.
  1039.              */
  1040.   else if(MATCH(NP) &&
  1041.       (curr_char == '(' || curr_char == '=') ) {
  1042.     ans = FALSE;
  1043.   }
  1044.  
  1045.             /* Likewise with those that must be followed by
  1046.                '(' but aren't  */
  1047.   else if(MATCH(MP) && curr_char != '(') {
  1048.     ans = FALSE;
  1049.   }
  1050.  
  1051.                 /* PRECISION always follows the word DOUBLE */
  1052.   else if( putative_keyword_class == tok_PRECISION ){
  1053.     ans = (prev_token_class == tok_DOUBLE);
  1054.   }
  1055.  
  1056.                 /* END DO: handle its DO here */
  1057.   else if( putative_keyword_class == tok_DO && curr_char == EOS ) {
  1058.     /* Also must have prev_token_class == tok_END, but
  1059.        no need to check since end-of-statement suffices. */
  1060.     ans = TRUE;
  1061.   }
  1062.  
  1063.  
  1064.                 /* Other type names always follow the word
  1065.                    IMPLICIT */
  1066.   else if( implicit_flag ) {
  1067.     ans =  MATCH(TY);
  1068.   }
  1069.  
  1070.   else {
  1071.              /* Remaining cases are keywords that must be in
  1072.             initial position. If followed by '=' must be an
  1073.             identifier.  If followed by '(' then may be an array
  1074.             or character lvalue, so use looking_at to scan ahead
  1075.             to see if this is an assignment statement. */
  1076.       ans =  looking_at_keywd(putative_keyword_class);
  1077.   }
  1078.  
  1079.  
  1080.             /* Save initial token class for use by parser.
  1081.                Either set it to keyword token or to id for
  1082.                assignment stmt. */
  1083.   if(initial_flag) {
  1084.     curr_stmt_class = (ans? keywords[i].class: tok_identifier);
  1085.   }
  1086.  
  1087.         /* Turn off the initial-keyword flag if this is a
  1088.            keyword that cannot be followed by another keyword
  1089.            or if it is not a keyword.
  1090.         */
  1091.   if(ans) {
  1092.     if(keywords[i].context & EK)
  1093.       initial_flag = FALSE;
  1094.     return keywords[i].class;
  1095.   }
  1096.   else {    /* If no more letters follow, then keyword here
  1097.            is ruled out.  Turn off initial_flag. */
  1098.     if( ! isalpha(curr_char) )
  1099.       initial_flag = FALSE;
  1100.     return 0;    /* Not found in list */
  1101.   }
  1102. }/* End of is_keyword */
  1103.  
  1104.  
  1105. /*    init_keyhashtab:
  1106. */
  1107.         /* Hashing is no longer used.  This guy now only
  1108.            initializes the table of indices that allow
  1109.            keywords to be looked up by their token class*/
  1110. void
  1111. init_keyhashtab()
  1112. {
  1113.   int i,k,kmin,kmax;
  1114.   kmin = kmax = keywords[0].class;    /* Find min and max token classes */
  1115.   for(i=1; i<NUM_KEYWORDS; i++) {
  1116.     k = keywords[i].class;
  1117.     if(k < kmin)  kmin = k;
  1118.     if(k > kmax)  kmax = k;
  1119.   }
  1120.  
  1121.   keytab_offset = kmin;    /* Index table from [kmin..kmax] -> [0..size-1] */
  1122.   keytab_size = (unsigned) (kmax-kmin+1);
  1123.   if( (keytab_index=(short *)calloc(keytab_size,sizeof(keytab_index[0])))
  1124.      == (short *)NULL) {
  1125.     oops_message(OOPS_FATAL,NO_LINE_NUM,NO_COL_NUM,
  1126.        "cannot allocate space for keytab_index");
  1127.   }
  1128.  
  1129.                 /* Now fill in the lookup table, indexed
  1130.                    by class - offset */
  1131.   for(i=0; i<NUM_KEYWORDS; i++) {
  1132.     k = keywords[i].class;
  1133.     keytab_index[k - keytab_offset] = i;
  1134.   }
  1135. }
  1136.  
  1137.  
  1138. PRIVATE void
  1139. get_illegal_token(token)    /* Handle an illegal input situation */
  1140.     Token *token;
  1141. {
  1142.     token->class = tok_illegal;
  1143.     token->src_text = new_src_text("",0);
  1144. #ifdef DEBUG_FORLEX
  1145.     if(debug_lexer)
  1146.          (void)fprintf(list_fd,"\nILLEGAL TOKEN");
  1147. #endif
  1148.  
  1149. } /* get_illegal_token */
  1150.  
  1151.  
  1152.  
  1153.         /* Read a label from label field. */
  1154. PRIVATE void
  1155. get_label(token)
  1156.     Token *token;
  1157. {
  1158.     int value=0;
  1159.     int space_seen=FALSE, has_embedded_space=FALSE;
  1160.     while( isadigit(curr_char) && col_num < 6 ) {
  1161.       if(space_seen)
  1162.         has_embedded_space = TRUE;
  1163.       value = value*10 + BCD(curr_char);
  1164.       src_text_buf[src_text_len++] = curr_char;
  1165.       advance();
  1166.       while(curr_char==' ' && col_num < 6) {
  1167.         space_seen = TRUE;
  1168.         advance();
  1169.       }
  1170.     }
  1171.     if(pretty_flag && has_embedded_space) {
  1172.           ugly_code(token->line_num,token->col_num,
  1173.             "label has embedded space");
  1174.     }
  1175.     token->class = tok_label;
  1176.     token->value.integer = value;
  1177.     token->src_text = new_src_text(src_text_buf,src_text_len);
  1178. #ifdef DEBUG_FORLEX
  1179.     if(debug_lexer)
  1180.         (void)fprintf(list_fd,"\nLabel:\t\t\t%d (from %s)",
  1181.                   value,
  1182.                   token->src_text);
  1183. #endif
  1184.  
  1185. } /* get_label */
  1186.  
  1187.  
  1188. PRIVATE void
  1189. get_letter(token)        /* Gets letter in IMPLICIT list */
  1190.     Token *token;
  1191. {
  1192.     token->class = tok_letter;
  1193.     src_text_buf[src_text_len++] = 
  1194.       token->subclass = makeupper(curr_char);
  1195.     token->src_text = new_src_text(src_text_buf,src_text_len);
  1196.  
  1197. #ifdef DEBUG_FORLEX
  1198.     if(debug_lexer)
  1199.     (void)fprintf(list_fd,"\nLetter:\t\t\t%s",token->src_text);
  1200. #endif
  1201.  
  1202.     advance();
  1203.  
  1204. } /* get_letter */
  1205.  
  1206.  
  1207.     /* get_number reads a number and determines data type: integer,
  1208.      * real, or double precision.
  1209.      */
  1210. /* This belongs in ftnchek.h, perhaps.  Defines number of significant
  1211.    figures that are reasonable for a single-precision real constant.
  1212.    Works out to 9 for wordsize=4, 21 for wordsize=8. These allow
  1213.    for a couple of extra digits for rounding. Used in -trunc warning. */
  1214. #define REAL_SIGFIGS (local_wordsize==0? 8: (local_wordsize-1)*3)
  1215.  
  1216. PRIVATE int getting_complex_const=FALSE;
  1217.  
  1218. PRIVATE void
  1219. get_number(token)
  1220.     Token *token;
  1221. {
  1222.     DBLVAL dvalue,leftside,rightside,pwr_of_ten;
  1223.     int exponent,datatype,c;
  1224. #ifdef DEBUG_FORLEX
  1225.     int expsign;
  1226. #endif
  1227.     int numdigits,    /* Count of digits in integer, significant or not */
  1228.         sigfigs;    /* Count of significant digits */
  1229.  
  1230.     initial_flag = FALSE;
  1231.  
  1232.     leftside = (DBLVAL)0;
  1233.     numdigits = sigfigs = 0;
  1234.     datatype = tok_integer_const;
  1235.     while(isadigit(curr_char)) {
  1236.         leftside = leftside*(DBLVAL)10 + (DBLVAL)BCD(curr_char);
  1237.         ++numdigits;
  1238.             /* Do not count leading zeroes as significant */
  1239.         if(sigfigs > 0 || curr_char != '0')
  1240.           ++sigfigs;
  1241.         if( !integer_context && makeupper(next_char) == 'H' )
  1242.           inside_hollerith = TRUE;/* get ready for hollerith*/
  1243.  
  1244.         if(src_text_len < MAX_SRC_TEXT)
  1245.           src_text_buf[src_text_len++] = curr_char;
  1246.                 /* Embedded space is worth preserving since
  1247.                    it is often used in long numbers.  Any
  1248.                    amount of blanks + tabs -> 1 blank.
  1249.                    Exception: integer_context says upcoming
  1250.                    item is a label or datatype length spec. */
  1251.         if(! integer_context &&
  1252.            (next_char == ' ' || next_char == '\t'))
  1253.           if(src_text_len < MAX_SRC_TEXT)
  1254.             src_text_buf[src_text_len++] = ' ';
  1255.  
  1256.         bi_advance();
  1257.     }
  1258.  
  1259.         /* If context specifies integer expected, skip to end.
  1260.            Otherwise scan on ahead for more. */
  1261.     if( integer_context) {
  1262.         if(numdigits == 0) {
  1263.         yyerror("integer expected");
  1264.         advance();    /* gobble something to avoid infinite loop */
  1265.     }
  1266.     }
  1267.     else {/* not integer_context */
  1268.     if( makeupper(curr_char) == 'H' ){      /* nnH means hollerith */
  1269.         if(leftside == (DBLVAL)0) {
  1270.             yyerror("Zero-length hollerith constant");
  1271.             inside_hollerith = FALSE;
  1272.             advance();
  1273.             get_illegal_token(token);
  1274.         }
  1275.         else {
  1276.             if(src_text_buf[src_text_len-1] == ' ')
  1277.               --src_text_len;
  1278.             get_hollerith(token, (int)leftside);
  1279.         }
  1280.         return;
  1281.     }
  1282.  
  1283.     rightside = (DBLVAL)0;
  1284.     pwr_of_ten = (DBLVAL)1;
  1285.     closeup();        /* Pull in the lookahead character */
  1286.     if( curr_char == '.' &&
  1287.                 /* don't be fooled by 1.eq.N or
  1288.                    I.eq.1.and. etc */
  1289.        !looking_at_relop() ) {
  1290.         datatype = tok_real_const;
  1291.         if(src_text_len < MAX_SRC_TEXT)
  1292.           src_text_buf[src_text_len++] = curr_char;
  1293.         bi_advance();
  1294.         while(isadigit(curr_char)) {
  1295.             rightside = rightside*(DBLVAL)10 + (DBLVAL)BCD(curr_char);
  1296.             ++numdigits;
  1297.             if(sigfigs > 0 || curr_char != '0')
  1298.               ++sigfigs;
  1299.             pwr_of_ten /= (DBLVAL)10;
  1300.  
  1301.             if(src_text_len < MAX_SRC_TEXT)
  1302.               src_text_buf[src_text_len++] = curr_char;
  1303.             if(next_char == ' ' || next_char == '\t')
  1304.               if(src_text_len < MAX_SRC_TEXT)
  1305.                 src_text_buf[src_text_len++] = ' ';
  1306.  
  1307.             bi_advance();
  1308.         }
  1309.     }
  1310. #ifdef DEBUG_FORLEX
  1311. if(debug_lexer)
  1312.     dvalue = leftside + rightside*pwr_of_ten;
  1313. else
  1314. #endif
  1315.     dvalue = (DBLVAL)0;
  1316.  
  1317.     exponent = 0;
  1318. #ifdef DEBUG_FORLEX
  1319.     expsign = 1;
  1320. #endif
  1321.         /* Integer followed by E or D gives a real/d.p constant */
  1322.  
  1323.     if( ( (c = makeupper(curr_char)) == 'E' || c == 'D' ) )
  1324.     {
  1325.         datatype = ((c == 'E')? tok_real_const: tok_dp_const);
  1326.         if(src_text_len < MAX_SRC_TEXT)
  1327.           src_text_buf[src_text_len++] = c;
  1328.         bi_advance();
  1329.         if(curr_char == '+') {
  1330. #ifdef DEBUG_FORLEX
  1331.             expsign = 1;
  1332. #endif
  1333.             if(src_text_len < MAX_SRC_TEXT)
  1334.               src_text_buf[src_text_len++] = curr_char;
  1335.             bi_advance();
  1336.         }
  1337.         else if(curr_char == '-') {
  1338. #ifdef DEBUG_FORLEX
  1339.             expsign = -1;
  1340. #endif
  1341.             if(src_text_len < MAX_SRC_TEXT)
  1342.               src_text_buf[src_text_len++] = curr_char;
  1343.             bi_advance();
  1344.         }
  1345.         if(!isadigit(curr_char)) {
  1346.             yyerror("Badly formed real constant");
  1347.         }
  1348.         else while(isadigit(curr_char)) {
  1349.             exponent = exponent*10 + (curr_char-'0');
  1350.             if(src_text_len < MAX_SRC_TEXT)
  1351.               src_text_buf[src_text_len++] = curr_char;
  1352.             bi_advance();
  1353.         }
  1354.  
  1355.     /*  Compute real value only if debugging. If it exceeds max magnitude,
  1356.         computing it may cause crash. At this time, value of real const
  1357.         is not used for anything. */
  1358. #ifdef DEBUG_FORLEX
  1359. if(debug_lexer)
  1360.           dvalue *= pow(10.0, (double)(exponent*expsign));
  1361. else
  1362. #endif
  1363.           dvalue = (DBLVAL)0;
  1364.  
  1365.     }
  1366.     }/* end if(!integer_context) */
  1367.  
  1368.         if(src_text_buf[src_text_len-1] == ' ')    /* remove any trailing blank */
  1369.       --src_text_len;
  1370.  
  1371.     token->class = datatype;
  1372.                 /* If this is part of complex const,
  1373.                    do not store src_text but arrange
  1374.                    so debugging works. */
  1375.     if(!getting_complex_const) {
  1376.       token->src_text = new_src_text(src_text_buf,src_text_len);
  1377.     }
  1378. #ifdef DEBUG_FORLEX
  1379.       else {
  1380.         src_text_buf[src_text_len] = '\0';
  1381.         token->src_text = src_text_buf;
  1382.       }
  1383. #endif
  1384.     switch(datatype) {
  1385.        case tok_integer_const:
  1386.         token->value.integer = (long)leftside;
  1387. #ifdef DEBUG_FORLEX
  1388. if(debug_lexer)
  1389. (void)fprintf(list_fd,"\nInteger const:\t\t%ld (from %s)",
  1390.           token->value.integer,
  1391.           token->src_text);
  1392. #endif
  1393.         break;
  1394.        case tok_real_const:
  1395.             /* store single as double lest it overflow */
  1396.         token->value.dbl = dvalue;
  1397.         if(trunc_check && sigfigs >= REAL_SIGFIGS) {
  1398.           warning(token->line_num,token->col_num,
  1399.     "Single-precision real constant has more digits than are stored");
  1400.         }
  1401. #ifdef DEBUG_FORLEX
  1402. if(debug_lexer)
  1403. (void)fprintf(list_fd,"\nReal const:\t\t%g (from %s)",
  1404.           (double)token->value.dbl,
  1405.           token->src_text);
  1406. #endif
  1407.         break;
  1408.        case tok_dp_const:
  1409.         token->value.dbl = dvalue;
  1410. #ifdef DEBUG_FORLEX
  1411. if(debug_lexer)
  1412. (void)fprintf(list_fd,"\nDouble const:\t\t%lg (from %s)",
  1413.           (double)token->value.dbl,
  1414.           token->src_text);
  1415. #endif
  1416.         break;
  1417.     }
  1418.  
  1419. } /* get_number */
  1420.  
  1421.      /* get_complex_constant reads an entity of the form (num,num)
  1422.       where num is any [signed] numeric constant.  It will only be
  1423.       called when looking_at() has guaranteed that there is one there.
  1424.       The token receives the real part as a number.  The imaginary part
  1425.       is not stored.  Whitespace is allowed between ( and num, around
  1426.       the comma, and between num and ) but not within num. */
  1427.  
  1428. PRIVATE void
  1429. get_complex_const(token)
  1430.     Token *token;
  1431. {
  1432.     Token imag_part;    /* temporary to hold imag part */
  1433. #ifdef DEBUG_FORLEX
  1434.     double sign=(DBLVAL)1;
  1435. #endif
  1436.     int dble_size=FALSE;    /* flag to set if parts are D floats */
  1437.     int imag_dble_size=FALSE;/* if imaginary part D float */
  1438.     unsigned comma_line_num,comma_col_num;
  1439.     getting_complex_const = TRUE;
  1440.     initial_flag = FALSE;
  1441.  
  1442.  
  1443.  
  1444.     bi_advance();    /* skip over the initial paren (already stored) */
  1445.  
  1446.  
  1447.     if(curr_char == '+' || curr_char == '-') {
  1448. #ifdef DEBUG_FORLEX
  1449.       if(curr_char == '-') sign = (DBLVAL)(-1);
  1450. #endif
  1451.       if(src_text_len < MAX_SRC_TEXT)
  1452.         src_text_buf[src_text_len++] = curr_char;
  1453.  
  1454.       bi_advance();
  1455.     }
  1456.  
  1457. #ifdef DEBUG_FORLEX
  1458. if(debug_lexer){
  1459. (void)fprintf(list_fd,"\nComplex const:(");
  1460. if(sign < 0.0) (void)fprintf(list_fd," -");
  1461. }
  1462. #endif
  1463.     get_number(token);
  1464.     switch((short)token->class) {
  1465.        case tok_integer_const:
  1466. #ifdef DEBUG_FORLEX
  1467. if(debug_lexer)
  1468.         token->value.dbl = sign*(double)token->value.integer;
  1469. else
  1470. #endif
  1471.         token->value.dbl = (DBLVAL)0;
  1472.         break;
  1473.        case tok_dp_const:
  1474.         dble_size=TRUE;
  1475.             /*FALLTHRU*/
  1476.        case tok_real_const:
  1477. #ifdef DEBUG_FORLEX
  1478. if(debug_lexer)
  1479.         token->value.dbl = sign*token->value.dbl;
  1480. else
  1481. #endif
  1482.         token->value.dbl = (DBLVAL)0;
  1483.         break;
  1484.     }
  1485.  
  1486.     while(iswhitespace(curr_char))
  1487.       advance();
  1488.  
  1489.  
  1490.     comma_line_num = line_num;
  1491.     comma_col_num = col_num;
  1492.  
  1493.     if(src_text_len < MAX_SRC_TEXT)
  1494.       src_text_buf[src_text_len++] = curr_char;
  1495.     if(next_char == ' ' || next_char == '\t') /* preserve space after , */
  1496.       if(src_text_len < MAX_SRC_TEXT)
  1497.         src_text_buf[src_text_len++] = ' ';
  1498.  
  1499.     bi_advance();        /* skip over the comma */
  1500.  
  1501.     if(curr_char == '+' || curr_char == '-') {
  1502. #ifdef DEBUG_FORLEX
  1503.          if(curr_char == '-') sign = (DBLVAL)(-1);
  1504. #endif
  1505.          if(src_text_len < MAX_SRC_TEXT)
  1506.         src_text_buf[src_text_len++] = curr_char;
  1507.  
  1508.          bi_advance();
  1509.     }
  1510. #ifdef DEBUG_FORLEX
  1511. if(debug_lexer){
  1512. (void)fprintf(list_fd,"\n,");
  1513. if(sign < 0.0) (void)fprintf(list_fd," -");
  1514. }
  1515. #endif
  1516.     get_number(&imag_part);
  1517.     imag_dble_size = (imag_part.class == tok_dp_const);
  1518.  
  1519.     if(dble_size != imag_dble_size) {
  1520.         warning(comma_line_num,comma_col_num,
  1521.           "different precision in real and imaginary parts");
  1522.     }
  1523.     else if(f77_standard) {
  1524.       if(dble_size)
  1525.         warning(token->line_num,token->col_num,
  1526.           "nonstandard double precision complex constant");
  1527.     }
  1528.  
  1529.     dble_size = (dble_size || imag_dble_size);
  1530.  
  1531.     while(iswhitespace(curr_char))
  1532.        advance();
  1533.  
  1534.  
  1535.     if(src_text_len < MAX_SRC_TEXT)
  1536.       src_text_buf[src_text_len++] = curr_char;
  1537.  
  1538.     advance();    /* skip over final paren */
  1539.  
  1540.     if(dble_size)
  1541.       token->class = tok_dcomplex_const;
  1542.     else
  1543.       token->class = tok_complex_const;
  1544.  
  1545.     token->src_text = new_src_text(src_text_buf,src_text_len);
  1546.  
  1547. #ifdef DEBUG_FORLEX
  1548. if(debug_lexer) {
  1549. (void)fprintf(list_fd,"\n\t\t\tsource text=%s",
  1550.           token->src_text);
  1551. (void)fprintf(list_fd,"\n)");
  1552. }
  1553. #endif
  1554.  
  1555.     getting_complex_const = FALSE;
  1556. }
  1557.  
  1558. #ifdef ALLOW_TYPELESS_CONSTANTS
  1559.         /* Routine to get constants of the forms:
  1560.             B'nnnn' 'nnnn'B  -- binary
  1561.             O'nnnn' 'nnnn'O  -- octal
  1562.             X'nnnn' Z'nnnn' 'nnnn'X 'nnnn'Z  -- hex
  1563.            No check of whether digits are less than base.
  1564.            Nonstandard warning is issued here since the constant
  1565.            looks like a normal integer by the time the parser sees it.
  1566.          */
  1567. PRIVATE void
  1568. get_binary_const(token,c)
  1569.      Token *token;
  1570.      int c;            /* base character: madeupper'ed by caller */
  1571. {
  1572.   long value=0;
  1573.   int base,digit;
  1574.   int i,j;            /* indices in src_text_buf for repacking */
  1575.   if(c == 'O')  base = 8;
  1576.   else if(c == 'X' || c == 'Z')  base = 16;
  1577.   else if(c == 'B') base = 2;
  1578.   else {
  1579.     syntax_error(token->line_num,token->col_num,
  1580.          "Unknown base for typeless constant -- octal assumed");
  1581.     base = 8;
  1582.   }
  1583.  
  1584.                 /* Advance i to starting digit */
  1585.   i = 0;
  1586.   while( ! isaquote(src_text_buf[i]) ) {
  1587.     ++i;
  1588.   }
  1589.   j = ++i;    /* Input = Output to start */
  1590.  
  1591.                 /* Scan the string, moving chars down
  1592.                    to change multi spaces to single
  1593.                    blanks, and converting digits. */
  1594.   while( ! isaquote(src_text_buf[i]) ) {
  1595.     digit=src_text_buf[i++];
  1596.     if( ishex(digit) ){
  1597.       value = value*base + HEX(digit);
  1598.       src_text_buf[j++] = digit;
  1599.     }
  1600.     else {            /* Anything else should be space */
  1601.       if( isspace(digit) ) {
  1602.     src_text_buf[j++] = ' ';
  1603.     while( isspace(src_text_buf[i]) ) {
  1604.       ++i;
  1605.     }
  1606.       }
  1607.       else {
  1608.     syntax_error(token->line_num,token->col_num,
  1609.              "badly formed typeless constant");
  1610.       }
  1611.     }
  1612.   }
  1613.  
  1614.   while(i < src_text_len)
  1615.     src_text_buf[j++] = src_text_buf[i++]; /* Copy the rest over */
  1616.  
  1617.   src_text_len = j;
  1618.  
  1619. #ifdef OLD_GET_BINARY_CONST
  1620.         /* Old arg: */
  1621.      char *s;            /* string of digits, or NULL */
  1622.  
  1623.     /* Two forms: X'nnnn' and 'nnnn'X. For the first, string has not
  1624.        been scanned yet, and s is null.  For second, s=digit string. */
  1625.   if(s == NULL) {
  1626.     if(src_text_len < MAX_SRC_TEXT)
  1627.       src_text_buf[src_text_len++] = curr_char;
  1628.     bi_advance();        /* gobble the leading quote */
  1629.  
  1630.     while(ishex(curr_char)){
  1631.       value = value*base + HEX(curr_char);
  1632.  
  1633.       if(src_text_len < MAX_SRC_TEXT)
  1634.     src_text_buf[src_text_len++] = curr_char;
  1635.       if(next_char == ' ' || next_char == '\t')
  1636.     if(src_text_len < MAX_SRC_TEXT)
  1637.       src_text_buf[src_text_len++] = ' ';
  1638.  
  1639.       bi_advance();
  1640.     }
  1641.     if(curr_char != '\'') {
  1642.       syntax_error(line_num,col_num, "Closing quote missing");
  1643.     }
  1644.     else {
  1645.       advance();        /* gobble the trailing quote */
  1646.     }
  1647.     if(src_text_len < MAX_SRC_TEXT)
  1648.       src_text_buf[src_text_len++] = '\''; /* put the quote there */
  1649.   }
  1650.   else {            /* Use the given string */
  1651.     if(src_text_len < MAX_SRC_TEXT)
  1652.       src_text_buf[src_text_len++] = '\''; /* put the leading quote */
  1653.     while(*s != '\0') {
  1654.       if(!isspace(*s)) {    /* skip blanks */
  1655.     value = value*base + HEX(*s);
  1656.  
  1657.     if(src_text_len < MAX_SRC_TEXT)
  1658.       src_text_buf[src_text_len++] = *s;
  1659.     s++;
  1660.       }
  1661.       else {
  1662.     if(src_text_len < MAX_SRC_TEXT)
  1663.       src_text_buf[src_text_len++] = ' ';
  1664.     do{ s++; } while(*s != '\0' && isspace(*s));
  1665.       }
  1666.     }
  1667.     if(src_text_len < MAX_SRC_TEXT)
  1668.       src_text_buf[src_text_len++] = '\''; /* put the trailing quote */
  1669.   }
  1670. #endif/*OLD_GET_BINARY_CONST*/
  1671.  
  1672.   token->class = tok_integer_const;
  1673.   token->value.integer = value;
  1674.   token->src_text = new_src_text(src_text_buf,src_text_len);
  1675.  
  1676.   if(f77_standard || !allow_typeless_constants) {
  1677.     nonstandard(token->line_num,token->col_num);
  1678.   }
  1679.  
  1680. #ifdef DEBUG_FORLEX
  1681. if(debug_lexer)
  1682. (void)fprintf(list_fd,"\nInteger const:\t\t%d (from %s)",
  1683.           token->value.integer,
  1684.           token->src_text);
  1685. #endif
  1686.  
  1687. }/*get_binary_const*/
  1688.  
  1689. #endif/*ALLOW_TYPELESS_CONSTANTS*/
  1690.  
  1691.  
  1692. PRIVATE void
  1693. get_punctuation(token)
  1694.     Token *token;
  1695. {
  1696.     src_text_buf[src_text_len++] = curr_char;
  1697.     initial_flag = FALSE;
  1698.     closeup();
  1699.     if(curr_char == '*' && next_char == '*') {
  1700.         token->class = tok_power;
  1701.         advance();
  1702.         src_text_buf[src_text_len++] = curr_char;
  1703.     }
  1704.     else if(curr_char == '/' && next_char == '/' ) {
  1705.         token->class = tok_concat;
  1706.         advance();
  1707.         src_text_buf[src_text_len++] = curr_char;
  1708.     }
  1709.         /* paren can be the start of complex constant if everything
  1710.            is just right. Maybe more tests needed here. */
  1711.     else if(complex_const_allowed && curr_char == '(' &&
  1712.          (  (prev_token_class<256 && ispunct(prev_token_class))
  1713.           || prev_token_class == tok_relop
  1714.           || prev_token_class == tok_power )
  1715.          && looking_at_cplx()) {
  1716.         get_complex_const(token);
  1717.         return;
  1718.     }
  1719.     else
  1720.         token->class = curr_char;
  1721.     token->src_text = new_src_text(src_text_buf,src_text_len);
  1722.     advance();
  1723.  
  1724. #ifdef DEBUG_FORLEX
  1725. if(debug_lexer) {
  1726.     if(token->class == EOS)
  1727.         (void)fprintf(list_fd,"\n\t\t\tEOS");
  1728.     else
  1729.         (void)fprintf(list_fd,"\nPunctuation:\t\t%s",token->src_text);
  1730.  }
  1731. #endif
  1732. } /* get_punctuation */
  1733.  
  1734.  
  1735. PRIVATE void
  1736. get_simple_punctuation(token)
  1737.     Token *token;
  1738. {
  1739.         /* Like get_punctuation but lacks special cases.  Just
  1740.            gets the punctuation character. Text is already in
  1741.            src_text_buf. */
  1742.  
  1743.     token->class = curr_char;
  1744.     token->src_text = new_src_text(src_text_buf,src_text_len);
  1745.     advance();
  1746. #ifdef DEBUG_FORLEX
  1747. if(debug_lexer) {
  1748.     if(token->class == EOS)
  1749.         (void)fprintf(list_fd,"\n\t\t\tEOS");
  1750.     else
  1751.         (void)fprintf(list_fd,"\nPunctuation:\t\t%s",token->src_text);
  1752. }
  1753. #endif
  1754. } /* get_simple_punctuation */
  1755.  
  1756.  
  1757. PRIVATE void
  1758. get_string(token)       /* Gets string of form 'aaaa' */
  1759.     Token *token;
  1760. {
  1761.     int len,last_col_num;
  1762.     int has_backslash = FALSE; /* for portability check */
  1763.  
  1764.     quote_char = curr_char; /* remember the delimiter */
  1765.     initial_flag = FALSE;
  1766.     inside_string = TRUE;
  1767.     last_col_num=col_num;
  1768.     src_text_buf[src_text_len++] = curr_char; /* store leading quote */
  1769.     advance();      /* Gobble leading quote */
  1770.     len = 0;
  1771.     for(;;) {
  1772.         while(curr_char == EOL) {
  1773.             /* Treat short line as if extended with blanks */
  1774.             int col;
  1775.             for(col=last_col_num; col<max_stmt_col; col++) {
  1776.  
  1777.               if(src_text_len < MAX_SRC_TEXT)
  1778.             src_text_buf[src_text_len++] = ' ';
  1779.  
  1780.               ++len;
  1781.             }
  1782.           last_col_num=col_num;
  1783.           advance();
  1784.         }
  1785.         if(curr_char == EOS || curr_char == EOF) {
  1786.             yyerror("Closing quote missing from string");
  1787.             break;
  1788.         }
  1789.         if(curr_char == quote_char) {
  1790.             inside_string = FALSE;/* assume so for now */
  1791.  
  1792. /* If LEX_RAWSTRINGS defined, stores doubled quotes and final quote.
  1793.    Otherwise initial quote is stored and doubled quotes are reduced to one. */
  1794. #ifdef LEX_RAWSTRINGS
  1795.                 /* Store the quote */
  1796.             if(src_text_len < MAX_SRC_TEXT)
  1797.               src_text_buf[src_text_len++] = curr_char;
  1798. #endif
  1799.  
  1800.                     /* Handle possible continuation */
  1801.             if(next_char == EOL && col_num == max_stmt_col)
  1802.               advance();
  1803.  
  1804.             last_col_num=col_num;
  1805.             advance();
  1806.  
  1807.             if(curr_char == quote_char){/* '' becomes ' in string */
  1808.                 inside_string = TRUE; /* not a closing quote */
  1809.  
  1810.                 if(src_text_len < MAX_SRC_TEXT)
  1811.                   src_text_buf[src_text_len++] = curr_char;
  1812.  
  1813.                 ++len;
  1814.                 last_col_num=col_num;
  1815.                 advance();
  1816.             }
  1817.             else {
  1818.                 break;  /* It was a closing quote after all */
  1819.             }
  1820.         }
  1821.         else {        /* ordinary character within quotes */
  1822.             int value=curr_char;
  1823.  
  1824.             if(curr_char == '\\') {
  1825.               if(!has_backslash) {/* only warn once per string */
  1826.                 if(port_check)
  1827.                   nonportable(line_num,col_num,
  1828.                "backslash treated incompatibly by some compilers");
  1829.               }
  1830.               has_backslash = TRUE;
  1831.  
  1832. #ifdef ALLOW_UNIX_BACKSLASH    /* This has problems: undigesting
  1833.                    a string gets complicated. */
  1834.               if(unix_backslash) {
  1835.                 if(f77_standard || !allow_unix_backslash) {
  1836.                   nonstandard(line_num,col_num);
  1837.                   msg_tail(": backslash escape sequence");
  1838.                 }
  1839. #ifdef LEX_RAWSTRINGS
  1840.                 /* Store the backslash */
  1841.                 if(src_text_len < MAX_SRC_TEXT)
  1842.                   src_text_buf[src_text_len++] = curr_char;
  1843. #endif
  1844.                 inside_string = FALSE;/* so inline_comment works */
  1845.                 advance(); /* gobble the backslash */
  1846.                 inside_string = TRUE;
  1847. #ifdef LEX_RAWSTRINGS
  1848.                 value = curr_char;
  1849. #else /* !LEX_RAWSTRINGS*/
  1850.                 if(isadigit(curr_char)) { /* \octal digits */
  1851.                   value = BCD(curr_char);
  1852.                   while(isadigit(next_char)) {
  1853.                 advance();
  1854.                 value = value*8 + BCD(curr_char);
  1855.                   }
  1856.                 }
  1857.                 else if(curr_char == 'x') {
  1858.                   advance(); /* gobble the 'x' */
  1859.                   value = HEX(curr_char);
  1860.                   while(ishex(next_char)) {
  1861.                 advance();
  1862.                 value = value*16 + HEX(curr_char);
  1863.                   }
  1864.                 }/* end if octal or hex */
  1865.                 else switch(curr_char) {
  1866. #if __STDC__ + 0
  1867.                   case 'a': value = '\a'; break; /* alarm */
  1868. #else
  1869.                   case 'a': value = '\007'; break; /* alarm */
  1870. #endif
  1871.                   case 'b': value = '\b'; break; /* backspace */
  1872.                   case 'f': value = '\f'; break; /* formfeed */
  1873.                   case 'n': value = '\n'; break; /* newline */
  1874.                   case 'r': value = '\r'; break; /* carr return */
  1875.                   case 't': value = '\t'; break; /* h tab */
  1876.                   case 'v': value = '\v'; break; /* v tab */
  1877.                   case EOS: value = '\n'; break; /* a no-no */
  1878.                 /* All others: \c --> c */
  1879.                   default:  value = curr_char; break;
  1880.                 }
  1881. #endif /* !LEX_RAWSTRINGS*/
  1882.               }/* end if unix_backslash */
  1883. #endif /*ALLOW_UNIX_BACKSLASH*/
  1884.  
  1885.             }/* end if curr_char == backslash */
  1886.  
  1887.             if(src_text_len < MAX_SRC_TEXT)
  1888.               src_text_buf[src_text_len++] = value;
  1889.  
  1890.             ++len;
  1891.             last_col_num=col_num;
  1892.             advance();
  1893.         }
  1894.     }
  1895.  
  1896. #ifdef ALLOW_TYPELESS_CONSTANTS
  1897.                 /* Watch for const like 'nnn'X */
  1898.     if(!inside_format) {
  1899.       while(iswhitespace(curr_char))
  1900.         advance();
  1901.       if(isaletter(curr_char)) {
  1902.         int c=makeupper(curr_char);
  1903. #ifndef LEX_RAWSTRINGS
  1904.         if(src_text_len < MAX_SRC_TEXT)
  1905.           src_text_buf[src_text_len++] = quote_char;
  1906. #endif
  1907.         if(src_text_len < MAX_SRC_TEXT)
  1908.           src_text_buf[src_text_len++] = c;
  1909.         advance();        /* Gobble the base character */
  1910.         get_binary_const(token,c);
  1911.         return;
  1912.       }
  1913.     }
  1914. #endif /*ALLOW_TYPELESS_CONSTANTS*/
  1915.  
  1916.     if(len == 0) {
  1917.         warning(line_num,col_num,
  1918.             "Zero-length string not allowed\n");
  1919.         len = 1;
  1920.     }
  1921.  
  1922.     if(quote_char != '\'') { /* Warn if quote is used instead of apost */
  1923.       if(f77_standard || !allow_quotemarks) {
  1924.         nonstandard(token->line_num,token->col_num);
  1925.         msg_tail(": character string should be delimited by apostrophes");
  1926.       }
  1927.     }
  1928.  
  1929.     inside_string = FALSE;
  1930.  
  1931.     token->class = tok_string;
  1932.     token->size = len;
  1933.     token->src_text = new_src_text(src_text_buf,src_text_len);
  1934. #ifdef LEX_RAWSTRINGS
  1935.     token->value.string = token->src_text; /* Includes the initial quote */
  1936. #else
  1937.     token->value.string = token->src_text+1; /* Skips the initial quote */
  1938. #endif
  1939.                 /* Under -port warn if char size > 255 */
  1940.     if(port_check) {
  1941.       if(len > 255)
  1942.         nonportable(line_num,col_num,
  1943.             "character constant length exceeds 255");
  1944.     }
  1945.  
  1946. #ifdef DEBUG_FORLEX
  1947.     if(debug_lexer
  1948.        && src_text_buf[0] == quote_char) { /* skip if doing X'nnnn' */
  1949.         (void)fprintf(list_fd,"\nString:\t\t\t%s",token->value.string);
  1950.         (void)fprintf(list_fd,"\n\t\t(from\t%s)",token->src_text);
  1951.     }
  1952. #endif
  1953.  
  1954. } /* get_string */
  1955.  
  1956.  
  1957. /* End of Forlex module */
  1958.  
  1959. /*
  1960. II. Advance
  1961. */
  1962.  
  1963. /* advance.c:
  1964.  
  1965.     Low-level input routines for Fortran program checker.
  1966.  
  1967.     Shared functions defined:
  1968.         init_scan()    Initializes an input stream.
  1969.         finish_scan()    Finishes processing an input stream.
  1970.         advance()    Reads next char, removing comments and
  1971.                 handling continuation lines.
  1972.             looking_at_x Handles lookahead up to end of line:
  1973.         looking_at_cplx() Identifies complex constant.
  1974.         looking_at_keywd() Identifies assgnmt stmts vs keywords.
  1975.         looking_at_relop() Distinguishes .EQ. from .Eexp .
  1976.         flush_line_out(n) Prints lines up to line n if not already
  1977.                 printed, so error messages come out looking OK.
  1978. */
  1979.  
  1980.  
  1981.     /* Define tab stops: nxttab[col_num] is column of next tab stop */
  1982.  
  1983. #define do8(X) X,X,X,X,X,X,X,X
  1984. PRIVATE int nxttab[]={ 0, do8(9), do8(17), do8(25), do8(33),
  1985.         do8(41), do8(49), do8(57), do8(65), do8(73), do8(81)};
  1986.  
  1987. PRIVATE int
  1988.     next_index,        /* Index in line of next_char */
  1989.     prev_comment_line,    /* True if previous line was comment */
  1990.     curr_comment_line,    /* True if current line is comment */
  1991.     noncomment_line_count,    /* Number of noncomment lines read so far */
  1992.     line_is_printed,    /* True if line has been flushed (printed) */
  1993.     prev_line_is_printed,    /* True if line has been flushed (printed) */
  1994.     sticky_EOF;        /* Signal to delay EOF a bit for sake
  1995.                    of error messages in include files. */
  1996. PRIVATE unsigned
  1997.     prev_line_num;        /* line number of previous input line */
  1998.  
  1999. unsigned prev_stmt_line_num;    /* line number of previous noncomment */
  2000.  
  2001.  
  2002. PRIVATE char
  2003.     lineA[MAXLINE+1],lineB[MAXLINE+1],  /* Buffers holding input lines */
  2004.     *prev_line,*line;            /* Pointers to input buffers */
  2005.  
  2006. PRIVATE char
  2007.     *getstrn();
  2008.  
  2009. #ifdef ALLOW_UNIX_CPP
  2010. PRIVATE int
  2011.     take_cpp_line();    /* Reads #line directives and ignores others */
  2012. #endif
  2013.  
  2014.     /* Lookahead routines that scan the input
  2015.        line for various things.  The is_whatever routines take a
  2016.        string as argument and return TRUE if it satisfies the
  2017.        criterion. The skip_whatever routines take an index and
  2018.        string as argument and return the index of the next
  2019.        nonspace character in the string after the expected thing,
  2020.        which must be there in a syntactically correct program.
  2021.        The given index points at the character after a known
  2022.        lead-in (except for see_a_number, which can be given the
  2023.        index of 1st char of number).  The see_whatever routines
  2024.        are similar but return -1 if the expected thing is not
  2025.        seen, which it need not be. */
  2026.  
  2027. PRIVATE int
  2028.     is_comment(), is_continuation(), is_overlength();
  2029.  
  2030. PRIVATE int
  2031.     see_a_number(), see_dowhile(), see_expression(), see_keyword();
  2032.  
  2033. PRIVATE int
  2034.     skip_balanced_parens(), skip_idletters(), skip_quoted_string(),
  2035.     skip_hollerith();
  2036.  
  2037. #ifdef ALLOW_INCLUDE
  2038. /* Definition of structure for saving the input stream parameters while
  2039.    processing an include file.
  2040. */
  2041.  
  2042. typedef struct {
  2043.   FILE     *input_fd;
  2044.   char       *fname;
  2045.   char     line[MAXLINE];  /* MAXLINE is defined in ftnchek.h */
  2046.   int      curr_char;
  2047.   int      next_char;
  2048.   int       next_index;
  2049.   int       col_num;
  2050.   int       next_col_num;
  2051.   int       line_is_printed;
  2052.   int       do_list;
  2053.   unsigned line_num;
  2054.   unsigned next_line_num;
  2055. } IncludeFileStack;
  2056.  
  2057. PRIVATE IncludeFileStack include_stack[MAX_INCLUDE_DEPTH];
  2058. PRIVATE FILE* find_include(), *fopen_with_path();
  2059.  
  2060. #endif /*ALLOW_INCLUDE*/
  2061.  
  2062. PRIVATE void
  2063.     init_stream();
  2064. PRIVATE int
  2065.     push_include_file(),pop_include_file();
  2066.  
  2067.  
  2068. #ifdef ALLOW_INCLUDE        /* defns of include-file handlers */
  2069.  
  2070. PRIVATE int
  2071. push_include_file(fname,fd,include_line_num)
  2072.     char *fname;
  2073.     FILE *fd;
  2074.     unsigned include_line_num;
  2075. {
  2076.      if (incdepth == MAX_INCLUDE_DEPTH) {
  2077.        oops_message(OOPS_NONFATAL,line_num,NO_COL_NUM,
  2078.             "include files nested too deep");
  2079.        return FALSE;
  2080.      }
  2081.  
  2082. #ifdef DEBUG_INCLUDE
  2083. if(debug_include){
  2084. (void)fprintf(list_fd,"\npush_include_file: curr_char=%c (%d)",curr_char,curr_char);
  2085. }
  2086. #endif
  2087.  
  2088.      if(incdepth == 0) /* Save line num of outermost include */
  2089.        top_file_line_num = include_line_num;
  2090.  
  2091.      include_stack[incdepth].input_fd = input_fd;
  2092.      input_fd = fd;
  2093.  
  2094.      include_stack[incdepth].fname = current_filename;
  2095.      current_filename = fname;
  2096.  
  2097.      (void)strcpy(include_stack[incdepth].line,line);
  2098.      include_stack[incdepth].curr_char = curr_char;
  2099.      include_stack[incdepth].next_char = next_char;
  2100.      include_stack[incdepth].next_index = next_index;
  2101.      include_stack[incdepth].col_num = col_num;
  2102.      include_stack[incdepth].next_col_num = next_col_num;
  2103.      include_stack[incdepth].line_is_printed = line_is_printed;
  2104.      include_stack[incdepth].line_num = line_num;
  2105.      include_stack[incdepth].next_line_num = next_line_num;
  2106.      include_stack[incdepth].do_list = do_list;
  2107.  
  2108.      incdepth++;
  2109.  
  2110.      init_stream();
  2111.  
  2112.      return TRUE;
  2113. }
  2114.  
  2115. PRIVATE int
  2116. pop_include_file()
  2117. {
  2118. #ifdef DEBUG_INCLUDE
  2119. if(debug_include){
  2120. (void)fprintf(list_fd,"\npop_include_file: line %u = %s depth %d",line_num,line,
  2121. incdepth);
  2122. }
  2123. #endif
  2124.  
  2125.      if (incdepth == 0) {    /* Stack empty: no include file to pop. */
  2126.        return FALSE;
  2127.      }
  2128.      incdepth--;
  2129.  
  2130.  
  2131.      if(do_list) {
  2132.        (void)flush_line_out(next_line_num);
  2133.        (void)fprintf(list_fd,"\nResuming file %s:",
  2134.            include_stack[incdepth].fname);
  2135.      }
  2136.  
  2137.      (void)fclose(input_fd);
  2138.      input_fd = include_stack[incdepth].input_fd;
  2139.  
  2140.      current_filename = include_stack[incdepth].fname;
  2141.  
  2142.      (void)strcpy(line,include_stack[incdepth].line);
  2143.      curr_char = include_stack[incdepth].curr_char;
  2144.      next_char = include_stack[incdepth].next_char;
  2145.      next_index = include_stack[incdepth].next_index;
  2146.      col_num = include_stack[incdepth].col_num;
  2147.      next_col_num = include_stack[incdepth].next_col_num;
  2148.      line_is_printed = include_stack[incdepth].line_is_printed;
  2149.      line_num = include_stack[incdepth].line_num;
  2150.      next_line_num = include_stack[incdepth].next_line_num;
  2151.      do_list = include_stack[incdepth].do_list;
  2152.  
  2153.      curr_comment_line = FALSE;
  2154.      prev_line_is_printed = TRUE;
  2155.      initial_flag = TRUE;
  2156.      sticky_EOF = TRUE;
  2157.  
  2158.      return TRUE;
  2159. }
  2160.  
  2161.  
  2162. void
  2163. open_include_file(fname,include_line_num)
  2164.      char *fname;
  2165.      unsigned include_line_num;
  2166. {
  2167.   FILE *fd;
  2168. #ifdef VMS_INCLUDE
  2169.   int list_option=FALSE;    /* /[NO]LIST qualifier: default=NOLIST */
  2170. #endif /*VMS_INCLUDE*/
  2171.  
  2172. #ifdef VMS_INCLUDE /* for VMS: default extension is .for */
  2173.   if(has_extension(fname,"/nolist")) {
  2174.     list_option = FALSE;
  2175.     fname[strlen(fname)-strlen("/nolist")] = '\0'; /* trim off qualifier */
  2176.   }
  2177.   else if(has_extension(fname,"/list")) {
  2178.     list_option = TRUE;
  2179.     fname[strlen(fname)-strlen("/list")] = '\0'; /* trim off qualifier */
  2180.   }
  2181.   fname = add_ext(fname, DEF_SRC_EXTENSION);
  2182. #endif
  2183.  
  2184.         /* Need to put the name in permanent space */
  2185.   fname = new_global_string(fname);
  2186.  
  2187.   if ((fd = find_include(&fname,"r")) == NULL) {
  2188.     (void)fprintf(stderr,"\nerror opening include file %s\n",fname);
  2189.     return;
  2190.   }
  2191.  
  2192.             /* Print the INCLUDE line if do_list */
  2193.   if(do_list)
  2194.     (void)flush_line_out(prev_line_num);
  2195.  
  2196.             /* Report inclusion of file */
  2197.   if(verbose || do_list)
  2198.     (void)fprintf(list_fd,"\nIncluding file %s:",fname);
  2199.  
  2200.         /* Save the current input stream and then open
  2201.            the include file as input stream. */
  2202.   if( push_include_file(fname,fd,include_line_num) ) {
  2203. #ifdef VMS_INCLUDE
  2204.     /* put /[NO]LIST option into effect */
  2205.       if(do_list != list_option)
  2206.     (void)fprintf(list_fd," (listing %s)", list_option? "on":"off");
  2207.       do_list = list_option;
  2208. #endif /*VMS_INCLUDE*/
  2209.   }
  2210.   else
  2211.     (void)fclose(fd);
  2212. }
  2213.  
  2214. PRIVATE FILE*
  2215. find_include(fname,mode)    /* looks for file locally or in include dir */
  2216.      char **fname,        /* If found, fname is returned with full path*/
  2217.      *mode;
  2218. {
  2219.   FILE *fp;
  2220.   char *env_include_var;
  2221.   IncludePathNode *p;
  2222.  
  2223.             /* Look first for bare filename */
  2224.   if( (fp=fopen(*fname,mode)) != NULL)
  2225.     return fp;
  2226.  
  2227.               /* If not found, look in directories given
  2228.              by include_path_list from -include options */
  2229.  
  2230.   for(p=include_path_list; p!=NULL; p=p->link) {
  2231.     if( (fp=fopen_with_path(p->include_path,fname,mode)) != (FILE *)NULL)
  2232.       return fp;
  2233.   }
  2234.  
  2235.               /* If not found, look in directory given by
  2236.              env variable ENV_INCLUDE_VAR (e.g. set by
  2237.              % setenv INCLUDE ~/myinclude ) */
  2238.  
  2239.   if( (env_include_var=getenv(ENV_INCLUDE_VAR)) != NULL) {
  2240.     if( (fp=fopen_with_path(env_include_var,fname,mode)) != (FILE *)NULL)
  2241.       return fp;
  2242.   }
  2243.  
  2244.             /* Still not found: look in systemwide
  2245.                default directory */
  2246.  
  2247. #ifdef DEFAULT_INCLUDE_DIR
  2248.   if( (fp=fopen_with_path(DEFAULT_INCLUDE_DIR,fname,mode)) != NULL)
  2249.     return fp;
  2250. #endif/* DEFAULT_INCLUDE_DIR */
  2251.  
  2252.                 /* Not found anywhere: fail */
  2253.   return (FILE *)NULL;
  2254. }/*find_include*/
  2255.  
  2256.         /* Routine to open file with name given by include_path
  2257.            followed by fname.  If successful, fname is replaced
  2258.            by pointer to full name.  */
  2259. PRIVATE FILE *
  2260. fopen_with_path(include_path,fname,mode)
  2261.      char *include_path, **fname, *mode;
  2262. {
  2263.     FILE *fp;
  2264.     char tmpname[256];        /* holds name with path prepended */
  2265.  
  2266.     (void)strcpy(tmpname,include_path);
  2267.                 /* Add "/" or "\" if not provided */
  2268. #ifdef UNIX
  2269.     if(tmpname[strlen(tmpname)-1] != '/')
  2270.       (void)strcat(tmpname,"/");
  2271. #endif
  2272. #ifdef MSDOS
  2273.     if(tmpname[strlen(tmpname)-1] != '\\')
  2274.       (void)strcat(tmpname,"\\");
  2275. #endif
  2276.     (void)strcat(tmpname,*fname);
  2277.  
  2278.     if( (fp=fopen(tmpname,mode)) != (FILE *)NULL) {
  2279.             /* Found: save new name in permanent space */
  2280.     *fname = new_global_string(tmpname);
  2281.     }
  2282.  
  2283.     return fp;
  2284. }/*fopen_with_path*/
  2285.  
  2286. #else /* no ALLOW_INCLUDE */
  2287.                 /* disabled forms of include handlers */
  2288. PRIVATE int
  2289. push_include_file(fname,fd,include_line_num)
  2290.     char *fname;
  2291.     FILE *fd;
  2292.     unsigned include_line_num;
  2293. {return FALSE;}
  2294.  
  2295. PRIVATE int
  2296. pop_include_file()
  2297. {return FALSE;}
  2298.  
  2299. void
  2300. open_include_file(fname)
  2301.      char *fname;
  2302. {}
  2303.  
  2304. #endif /*ALLOW_INCLUDE*/
  2305.  
  2306. PRIVATE int line_is_overlength, prev_line_overlength;
  2307.  
  2308. void
  2309. init_scan()            /* Starts reading a file */
  2310. {
  2311.     tab_count = 0;
  2312.     incdepth = 0;
  2313.     top_file_line_num = 1;
  2314.  
  2315.     line = lineA;        /* Start out reading into buffer A */
  2316.     prev_line = lineB;
  2317.  
  2318.     init_stream();
  2319. }
  2320.  
  2321. PRIVATE void
  2322. init_stream()        /* Initializes a new input stream */
  2323. {
  2324.     curr_comment_line = FALSE;
  2325.     inside_string = FALSE;
  2326.     inside_hollerith = FALSE;
  2327.     line_is_printed = TRUE;
  2328.     prev_line_is_printed = TRUE;
  2329.     line_is_overlength = prev_line_overlength = FALSE;
  2330.     noncomment_line_count = 0;
  2331.  
  2332.     next_index = -1;    /* Startup as if just read a blank line */
  2333.     next_char = EOS;
  2334.     curr_char = EOS;
  2335.     next_col_num = 0;
  2336.     next_line_num = 0;
  2337.     prev_line_num = prev_stmt_line_num = 0;
  2338.     sticky_EOF = TRUE;
  2339.     contin_count = 0;
  2340.  
  2341.     line[0] = '\0';
  2342.     advance();        /* put 1st two chars in the pipeline */
  2343.     advance();
  2344.     advance();        /* gobble the artificial initial EOS */
  2345. }
  2346.  
  2347.  
  2348. void
  2349. finish_scan()
  2350. {
  2351.         /* clean up if no END statement at EOF */
  2352.     check_seq_header((Token *)NULL);
  2353.         /* print last line if not already done */
  2354.     if(do_list)
  2355.         (void)flush_line_out(line_num);
  2356. }
  2357.  
  2358. #ifdef INLINE_COMMENT_CHAR
  2359.     /* macro is used on next_char: must look at curr_char to avoid
  2360.        being fooled by '!' without messing up on 'xxx'! either.
  2361.        Also don't be fooled by '''!''' which is the string '!'
  2362.        Note that inside_string does not yet reflect curr_char.
  2363.        Test is that inside_string is true but about to become false,
  2364.        or false and not about to become true. Think about it. */
  2365.  
  2366. #define inline_comment(c) ( ((c)==INLINE_COMMENT_CHAR) &&\
  2367.     (inside_string? (curr_char == quote_char) : !isaquote(curr_char)) &&\
  2368.     (!inside_hollerith) )
  2369. #endif
  2370.  
  2371.     /* closeup: Advances input stream till next_char is nonspace.  Fudges
  2372.        things so that curr_char remains as it was. */
  2373. PRIVATE void
  2374. closeup()
  2375. {
  2376.   int
  2377.     save_curr_char = curr_char,
  2378.     save_prev_char = prev_char,
  2379.     save_line_num = line_num,
  2380.     save_col_num = col_num;
  2381.  
  2382.   while(iswhitespace(next_char))
  2383.     advance();
  2384.  
  2385.   curr_char = save_curr_char;
  2386.   prev_char = save_prev_char;
  2387.   line_num = save_line_num;
  2388.   col_num = save_col_num;
  2389. }
  2390.  
  2391.  
  2392. LEX_SHARED void
  2393. advance()
  2394. {
  2395. #ifdef EOLSKIP
  2396.     int eol_skip = FALSE;
  2397. #endif
  2398.     prev_char = curr_char;
  2399. #ifdef EOLSKIP
  2400.     do{
  2401. #endif
  2402.     if(next_char == EOF) {      /* Don't advance past EOF */
  2403.         if(curr_char == EOS || curr_char == EOF) {
  2404.  
  2405.              /* Pause to allow parse actions at end of stmt
  2406.                 to have correct file context before popping
  2407.                 the include file.  Effect is to send an extra
  2408.                 EOS to parser at end of file. */
  2409.           if(sticky_EOF) {
  2410.             sticky_EOF = FALSE;
  2411.             return;
  2412.           }
  2413.                 /* At EOF: close include file if any,
  2414.                    otherwise yield an EOF character. */
  2415.           if( ! pop_include_file() ) {
  2416.             curr_char = EOF;
  2417.           }
  2418.           return;
  2419.         }
  2420.         else {
  2421.           curr_char = EOS;
  2422.           return;
  2423.         }
  2424.     }
  2425.  
  2426.     if(curr_char == EOS)
  2427.         initial_flag = TRUE;
  2428.  
  2429. #ifdef EOLSKIP
  2430.     if(! eol_skip) {
  2431. #endif
  2432.         curr_char = next_char;      /* Step to next char of input */
  2433.         col_num = next_col_num;
  2434.         line_num = next_line_num;
  2435.         if(col_num > 72 && !iswhitespace(curr_char)) {
  2436.            line_is_overlength = TRUE;
  2437.          }
  2438. #ifdef EOLSKIP
  2439.     }
  2440. #endif
  2441.  
  2442.     if(next_char == '\t'){       /* Handle tabs in input */
  2443.  
  2444. #ifdef DEC_TABS    /* support for DEC tab formatting */
  2445.         if(dec_tabs && next_col_num < 7) {
  2446.           next_col_num = 7; /* initial DEC tab -> col 7  */
  2447.         }
  2448.         else
  2449. #endif
  2450.         {
  2451.           next_col_num = nxttab[next_col_num];
  2452.         }
  2453.  
  2454.         if( ! (inside_string || inside_hollerith) )
  2455.             tab_count++;    /*  for portability warning */
  2456.     }
  2457.     else {
  2458.         next_col_num++;
  2459.     }
  2460.  
  2461.     next_char = line[++next_index];
  2462.  
  2463.             /* If end of line is reached, input a new line.
  2464.              */
  2465.     while(next_col_num > max_stmt_col || next_char == '\0'
  2466. #ifdef INLINE_COMMENT_CHAR
  2467.     || inline_comment(next_char)
  2468. #endif
  2469.     ){
  2470.         do{
  2471.             if(do_list) /* print prev line if not printed yet */
  2472.               (void)flush_line_out(prev_line_num);
  2473.  
  2474.                 /* Warn if stmt field has been extended
  2475.                    and the extended part has been used. */
  2476.             if(!prev_comment_line) {
  2477.               if( (f77_standard && max_stmt_col>72)
  2478.                  && prev_line_overlength){
  2479.                   nonstandard(prev_line_num,(unsigned)73);
  2480.                   msg_tail(
  2481.                    ": significant characters past 72 columns");
  2482.               }
  2483.                 /* Otherwise warn if any chars past 72 cols */
  2484.               else if(pretty_flag
  2485.                  && is_overlength(prev_line,MAXLINE)) {
  2486.                    ugly_code(prev_line_num,(unsigned)73,
  2487.                   "characters past 72 columns");
  2488.               }
  2489.             }
  2490. #ifdef INLINE_COMMENT_CHAR
  2491.             if( f77_standard ) {
  2492.               if( !curr_comment_line && inline_comment(next_char)){
  2493.                   nonstandard(next_line_num,next_col_num);
  2494.                   msg_tail(": inline comment");
  2495.               }
  2496.             }
  2497. #endif
  2498.  
  2499.                 /* Swap input buffers to get ready for new line.
  2500.                    But throw away comment lines if do_list is
  2501.                    false, so error messages will work right.
  2502.                  */
  2503.             if(do_list || ! curr_comment_line) {
  2504.                 char *temp=line;
  2505.                 line = prev_line;
  2506.                 prev_line=temp;
  2507.                 if(! curr_comment_line)
  2508.                   prev_stmt_line_num = line_num;
  2509.                 prev_line_num = next_line_num;
  2510.                 prev_line_is_printed = line_is_printed;
  2511.                 prev_line_overlength = line_is_overlength;
  2512.                 line_is_overlength = FALSE;
  2513.             }
  2514.  
  2515.             ++next_line_num;
  2516.             line_is_printed = FALSE;
  2517.             if( getstrn(line,MAXLINE+1,input_fd) == NULL ) {
  2518.                 next_char = EOF;
  2519.                 line_is_printed = TRUE;
  2520.                 return;
  2521.             }
  2522. #ifdef ALLOW_UNIX_CPP
  2523.             else
  2524.               if(line[0] == '#')
  2525.                 cpp_handled = take_cpp_line(line);
  2526. #endif
  2527.             ++tot_line_count; /* count lines processed */
  2528.  
  2529.             /*  Keep track of prior-comment-line situation */
  2530.             prev_comment_line = curr_comment_line;
  2531.  
  2532.         } while( (curr_comment_line = is_comment(line)) != FALSE);
  2533.         ++tot_stmt_line_count;
  2534.         ++noncomment_line_count;
  2535.  
  2536.             /* Handle continuation lines */
  2537.         if( is_continuation(line,&next_index,&next_col_num) ) {
  2538.                 /* It is a continuation */
  2539. #ifdef EOLSKIP
  2540.             if(eol_is_space) {
  2541. #endif
  2542.             next_char = EOL;
  2543. #ifdef EOLSKIP
  2544.             }
  2545.             else {
  2546.             next_char = line[++next_index];
  2547.             next_col_num = 7;
  2548.             eol_skip = TRUE; /* skip continued leading space */
  2549.             }
  2550. #endif
  2551.                 /* Issue warnings if contin in funny places */
  2552.             if(noncomment_line_count == 1)
  2553.                 warning(next_line_num,(unsigned)6,
  2554.             "Continuation mark found in first statement of file");
  2555.             if( pretty_flag && prev_comment_line )
  2556.                 ugly_code(next_line_num,(unsigned)6,
  2557.             "Continuation follows comment or blank line");
  2558.             if(contin_count++ == 19)
  2559.               if(f77_standard) {
  2560.                 nonstandard(next_line_num,(unsigned)6);
  2561.                 msg_tail(": > 19 continuation lines");
  2562.               }
  2563.         }
  2564.         else {
  2565.                 /* It is not a continuation */
  2566.             next_char = EOS;
  2567.             next_col_num = 0;
  2568.             next_index = -1;
  2569.             contin_count = 0;
  2570.         }
  2571.     }/*end while( end of line reached )*/
  2572.  
  2573.         /* Avoid letting a '0' in column 6 become a token */
  2574.     if(next_col_num == 6 && next_char == '0')
  2575.         next_char = ' ';
  2576.  
  2577. #ifdef EOLSKIP
  2578.             /* elide EOL and following space of continued
  2579.                stmts if requested */
  2580.     eol_skip = (eol_skip && isspace(next_char));
  2581.    }while(eol_skip);/*end do*/
  2582. #endif
  2583.  
  2584. }/* end advance */
  2585.  
  2586.  
  2587.     /*  Function which returns 0 if line is not a comment, 1 if it is.
  2588.      *  Comment is ANSI standard: C or c or * in column 1, or blank line.
  2589.      */
  2590.  
  2591. PRIVATE int
  2592. is_comment(s)
  2593.     char s[];
  2594. {
  2595.     int i,c= makeupper(s[0]);
  2596.     unsigned col;
  2597.  
  2598.                 /* Handle standard comments here. */
  2599.     if( c == 'C' || c == '*' )
  2600.         return TRUE;
  2601.  
  2602.                 /* Tolerate D comment lines.  There is
  2603.                    no provision for optionally
  2604.                    treating them as source code lines.
  2605.                  */
  2606.     if( c == 'D' ) {
  2607.         if(f77_standard) {
  2608.           nonstandard(next_line_num,1);
  2609.           msg_tail(": D in column 1 (treated as comment)");
  2610.         }
  2611.         return TRUE;
  2612.     }
  2613.  
  2614.                 /* Now see if line is blank or only contains
  2615.                    an inline comment.
  2616.                  */
  2617.     for(i=0,col=1; s[i] != '\0'; i++)
  2618.         if( !isspace(s[i]))
  2619. #ifdef INLINE_COMMENT_CHAR
  2620.         /* Initial "!" starts a comment, except in col. 6 it
  2621.            must be taken as continuation mark */
  2622.              if(s[i]==INLINE_COMMENT_CHAR && col != 6) {
  2623.                  if(f77_standard) {
  2624.                  nonstandard(next_line_num,col);
  2625.                  msg_tail(": inline comment");
  2626.                  }
  2627.                  return TRUE;
  2628.               }
  2629.               else
  2630.                   return FALSE;
  2631.         else
  2632.               if(s[i] == '\t') col = nxttab[col];
  2633.               else           col++;
  2634. #else
  2635.             return FALSE;
  2636. #endif
  2637.     return TRUE;        /* blank line */
  2638. }
  2639.  
  2640.     /* Here we handle Unix preprocessor lines.  The only ones
  2641.        processed now are those that set the line number and filename.
  2642.          Form 1: # line 10 "filename"
  2643.          Form 2: # 10 "filename"
  2644.        We replace next_filename and next_line_num by the
  2645.        given values.
  2646.      */
  2647. #ifdef ALLOW_UNIX_CPP
  2648. PRIVATE int
  2649. take_cpp_line(s)
  2650.      char *s;
  2651. {
  2652.   int linenum, nchars, handled;
  2653.   char *filename;
  2654.  
  2655.   handled=FALSE;
  2656.  
  2657.   do { ++s; } while( isspace(*s) );    /* Skip space after the '#' */
  2658.  
  2659.   if(strncmp(s,"line",4) == 0) {    /* Look for the keyword "line" */
  2660.     s += 4;            /* Skip the word "line" */
  2661.     while( isspace(*s) ) ++s;    /* Skip space after the word "line" */
  2662.   }
  2663.  
  2664.   if( isdigit(*s) ) {        /* See that we are now looking at a number */
  2665.     handled = TRUE;
  2666.  
  2667.             /* Get the line number */
  2668.     linenum=0;
  2669.     while( isdigit(*s) )
  2670.       linenum = linenum*10 + BCD(*s++);
  2671.  
  2672.             /* Now find the filename */
  2673.  
  2674.     filename = (char *)NULL;
  2675.     while( isspace(*s) ) ++s;    /* Skip space after the line number */
  2676.  
  2677.     if( *s == '"') {        /* Filename must be preceded by " */
  2678.  
  2679.       ++s;            /* Skip the " */
  2680.  
  2681.       nchars = 0;        /* Count chars in the filename */
  2682.       while( s[nchars] != '"' && s[nchars] != '\0')
  2683.     ++nchars;
  2684.  
  2685.       if( s[nchars] == '"') {    /* Filename must be followed by " */
  2686.  
  2687.     s[nchars] = '\0';/* terminate it temporarily */
  2688.  
  2689.     filename = new_global_string(s); /* put it in permanent space */
  2690.  
  2691.     s[nchars] = '"'; /* restore line as it was */
  2692.  
  2693.       }
  2694.     }
  2695.   }/*end handling #line */
  2696.  
  2697.   if(handled) {
  2698.     next_line_num = linenum-1;
  2699.     next_filename = filename;
  2700.   }
  2701.  
  2702.   return handled;        /* Return TRUE if it was a #line category */
  2703.  
  2704. }/*take_cpp_line*/
  2705. #endif
  2706.  
  2707.     /* Function which returns FALSE if line is a not continuation
  2708.      *  line.  If line is a continuation, returns TRUE.  In either
  2709.      *  case, sets cont_index to index in line of the continuation
  2710.      *  mark and cont_col_num to corresponding column number.  If
  2711.      *  dec_tabs in effect, tab moves to column 7 and a nonzero
  2712.      *  digit there implies continuation.  */
  2713. PRIVATE int
  2714. is_continuation(s,cont_index,cont_col_num)
  2715.     char s[];
  2716.         int *cont_index,*cont_col_num;
  2717. {
  2718.     int col,i,c;
  2719.  
  2720.                 /* Handle DEC tabs: <tab><digit> is a
  2721.                    continuation card */
  2722. #ifdef DEC_TABS
  2723.     if( dec_tabs && s[0] == '\t' ) {
  2724.       if( isadigit((int)s[1]) && s[1] != '0' ) {
  2725.         if(f77_standard) {
  2726.           nonstandard(next_line_num,7);
  2727.           msg_tail(": continuation mark not in column 6");
  2728.         }
  2729.         (*cont_index) = 1;
  2730.         (*cont_col_num) = 7;
  2731.         return TRUE;
  2732.       }
  2733.       else {        /* Tab then non-digit: regular stmt */
  2734.         (*cont_index) = 0;
  2735.         (*cont_col_num) = 7; /* (not used) */
  2736.         return FALSE;
  2737.       }
  2738.     }
  2739. #endif
  2740.                 /* skip to col 6 */
  2741.     for(i=0,col=1; col < 6 && s[i] != '\0'; i++) {
  2742.         if(s[i] == '\t')
  2743.             col = nxttab[col];
  2744.         else
  2745.             col++;
  2746.     }
  2747.     c = s[i];
  2748.  
  2749.     if ( col == 6 && c != '\0' && !isspace(c) && c != '0'
  2750. #ifdef ALLOW_UNIX_CPP
  2751.                 /* Veto if it is a preprocessor line */
  2752.         && s[0] != '#'
  2753. #endif
  2754.         ) {
  2755.            (*cont_index) = i;
  2756.            (*cont_col_num) = 6;
  2757.            return TRUE;
  2758.     }
  2759.     else {
  2760.            (*cont_index) = 0;
  2761.            (*cont_col_num) = -1; /* (not used) */
  2762.            return FALSE;
  2763.     }
  2764. }
  2765.  
  2766. int
  2767. flush_line_out(n)    /* Prints lines up to line #n if not yet printed */
  2768.     unsigned n;        /* Returns TRUE if line was printed, else FALSE */
  2769. {
  2770.             /* Print previous line only if do_list TRUE */
  2771.     if( !prev_line_is_printed
  2772.      && ((n == prev_line_num) || (n > prev_line_num && do_list)) ) {
  2773.        print_a_line(list_fd,prev_line,prev_line_num);
  2774.        prev_line_is_printed = TRUE;
  2775.     }
  2776.     if(n >= next_line_num && !line_is_printed) {
  2777.        print_a_line(list_fd,line,next_line_num);
  2778.        line_is_printed = TRUE;
  2779.     }
  2780.     return ( do_list ||
  2781.          (prev_line_is_printed && n == prev_line_num) ||
  2782.          (line_is_printed && n == next_line_num) );
  2783. }
  2784.  
  2785.  
  2786.     /*  Function to read n-1 characters, or up to newline, whichever
  2787.      *  comes first.  Differs from fgets in that the newline is replaced
  2788.      *  by null, and characters up to newline (if any) past the n-1st
  2789.      *  are read and thrown away.
  2790.      *  Returns NULL when end-of-file or error is encountered.
  2791.      */
  2792. PRIVATE char *
  2793. getstrn(s,n,fd)
  2794.     char s[];
  2795.     int n;
  2796.     FILE *fd;
  2797. {
  2798.     int i=0,c;
  2799.  
  2800.     while( (c=getc(fd)) != '\n' ) {
  2801.         if(c == EOF)
  2802.             return NULL;
  2803.  
  2804.         if(i < n-1)
  2805.             s[i++] = c;
  2806.     }
  2807.     s[i] = '\0';
  2808.     return s;
  2809. }
  2810.  
  2811.  
  2812.     /* Functions which look ahead as far as end of line to see if input
  2813.        cursor is sitting at start of a token of the given class.  Used
  2814.        to resolve ambiguities that need more than one token of lookahead.
  2815.        */
  2816.  
  2817. LEX_SHARED int
  2818. looking_at_cplx()
  2819. {
  2820.     int indx;
  2821.  
  2822.     if( next_char != EOS )    /* Looking at next line already */
  2823.     {
  2824.     indx = next_index;
  2825.  
  2826.     if( (indx = see_a_number(indx,line,FALSE)) < 0 )
  2827.       return FALSE;
  2828.     while(line[indx] != '\0' && isspace(line[indx]))
  2829.       indx++;
  2830.  
  2831.     if( line[indx] != ',' )
  2832.       return FALSE;
  2833.     ++indx;
  2834.  
  2835.     if( (indx = see_a_number(indx,line,FALSE)) < 0 )
  2836.       return FALSE;
  2837.     while(line[indx] != '\0' && isspace(line[indx]))
  2838.       indx++;
  2839.  
  2840.     if(line[indx] != ')')
  2841.       return FALSE;
  2842.     }
  2843.  
  2844.     return TRUE;    /* passed all the tests */
  2845.  
  2846. }
  2847.  
  2848. LEX_SHARED int
  2849. looking_at_keywd(token_class)
  2850.     int token_class;    /* Keyword class to be checked out */
  2851. {
  2852.                 /* Distinguishing identifier from keyword.
  2853.                    If not sure, assumes true.   Ambiguity
  2854.                    must be resolved in current line. */
  2855.     int indx;
  2856.     int c;
  2857.  
  2858.     if( next_char != EOS )    /* Looking at next line already */
  2859.     {
  2860. #ifdef DEBUG_IS_KEYWORD
  2861. if(debug_lexer && getenv("VERBOSE"))
  2862. (void)fprintf(list_fd,"\nlooking_at: curr_char=%c then %c",
  2863. curr_char,line[next_index]);
  2864. #endif
  2865.                 /* Skip over leading
  2866.                    stuff that could be rest of identifier */
  2867.  
  2868.     if(isidletter(curr_char) || isdigit(curr_char) ||
  2869.        isspace(curr_char)){
  2870.       indx = skip_idletters(next_index,line);
  2871.       c = line[indx];    /* Store following character in c */
  2872.       ++indx;   /* Leave index pointing at char after c */
  2873.     }
  2874.     else {
  2875.       c = curr_char;    /* Otherwise next input char is c */
  2876.       indx = next_index;
  2877.     }
  2878.  
  2879. #ifdef DEBUG_IS_KEYWORD
  2880. if(debug_lexer && getenv("VERBOSE"))
  2881. (void)fprintf(list_fd," c=%c then %c",c,line[indx]);
  2882. #endif
  2883.  
  2884.     if(token_class == tok_DO) {
  2885.       int opt_comma = FALSE;
  2886.  
  2887.         /* DO: we must by now have skipped over optional label
  2888.           to optional comma or over optional label and
  2889.           variable name to = sign.  Look for expression and comma.
  2890.           DOWHILE will be found as single keyword, but we have
  2891.           to spot DO label WHILE(expr) here.  DO of END DO
  2892.           is not seen here. */
  2893.  
  2894.       WHILE_expected = FALSE; /* most cases do not use it */
  2895.  
  2896.       if(c == ',' && isdigit(curr_char)) {
  2897.                 /* Skip optional comma after label.
  2898.                    First, back up and check that we saw
  2899.                    only digits so far. Do it here since
  2900.                    this is rare and not worth cluttering
  2901.                    the foregoing code. */
  2902.         int i=next_index;
  2903.         while(isdigit(line[i]) || isspace(line[i]))
  2904.           ++i;
  2905.         if(line[i] != ',')
  2906.           return FALSE;
  2907.                 /* Checks out OK: */
  2908.         indx = skip_idletters(indx,line);    /* skip DO index or WHILE */
  2909.         c = line[indx];
  2910.         ++indx;
  2911.         opt_comma = TRUE;
  2912.       }
  2913.  
  2914.       if(c == '=') {    /* Traditional DO form */
  2915.         indx = see_expression(indx,line);
  2916.         return (indx != -1 && line[indx] == ',') || opt_comma;
  2917.       }
  2918.       else {        /* Nonstandard variants */
  2919.         if(c == '(') {
  2920.                 /* DO label WHILE (expr): rescan from the
  2921.                    word DO to see if it fits. */
  2922.           if( see_dowhile(next_index,line) != -1 )
  2923.         WHILE_expected = TRUE;
  2924.           return WHILE_expected || opt_comma;
  2925.         }
  2926.         else
  2927.           return opt_comma;    /* The comma is found only in DO forms */
  2928.       }
  2929.     }/* end of tok_DO forms */
  2930.  
  2931.         /* Otherwise, look for an assignment statement.  If there
  2932.            is no left paren, then must be an equals sign here
  2933.            if it is an assignment statement. */
  2934.     if(c != '(') {
  2935. #ifdef DEBUG_IS_KEYWORD
  2936. if(debug_lexer && getenv("VERBOSE"))
  2937. (void)fprintf(list_fd,"\n Conclude %s",
  2938.     (c != '=')? "keyword": "assignment stmt");
  2939. #endif
  2940.           return (c != '=');
  2941.     }
  2942.  
  2943.     else {            /* sitting at parenthesis */
  2944.  
  2945.         /* Skip to end of balancing parenthesis. Then if = sign, it
  2946.            must be an assignment statement.  If ( is found,
  2947.            presumably it is an array substring assignment. So skip
  2948.            once more to check for the = sign.) */
  2949.  
  2950.  
  2951.     indx = skip_balanced_parens(indx,line);
  2952.  
  2953. #ifdef DEBUG_IS_KEYWORD
  2954. if(debug_lexer && getenv("VERBOSE"))
  2955. (void)fprintf(list_fd," to %c",line[indx]);
  2956. #endif
  2957.  
  2958.     if(line[indx] == '(') {
  2959.       ++indx;        /* Move past the paren */
  2960.       indx = skip_balanced_parens(indx,line);
  2961.  
  2962. #ifdef DEBUG_IS_KEYWORD
  2963. if(debug_lexer && getenv("VERBOSE"))
  2964. (void)fprintf(list_fd," to %c",line[indx]);
  2965. #endif
  2966.  
  2967.     }
  2968.  
  2969. #ifdef DEBUG_IS_KEYWORD
  2970. if(debug_lexer && getenv("VERBOSE"))
  2971. (void)fprintf(list_fd," conclude %s",line[indx]!= '='?"keyword":"variable");
  2972. #endif
  2973.  
  2974.     return (line[indx] != '=');
  2975.       }
  2976.     }
  2977.                 /* End of line: must be a keyword */
  2978.     return TRUE;
  2979.  
  2980. }/*looking_at_keywd*/
  2981.  
  2982.         /* This guy is called when an integer is followed by '.'
  2983.            in cases where a real number or expression is allowed.
  2984.            When an integer is followed by .E, it can either be a real
  2985.            like 1.E10, or a comparison like (1.EQ.I).  This requires
  2986.            looking for the 'Q' after the 'E'.  The other cases,
  2987.            like ... 1.AND. ... are resolved by looking at next_char
  2988.            to see if it is the 'D' of a d.p. constant or not.
  2989.           */
  2990. LEX_SHARED int
  2991. looking_at_relop()
  2992. {
  2993.     int indx;
  2994.     int c;
  2995.  
  2996.  
  2997.     if( next_char != EOS )    /* Looking at next line already */
  2998.     {
  2999.  
  3000. #if 0                /* With closeup() this is no longer valid */
  3001.     if( eol_is_space && line_num != next_line_num )
  3002.     return FALSE;    /* Looking at next line already */
  3003. #endif
  3004.     indx = next_index;/* Start at next_char */
  3005.  
  3006.     while( (c=line[indx]) != '\0' && isspace(c))
  3007.       ++indx;
  3008.  
  3009.     if( !isaletter( c ) )    /* next char must be letter */
  3010.         return FALSE;
  3011.     c = makeupper(c);
  3012.     if( c == 'D' )    /* D.P. exponent */
  3013.       return FALSE;    /* No dotted keywords start with D */
  3014.  
  3015.             /* If next char is any other letter but 'E', cannot be
  3016.                 exponent.  If it is 'E', must be EQ or EQV to
  3017.                 be a relop.  So look ahead for the 'Q'. */
  3018.     else if( c == 'E' ) {
  3019.       do {
  3020.         ++indx;
  3021.       } while( (c=line[indx]) != '\0' && isspace(c));
  3022.  
  3023.       c = makeupper(c);
  3024.       return (c == 'Q');
  3025.     }
  3026.     else        /* Next char not D or E: must be a dotted keyword */
  3027.       return TRUE;
  3028.     }
  3029.                 /* If EOS, then it is stmt like x=1. */
  3030.     return FALSE;
  3031.  
  3032. }
  3033.  
  3034.     /* see_a_number returns -1 if there is no valid numeric constant
  3035.        in string s starting at index i.  If valid number found, it
  3036.        returns the index of the next character after the constant.
  3037.        Leading whitespace in s is skipped.*/
  3038.  
  3039.  
  3040. #define SKIP_SPACE    while(s[i] != '\0' && isspace(s[i])) i++
  3041.  
  3042. PRIVATE int
  3043. see_a_number(i,s,can_be_holl)
  3044.    int i;
  3045.    char s[];
  3046.    int can_be_holl;/* context indication */
  3047. {
  3048.    int digit_seen = FALSE;
  3049.    int isave;
  3050.    while(s[i] != '\0' && isspace(s[i]))
  3051.      i++;
  3052.  
  3053.             /* move past optional preceding sign */
  3054.    if(s[i] == '-' || s[i] == '+' ) {
  3055.      i++;
  3056.      SKIP_SPACE;
  3057.      can_be_holl = FALSE;
  3058.    }
  3059.    isave=i;
  3060.  
  3061.         /* move past ddd or ddd. or .ddd or ddd.ddd */
  3062.    if(isdigit(s[i]))
  3063.      digit_seen = TRUE;
  3064.    while(isdigit(s[i])) {
  3065.      i++;
  3066.      SKIP_SPACE;
  3067.    }
  3068.    if(s[i] == 'H' && can_be_holl) {
  3069.      return skip_hollerith(isave,s);
  3070.    }
  3071.    if(s[i] == '.') {
  3072.      i++;
  3073.      SKIP_SPACE;
  3074.      if(isdigit(s[i]))
  3075.        digit_seen = TRUE;
  3076.      while(isdigit(s[i])) {
  3077.        i++;
  3078.        SKIP_SPACE;
  3079.      }
  3080.    }
  3081.  
  3082.         /* no digits seen: bail out now */
  3083.    if(! digit_seen)
  3084.      return -1;
  3085.  
  3086.         /* look for exponential part.  The standard does not
  3087.            allow D, but we will, just in case. */
  3088.    if(makeupper(s[i]) == 'E' || makeupper(s[i]) == 'D') {
  3089.      i++;
  3090.      SKIP_SPACE;
  3091.      if(s[i] == '+' || s[i] == '-') {
  3092.        i++;
  3093.        SKIP_SPACE;
  3094.      }
  3095.      if(!isdigit(s[i]))
  3096.        return -1;
  3097.      while(isdigit(s[i]) || isspace(s[i]))
  3098.        i++;
  3099.    }
  3100.  
  3101.    return i;
  3102. }/*see_a_number*/
  3103.  
  3104.     /* see_dowhile returns TRUE only if the stuff following the initial
  3105.        DO is a label and the word WHILE followed by a parenthesized expr.
  3106.        If not resolved on current line, assumes TRUE (how many arrays
  3107.        are named DO10WHILE?).  The "DO WHILE" form is not handled
  3108.        here so that DOWHILE will be gotten as a single token later.
  3109.      */
  3110. PRIVATE int
  3111. see_dowhile(indx,ll)
  3112.      int indx;
  3113.      char ll[];
  3114. {
  3115.     int c;
  3116.                 /* Skip over the label */
  3117.     while(isdigit(c=ll[indx]) || isspace(c) )
  3118.       ++indx;
  3119.  
  3120.     if(c == ',')        /* Skip optional comma */
  3121.       ++indx;
  3122.  
  3123.     indx = see_keyword(indx,ll,"WHILE");
  3124.  
  3125.     if( indx == -1 || ll[indx] != '(')  /* Look for the opening paren */
  3126.       return -1;
  3127.  
  3128.     ++indx;            /* skip the opening paren */
  3129.     indx = skip_balanced_parens(indx,ll);
  3130.                 /* Now we should be at end of statement */
  3131.     return (ll[indx] == '\0')? indx: -1;
  3132. }/*see_dowhile*/
  3133.  
  3134.  
  3135.     /* Crude routine to scan forward past arithmetic expressions.
  3136.        Function invocations and array or character elements will
  3137.        have their parentheses skipped by skip_balanced_parens;
  3138.        outside parens a comma will cause a halt.  Returns the index
  3139.        of the nonblank character following the expression, or
  3140.        -1 if something non-kosher was found (e.g. a faulty number)
  3141.        It can be confused by holleriths containing significant
  3142.        characters, i.e. ( ) ' !  and occurring outside parentheses.
  3143.      */
  3144. PRIVATE int
  3145. see_expression(indx,ll)
  3146.      int indx;
  3147.      char ll[];
  3148. {
  3149.     int c;
  3150.     while(indx != -1 && (c=ll[indx]) != '=' && c != '\0') {
  3151.     if(isidletter(c))
  3152.       indx = skip_idletters(indx,ll);
  3153.     else if(isdigit(c))
  3154.       indx = see_a_number(indx,ll,TRUE);
  3155.     else if(isspace(c))
  3156.       ++indx;
  3157.     else if(c == '(')
  3158.       indx = skip_balanced_parens(indx+1,ll);
  3159.     else if(c == '+' || c == '-' || c == '/' || c == '*' || c == '.')
  3160.       ++indx;
  3161.     else if(c == '\'')    /* embedded strings confuse things */
  3162.       indx = skip_quoted_string(indx+1,ll);
  3163.     else break;
  3164.     }
  3165.     return indx;
  3166. }/*see_expression*/
  3167.  
  3168.     /* see_keyword returns -1 if the line (ignoring blanks and
  3169.        uppercasing alphabetics) does not match the given string
  3170.        matchstr.  If it does match, returns index of next nonspace
  3171.        character. Note that index must be at start of keyword. */
  3172.  
  3173. PRIVATE int
  3174. see_keyword(indx,ll,matchstr)
  3175.      int indx;
  3176.      char ll[];
  3177.      char *matchstr;
  3178. {
  3179.     int c;
  3180.     while(*matchstr != 0 && (c=ll[indx]) != '\0') {
  3181.       if(! isspace(c) ) {
  3182.     if(makeupper(c) != *matchstr++)
  3183.       return -1;
  3184.       }
  3185.       ++indx;
  3186.     }
  3187.     if(*matchstr == '\0') {    /* Match found */
  3188.       while(isspace(ll[indx]))
  3189.     ++indx;
  3190.       return indx;
  3191.     }
  3192.     else            /* No match */
  3193.       return -1;
  3194. }/*see_keyword*/
  3195.  
  3196.         /* skip_balanced_parens returns index of the nonspace character
  3197.            following the closing ')' that balances the opening
  3198.            '(' preceding ll[indx], or of final nul if the
  3199.            parentheses are not balanced within the line.
  3200.         */
  3201. PRIVATE int
  3202. skip_balanced_parens(indx,ll)
  3203.      int indx;
  3204.      char ll[];
  3205. {
  3206.   int depth=1;        /* nesting depth in parens */
  3207.   int prevchar = '+';    /* arbitrary punctuation */
  3208. #ifdef DEBUG_IS_KEYWORD
  3209. if(debug_lexer && getenv("VERBOSE"))
  3210. (void)fprintf(list_fd,"\nskipping ()...");
  3211. #endif
  3212.  
  3213.   while(ll[indx] != '\0' && depth > 0) {
  3214. #ifdef INLINE_COMMENT_CHAR
  3215.     if(ll[indx] == INLINE_COMMENT_CHAR) /* inline comment ends line */
  3216.       break;
  3217. #endif
  3218.     if(ll[indx] == '\'') {    /* embedded strings confuse things */
  3219.       indx = skip_quoted_string(indx+1,ll);
  3220.       prevchar = 'X';    /* Arbitrary non punctuation */
  3221.     }
  3222.     else if(ispunct(prevchar) && isdigit(ll[indx])) {
  3223.       indx = skip_hollerith(indx,ll); /* Skip hollerith or number */
  3224.       prevchar = ll[indx];
  3225.     }
  3226.     else {
  3227.                 /* Keep track of nesting */
  3228.       if     (ll[indx] == '(') ++depth;
  3229.       else if(ll[indx] == ')') --depth;
  3230.  
  3231.       if(! isspace(ll[indx]) )
  3232.     prevchar = ll[indx];
  3233.  
  3234.       ++indx;
  3235.     }
  3236.   }
  3237.  
  3238.                 /* We are now past the closing paren */
  3239.   while(ll[indx] != '\0' && isspace(ll[indx]))
  3240.     indx++;        /* skip trailing space */
  3241.  
  3242.   return indx;
  3243. }/*skip_balanced_parens*/
  3244.  
  3245.  
  3246.         /* skip_idletters returns index of the nonspace character
  3247.            following a string of idletters: alphabetic characters
  3248.            or digits, or underscore or dollar if those options are
  3249.            enabled.  It does not look out for hollerith constants.
  3250.         */
  3251. PRIVATE int
  3252. skip_idletters(indx,ll)
  3253.      int indx;
  3254.      char ll[];
  3255. {
  3256.     int c;
  3257. #ifdef DEBUG_IS_KEYWORD
  3258. if(debug_lexer && getenv("VERBOSE"))
  3259. (void)fprintf(list_fd,": skipping letters...");
  3260. #endif
  3261.     while(isidletter(c=ll[indx])
  3262.           || isadigit(c) || isspace(c))
  3263.       ++indx;
  3264.     return indx;
  3265. }/*skip_idletters*/
  3266.  
  3267.         /* Returns index of nonspace character following
  3268.            quote mark that closes string whose opening quote
  3269.            mark is before index. */
  3270. PRIVATE int
  3271. skip_quoted_string(indx,ll)
  3272.      int indx;
  3273.      char ll[];
  3274. {
  3275.   while(ll[indx] != '\0') {
  3276.     if(ll[indx] == '\'') {    /* Closing quote? */
  3277.       if(ll[++indx] != '\'') /* Quoted quote? */
  3278.     break;
  3279.     }
  3280.     ++indx;
  3281.   }
  3282.  
  3283.                 /* We are now past the closing quote mark */
  3284.   while(ll[indx] != '\0' && isspace(ll[indx]))
  3285.     indx++;        /* skip trailing space */
  3286.  
  3287.   return indx;
  3288. }/*skip_quoted_string*/
  3289.  
  3290.  
  3291.             /* Skips holleriths.  Note: treats tabs within
  3292.                hollerith as single characters. */
  3293. PRIVATE int
  3294. skip_hollerith(i,s)
  3295.    int i;
  3296.    char s[];
  3297. {
  3298.   int len=0;
  3299.   while(isdigit(s[i])) {
  3300.     len = len*10 + BCD(s[i]);
  3301.     i++;
  3302.     SKIP_SPACE;
  3303.   }
  3304. #ifdef DEBUG_IS_KEYWORD
  3305. if(debug_lexer && getenv("VERBOSE"))
  3306.   (void)fprintf(list_fd,"\nskip_hollerith: %d then %c:",
  3307. len,s[i]);
  3308. #endif
  3309.   if(makeupper(s[i]) != 'H')
  3310.     return i;
  3311.  
  3312.   i++;                /* Skip the 'H' */
  3313.  
  3314.   while(s[i] != '\0' && len > 0){ /* Move forward len characters */
  3315.  
  3316. #ifdef DEBUG_IS_KEYWORD
  3317. if(debug_lexer && getenv("VERBOSE"))
  3318.   (void)fprintf(list_fd,"%c",s[i]);
  3319. #endif
  3320.     --len; i++;
  3321.   }
  3322. #ifdef DEBUG_IS_KEYWORD
  3323. if(debug_lexer && getenv("VERBOSE"))
  3324.   (void)fprintf(list_fd," to %c",s[i]);
  3325. #endif
  3326.   return i;
  3327. }/*skip_hollerith*/
  3328.  
  3329.  
  3330. PRIVATE int
  3331. is_overlength(s,maxcol)    /* checks line for having nonblanks past col 72 */
  3332.     char *s;        /* The line to check */
  3333.     int maxcol;        /* Max columns to check to */
  3334. {
  3335.     int i=0,col=1;
  3336.  
  3337. #ifdef DEC_TABS    /* support for DEC tab formatting */
  3338.     if(dec_tabs && s[i] == '\t') {
  3339.       ++i;
  3340.       if( isadigit((int)s[i]) )
  3341.         col = 6; /* continuation column */
  3342.       else
  3343.         col = 7; /* start of statement */
  3344.     }
  3345. #endif
  3346.  
  3347.     for( ; col<=maxcol && s[i] != '\0'; i++) {
  3348.             /* Inline comments are allowed to run past 72
  3349.                columns without complaint.  The following test
  3350.                will be fooled by ! in quote or hollerith, but
  3351.                it isn't worth the trouble to catch those.  */
  3352. #ifdef INLINE_COMMENT_CHAR
  3353.         if(col != 6 && s[i] == INLINE_COMMENT_CHAR)
  3354.           return FALSE;
  3355. #endif
  3356.         if(col > 72 && !isspace(s[i]))
  3357.         return TRUE;
  3358.  
  3359.             /* Count columns taking tabs into consideration */
  3360.         if(s[i] == '\t')
  3361.         col = nxttab[col];
  3362.         else
  3363.         ++col;
  3364.     }
  3365.     return FALSE;
  3366. }/*is_overlength*/
  3367.  
  3368. /* End of module Advance */
  3369.