This code will certainly not handle all valid LC-3 assembly programs. In addition, it will not detect all invalid LC-3 assembly programs. All that it does is lexical analysis, or tokenization.
You will implement the functions scan_token
and
free_token
, as described in token.h.
Put them in the file token.c
, and turn it in.
struct Token *scan_token(FILE *);
NULL
at end of file.
Otherwise, allocate a struct Token
,
fill it in, and return a pointer to it.
void free_token(struct Token *);
scan_token()
.
#include "token.h" #include <stdio.h> int main(int argc, char *argv[]) { FILE *f = fopen("test.asm", "r"); if (f==NULL) { fprintf(stderr, "%s: can’t open test.asm.\n", argv[0]); return 1; } struct Token *t; while ((t=scan_token(f)) != NULL) { printf("Line %d: ", t->line_number); switch (t->type) { case TOKEN_INVALID: printf("invalid \"%s\"", t->str); break; case TOKEN_LINE_LABEL: printf("line label \"%s\"", t->str); break; case TOKEN_OPCODE: printf("opcode %d", t->opcode); break; case TOKEN_COMMA: printf("comma"); break; case TOKEN_INT: printf("integer %d", t->val); break; case TOKEN_REGISTER: printf("register R%d", t->val); break; case TOKEN_LABEL: printf("label \"%s\"", t->str); break; default: printf("Huh?"); break; } printf("\n"); free_token(t); } fclose(f); return 0; }When run with this wretched assembly source file:
; Test file ADD r2 , x15 ; R2, how strange! BRz AWAY r8this and #-12it should produce this output:
Line 3: opcode 0 Line 3: register R2 Line 3: comma Line 3: integer 21 Line 4: opcode 3 Line 4: label "AWAY" Line 5: line label "r8this" Line 5: opcode 1 Line 5: integer -12
Token.str
must retain its
original case for error messages.
static
.
token.h
.
We will use our own, unchanged, token.h
,
when testing your code.
;
comments are completely ignored.
'\t'
) are treated the same as spaces.
Token.line_number
and Token.type
are valid for all tokens. The other fields are only valid
for certain tokens.
Token.str
, when used, must be
dynamically allocated using malloc
or a similar
function.