Don't read yytext
in the parser -- as you've discovered it can be unpredictable which token it contains, as the parser may read ahead a token to decide when to shift or reduce.
Instead, you should read yytext
in the lexer (and only in the lexer) and make a copy of it if you'll need the value in the parser. So you end up with a lexer rule like:
[a-zA-Z][a-zA-Z0-9]* { yylval.str = strdup(yytext); return NAME }
and in your parser:
%union {
char *str;
:
}
%token <str> NAME
:
%%
:
table: NAME { setGlobalTableName($1); }
| NAME '.' NAME ...