-
Notifications
You must be signed in to change notification settings - Fork 1
/
Test.c
67 lines (62 loc) · 1.72 KB
/
Test.c
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
66
67
/*** Compiler Front-End Test automatically generated by the BNF Converter ***/
/* */
/* This test will parse a file, print the abstract syntax tree, and then */
/* pretty-print the result. */
/* */
/****************************************************************************/
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "Parser.h"
#include "Printer.h"
#include "Absyn.h"
#include "CheckDoge.h"
int main(int argc, char ** argv)
{
FILE *input;
FILE *output;
Program parse_tree;
char *str;
char *fileName;
if (argc > 1)
{
input = fopen(argv[1], "r");
if (!input)
{
fprintf(stderr, "Error opening input file.\n");
exit(1);
}
}
else input = stdin;
/* The default entry point is used. For other options see Parser.h */
parse_tree = pProgram(input);
if (parse_tree)
{
printf("\nParse Succesful!\n");
printf("\n[Abstract Syntax]\n");
printf("%s\n\n", showProgram(parse_tree));
printf("Parsing AST for doge now...\n");
visitProgram(parse_tree);
printf("Done!\n");
str = printProgram(parse_tree);
if (input != stdin)
{
fileName = strdup(argv[1]);
fileName = realloc(fileName, strlen(fileName) + 3);
strcat(fileName, ".c");
output = fopen(fileName, "w");
fwrite(str, strlen(str), 1, output);
printf("Wrote program to %s\n", fileName);
free(fileName);
}
else
{
printf("[Linearized Tree]\n");
printf("%s\n\n", str);
}
if (str)
free(str);
return 0;
}
return 1;
}