This repository was archived by the owner on Feb 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
70 lines (57 loc) · 1.4 KB
/
main.cpp
File metadata and controls
70 lines (57 loc) · 1.4 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
66
67
68
69
// Main file for rewriting infix expressions as prefix
// and simplifying them
//
// Created by: David Bell
// April 26th, 2012
#include "expr_tree.h"
#include "visitor.h"
#include "stdio.h"
#include "stdlib.h"
#include "cstring"
// For communicating with Bison/Flex
extern int yyparse();
extern "C" FILE *yyin;
ExprTree *et;
// void printTree(ExprTree*);
// void simplifyTree(ExprTree*);
int main(int argc, char** argv)
{
bool simplify = false;
char *input_file = NULL;
// Set up state according to CL arguments
if (argc < 2) // Not enough arguments
{
printf("%s: (-r) FILENAME\n",argv[0]);
exit(1);
} else if (argc == 2) // Just a file name
{
input_file = argv[1];
} else if (argc == 3) // optional parameter and file
{
if (strcmp(argv[1],"-r")) // Not a valid parameter
{
printf("Invalid argument %s\n%s: (-r) FILENAME\n",argv[1],argv[0]);
exit(1);
}
simplify = true;
input_file = argv[2];
} else // Too many arguments!
{
printf("Too many arguments\n%s: (-r) FILENAME\n",argv[0]);
exit(1);
}
// Try and open our file argument
FILE *myfile = fopen(input_file, "r");
if (!myfile) // File does not exist
{
printf("File %s does not exist!\n",input_file);
exit(1);
}
yyin = myfile; // Sets lex to use our file as input
yyparse(); // Creates ExprTree
if (simplify) // Simplify the tree
simplifyTree(et);
// Print our tree in prefix notation
printTree(et);
printf("\n");
}