1989-02-07 11:04:05 +00:00
|
|
|
/*
|
|
|
|
* (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
|
|
|
|
* See the copyright notice in the ACK home directory, in the file "Copyright".
|
|
|
|
*/
|
1994-06-27 08:03:14 +00:00
|
|
|
/* $Id$ */
|
1989-02-07 11:04:05 +00:00
|
|
|
/* L E X I C A L A N A L Y Z E R */
|
|
|
|
|
2018-09-02 09:16:06 +00:00
|
|
|
#include <assert.h>
|
|
|
|
#include <alloc.h>
|
|
|
|
#include "parameters.h"
|
|
|
|
#include "input.h"
|
|
|
|
#include "arith.h"
|
|
|
|
#include "def.h"
|
|
|
|
#include "macro.h"
|
|
|
|
#include "idf.h"
|
|
|
|
#include "LLlex.h"
|
|
|
|
#include "Lpars.h"
|
|
|
|
#include "class.h"
|
|
|
|
#include "sizes.h"
|
|
|
|
#include "specials.h" /* registration of special identifiers */
|
1989-02-07 11:04:05 +00:00
|
|
|
|
|
|
|
/* Data about the token yielded */
|
|
|
|
struct token dot, ahead, aside;
|
2018-09-02 09:16:06 +00:00
|
|
|
int token_nmb = 0; /* number of the ahead token */
|
|
|
|
int tk_nmb_at_last_syn_err = -5 /*ERR_SHADOW*/;
|
|
|
|
/* token number at last syntax error */
|
1991-06-19 16:12:56 +00:00
|
|
|
int idfsize = IDFSIZE;
|
2018-09-02 09:16:06 +00:00
|
|
|
char sp_occurred[SP_TOTAL + 1];
|
1989-02-07 11:04:05 +00:00
|
|
|
|
2018-09-02 09:16:06 +00:00
|
|
|
int AccFileSpecifier = 0; /* return filespecifier <...> */
|
|
|
|
int EoiForNewline = 0; /* return EOI upon encountering newline */
|
|
|
|
int File_Inserted = 0; /* a file has just been inserted */
|
|
|
|
int LexSave = 0; /* last character read by GetChar */
|
|
|
|
#define MAX_LL_DEPTH 2
|
|
|
|
|
|
|
|
#define FLG_ESEEN 0x01 /* possibly a floating point number */
|
|
|
|
#define FLG_DOTSEEN 0x02 /* certainly a floating point number */
|
1989-09-19 16:13:23 +00:00
|
|
|
extern arith full_mask[];
|
|
|
|
|
1993-06-09 09:40:54 +00:00
|
|
|
#ifdef LINT
|
|
|
|
extern int lint_skip_comment;
|
|
|
|
#endif
|
|
|
|
|
2018-09-02 09:16:06 +00:00
|
|
|
int LLlex()
|
1989-02-07 11:04:05 +00:00
|
|
|
{
|
|
|
|
/* LLlex() plays the role of Lexical Analyzer for the C parser.
|
2018-09-02 09:16:06 +00:00
|
|
|
The look-ahead and putting aside of tokens are taken into
|
|
|
|
account.
|
1989-02-07 11:04:05 +00:00
|
|
|
*/
|
2018-09-02 09:16:06 +00:00
|
|
|
if (ASIDE)
|
|
|
|
{ /* a token is put aside */
|
1989-02-07 11:04:05 +00:00
|
|
|
dot = aside;
|
|
|
|
ASIDE = 0;
|
|
|
|
}
|
2018-09-02 09:16:06 +00:00
|
|
|
else
|
|
|
|
{ /* read ahead and return the old one */
|
|
|
|
#ifdef LINT
|
1989-02-07 11:04:05 +00:00
|
|
|
lint_comment_ahead();
|
2018-09-02 09:16:06 +00:00
|
|
|
#endif /* LINT */
|
1989-02-07 11:04:05 +00:00
|
|
|
dot = ahead;
|
|
|
|
/* the following test is performed due to the dual
|
2018-09-02 09:16:06 +00:00
|
|
|
task of LLlex(): it is also called for parsing the
|
|
|
|
restricted constant expression following a #if or
|
|
|
|
#elif. The newline character causes EOF to be
|
|
|
|
returned in this case to stop the LLgen parsing task.
|
1989-02-07 11:04:05 +00:00
|
|
|
*/
|
|
|
|
if (DOT != EOI)
|
|
|
|
GetToken(&ahead);
|
|
|
|
else
|
|
|
|
DOT = EOF;
|
|
|
|
}
|
|
|
|
return DOT;
|
|
|
|
}
|
|
|
|
|
2018-09-02 09:16:06 +00:00
|
|
|
char* string_token();
|
|
|
|
arith char_constant();
|
1989-02-07 11:04:05 +00:00
|
|
|
|
2018-09-02 09:16:06 +00:00
|
|
|
int GetToken(ptok) register struct token* ptok;
|
1989-02-07 11:04:05 +00:00
|
|
|
{
|
1989-09-19 16:13:23 +00:00
|
|
|
/* GetToken() is the actual token recognizer. It calls the
|
2018-09-02 09:16:06 +00:00
|
|
|
control line interpreter if it encounters a "\n{w}*#"
|
|
|
|
combination. Macro replacement is also performed if it is
|
|
|
|
needed.
|
1989-02-07 11:04:05 +00:00
|
|
|
*/
|
|
|
|
char buf[(IDFSIZE > NUMSIZE ? IDFSIZE : NUMSIZE) + 1];
|
|
|
|
register int ch, nch;
|
|
|
|
|
1989-11-08 16:52:34 +00:00
|
|
|
token_nmb++;
|
|
|
|
|
2018-09-02 09:16:06 +00:00
|
|
|
if (File_Inserted)
|
|
|
|
{
|
1989-02-07 11:04:05 +00:00
|
|
|
File_Inserted = 0;
|
|
|
|
goto firstline;
|
|
|
|
}
|
|
|
|
|
2018-09-02 09:16:06 +00:00
|
|
|
again: /* rescan the input after an error or replacement */
|
1989-02-07 11:04:05 +00:00
|
|
|
ch = GetChar();
|
2018-09-02 09:16:06 +00:00
|
|
|
go_on: /* rescan, the following character has been read */
|
1989-02-07 11:04:05 +00:00
|
|
|
if ((ch & 0200) && ch != EOI) /* stop on non-ascii character */
|
1989-09-19 16:13:23 +00:00
|
|
|
{
|
1989-02-07 11:04:05 +00:00
|
|
|
fatal("non-ascii '\\%03o' read", ch & 0377);
|
1989-09-19 16:13:23 +00:00
|
|
|
}
|
1989-02-07 11:04:05 +00:00
|
|
|
/* keep track of the place of the token in the file */
|
|
|
|
ptok->tk_file = FileName;
|
|
|
|
ptok->tk_line = LineNumber;
|
|
|
|
|
2018-09-02 09:16:06 +00:00
|
|
|
switch (class(ch))
|
|
|
|
{ /* detect character class */
|
|
|
|
case STNL: /* newline, vertical space or formfeed */
|
|
|
|
firstline:
|
|
|
|
LineNumber++; /* also at vs and ff */
|
|
|
|
ptok->tk_file = FileName;
|
|
|
|
ptok->tk_line = LineNumber;
|
|
|
|
if (EoiForNewline) /* called in control line */
|
|
|
|
/* a newline in a control line indicates the
|
|
|
|
end-of-information of the line.
|
|
|
|
*/
|
|
|
|
return ptok->tk_symb = EOI;
|
|
|
|
|
|
|
|
while ((ch = GetChar()), (ch == '#'
|
|
|
|
|| class(ch) == STSKIP))
|
|
|
|
{
|
|
|
|
/* blanks are allowed before hashes */
|
|
|
|
if (ch == '#')
|
|
|
|
{
|
|
|
|
/* a control line follows */
|
|
|
|
domacro();
|
1989-11-22 13:58:36 +00:00
|
|
|
}
|
1989-02-07 11:04:05 +00:00
|
|
|
}
|
|
|
|
/* We have to loop here, because in
|
2018-09-02 09:16:06 +00:00
|
|
|
`domacro' the nl, vt or ff is read. The
|
|
|
|
character following it may again be a `#'.
|
1989-02-07 11:04:05 +00:00
|
|
|
*/
|
2018-09-02 09:16:06 +00:00
|
|
|
goto go_on;
|
|
|
|
case STSKIP: /* just skip the skip characters */
|
|
|
|
goto again;
|
|
|
|
case STGARB: /* garbage character */
|
|
|
|
if (040 < ch && ch < 0177)
|
|
|
|
{
|
|
|
|
return ptok->tk_symb = ch;
|
1989-09-19 16:13:23 +00:00
|
|
|
}
|
2018-09-02 09:16:06 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
lexerror("garbage char \\%03o", ch);
|
1989-09-19 16:13:23 +00:00
|
|
|
}
|
2018-09-02 09:16:06 +00:00
|
|
|
goto again;
|
|
|
|
case STSIMP: /* a simple character, no part of compound token*/
|
|
|
|
return ptok->tk_symb = ch;
|
|
|
|
case STCOMP: /* maybe the start of a compound token */
|
|
|
|
nch = GetChar(); /* character lookahead */
|
|
|
|
switch (ch)
|
|
|
|
{
|
|
|
|
case '!':
|
|
|
|
if (nch == '=')
|
|
|
|
return ptok->tk_symb = NOTEQUAL;
|
|
|
|
break;
|
|
|
|
case '&':
|
|
|
|
if (nch == '&')
|
|
|
|
return ptok->tk_symb = AND;
|
|
|
|
if (nch == '=')
|
|
|
|
return ptok->tk_symb = ANDAB;
|
|
|
|
break;
|
|
|
|
case '+':
|
|
|
|
if (nch == '+')
|
|
|
|
return ptok->tk_symb = PLUSPLUS;
|
|
|
|
if (nch == '=')
|
|
|
|
return ptok->tk_symb = PLUSAB;
|
|
|
|
break;
|
|
|
|
case '-':
|
|
|
|
if (nch == '-')
|
|
|
|
return ptok->tk_symb = MINMIN;
|
|
|
|
if (nch == '>')
|
|
|
|
return ptok->tk_symb = ARROW;
|
|
|
|
if (nch == '=')
|
|
|
|
return ptok->tk_symb = MINAB;
|
|
|
|
break;
|
|
|
|
case '<':
|
|
|
|
if (AccFileSpecifier)
|
|
|
|
{
|
|
|
|
UnGetChar(); /* pushback nch */
|
|
|
|
ptok->tk_bts = string_token("file specifier", '>', &(ptok->tk_len));
|
|
|
|
return ptok->tk_symb = FILESPECIFIER;
|
|
|
|
}
|
|
|
|
if (nch == '<')
|
|
|
|
{
|
|
|
|
if ((nch = GetChar()) == '=')
|
|
|
|
return ptok->tk_symb = LEFTAB;
|
|
|
|
UnGetChar();
|
|
|
|
return ptok->tk_symb = LEFT;
|
|
|
|
}
|
|
|
|
if (nch == '=')
|
|
|
|
return ptok->tk_symb = LESSEQ;
|
|
|
|
break;
|
|
|
|
case '=':
|
|
|
|
if (nch == '=')
|
|
|
|
return ptok->tk_symb = EQUAL;
|
|
|
|
break;
|
|
|
|
case '>':
|
|
|
|
if (nch == '=')
|
|
|
|
return ptok->tk_symb = GREATEREQ;
|
|
|
|
if (nch == '>')
|
|
|
|
{
|
|
|
|
if ((nch = GetChar()) == '=')
|
|
|
|
return ptok->tk_symb = RIGHTAB;
|
|
|
|
UnGetChar();
|
|
|
|
return ptok->tk_symb = RIGHT;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '|':
|
|
|
|
if (nch == '|')
|
|
|
|
return ptok->tk_symb = OR;
|
|
|
|
if (nch == '=')
|
|
|
|
return ptok->tk_symb = ORAB;
|
|
|
|
break;
|
|
|
|
case '%':
|
|
|
|
if (nch == '=')
|
|
|
|
return ptok->tk_symb = MODAB;
|
|
|
|
break;
|
|
|
|
case '*':
|
|
|
|
if (nch == '=')
|
|
|
|
return ptok->tk_symb = TIMESAB;
|
|
|
|
break;
|
|
|
|
case '^':
|
|
|
|
if (nch == '=')
|
|
|
|
return ptok->tk_symb = XORAB;
|
|
|
|
break;
|
|
|
|
case '/':
|
|
|
|
if (nch == '=')
|
|
|
|
return ptok->tk_symb = DIVAB;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
crash("bad class for char 0%o", ch);
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
UnGetChar();
|
|
|
|
return ptok->tk_symb = ch;
|
|
|
|
case STCHAR: /* character constant */
|
|
|
|
ptok->tk_ival = char_constant("character");
|
1989-02-07 11:04:05 +00:00
|
|
|
ptok->tk_fund = INT;
|
|
|
|
return ptok->tk_symb = INTEGER;
|
2018-09-02 09:16:06 +00:00
|
|
|
case STSTR: /* string */
|
|
|
|
ptok->tk_bts = string_token("string", '"', &(ptok->tk_len));
|
|
|
|
ptok->tk_fund = CHAR; /* string of characters */
|
|
|
|
return ptok->tk_symb = STRING;
|
|
|
|
case STELL: /* wide character constant/string prefix */
|
|
|
|
nch = GetChar();
|
|
|
|
if (nch == '"')
|
|
|
|
{
|
|
|
|
ptok->tk_bts = string_token("wide character string", '"', &(ptok->tk_len));
|
|
|
|
ptok->tk_fund = WCHAR; /* string of wide characters */
|
|
|
|
return ptok->tk_symb = STRING;
|
|
|
|
}
|
|
|
|
else if (nch == '\'')
|
|
|
|
{
|
|
|
|
ptok->tk_ival = char_constant("wide character");
|
|
|
|
ptok->tk_fund = INT;
|
|
|
|
return ptok->tk_symb = INTEGER;
|
|
|
|
}
|
|
|
|
UnGetChar();
|
1989-09-19 16:13:23 +00:00
|
|
|
/* fallthrough */
|
2018-09-02 09:16:06 +00:00
|
|
|
case STIDF:
|
|
|
|
{
|
|
|
|
register char* tg = &buf[0];
|
|
|
|
register int pos = -1;
|
|
|
|
register struct idf* idef;
|
|
|
|
extern int idfsize; /* ??? */
|
|
|
|
do
|
|
|
|
{ /* read the identifier */
|
|
|
|
if (++pos < idfsize)
|
|
|
|
{
|
|
|
|
*tg++ = ch;
|
|
|
|
}
|
|
|
|
ch = GetChar();
|
|
|
|
} while (in_idf(ch));
|
1989-02-07 11:04:05 +00:00
|
|
|
|
2018-09-02 09:16:06 +00:00
|
|
|
if (ch != EOI)
|
|
|
|
UnGetChar();
|
|
|
|
*tg++ = '\0'; /* mark the end of the identifier */
|
|
|
|
idef = ptok->tk_idf = str2idf(buf, 1);
|
|
|
|
sp_occurred[idef->id_special] = 1;
|
|
|
|
idef->id_file = ptok->tk_file;
|
|
|
|
idef->id_line = ptok->tk_line;
|
|
|
|
ptok->tk_symb
|
|
|
|
= (idef->id_reserved
|
|
|
|
? idef->id_reserved
|
|
|
|
: idef->id_def && idef->id_def->df_sc == TYPEDEF ? TYPE_IDENTIFIER
|
|
|
|
: IDENTIFIER);
|
|
|
|
return IDENTIFIER;
|
|
|
|
}
|
|
|
|
case STNUM: /* a numeric constant */
|
|
|
|
{
|
|
|
|
register int siz_left = NUMSIZE - 1;
|
|
|
|
register char* np = &buf[0];
|
|
|
|
int flags = 0;
|
|
|
|
|
|
|
|
#define store(ch) \
|
|
|
|
if (--siz_left >= 0) \
|
|
|
|
*np++ = ch;
|
|
|
|
|
|
|
|
if (ch == '.')
|
|
|
|
{
|
|
|
|
/* An embarrasing ambiguity. We have either a
|
|
|
|
pp-number, a field operator, an ELLIPSIS or
|
|
|
|
an error (..).
|
|
|
|
*/
|
|
|
|
ch = GetChar();
|
|
|
|
if (!is_dig(ch))
|
|
|
|
{ /* . or ... */
|
|
|
|
if (ch == '.')
|
|
|
|
{
|
|
|
|
if ((ch = GetChar()) == '.')
|
|
|
|
return ptok->tk_symb = ELLIPSIS;
|
|
|
|
UnGetChar(); /* not '.' */
|
|
|
|
ChPushBack('.'); /* sigh ... */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
UnGetChar(); /* not '.' */
|
|
|
|
return ptok->tk_symb = '.';
|
|
|
|
}
|
|
|
|
UnGetChar();
|
|
|
|
ch = '.';
|
|
|
|
flags |= FLG_DOTSEEN;
|
1989-02-07 13:16:02 +00:00
|
|
|
}
|
1989-09-19 16:13:23 +00:00
|
|
|
store(ch);
|
2018-09-02 09:16:06 +00:00
|
|
|
ch = GetChar();
|
|
|
|
while (in_idf(ch) || ch == '.')
|
|
|
|
{
|
|
|
|
store(ch);
|
|
|
|
if (ch == '.')
|
|
|
|
flags |= FLG_DOTSEEN;
|
|
|
|
if (ch == 'e' || ch == 'E')
|
|
|
|
{
|
|
|
|
flags |= FLG_ESEEN;
|
1989-09-19 16:13:23 +00:00
|
|
|
ch = GetChar();
|
2018-09-02 09:16:06 +00:00
|
|
|
if (ch == '+' || ch == '-')
|
|
|
|
{
|
|
|
|
flags |= FLG_DOTSEEN; /* trick */
|
|
|
|
store(ch);
|
|
|
|
ch = GetChar();
|
|
|
|
}
|
1989-02-07 11:04:05 +00:00
|
|
|
}
|
2018-09-02 09:16:06 +00:00
|
|
|
else
|
|
|
|
ch = GetChar();
|
|
|
|
}
|
|
|
|
store('\0');
|
|
|
|
UnGetChar();
|
1989-02-07 11:04:05 +00:00
|
|
|
|
2018-09-02 09:16:06 +00:00
|
|
|
np = &buf[0];
|
|
|
|
ch = *np++;
|
|
|
|
if (siz_left < 0)
|
|
|
|
{
|
|
|
|
lexerror("number too long");
|
|
|
|
if ((flags & FLG_DOTSEEN)
|
|
|
|
|| (flags & FLG_ESEEN && !(ch == '0' && (*np == 'x' || *np == 'X'))))
|
|
|
|
{
|
|
|
|
ptok->tk_fval = Salloc("0.0", (unsigned)4);
|
|
|
|
ptok->tk_fund = DOUBLE;
|
|
|
|
return ptok->tk_symb = FLOATING;
|
|
|
|
}
|
|
|
|
ptok->tk_ival = 1;
|
|
|
|
ptok->tk_fund = ULONG;
|
|
|
|
ptok->tk_symb = INTEGER;
|
|
|
|
}
|
|
|
|
/* Now, the pp-number must be converted into a token */
|
1989-09-19 16:13:23 +00:00
|
|
|
if ((flags & FLG_DOTSEEN)
|
2018-09-02 09:16:06 +00:00
|
|
|
|| (flags & FLG_ESEEN && !(ch == '0' && (*np == 'x' || *np == 'X'))))
|
|
|
|
{
|
|
|
|
strflt2tok(&buf[0], ptok);
|
|
|
|
return ptok->tk_symb = FLOATING;
|
1989-09-19 16:13:23 +00:00
|
|
|
}
|
2018-09-02 09:16:06 +00:00
|
|
|
strint2tok(&buf[0], ptok);
|
|
|
|
return ptok->tk_symb = INTEGER;
|
1989-02-07 11:04:05 +00:00
|
|
|
}
|
2018-09-02 09:16:06 +00:00
|
|
|
case STEOI: /* end of text on source file */
|
|
|
|
return ptok->tk_symb = EOI;
|
|
|
|
default: /* this cannot happen */
|
|
|
|
crash("bad class for char 0%o", ch);
|
1989-02-07 11:04:05 +00:00
|
|
|
}
|
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
|
|
|
|
2018-09-02 09:16:06 +00:00
|
|
|
arith char_constant(nm) char* nm;
|
1989-02-07 11:04:05 +00:00
|
|
|
{
|
|
|
|
register arith val = 0;
|
|
|
|
register int ch;
|
|
|
|
int size = 0;
|
|
|
|
|
|
|
|
ch = GetChar();
|
|
|
|
if (ch == '\'')
|
|
|
|
lexerror("%s constant too short", nm);
|
|
|
|
else
|
2018-09-02 09:16:06 +00:00
|
|
|
while (ch != '\'')
|
|
|
|
{
|
|
|
|
if (ch == '\n')
|
|
|
|
{
|
|
|
|
lexerror("newline in %s constant", nm);
|
|
|
|
LineNumber++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (ch == '\\')
|
|
|
|
ch = quoted(GetChar());
|
|
|
|
if (ch >= 128)
|
|
|
|
ch -= 256;
|
|
|
|
if (size < (int)int_size)
|
|
|
|
val |= ch << 8 * size;
|
|
|
|
size++;
|
|
|
|
ch = GetChar();
|
1989-02-07 11:04:05 +00:00
|
|
|
}
|
|
|
|
if (size > 1)
|
1989-11-08 16:52:34 +00:00
|
|
|
lexstrict("%s constant includes more than one character", nm);
|
1989-02-07 11:04:05 +00:00
|
|
|
if (size > (int)int_size)
|
|
|
|
lexerror("%s constant too long", nm);
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2018-09-02 09:16:06 +00:00
|
|
|
char* string_token(nm, stop_char, plen) char* nm;
|
|
|
|
int* plen;
|
1989-02-07 11:04:05 +00:00
|
|
|
{
|
|
|
|
register int ch;
|
|
|
|
register int str_size;
|
2018-09-02 09:16:06 +00:00
|
|
|
register char* str = Malloc((unsigned)(str_size = ISTRSIZE));
|
1989-02-07 11:04:05 +00:00
|
|
|
register int pos = 0;
|
2018-09-02 09:16:06 +00:00
|
|
|
|
1989-02-07 11:04:05 +00:00
|
|
|
ch = GetChar();
|
2018-09-02 09:16:06 +00:00
|
|
|
while (ch != stop_char)
|
|
|
|
{
|
|
|
|
if (ch == '\n')
|
|
|
|
{
|
1989-02-07 11:04:05 +00:00
|
|
|
lexerror("newline in %s", nm);
|
|
|
|
LineNumber++;
|
|
|
|
break;
|
|
|
|
}
|
2018-09-02 09:16:06 +00:00
|
|
|
if (ch == EOI)
|
|
|
|
{
|
1989-02-07 11:04:05 +00:00
|
|
|
lexerror("end-of-file inside %s", nm);
|
|
|
|
break;
|
|
|
|
}
|
1989-09-19 16:13:23 +00:00
|
|
|
if (ch == '\\' && !AccFileSpecifier)
|
1989-02-07 11:04:05 +00:00
|
|
|
ch = quoted(GetChar());
|
|
|
|
str[pos++] = ch;
|
|
|
|
if (pos == str_size)
|
2018-09-02 09:16:06 +00:00
|
|
|
str = Realloc(str, (unsigned)(str_size += RSTRSIZE));
|
1989-02-07 11:04:05 +00:00
|
|
|
ch = GetChar();
|
|
|
|
}
|
|
|
|
str[pos++] = '\0'; /* for filenames etc. */
|
|
|
|
*plen = pos;
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2018-09-02 09:16:06 +00:00
|
|
|
int quoted(ch) register int ch;
|
|
|
|
{
|
1989-02-07 11:04:05 +00:00
|
|
|
/* quoted() replaces an escaped character sequence by the
|
2018-09-02 09:16:06 +00:00
|
|
|
character meant.
|
1989-02-07 11:04:05 +00:00
|
|
|
*/
|
|
|
|
/* first char after backslash already in ch */
|
2018-09-02 09:16:06 +00:00
|
|
|
if (!is_oct(ch))
|
|
|
|
{ /* a quoted char */
|
|
|
|
switch (ch)
|
1989-02-07 11:04:05 +00:00
|
|
|
{
|
2018-09-02 09:16:06 +00:00
|
|
|
case 'n':
|
|
|
|
ch = '\n';
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
ch = '\t';
|
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
ch = '\b';
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
ch = '\r';
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
ch = '\f';
|
|
|
|
break;
|
|
|
|
case 'a': /* alert */
|
|
|
|
ch = '\007';
|
|
|
|
break;
|
|
|
|
case 'v': /* vertical tab */
|
|
|
|
ch = '\013';
|
|
|
|
break;
|
|
|
|
case 'x': /* quoted hex */
|
|
|
|
{
|
|
|
|
register int hex = 0;
|
|
|
|
register int vch;
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
ch = GetChar();
|
|
|
|
if ((vch = hex_val(ch)) == -1)
|
|
|
|
break;
|
|
|
|
hex = hex * 16 + vch;
|
|
|
|
}
|
|
|
|
UnGetChar();
|
|
|
|
ch = hex;
|
1989-02-07 11:04:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-09-02 09:16:06 +00:00
|
|
|
else
|
|
|
|
{ /* a quoted octal */
|
1989-02-07 11:04:05 +00:00
|
|
|
register int oct = 0, cnt = 0;
|
|
|
|
|
2018-09-02 09:16:06 +00:00
|
|
|
do
|
|
|
|
{
|
|
|
|
oct = oct * 8 + (ch - '0');
|
1989-02-07 11:04:05 +00:00
|
|
|
ch = GetChar();
|
|
|
|
} while (is_oct(ch) && ++cnt < 3);
|
|
|
|
UnGetChar();
|
|
|
|
ch = oct;
|
|
|
|
}
|
2018-09-02 09:16:06 +00:00
|
|
|
return ch & 0377;
|
1989-02-07 11:04:05 +00:00
|
|
|
}
|
|
|
|
|
2018-09-02 09:16:06 +00:00
|
|
|
int hex_val(ch) register int ch;
|
1989-02-07 11:04:05 +00:00
|
|
|
{
|
2018-09-02 09:16:06 +00:00
|
|
|
return is_dig(ch) ? ch - '0' : is_hex(ch) ? (ch - 'a' + 10) & 017 : -1;
|
1989-02-07 11:04:05 +00:00
|
|
|
}
|
|
|
|
|
2018-09-02 09:16:06 +00:00
|
|
|
int GetChar()
|
1989-02-07 11:04:05 +00:00
|
|
|
{
|
|
|
|
/* The routines GetChar and trigraph parses the trigraph
|
2018-09-02 09:16:06 +00:00
|
|
|
sequences and removes occurences of \\\n.
|
1989-02-07 11:04:05 +00:00
|
|
|
*/
|
|
|
|
register int ch;
|
|
|
|
|
|
|
|
LoadChar(ch);
|
2018-09-02 09:16:06 +00:00
|
|
|
return (LexSave = ch);
|
1989-02-07 11:04:05 +00:00
|
|
|
}
|
|
|
|
|
1989-09-19 16:13:23 +00:00
|
|
|
/* strflt2tok only checks the syntax of the floating-point number and
|
|
|
|
* selects the right type for the number.
|
|
|
|
*/
|
2018-09-02 09:16:06 +00:00
|
|
|
strflt2tok(fltbuf, ptok) char fltbuf[];
|
|
|
|
struct token* ptok;
|
1989-09-19 16:13:23 +00:00
|
|
|
{
|
2018-09-02 09:16:06 +00:00
|
|
|
register char* cp = fltbuf;
|
1989-09-19 16:13:23 +00:00
|
|
|
int malformed = 0;
|
|
|
|
|
2018-09-02 09:16:06 +00:00
|
|
|
while (is_dig(*cp))
|
1989-09-19 16:13:23 +00:00
|
|
|
cp++;
|
2018-09-02 09:16:06 +00:00
|
|
|
if (*cp == '.')
|
|
|
|
{
|
|
|
|
cp++;
|
|
|
|
while (is_dig(*cp))
|
|
|
|
cp++;
|
1989-09-19 16:13:23 +00:00
|
|
|
}
|
2018-09-02 09:16:06 +00:00
|
|
|
if (*cp == 'e' || *cp == 'E')
|
|
|
|
{
|
1989-09-19 16:13:23 +00:00
|
|
|
cp++;
|
|
|
|
if (*cp == '+' || *cp == '-')
|
|
|
|
cp++;
|
2018-09-02 09:16:06 +00:00
|
|
|
if (!is_dig(*cp))
|
|
|
|
malformed++;
|
|
|
|
while (is_dig(*cp))
|
|
|
|
cp++;
|
1989-09-19 16:13:23 +00:00
|
|
|
}
|
2018-09-02 09:16:06 +00:00
|
|
|
if (*cp == 'f' || *cp == 'F')
|
|
|
|
{
|
|
|
|
if (*(cp + 1))
|
|
|
|
malformed++;
|
1989-09-19 16:13:23 +00:00
|
|
|
*cp = '\0';
|
|
|
|
ptok->tk_fund = FLOAT;
|
2018-09-02 09:16:06 +00:00
|
|
|
}
|
|
|
|
else if (*cp == 'l' || *cp == 'L')
|
|
|
|
{
|
|
|
|
if (*(cp + 1))
|
|
|
|
malformed++;
|
1989-09-19 16:13:23 +00:00
|
|
|
*cp = '\0';
|
|
|
|
ptok->tk_fund = LNGDBL;
|
2018-09-02 09:16:06 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (*cp)
|
|
|
|
malformed++;
|
1989-09-19 16:13:23 +00:00
|
|
|
ptok->tk_fund = DOUBLE;
|
|
|
|
}
|
2018-09-02 09:16:06 +00:00
|
|
|
if (malformed)
|
|
|
|
{
|
1989-09-19 16:13:23 +00:00
|
|
|
lexerror("malformed floating constant");
|
2018-09-02 09:16:06 +00:00
|
|
|
ptok->tk_fval = Salloc("0.0", (unsigned)4);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ptok->tk_fval = Salloc(fltbuf, (unsigned)(cp - fltbuf + 1));
|
1989-09-19 16:13:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-02 09:16:06 +00:00
|
|
|
strint2tok(intbuf, ptok) char intbuf[];
|
|
|
|
struct token* ptok;
|
1989-09-19 16:13:23 +00:00
|
|
|
{
|
2018-09-02 09:16:06 +00:00
|
|
|
register char* cp = intbuf;
|
1989-09-19 16:13:23 +00:00
|
|
|
int base = 10;
|
|
|
|
arith val = 0, dig, ubound;
|
|
|
|
int uns_flg = 0, lng_flg = 0, malformed = 0, ovfl = 0;
|
|
|
|
int fund;
|
|
|
|
|
2017-11-10 03:22:13 +00:00
|
|
|
assert(*cp != '-');
|
2018-09-02 09:16:06 +00:00
|
|
|
if (*cp == '0')
|
|
|
|
{
|
1989-09-19 16:13:23 +00:00
|
|
|
cp++;
|
2018-09-02 09:16:06 +00:00
|
|
|
if (*cp == 'x' || *cp == 'X')
|
|
|
|
{
|
1989-09-19 16:13:23 +00:00
|
|
|
cp++;
|
|
|
|
base = 16;
|
2018-09-02 09:16:06 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
base = 8;
|
1989-09-19 16:13:23 +00:00
|
|
|
}
|
|
|
|
/* The upperbound will be the same as when computed with
|
|
|
|
* max_unsigned_arith / base (since base is even). The problem here
|
|
|
|
* is that unsigned arith is not accepted by all compilers.
|
|
|
|
*/
|
|
|
|
ubound = max_arith / (base / 2);
|
|
|
|
|
2018-09-02 09:16:06 +00:00
|
|
|
while (is_hex(*cp))
|
|
|
|
{
|
1989-10-18 13:12:31 +00:00
|
|
|
dig = hex_val(*cp);
|
2018-09-02 09:16:06 +00:00
|
|
|
if (dig >= base)
|
|
|
|
{
|
|
|
|
malformed++; /* ignore */
|
1989-09-19 16:13:23 +00:00
|
|
|
}
|
2018-09-02 09:16:06 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (val < 0 || val > ubound)
|
|
|
|
ovfl++;
|
1989-09-19 16:13:23 +00:00
|
|
|
val *= base;
|
2018-09-02 09:16:06 +00:00
|
|
|
if (val < 0 && val + dig >= 0)
|
|
|
|
ovfl++;
|
1989-09-19 16:13:23 +00:00
|
|
|
val += dig;
|
|
|
|
}
|
|
|
|
cp++;
|
|
|
|
}
|
|
|
|
|
2018-09-02 09:16:06 +00:00
|
|
|
while (*cp)
|
|
|
|
{
|
|
|
|
if (*cp == 'l' || *cp == 'L')
|
|
|
|
lng_flg++;
|
|
|
|
else if (*cp == 'u' || *cp == 'U')
|
|
|
|
uns_flg++;
|
|
|
|
else
|
|
|
|
break;
|
1989-09-19 16:13:23 +00:00
|
|
|
cp++;
|
|
|
|
}
|
2018-09-02 09:16:06 +00:00
|
|
|
if (*cp)
|
|
|
|
{
|
|
|
|
malformed++;
|
1989-09-19 16:13:23 +00:00
|
|
|
}
|
2018-09-02 09:16:06 +00:00
|
|
|
if (malformed)
|
|
|
|
{
|
|
|
|
lexerror(
|
|
|
|
"malformed %s integer constant",
|
|
|
|
(base == 10 ? "decimal" : (base == 8 ? "octal" : "hexadecimal")));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
1989-09-19 16:13:23 +00:00
|
|
|
if (lng_flg > 1)
|
|
|
|
lexerror("only one long suffix allowed");
|
|
|
|
if (uns_flg > 1)
|
|
|
|
lexerror("only one unsigned suffix allowed");
|
|
|
|
}
|
2018-09-02 09:16:06 +00:00
|
|
|
if (ovfl)
|
|
|
|
{
|
1989-09-19 16:13:23 +00:00
|
|
|
lexwarning("overflow in constant");
|
|
|
|
fund = ULONG;
|
2018-09-02 09:16:06 +00:00
|
|
|
}
|
|
|
|
else if (!lng_flg && (val & full_mask[(int)int_size]) == val)
|
|
|
|
{
|
|
|
|
if (val >= 0 && val <= max_int)
|
|
|
|
{
|
1989-12-19 11:07:03 +00:00
|
|
|
fund = INT;
|
2018-09-02 09:16:06 +00:00
|
|
|
}
|
|
|
|
else if (int_size == long_size)
|
|
|
|
{
|
1989-12-19 11:07:03 +00:00
|
|
|
fund = UNSIGNED;
|
2018-09-02 09:16:06 +00:00
|
|
|
}
|
|
|
|
else if (base == 10 && !uns_flg)
|
|
|
|
fund = LONG;
|
|
|
|
else
|
|
|
|
fund = UNSIGNED;
|
|
|
|
}
|
|
|
|
else if ((val & full_mask[(int)long_size]) == val)
|
|
|
|
{
|
|
|
|
if (val >= 0)
|
1989-12-19 11:07:03 +00:00
|
|
|
fund = LONG;
|
2018-09-02 09:16:06 +00:00
|
|
|
else
|
|
|
|
fund = ULONG;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ /* sizeof(arith) is greater than long_size */
|
2017-11-10 03:22:13 +00:00
|
|
|
assert(arith_size > long_size);
|
1989-09-19 16:13:23 +00:00
|
|
|
lexwarning("constant too large for target machine");
|
|
|
|
/* cut the size to prevent further complaints */
|
|
|
|
val &= full_mask[(int)long_size];
|
|
|
|
fund = ULONG;
|
|
|
|
}
|
2018-09-02 09:16:06 +00:00
|
|
|
if (lng_flg)
|
|
|
|
{
|
1989-12-19 11:07:03 +00:00
|
|
|
/* fund can't be INT */
|
2018-09-02 09:16:06 +00:00
|
|
|
if (fund == UNSIGNED)
|
|
|
|
fund = ULONG;
|
1989-09-19 16:13:23 +00:00
|
|
|
}
|
2018-09-02 09:16:06 +00:00
|
|
|
if (uns_flg)
|
|
|
|
{
|
|
|
|
if (fund == INT)
|
|
|
|
fund = UNSIGNED;
|
|
|
|
else if (fund == LONG)
|
|
|
|
fund = ULONG;
|
1989-09-19 16:13:23 +00:00
|
|
|
}
|
|
|
|
ptok->tk_fund = fund;
|
|
|
|
ptok->tk_ival = val;
|
|
|
|
}
|