Repository navigation
Expand file tree
/
Copy pathshiftReduce.cpp
More file actions
93 lines (91 loc) · 1.64 KB
/
Copy pathshiftReduce.cpp
File metadata and controls
93 lines (91 loc) · 1.64 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
#include "shiftReduce.h"
#include <iostream>
using namespace std;
//second column:
// 0-> id
// 1-> +
// 2-> *
// 3-> (
// 4-> )
// 5-> $
int main()
{
srMap srMap;
string table[12][6];
CreateTable(table);
string input;
cout << "Enter your string:" << endl;
cin >> input;
input.push_back('$');
bool invalidFlag = false;
for (int i = 0; input[i] != NULL && !invalidFlag; i++)
{
bool onRepeat = true;
while(onRepeat)
{
string action;
int tempIndex;
tempIndex = srMap.PeekStack();
switch (input[i])
{
case 'i':
if(input[i+1] == 'd')
{
action = table[tempIndex][0];
break;
}
break;
case '+':
action = table[tempIndex][1];
break;
case '*':
action = table[tempIndex][2];
break;
case '(':
action = table[tempIndex][3];
break;
case ')':
action = table[tempIndex][4];
break;
case '$':
action = table[tempIndex][5];
break;
}
if (action[0] == 'S')
{
if(action[2]==NULL)
{
srMap.Shift(input[i], action[1] - 48);
}
else
{
srMap.Shift(input[i], ((action[1]-48)*10+(action[2]-48)));
}
onRepeat = false;
}
else if (action[0] == 'R')
{
srMap.Reduce(action[1] - 48);
}
else if (action[0] == 'a')
{
cout << "VALID string entered. ACCEPTED!" << endl;
break;
}
else
{
onRepeat = false;
if (input[i] != 'd')
{
if(input[i-1] != 'i')
{
cout << "INVALID string entered. SYNTAX ERROR!" << endl;
invalidFlag = true;
break;
}
}
}
}
}
return 0;
}