-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdriver.cpp
72 lines (62 loc) · 1.26 KB
/
driver.cpp
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
70
71
/*
* @name driver.cpp
* @description A driver for parse program
* @date 03/31/2020
* @author Lrq
*/
#include <iostream>
#include <unistd.h>
#include "bison.tab.hxx"
void usage();
int main(int argc, char** argv)
{
/* Get command from shell */
int eval = 0;
int postfix = 0;
char opt;
while((opt = getopt(argc, argv, "hep")) != -1){
switch (opt){
case 'h':
usage();
break;
case 'e':
eval = 1;
break;
case 'p':
postfix = 1;
break;
default:
usage();
}
}
/* No argument */
if(eval == 0 && postfix == 0) usage();
/* Parse */
Expression *root;
yy::parser parser(&root);
parser.parse();
if(root == NULL){
std::cerr << "error!!" << std::endl;
exit(0);
}
/* Analyse Expression */
if(eval == 1){
EvalVisitor* ev = new EvalVisitor();
double v = root->accept(ev);
std::cout << "By Visitor Pattern: Value=" << v << std::endl;
}
if(postfix == 1){
PostfixVisitor* pv = new PostfixVisitor();
std::cout << "Postfix Expression:";
root->accept(pv);
std::cout << std::endl;
}
return 0;
}
void usage(){
printf("Usage: target [-hei]\n");
printf(" -h print this message\n");
printf(" -e print evaluation of target expression\n");
printf(" -p print postfix of target expression\n");
exit(1);
}