Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
50 changed files
with
23,787 additions
and
50 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# | ||
# makefile for TINY | ||
# Borland C Version | ||
# K. Louden 2/3/98 | ||
# | ||
|
||
CC = gcc | ||
|
||
CFLAGS = -g | ||
|
||
OBJS = main.o util.o scan.o parse.o symtab.o analyze.o code.o cgen.o | ||
|
||
tiny: $(OBJS) | ||
$(CC) $(CFLAGS) $(OBJS) -o tiny | ||
|
||
main.o: main.c globals.h util.h scan.h parse.h analyze.h cgen.h | ||
$(CC) $(CFLAGS) -c main.c | ||
|
||
util.o: util.c util.h globals.h | ||
$(CC) $(CFLAGS) -c util.c | ||
|
||
scan.o: scan.c scan.h util.h globals.h | ||
$(CC) $(CFLAGS) -c scan.c | ||
|
||
parse.o: parse.c parse.h scan.h globals.h util.h | ||
$(CC) $(CFLAGS) -c parse.c | ||
|
||
symtab.o: symtab.c symtab.h | ||
$(CC) $(CFLAGS) -c symtab.c | ||
|
||
analyze.o: analyze.c globals.h symtab.h analyze.h | ||
$(CC) $(CFLAGS) -c analyze.c | ||
|
||
code.o: code.c code.h globals.h | ||
$(CC) $(CFLAGS) -c code.c | ||
|
||
cgen.o: cgen.c globals.h symtab.h code.h cgen.h | ||
$(CC) $(CFLAGS) -c cgen.c | ||
|
||
clean: | ||
-rm tiny | ||
-rm tm | ||
-rm main.o | ||
-rm util.o | ||
-rm scan.o | ||
-rm parse.o | ||
-rm symtab.o | ||
-rm analyze.o | ||
-rm code.o | ||
-rm cgen.o | ||
-rm tm.o | ||
|
||
tm: tm.c | ||
$(CC) $(CFLAGS) tm.c -o tm | ||
|
||
all: tiny tm | ||
|
48 changes: 48 additions & 0 deletions
48
compiler-construction/compiler-construction-code/README.DOS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
This is the README.DOS file included in the loucomp.zip file. | ||
The other files in the distribution are the source code files | ||
in standard C for the TINY compiler and Tiny Machine simulator | ||
as described in the text: | ||
|
||
Compiler Construction - Principles and Practice, by Kenneth C. Louden, | ||
PWS Publishing Co., 1997. | ||
|
||
They are (with very minor variations) all the files as listed in Appendices | ||
B and C of the text. | ||
|
||
At the top level are: | ||
|
||
The file you are now reading (README.DOS): | ||
|
||
The sixteen files as listed on page 23 of the text (which are | ||
used to make the TINY compiler) | ||
|
||
The tm.c source code file for the TM machine interpreter | ||
|
||
The TINY program of Figure 1.4, page 23 (called sample.tny) | ||
|
||
Two DOS executables tiny.exe and tm.exe for your convenience. | ||
|
||
A Makefile for the TINY compiler and TM simulator for Borland's | ||
MAKE utility (with minor edits this should work for other MAKEs). | ||
|
||
There are also two subdirectories: lex and yacc (assuming you have | ||
unzipped with the -d option). | ||
|
||
The lex subdirectory contains the single file tiny.l | ||
as described in the text on pages 90-91, which can be used to build | ||
a lex/flex version of the scanner. | ||
|
||
The yacc subdirectory contains the two files tiny.y and globals.h | ||
as described on pages 243-245 of the text, which can be used | ||
to build a Yacc/Bison version of the parser. Note that, due to | ||
the considerable variation among Yacc/Bison versions, these files | ||
will probably need some minor editing in order to get them to | ||
work correctly. | ||
|
||
All source code has been tested with the Borland 3.0 and 4.0 compilers, | ||
as well as with the Gnu C compiler and the Sun Ansi C compiler (version 2.0). | ||
Any Ansi C compiler should be usable to compile this code, but there is | ||
no guarantee that your favorite compiler will work. Nevertheless, if you | ||
have a problem, I would appreciate hearing about it, and I will respond if I | ||
can find a solution. | ||
|
120 changes: 120 additions & 0 deletions
120
compiler-construction/compiler-construction-code/YACC/GLOBALS.H
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
/****************************************************/ | ||
/* File: globals.h */ | ||
/* Yacc/Bison Version */ | ||
/* Global types and vars for TINY compiler */ | ||
/* must come before other include files */ | ||
/* Compiler Construction: Principles and Practice */ | ||
/* Kenneth C. Louden */ | ||
/****************************************************/ | ||
|
||
#ifndef _GLOBALS_H_ | ||
#define _GLOBALS_H_ | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <ctype.h> | ||
#include <string.h> | ||
|
||
/* Yacc/Bison generates internally its own values | ||
* for the tokens. Other files can access these values | ||
* by including the tab.h file generated using the | ||
* Yacc/Bison option -d ("generate header") | ||
* | ||
* The YYPARSER flag prevents inclusion of the tab.h | ||
* into the Yacc/Bison output itself | ||
*/ | ||
|
||
#ifndef YYPARSER | ||
|
||
/* the name of the following file may change */ | ||
#include "y.tab.h" | ||
|
||
/* ENDFILE is implicitly defined by Yacc/Bison, | ||
* and not included in the tab.h file | ||
*/ | ||
#define ENDFILE 0 | ||
|
||
#endif | ||
|
||
#ifndef FALSE | ||
#define FALSE 0 | ||
#endif | ||
|
||
#ifndef TRUE | ||
#define TRUE 1 | ||
#endif | ||
|
||
/* MAXRESERVED = the number of reserved words */ | ||
#define MAXRESERVED 8 | ||
|
||
/* Yacc/Bison generates its own integer values | ||
* for tokens | ||
*/ | ||
typedef int TokenType; | ||
|
||
extern FILE* source; /* source code text file */ | ||
extern FILE* listing; /* listing output text file */ | ||
extern FILE* code; /* code text file for TM simulator */ | ||
|
||
extern int lineno; /* source line number for listing */ | ||
|
||
/**************************************************/ | ||
/*********** Syntax tree for parsing ************/ | ||
/**************************************************/ | ||
|
||
typedef enum {StmtK,ExpK} NodeKind; | ||
typedef enum {IfK,RepeatK,AssignK,ReadK,WriteK} StmtKind; | ||
typedef enum {OpK,ConstK,IdK} ExpKind; | ||
|
||
/* ExpType is used for type checking */ | ||
typedef enum {Void,Integer,Boolean} ExpType; | ||
|
||
#define MAXCHILDREN 3 | ||
|
||
typedef struct treeNode | ||
{ struct treeNode * child[MAXCHILDREN]; | ||
struct treeNode * sibling; | ||
int lineno; | ||
NodeKind nodekind; | ||
union { StmtKind stmt; ExpKind exp;} kind; | ||
union { TokenType op; | ||
int val; | ||
char * name; } attr; | ||
ExpType type; /* for type checking of exps */ | ||
} TreeNode; | ||
|
||
/**************************************************/ | ||
/*********** Flags for tracing ************/ | ||
/**************************************************/ | ||
|
||
/* EchoSource = TRUE causes the source program to | ||
* be echoed to the listing file with line numbers | ||
* during parsing | ||
*/ | ||
extern int EchoSource; | ||
|
||
/* TraceScan = TRUE causes token information to be | ||
* printed to the listing file as each token is | ||
* recognized by the scanner | ||
*/ | ||
extern int TraceScan; | ||
|
||
/* TraceParse = TRUE causes the syntax tree to be | ||
* printed to the listing file in linearized form | ||
* (using indents for children) | ||
*/ | ||
extern int TraceParse; | ||
|
||
/* TraceAnalyze = TRUE causes symbol table inserts | ||
* and lookups to be reported to the listing file | ||
*/ | ||
extern int TraceAnalyze; | ||
|
||
/* TraceCode = TRUE causes comments to be written | ||
* to the TM code file as code is generated | ||
*/ | ||
extern int TraceCode; | ||
|
||
/* Error = TRUE prevents further passes if an error occurs */ | ||
extern int Error; | ||
#endif |
163 changes: 163 additions & 0 deletions
163
compiler-construction/compiler-construction-code/YACC/TINY.Y
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
/****************************************************/ | ||
/* File: tiny.y */ | ||
/* The TINY Yacc/Bison specification file */ | ||
/* Compiler Construction: Principles and Practice */ | ||
/* Kenneth C. Louden */ | ||
/****************************************************/ | ||
%{ | ||
#define YYPARSER /* distinguishes Yacc output from other code files */ | ||
|
||
#include "globals.h" | ||
#include "util.h" | ||
#include "scan.h" | ||
#include "parse.h" | ||
|
||
#define YYSTYPE TreeNode * | ||
static char * savedName; /* for use in assignments */ | ||
static int savedLineNo; /* ditto */ | ||
static TreeNode * savedTree; /* stores syntax tree for later return */ | ||
|
||
%} | ||
|
||
%token IF THEN ELSE END REPEAT UNTIL READ WRITE | ||
%token ID NUM | ||
%token ASSIGN EQ LT PLUS MINUS TIMES OVER LPAREN RPAREN SEMI | ||
%token ERROR | ||
|
||
%% /* Grammar for TINY */ | ||
|
||
program : stmt_seq | ||
{ savedTree = $1;} | ||
; | ||
stmt_seq : stmt_seq SEMI stmt | ||
{ YYSTYPE t = $1; | ||
if (t != NULL) | ||
{ while (t->sibling != NULL) | ||
t = t->sibling; | ||
t->sibling = $3; | ||
$$ = $1; } | ||
else $$ = $3; | ||
} | ||
| stmt { $$ = $1; } | ||
; | ||
stmt : if_stmt { $$ = $1; } | ||
| repeat_stmt { $$ = $1; } | ||
| assign_stmt { $$ = $1; } | ||
| read_stmt { $$ = $1; } | ||
| write_stmt { $$ = $1; } | ||
| error { $$ = NULL; } | ||
; | ||
if_stmt : IF exp THEN stmt_seq END | ||
{ $$ = newStmtNode(IfK); | ||
$$->child[0] = $2; | ||
$$->child[1] = $4; | ||
} | ||
| IF exp THEN stmt_seq ELSE stmt_seq END | ||
{ $$ = newStmtNode(IfK); | ||
$$->child[0] = $2; | ||
$$->child[1] = $4; | ||
$$->child[2] = $6; | ||
} | ||
; | ||
repeat_stmt : REPEAT stmt_seq UNTIL exp | ||
{ $$ = newStmtNode(RepeatK); | ||
$$->child[0] = $2; | ||
$$->child[1] = $4; | ||
} | ||
; | ||
assign_stmt : ID { savedName = copyString(tokenString); | ||
savedLineNo = lineno; } | ||
ASSIGN exp | ||
{ $$ = newStmtNode(AssignK); | ||
$$->child[0] = $4; | ||
$$->attr.name = savedName; | ||
$$->lineno = savedLineNo; | ||
} | ||
; | ||
read_stmt : READ ID | ||
{ $$ = newStmtNode(ReadK); | ||
$$->attr.name = | ||
copyString(tokenString); | ||
} | ||
; | ||
write_stmt : WRITE exp | ||
{ $$ = newStmtNode(WriteK); | ||
$$->child[0] = $2; | ||
} | ||
; | ||
exp : simple_exp LT simple_exp | ||
{ $$ = newExpNode(OpK); | ||
$$->child[0] = $1; | ||
$$->child[1] = $3; | ||
$$->attr.op = LT; | ||
} | ||
| simple_exp EQ simple_exp | ||
{ $$ = newExpNode(OpK); | ||
$$->child[0] = $1; | ||
$$->child[1] = $3; | ||
$$->attr.op = EQ; | ||
} | ||
| simple_exp { $$ = $1; } | ||
; | ||
simple_exp : simple_exp PLUS term | ||
{ $$ = newExpNode(OpK); | ||
$$->child[0] = $1; | ||
$$->child[1] = $3; | ||
$$->attr.op = PLUS; | ||
} | ||
| simple_exp MINUS term | ||
{ $$ = newExpNode(OpK); | ||
$$->child[0] = $1; | ||
$$->child[1] = $3; | ||
$$->attr.op = MINUS; | ||
} | ||
| term { $$ = $1; } | ||
; | ||
term : term TIMES factor | ||
{ $$ = newExpNode(OpK); | ||
$$->child[0] = $1; | ||
$$->child[1] = $3; | ||
$$->attr.op = TIMES; | ||
} | ||
| term OVER factor | ||
{ $$ = newExpNode(OpK); | ||
$$->child[0] = $1; | ||
$$->child[1] = $3; | ||
$$->attr.op = OVER; | ||
} | ||
| factor { $$ = $1; } | ||
; | ||
factor : LPAREN exp RPAREN | ||
{ $$ = $2; } | ||
| NUM | ||
{ $$ = newExpNode(ConstK); | ||
$$->attr.val = atoi(tokenString); | ||
} | ||
| ID { $$ = newExpNode(IdK); | ||
$$->attr.name = | ||
copyString(tokenString); | ||
} | ||
| error { $$ = NULL; } | ||
; | ||
|
||
%% | ||
|
||
int yyerror(char * message) | ||
{ fprintf(listing,"Syntax error at line %d: %s\n",lineno,message); | ||
fprintf(listing,"Current token: "); | ||
printToken(yychar,tokenString); | ||
Error = TRUE; | ||
return 0; | ||
} | ||
|
||
/* yylex calls getToken to make Yacc/Bison output | ||
* compatible with ealier versions of the TINY scanner | ||
*/ | ||
static int yylex(void) | ||
{ return getToken(); } | ||
|
||
TreeNode * parse(void) | ||
{ yyparse(); | ||
return savedTree; | ||
} | ||
|
Binary file not shown.
Oops, something went wrong.