scc

Simple C99 Compiler
Log | Files | Refs | README | LICENSE

commit dc982aab2e3804e8b780fe3a04767d54c8695f5b
parent aa6b39201b88573be81bdadf82bd8c315221fa88
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Sun, 26 Aug 2012 20:29:44 +0200

Rewrite of ahead function

ahead is the function used for read ahead a token in order to can difference
the rule to apply in some cases (for example in the case of and IDEN which
can be a label or a expression). This imply have to read the full token, and
maybe save the content of yytex, for later restauring, and some ugly stricks
in next() function (it has to test it there is some previous token due to
ahead()).

Instead of doing the test of the token we can test only the next character
(after skipping blanks of course), because this will be enough (for example
in the label case, next character must be a ':'), and we don't have to worry
about yytext or near calls to next() (character read is returned to the
stream using ungetc).

Diffstat:
lex.c | 20+++++++-------------
tokens.h | 7+++++--
2 files changed, 12 insertions(+), 15 deletions(-)

diff --git a/lex.c b/lex.c @@ -17,7 +17,6 @@ unsigned columnum; const char *filename; static FILE *yyin; -static unsigned char aheadtok = NOTOK; static char number(void) @@ -153,10 +152,7 @@ next(void) { register unsigned char c; - if (aheadtok != NOTOK) { - yytoken = aheadtok; - aheadtok = NOTOK; - } else if (!skip()) { + if (!skip()) { yytoken = EOFTOK; } else { ungetc(c = getc(yyin), yyin); @@ -169,16 +165,14 @@ next(void) } } -unsigned char -ahead(void) +bool +ahead(register char tok) { - static unsigned char oldtok; + register char c; - oldtok = yytoken; - next(); - aheadtok = yytoken; - yytoken = oldtok; - return aheadtok; + skip(); + ungetc(c = getc(yyin), yyin); + return c == tok; } char diff --git a/tokens.h b/tokens.h @@ -1,6 +1,10 @@ #ifndef TOKENS_H #define TOKENS_H +#if ! __bool_true_false_are_defined +# include <stdbool.h> +#endif + /* Don't change this codification because program used it!!! */ enum tokens { @@ -35,7 +39,6 @@ extern union yyval yyval; extern char yytext[]; extern size_t yylen; extern unsigned char yytoken; -extern union yyval yyval; extern void init_lex(void); @@ -43,5 +46,5 @@ extern void next(void); extern char accept(unsigned char tok); extern void expect(unsigned char tok); extern void init_keywords(void); -extern unsigned char ahead(void); +extern bool ahead(char c); #endif