1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
|
/* ----------------------------------------------------------------------
* FILE: lexer.c
* PACKAGE: as31 - 8031/8051 Assembler.
*
* DESCRIPTION:
* This file contains the lexical tokenizer for the assembler.
* Since yacc is being used the lexer is called yylex().
*
* In order to produce a listing, some record of the users
* source line must be kept. This is done by adding
* get_ch(), and unget_ch() routine which returns/ungets a character
* but also places information into a secret array.
*
* When a newline is encountered the text line is returned as
* an attribute on the '\n' character.
*
* REVISION HISTORY:
* Jan. 19, 1990 - Created. (Ken Stauffer)
*
* AUTHOR:
* All code in this file written by Ken Stauffer (University of Calgary).
* January, 1990.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include "as31.h"
#include "parser.h"
int lineno;
int last_token_len=0;
char last_token[256], prev_token[256];
static char line[100];
static char *lineptr=line;
/* ----------------------------------------------------------------------
* get_ch:
* Get a character from stdin, place char in line[]
*/
int get_ch(void)
{
register int c;
c = getchar();
if (last_token_len >=0 && last_token_len < 254 &&
c != EOF && (last_token_len > 0 || !isspace(c))) {
last_token[last_token_len++] = c;
}
if( c != EOF && lineptr - line < sizeof(line) )
*lineptr++ = c;
return(c);
}
/* ----------------------------------------------------------------------
* unget_ch:
* Unget a character and move lineptr back by one.
*/
void unget_ch(int c)
{
ungetc(c,stdin);
if( lineptr > line )
lineptr--;
}
/* when there is a parse error, yyparse should call here to */
/* advance to the end of the current line, so we can continue */
/* parsing the rest of the file */
int seek_eol(void)
{
int c;
last_token_len = -1;
do {
c = get_ch();
} while (c != '\n' && c != EOF);
if (c == '\n') {
unget_ch(c);
return 1;
}
return 0;
}
/* return a pointer to the last successfully parsed token, so */
/* we can give a nicer error message when there is a syntax error */
const char *get_last_token(void)
{
/*
if (last_token_len < 0) last_token_len = 0;
last_token[last_token_len] = '\0';
*/
return prev_token;
}
/* ----------------------------------------------------------------------
* yylex:
* The tokens themselves are returned via return(token)
*
* Some tokens have attributes. These attributes are returned
* by setting a global variable yylval:
*
* yylval.value
* numbers (any base)
* strings (in pass 1).
* bit positions .0, .1, .2, ...
*
* yylval.str
* strings (in pass 2).
* '\n' (both passes).
*
* yylval.sym
* User defined symbols.
*
* yylval.op
* Reserved keyword (opcode/directive/misc.)
*
* No other fields in yylval are used by yylex().
*
* Characters that do not have an attribute do
* not set anything in the yylval variable.
*
*/
int yylex(void)
{
static int nl_flag=0; /* sync. error messages and the cur. line */
register int c;
char buf[1024]; /* temporary buffer */
char *p; /* general pointer */
struct symbol *sym;
struct opcode *op;
int octal=0,hex=0,decimal=0,binary=0;
register long value = 0;
if (last_token_len > 0) {
last_token[last_token_len] = '\0';
strcpy(prev_token, last_token);
} else {
*prev_token = '\0';
}
last_token_len=0;
if( nl_flag ) {
nl_flag = 0;
lineno++;
}
for(;;) {
c = get_ch();
switch(c) {
case EOF: return(EOF);
case ' ':
case '\r':
case '\t':
break;
case '\n':
nl_flag = 1;
yylval.str = line;
*lineptr = '\0';
lineptr = line;
return('\n');
case ';':
while((c=get_ch()) != EOF && c!='\n');
nl_flag= 1;
yylval.str = line;
*lineptr = '\0';
lineptr = line;
return(c);
case '"':
p = buf;
while((c=get_ch()) != EOF && c!='"' && c!='\n') {
if( c == '\\' ) {
switch(c=get_ch()) {
case 'n': c = '\n'; break;
case 'r': c = '\r'; break;
case 't': c = '\t'; break;
case 'b': c = '\b'; break;
case '"': c = '"'; break;
case '\\': c = '\\'; break;
default:
warn("Invalid escape character: \\%c",c);
}
}
if( p-buf<sizeof(buf)-1 ) {
*p++ = c;
} else {
error("String constant longer than %d bytes",
sizeof(buf));
}
}
*p = '\0';
if( c == '\n' || c == EOF ) {
warn("String terminated improperly");
unget_ch(c);
}
if (pass1) {
yylval.value = strlen(buf);
} else {
p = (char *)malloc(strlen(buf) + 1);
if (p == NULL) {
error("Cannot allocate %d bytes",
strlen(buf) + 1);
yylval.str = "as31 ran out of memory!";
return(STRING);
}
strcpy(p, buf);
yylval.str = p;
}
return(STRING);
case '.':
if( (c=get_ch())>='0' && c<='7' ) {
yylval.value = c-'0';
return(BITPOS);
}
unget_ch(c);
return('.');
case '\'':
c = get_ch();
if( c=='\\' ) {
switch(c=get_ch()) {
case 'n': c = '\n'; break;
case 'r': c = '\r'; break;
case 't': c = '\t'; break;
case 'b': c = '\b'; break;
case '0': c = 0; break;
case 'o': c = 0; break;
case 'O': c = 0; break;
case '\\': c = '\\'; break;
case '\'': c = '\''; break;
default:
warn("Invalid escape character: \\%c",c);
}
}
if( get_ch() != '\'' )
warn("Missing quote in character constant");
yylval.value = c;
return(VALUE);
case '0': /* parse a number */
case '1': /* could be followed by a: */
case '2': /* 'b','B' - Binary */
case '3': /* 'h','H' - Hex */
case '4': /* 'd','D' - Decimal */
case '5': /* 'o','O' - Octal */
case '6': /* *** Numbers must start with a digit */
case '7': /* Numbers could be also preceeded by: */
case '8': /* 0x - Hex */
case '9': /* 0b - binary */
p = buf;
do {
if( p-buf<sizeof(buf)-1 )
*p++ = c;
c = get_ch();
} while( c=='H' || c=='h' || c=='O' || c=='o' ||
c=='x' || c=='X' || isxdigit(c) );
unget_ch(c);
*p = '\0';
/* Check any preceeding chars */
if( buf[0]=='0' && (buf[1]=='x' || buf[1]=='X') ) {
hex++;
buf[1] = '0';
} else {
if( buf[0]=='0' &&
(buf[1]=='b' || buf[1]=='B') &&
*(p-1) != 'h' && *(p-1) != 'H') {
binary++;
buf[1] = '0';
}
}
/* check any trailing chars */
c = *(p-1);
if( !hex && (c=='b' || c=='B') )
{ binary++; *(p-1) = '\0'; }
else if( c=='H' || c=='h' )
{ hex++; *(p-1) = '\0'; }
else if( !hex && (c=='D' || c=='d') )
{ decimal++; *(p-1) = '\0'; }
else if( c=='O' || c=='o' )
{ octal++; *(p-1) = '\0'; }
else if( !hex && !octal && !binary) decimal++;
if (binary) {
if (hex) warn("ambiguous number, bin or hex");
if (decimal) warn("ambiguous number, bin or dec");
if (octal) warn("ambiguous number, bin or oct");
for(p=buf; *p; p++ ) {
if( *p=='1' ) value = value * 2 + 1;
else if( *p=='0' ) value = value * 2;
else
warn("Invalid binary digit: %c",*p);
}
yylval.value = value;
return(VALUE);
}
if (hex) {
if (binary) warn("ambiguous number, hex or bin");
if (decimal) warn("ambiguous number, hex or dec");
if (octal) warn("ambiguous number, hex or oct");
for(p=buf; *p; p++ ) {
value <<= 4;
if( isdigit(*p) )
value += *p-'0';
else if( *p>='a' && *p<='f' )
value += *p-'a'+ 10;
else if( *p>='A' && *p<='F' )
value += *p-'A'+ 10;
else
warn("Invalid hex digit: %c",*p);
}
yylval.value = value;
return(VALUE);
}
if (decimal) {
if (hex) warn("ambiguous number, dec or hex");
if (binary) warn("ambiguous number, dec or bin");
if (octal) warn("ambiguous number, dec or oct");
for(p=buf; *p; p++ ) {
if( isdigit(*p) )
value = value*10 + *p-'0';
else
warn("Invalid decimal digit: %c",*p);
}
yylval.value = value;
return(VALUE);
}
if (octal) {
if (hex) warn("ambiguous number, oct or hex");
if (binary) warn("ambiguous number, oct or bin");
if (decimal) warn("ambiguous number, oct or dec");
for(p=buf; *p; p++ ) {
if( *p>='0' && *p<='7' )
value = (value << 3) + (*p - '0');
else
warn("Invalid octal digit: %c",*p);
}
yylval.value = value;
return(VALUE);
}
default:
if( isalpha(c) || c=='_' ) {
p = buf;
do {
if( p-buf<sizeof(buf)-1 )
*p++ = c;
c = get_ch();
} while( isalnum(c) || c=='_' );
*p = '\0';
unget_ch(c);
if ( (op = lookop(buf)) != NULL ) {
yylval.op = op;
return(op->type);
}
sym = looksym(buf);
yylval.sym = sym;
return(SYMBOL);
} else
return(c);
} /* switch */
} /* for */
} /* yylex */
|