Skip to content

Commit c429f39

Browse files
v1
1 parent f390fb6 commit c429f39

25 files changed

+2700
-0
lines changed

LEX/TINY.L

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/****************************************************/
2+
/* File: tiny.l */
3+
/* Lex specification for TINY */
4+
/* Compiler Construction: Principles and Practice */
5+
/* Kenneth C. Louden */
6+
/****************************************************/
7+
8+
%{
9+
#include "globals.h"
10+
#include "util.h"
11+
#include "scan.h"
12+
/* lexeme of identifier or reserved word */
13+
char tokenString[MAXTOKENLEN+1];
14+
%}
15+
16+
digit [0-9]
17+
number {digit}+
18+
letter [a-zA-Z]
19+
identifier {letter}+
20+
newline \n
21+
whitespace [ \t]+
22+
23+
%%
24+
25+
"if" {return IF;}
26+
"then" {return THEN;}
27+
"else" {return ELSE;}
28+
"end" {return END;}
29+
"repeat" {return REPEAT;}
30+
"until" {return UNTIL;}
31+
"read" {return READ;}
32+
"write" {return WRITE;}
33+
":=" {return ASSIGN;}
34+
"=" {return EQ;}
35+
"<" {return LT;}
36+
"+" {return PLUS;}
37+
"-" {return MINUS;}
38+
"*" {return TIMES;}
39+
"/" {return OVER;}
40+
"(" {return LPAREN;}
41+
")" {return RPAREN;}
42+
";" {return SEMI;}
43+
{number} {return NUM;}
44+
{identifier} {return ID;}
45+
{newline} {lineno++;}
46+
{whitespace} {/* skip whitespace */}
47+
"{" { char c;
48+
do
49+
{ c = input();
50+
if (c == EOF) break;
51+
if (c == '\n') lineno++;
52+
} while (c != '}');
53+
}
54+
. {return ERROR;}
55+
56+
%%
57+
58+
TokenType getToken(void)
59+
{ static int firstTime = TRUE;
60+
TokenType currentToken;
61+
if (firstTime)
62+
{ firstTime = FALSE;
63+
lineno++;
64+
yyin = source;
65+
yyout = listing;
66+
}
67+
currentToken = yylex();
68+
strncpy(tokenString,yytext,MAXTOKENLEN);
69+
if (TraceScan) {
70+
fprintf(listing,"\t%d: ",lineno);
71+
printToken(currentToken,tokenString);
72+
}
73+
return currentToken;
74+
}
75+

Makefile

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
CC = gcc
2+
3+
CFLAGS = -g -Wall
4+
5+
OBJS = main.o util.o scan.o parse.o symtab.o analyze.o code.o cgen.o
6+
7+
tiny: $(OBJS)
8+
$(CC) $(CFLAGS) -o tiny $(OBJS)
9+
10+
main.o: main.c globals.h util.h scan.h parse.h analyze.h cgen.h
11+
$(CC) $(CFLAGS) -c main.c
12+
13+
util.o: util.c util.h globals.h
14+
$(CC) $(CFLAGS) -c util.c
15+
16+
scan.o: scan.c scan.h util.h globals.h
17+
$(CC) $(CFLAGS) -c scan.c
18+
19+
parse.o: parse.c parse.h scan.h globals.h util.h
20+
$(CC) $(CFLAGS) -c parse.c
21+
22+
symtab.o: symtab.c symtab.h
23+
$(CC) $(CFLAGS) -c symtab.c
24+
25+
analyze.o: analyze.c globals.h symtab.h analyze.h
26+
$(CC) $(CFLAGS) -c analyze.c
27+
28+
code.o: code.c code.h globals.h
29+
$(CC) $(CFLAGS) -c code.c
30+
31+
cgen.o: cgen.c globals.h symtab.h code.h cgen.h
32+
$(CC) $(CFLAGS) -c cgen.c
33+
34+
clean:
35+
rm -rf tiny
36+
rm -rf tm
37+
rm -rf main.o
38+
rm -rf util.o
39+
rm -rf scan.o
40+
rm -rf parse.o
41+
rm -rf symtab.o
42+
rm -rf analyze.o
43+
rm -rf code.o
44+
rm -rf cgen.o
45+
rm -rf tm.o
46+
rm -rf *.o
47+
48+
tm: tm.c
49+
$(CC) $(CFLAGS) -o tm tm.c
50+
51+
all: tiny tm

0 commit comments

Comments
 (0)