source: liacs/coco/assignment3/main.cc@ 115

Last change on this file since 115 was 2, checked in by Rick van der Zwet, 15 years ago

Initial import of data of old repository ('data') worth keeping (e.g. tracking
means of URL access statistics)

File size: 3.3 KB
RevLine 
[2]1/*
2 * main.cc
3 *
4 * This file contains some generic functions. It is part of
5 * the assignment for the Compiler Construction course at the Leiden Institute
6 * of Advanced Computer Science (LIACS), Leiden University.
7 */
8
9#include "ICGenerator.h"
10#include "SymbolTable.h"
11#include "debug.h"
12
13/* Globals */
14ICGenerator * icg;
15IntermediateCode * icode;
16bool bWarnings = true;
17bool bOptimize1 = false;
18bool extraDump = false;
19
20
21/* Extern functions */
22int yyparse();
23
24
25/* Extern variables */
26extern int lineno;
27extern SyntaxTree * tree;
28extern SymbolTable * symtab;
29extern int gErrorCount;
30extern int gWarningCount;
31extern int msglevel;
32
33
34
35
36void PrintError (char * msg) {
37 fprintf (stderr, "Error on line %d: %s\n", lineno, msg);
38 gErrorCount++;
39}//PrintError
40
41
42void PrintWarning (char * msg) {
43 if (bWarnings) {
44 fprintf (stderr, "Warning on line %d: %s\n", lineno, msg);
45 gWarningCount++;
46 }
47}//PrintWarning
48
49void usage(char * argv0) {
50 fprintf (stderr, "Usage: %s [OPTION]\n", argv0);
51 fprintf (stderr, "Defaults: %s -w -O0\n", argv0);
52 fprintf (stderr, "-h = Reading right now ;-)\n");
53 fprintf (stderr, "-w = Enable warnings\n");
54 fprintf (stderr, "-O0 = No optimalisation\n");
55 fprintf (stderr, "-O1 = Use otimalisation\n");
56 fprintf (stderr, "-g = Debug, more flags higher level\n");
57 fprintf (stderr, "-s = Dump symbolTable after ICG\n");
58}
59
60/* Parse the command line */
61int ParseCommandline(int argc, char ** argv)
62{
63 for (int i = 1; i < argc; i++) {
64 if (strcmp(argv[i], "-w") == 0) {
65 bWarnings = false;
66 }
67 else if (strcmp(argv[i], "-O0") == 0) {
68 bOptimize1 = false;
69 }
70 else if (strcmp(argv[i], "-O1") == 0) {
71 bOptimize1 = true;
72 }
73 else if (strcmp(argv[i], "-h") == 0) {
74 usage(argv[0]);
75 return 1;
76 }
77 else if (strcmp(argv[i], "-g") == 0) {
78 msglevel += 30;
79 }
80 else if (strcmp(argv[i], "-s") == 0) {
81 extraDump = true;
82 }
83 else {
84 fprintf(stderr, "Error: unrecognized command line option '%s'\n", argv[i]);
85 usage(argv[0]);
86 return 1;
87 }
88 }
89
90 return 0;
91}
92
93
94void initialize()
95{
96 /* Create & initialize the abstract data structures */
97 tree = new SyntaxTree;
98 symtab = new SymbolTable;
99 icg = new ICGenerator;
100 icode = NULL;
101
102 gErrorCount = 0;
103 gWarningCount = 0;
104}
105
106
107void cleanup()
108{
109 /* Free objects */
110 delete icg;
111 delete tree;
112 delete symtab;
113 if (icode) delete icode;
114}
115
116
117int main(int argc, char ** argv)
118{
119 int parseResult;
120
121 if (ParseCommandline(argc, argv) == 1) {
122 return 1;
123 }
124
125 initialize();
126
127 /* Start the parsing procedure */
128 parseResult = yyparse();
129
130 /* Dump the syntax tree & symbol table */
131 tree->Dump (stdout);
132 printf("\n");
133 symtab->Dump (stdout);
134
135 /* Generate intermediate code, if everything is still ok */
136 if (!(parseResult || gErrorCount)) {
137 if (bOptimize1) {
138 icg->Preprocess(tree, symtab);
139 tree->Dump (stdout);
140 }
141 icode = icg->GenerateIntermediateCode(tree, symtab);
142 if (bOptimize1) {
143 icg->Postprocess(icode, symtab);
144 }
145 icode->Dump (stdout);
146
147 /* Dump Extra symbol table at the end, to view edited variables on the symbol table */
148 if (extraDump) {
149 printf("\n");
150 symtab->Dump (stdout);
151 }
152 }
153
154 printf("%d error(s), %d warning(s)\n", gErrorCount + parseResult, gWarningCount);
155
156 cleanup();
157
158 return (parseResult || gErrorCount);
159
160}
Note: See TracBrowser for help on using the repository browser.