From 4d75955e833201c4f1d0219d48676521aeee1afa Mon Sep 17 00:00:00 2001 From: Sergey Poznyakoff Date: Mon, 9 Nov 2009 00:04:44 +0200 Subject: Fix parsing of typedefs after `struct'. * src/c.l: Include cflow.h (and, consequently, config.h) at the top of the generated source. (prev_token): New static. (get_token): Set prev_token. (ident): Treat any valid identifier after struct/union/enum as identifier (do not attempt any symbol lookup). --- src/c.l | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/src/c.l b/src/c.l index 05f2d6e..d64c8f1 100644 --- a/src/c.l +++ b/src/c.l @@ -16,14 +16,17 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ +%top { +#include +#include +#include +} + %x comment %x string %x stringwait %x longline %{ -#include -#include -#include struct obstack string_stk; @@ -37,6 +40,11 @@ int ident(); void update_loc(); #define lex_error(msg) error_at_line(0, 0, filename, line_num, "%s", msg) +/* Keep the token returned at the previous call to yylex. This is used + as a lexical tie-in to ensure that the next token after STRUCT is + IDENTIFIER. See get_token and ident below. */ +static int prev_token; + %} FILENAME [^\n*?]* ONUMBER (0[0-7]*) @@ -221,12 +229,20 @@ init_lex(int debug_level) int ident() { - Symbol *sp; + /* Do not attempt any symbol table lookup if the previous token was + STRUCT. This helps properly parse constructs like: - sp = lookup(yytext); - if (sp && sp->type == SymToken) { - yylval.str = sp->name; - return sp->token_type; + typedef struct foo foo; + struct foo { + int dummy; + }; + */ + if (prev_token != STRUCT) { + Symbol *sp = lookup(yytext); + if (sp && sp->type == SymToken) { + yylval.str = sp->name; + return sp->token_type; + } } obstack_grow(&string_stk, yytext, yyleng); obstack_1grow(&string_stk, 0); @@ -320,7 +336,7 @@ yywrap() fclose(yyin); yyin = NULL; #ifdef FLEX_SCANNER - yy_delete_buffer(yy_current_buffer); + yy_delete_buffer(YY_CURRENT_BUFFER); #endif delete_statics(); return 1; @@ -329,7 +345,9 @@ yywrap() int get_token() { - return yyin ? yylex() : 0; + int tok = yylex(); + prev_token = tok; + return tok; } int -- cgit v1.2.1