-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInfix_Parser.cpp
More file actions
150 lines (137 loc) · 3.16 KB
/
Infix_Parser.cpp
File metadata and controls
150 lines (137 loc) · 3.16 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include "Infix_Parser.h"
//Default Constructor
Infix_Parser::Infix_Parser()
{
expression = "";
}
//Non-default Constructor
Infix_Parser::Infix_Parser(string exp)
{
expression = exp;
}
//Returns the infix expression
string Infix_Parser::getExpression()
{
return expression;
}
//Sets the infix expression
void Infix_Parser::setExpression(string exp)
{
expression = exp;
}
//Reads in and returns the processed operator
string Infix_Parser::processOperator(istringstream &in, string first, int index)
{
string opr = first;
string next = "";
in >> next;
//Processing 2-character operators with the same characters
if (next == opr)
{
opr += next; //Appends the second operator character to the operator string
return opr;
}
//Processing 2-character operators of different characters
else if ((opr == "!" && next == "=") || (opr == "<" && next == "=") || (opr == ">" && next == "="))
{
opr += next;
return opr;
}
char op = next.at(0); //Converts next into a char that can be put back into the read-in expression
in.putback(op);
return opr;
}
//Reads in and returns the processed operand
int Infix_Parser::processOperand(istringstream &in, char first)
{
//Reads in operands of multiple characters
int value;
in.putback(first);
in >> value;
return value;
}
//Parses the expression
vector<Token> Infix_Parser::parse()
{
//Initialized Variables
vector<Token> v;
istringstream temp(expression);
int index = 0;
char next = ' ';
char prev = ' ';
//Reading in the expression
while (temp >> next)
{
char next_next = temp.peek();
//Checking for spaces
if (isspace(next))
{
continue;
}
//Checking for operands
else if (isdigit(next))
{
Token t(Infix_Parser::processOperand(temp, next));
v.push_back(t);
}
//Checking for single-character operators
else if (next == '-' && v.empty())
{
Token t('-');
v.push_back(t);
}
else if (!isdigit(prev) && next == '-')
{
Token t('-');
v.push_back(t);
}
else if (next == '>')
{
Token t(Infix_Parser::processOperator(temp, ">", index));
v.push_back(t);
}
else if (next == '<')
{
Token t(Infix_Parser::processOperator(temp, "<", index));
v.push_back(t);
}
else if (next == '!')
{
Token t(Infix_Parser::processOperator(temp, "!", index));
v.push_back(t);
}
//Checking for 2-character operators
else if (next == '-' && next_next == '-')
{
Token t(Infix_Parser::processOperator(temp, "-", index));
v.push_back(t);
}
else if (next == '+' && next_next == '+')
{
Token t(Infix_Parser::processOperator(temp, "+", index));
}
else if (next == '&' && next_next == '&')
{
Token t(Infix_Parser::processOperator(temp, "&", index));
v.push_back(t);
}
else if (next == '|' && next_next == '|')
{
Token t(Infix_Parser::processOperator(temp, "|", index));
v.push_back(t);
}
else if (next == '=' && next_next == '=')
{
Token t(Infix_Parser::processOperator(temp, "=", index));
v.push_back(t);
}
else
{
Token t(next);
v.push_back(t);
}
prev = next;
index++;
}
return v;
}