-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExpression.cpp
More file actions
103 lines (97 loc) · 1.86 KB
/
Expression.cpp
File metadata and controls
103 lines (97 loc) · 1.86 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
#include "Expression.h"
#include "Token.h"
#include "BinaryTree.h"
void Expression::IsValidContext(Context context)
{
if(context.GetBegin()==NULL||context.GetEnd()==NULL)
{
throw std::bad_typeid("null context");
}
}
const char* Expression::FindOccurrenceOf(const char *symbol, Context context)
{
unsigned index = 0;
const char* hit = NULL;
if(context.GetBegin()==NULL)
{
throw std::bad_typeid("error in trying to search a null string");
}
while((context.GetBegin()+index)!= context.GetEnd()&&*(context.GetBegin()+index)!='\0')
{
if(*(context.GetBegin()+index)==*symbol)
{
hit = context.GetBegin()+index;
break;
}
else
index++;
}
return hit;
}
const char* Expression::FindOccurenceOfStr(std::string *str,Context context)
{
const char* result = NULL;
bool isMatch = true;
int i=0;
for(;context.GetBegin()+i!=context.GetEnd()&&isMatch==true;i++)
{
if(i>=str->length())
{
isMatch = false;
}
else if(*(context.GetBegin()+i) != (*str)[i])
{
isMatch = false;
}
}
if(isMatch)
{
result = context.GetBegin()+i;
}
return result;
}
void Expression::SafeDelete(Expression *re)
{
if(re != NULL)
{
delete re;
re = NULL;
}
}
void Expression::UpdateContext(Context &context)
{
bool found_end = false;
const char* traverse = context.GetBegin();
for(;traverse!=context.GetEnd() && found_end != true;traverse++)
{
if(*traverse == *_terminatingChar)
{
found_end = true;
}
}
if(found_end==false)
throw std::invalid_argument("error:could not find terminating character");
else
{
context.SetBegin(traverse+1);
}
}
TerminalExpression::TerminalExpression()
{
}
TerminalExpression::~TerminalExpression()
{
if(_literal != NULL)
{
delete _literal;
_literal = NULL;
}
}
NonTerminalExpression::NonTerminalExpression()
{
_nonterminalExpression = NULL;
_terminalExpression = NULL;
}
NonTerminalExpression::~NonTerminalExpression()
{
}