-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
executable file
·65 lines (53 loc) · 1.43 KB
/
main.cpp
File metadata and controls
executable file
·65 lines (53 loc) · 1.43 KB
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
#include <stdio.h>
#include <iostream>
#include "main.h"
#include "CodeGen.h"
// Using extern to declare their existance as global variables
extern int yylex(void);
extern FILE* yyin;
extern int yyparse();
char *filename = NULL;
ast_program program_ast;
// Logs errors
void error_message(int lineno, const char *error, ...)
{
fprintf(stderr, "%s:%d: ", filename, lineno);
va_list args;
va_start (args, error);
vfprintf (stderr, error, args);
va_end (args);
fputs("\n", stderr);
exit(1);
}
// Return 1 if there is an error, otherwise return 0 for normal exit.
int main(int argc, char *argv[])
{
// get the file
filename = argv[1];
if(filename == NULL) {
fputs("usage: main [file.java]\n", stderr);
return 1;
}
// Open our passed in file. `$ ./main <filename>`
// `yyin` is a a global variable that our lexical analyzer uses to determine which file to scan
yyin = fopen(filename, "r");
if(yyin == NULL)
{
fprintf(stderr, "%s: ", argv[1]);
perror("");
return 1;
}
// Call the function yyparse to cause parsing to occur
// The value returned by yyparse is 0 if parsing was successful.
// Return 1 if unsuccessful.
if(yyparse() == 1){
return 1;
}
// Print out the AST generated for the `<file>.java`
FILE *out_file;
out_file = fopen("Output_AST.txt", "w");
ast_program_print(out_file, 0, &program_ast);
//Call **CodeGen.cpp** and pass in our parsed ast `ast_program ast`
makeProgram(program_ast);
return 0;
}