scc

simple C compiler
git clone git://git.2f30.org/scc
Log | Files | Refs | README | LICENSE

commit f18d5404c710db34fcd2a945ba0fddb9e70d80c5
parent e11576874bba09ca8d7e52be405c9c29518b49bd
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Sun,  9 Mar 2014 16:24:11 +0100

Use a TYPE token

This token help us in order to know that the next token is the
name of a type, so it is easier to detect a declaration.

Diffstat:
Mdecl.c | 8++------
Mlex.c | 81++++++++++++++++++++++++++++++++++++++++++-------------------------------------
Mtokens.h | 4++--
3 files changed, 47 insertions(+), 46 deletions(-)

diff --git a/decl.c b/decl.c @@ -185,12 +185,8 @@ specifier(register struct ctype *tp, case AUTO: case REGISTER: store = storage(store, yytoken); break; - case UNSIGNED: case SIGNED: - case COMPLEX: case IMAGINARY: - case FLOAT: case DOUBLE: case BOOL: - case VOID: case CHAR: case SHORT: - case INT: case LONG: - tp = ctype(tp, yytoken); + case TYPE: + tp = ctype(tp, yyval->c); break; case ENUM: tp = ctype(tp, ENUM); diff --git a/lex.c b/lex.c @@ -20,6 +20,7 @@ const char *filename; struct keyword { char *str; unsigned char tok; + unsigned char value; struct keyword *next; }; @@ -122,43 +123,43 @@ void init_keywords(void) { static struct keyword buff[] = { - {"auto", AUTO, NULL}, - {"break", BREAK, NULL}, - {"_Bool", CHAR, NULL}, - {"_Complex", COMPLEX, NULL}, - {"case", CASE, NULL}, - {"char", CHAR, NULL}, - {"const", CONST, NULL}, - {"continue", CONTINUE, NULL}, - {"default", DEFAULT, NULL}, - {"do", DO, NULL}, - {"double", DOUBLE, NULL}, - {"else", ELSE, NULL}, - {"enum", ENUM, NULL}, - {"extern", EXTERN, NULL}, - {"float", FLOAT, NULL}, - {"for", FOR, NULL}, - {"goto", GOTO, NULL}, - {"if", IF, NULL}, - {"int", INT, NULL}, - {"_Imaginary", IMAGINARY, NULL}, - {"long", LONG, NULL}, - {"register", REGISTER, NULL}, - {"restricted", RESTRICT, NULL}, - {"return", RETURN, NULL}, - {"short", SHORT, NULL}, - {"signed", SIGNED, NULL}, - {"sizeof", SIZEOF, NULL}, - {"static", STATIC, NULL}, - {"struct", STRUCT, NULL}, - {"switch", SWITCH, NULL}, - {"typedef", TYPEDEF, NULL}, - {"union", UNION, NULL}, - {"unsigned", UNSIGNED, NULL}, - {"void", VOID, NULL}, - {"volatile", VOLATILE, NULL}, - {"while", WHILE, NULL}, - {NULL, 0, NULL}, + {"auto", AUTO, AUTO}, + {"break", BREAK, BREAK}, + {"_Bool", TYPE, BOOL}, + {"_Complex", TYPE, COMPLEX}, + {"case", CASE, CASE}, + {"char", TYPE, CHAR}, + {"const", CONST, CONST}, + {"continue", CONTINUE, CONTINUE}, + {"default", DEFAULT, DEFAULT}, + {"do", DO, DO}, + {"double", TYPE, DOUBLE}, + {"else", ELSE, ELSE}, + {"enum", ENUM, ENUM}, + {"extern", EXTERN, EXTERN}, + {"float", TYPE, FLOAT}, + {"for", FOR, FOR}, + {"goto", GOTO, GOTO}, + {"if", IF, IF}, + {"int", TYPE, INT}, + {"_Imaginary", TYPE, IMAGINARY}, + {"long", TYPE, LONG}, + {"register", REGISTER, REGISTER}, + {"restricted", RESTRICT, RESTRICT}, + {"return", RETURN, RETURN}, + {"short", TYPE, SHORT}, + {"signed", TYPE, SIGNED}, + {"sizeof", SIZEOF, SIZEOF}, + {"static", STATIC, STATIC}, + {"struct", STRUCT, STRUCT}, + {"switch", SWITCH, SWITCH}, + {"typedef", TYPEDEF, TYPEDEF}, + {"union", UNION, UNION}, + {"unsigned", TYPE, UNSIGNED}, + {"void", TYPE, VOID}, + {"volatile", VOLATILE, VOLATILE}, + {"while", WHILE, WHILE}, + {NULL, 0, 0}, }; register struct keyword *bp; @@ -173,10 +174,14 @@ static unsigned char keyword(register char *s) { register struct keyword *bp; + static struct symbol sym; for (bp = ktab[hash(s) & NR_KEYW_HASH-1]; bp; bp = bp->next) { - if (*s == *bp->str && !strcmp(bp->str, s)) + if (*s == *bp->str && !strcmp(bp->str, s)) { + sym.c = bp->value; + yyval = &sym; return bp->tok; + } } return 0; } diff --git a/tokens.h b/tokens.h @@ -11,7 +11,7 @@ enum tokens { /* types */ INT = 1, CHAR, FLOAT, LONG, LLONG, SHORT, VOID, DOUBLE, LDOUBLE, STRUCT, UNION, ENUM, BOOL, ARY, PTR, FTN, - COMPLEX, IMAGINARY, BITFLD, + COMPLEX, IMAGINARY, BITFLD, TYPE, /* storage specifier */ TYPEDEF, EXTERN, STATIC, AUTO, REGISTER, /* type qualifier */ @@ -24,7 +24,7 @@ enum tokens { LE, GE, EQ, NE, AND, OR, MUL_EQ, DIV_EQ, MOD_EQ, ADD_EQ, SUB_EQ, AND_EQ, XOR_EQ, OR_EQ, SHL_EQ, SHR_EQ, - TYPE_NAME, ELLIPSIS, + ELLIPSIS, CASE, DEFAULT, IF, ELSE, SWITCH, WHILE, DO, FOR, GOTO, CONTINUE, BREAK, RETURN, EOFTOK, NOTOK };