-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.cc
More file actions
101 lines (88 loc) · 2.41 KB
/
Copy pathparser.cc
File metadata and controls
101 lines (88 loc) · 2.41 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include "parser.h"
#include "treeNode.h"
#include "tokenizer.h"
#include<iostream>
#include<stdio.h>
FormulaParser::FormulaParser(std::string ln)
{
this->tknzr = new Tokenizer(ln);
std::vector<Token> ftoken = tknzr->getTokens();
}
TreeNode * FormulaParser::getTreeRoot()
{
// your code starts here
TreeNode* Root= parseFormula();
///TreeNode* test;
// test->printDFS_post(Root);
if (tknzr->hasToken())
{
throw std::invalid_argument("invalid input");
}
return Root;
}
TreeNode *FormulaParser::parseFormula()
{
// your code starts here
TreeNode *leftchildconj =parseConjTerm();
while(tknzr->hasToken()&&tknzr->getToken().content=="+")
{ TreeNode* ornode= new OperatorNode("+");
tknzr->advanceToken();
TreeNode* Rightchildconj = parseFormula();
ornode->updateChildren(leftchildconj,Rightchildconj);
return ornode;
}
return leftchildconj;
}
TreeNode *FormulaParser::parseConjTerm() {
// your code starts here
TreeNode* leftchildterm = parseTerm();
while(tknzr->hasToken()&& tknzr->getToken().content=="*")
{ tknzr->advanceToken();
TreeNode* andnode = new OperatorNode("*");
TreeNode* Rightchildterm = parseConjTerm();
andnode->updateChildren(leftchildterm,Rightchildterm);
return andnode;
}
return leftchildterm;
}
TreeNode *FormulaParser::parseTerm()
{
// your code starts here
if (tknzr->hasToken()&&tknzr->getToken().content=="(")
{ tknzr->advanceToken();
TreeNode* leftchildfinal= parseFormula();
if (tknzr->getToken().content==")")
{
tknzr->advanceToken();
return leftchildfinal;
}
else
throw std::invalid_argument("invalid input");
}
else if (tknzr->hasToken()&&tknzr->getToken().type=="Constant")
{ TreeNode* constnode= new ConstantNode(tknzr->getToken().content);
tknzr->advanceToken();
return constnode;
}
else if(tknzr->hasToken()&&tknzr->getToken().type=="VarName")
{ TreeNode* varnode=new VariableNode(tknzr->getToken().content);
tknzr->advanceToken();
return varnode;
}
else if (tknzr->hasToken()&& tknzr->getToken().content=="-")
{
TreeNode* notNode = new OperatorNode(tknzr->getToken().content);
tknzr->advanceToken();
TreeNode* term= parseTerm();
notNode->updateLeftChild(term);
return notNode;
}
else
{
throw std::invalid_argument("invalid input");
}
}
FormulaParser::~FormulaParser() {
// your code starts here
delete tknzr;
}