* ========== Copyright Header Begin ==========================================
* OpenSPARC T2 Processor File: lexer.l
* Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES.
* The above named program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License version 2 as published by the Free Software Foundation.
* The above named program is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
* You should have received a copy of the GNU General Public
* License along with this work; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
* ========== Copyright Header End ============================================
/* any C includes here */
lexer_t lex; /* additional return value info */
DECNUM ("0"|([1-9][0-9]*))
if (sscanf(lextext,"# %d \"%[^\"]\"", &num, lbuf)!=2)
lex_fatal("Illegal # directive");
if (lex.fnamep != (char*)0) Xfree(lex.fnamep);
lex.fnamep = Xstrdup(lbuf);
<string>\" BEGIN(INITIAL);
<string>([^"\n]|(\\\"))* {
lex_fatal("unterminated string");
lex.val = strtoull(lextext, (char **)NULL, 16);
lex.val = strtoull(lextext, (char **)NULL, 16);
lex.val = atoll(lextext);
lex.val = strtoull(lextext, (char **)NULL, 16);
lex.val = strtoull(lextext, (char **)NULL, 16);
lex.val = strtoull(lextext, &ep, 10);
lex_fatal("parsing number");
/* Note: . = any character EXCEPT newline */
[\t ]* { /* nada - swallow white space */ }
lex_fatal("Illegal character %s", lextext);
void init_lexer(char * fnamep, FILE *fp, char * cleanup_filep)
lex.cleanup_filep = cleanup_filep ? Xstrdup(cleanup_filep) : (char*)0;
lex.fnamep = Xstrdup(fnamep);
lex.ungot_available = false;
lex.last_token = T_Error;
lexer_tok_t lex_get_token()
if (lex.ungot_available) {
lex.ungot_available = false;
lex.last_token = lexlex();
if (lex.ungot_available) fatal("Internal error, lex_unget with token already ungot");
lex.ungot_available = true;
void lex_get(lexer_tok_t expected)
if (tok == expected) return;
case T_EOF: s="end of file"; break;
case T_L_Brace: s="{"; break;
case T_R_Brace: s="}"; break;
case T_S_Colon: s=";"; break;
case T_Plus: s="+"; break;
case T_Minus: s="-"; break;
case T_Number: s="number"; break;
case T_HexString: s="hex string"; break;
case T_String: s="string"; break;
sprintf(buffer,"token %s", lex.strp);
case T_Error: s="error"; break;
s="unknown token - internal error";
lex_fatal("unexpected %s", s);
* Special version of fatal for the lexer
* to enable cleanup of stuff before death
void lex_fatal(char * fmt, ...)
if (errno!=0) perror("FATAL: "); else fprintf(stderr,"FATAL: ");
(void)vfprintf(stderr, fmt, args);
if (lex.cleanup_filep != (char *)0) {
unlink(lex.cleanup_filep);
Xfree(lex.cleanup_filep);
fprintf(stderr," at line %d of %s\n", lex.linenum, lex.fnamep);
SANITY( lex.fnamep = (char*)0; );
SANITY( lex.cleanup_filep = (char*)0; );
fprintf(stderr,"token = %d at line %d in %s\n",tok, lex.linenum, lex.fnamep);
} while (tok!=T_Error && tok!=T_EOF);
void fatal(char* fmt, ...)
if (errno!=0) perror("FATAL: "); else fprintf(stderr,"FATAL: ");
(void)vfprintf(stderr, fmt, args);