ref: dc982aab2e3804e8b780fe3a04767d54c8695f5b
parent: aa6b39201b88573be81bdadf82bd8c315221fa88
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Sun Aug 26 16:29:44 EDT 2012
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).
--- a/lex.c
+++ b/lex.c
@@ -17,7 +17,6 @@
const char *filename;
static FILE *yyin;
-static unsigned char aheadtok = NOTOK;
static char
number(void)
@@ -153,10 +152,7 @@
{
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 @@
}
}
-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
--- a/tokens.h
+++ b/tokens.h
@@ -1,7 +1,11 @@
#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 {
/* types */
@@ -35,7 +39,6 @@
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 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
--
⑨