-
Notifications
You must be signed in to change notification settings - Fork 0
/
LRAction.cpp
114 lines (93 loc) · 2.12 KB
/
LRAction.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
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
#include "LRAction.hpp"
#include <memory>
/********************----- CLASS: LRAction -----********************/
LRAction::LRAction(Production const * const i_production)
:m_production(i_production),m_state(0),m_type(LRAction::Type::REDUCE)
{
if(m_production == nullptr)
{
throw std::bad_weak_ptr();
}
}
LRAction::LRAction(LRState const i_state)
:m_production(nullptr),m_state(i_state),m_type(LRAction::Type::SHIFT)
{
}
LRAction::LRAction(LRAction::Type const i_type)
:m_production(nullptr),m_state(0),m_type(i_type)
{
}
bool LRAction::isAccept() const
{
return (m_type == LRAction::Type::ACCEPT);
}
bool LRAction::isReduce() const
{
return (m_type == LRAction::Type::REDUCE);
}
bool LRAction::isShift() const
{
return (m_type == LRAction::Type::SHIFT);
}
Production const &LRAction::production() const
{
if(m_production == nullptr)
{
throw std::bad_weak_ptr();
}
return (*m_production);
}
LRState const &LRAction::state() const
{
return m_state;
}
std::string LRAction::toString() const
{
std::string outputString;
switch(m_type)
{
case LRAction::Type::ACCEPT:
outputString += "ACCEPT()";
break;
case LRAction::Type::REDUCE:
outputString += "REDUCE(" + m_production->toString() + ")";
break;
case LRAction::Type::SHIFT:
outputString += "SHIFT(" + std::to_string(m_state) + ")";
break;
default:
throw std::logic_error("Unknown type");
}
return outputString;
}
bool LRAction::operator==(LRAction const &i_otherAction) const
{
if(m_type != i_otherAction.m_type)
{
return false;
}
if(m_state != i_otherAction.m_state)
{
return false;
}
if(m_production != i_otherAction.m_production)
{
return false;
}
return true;
}
/**************************************************/
/********************----- Helper Functions -----********************/
LRAction ACCEPT()
{
return LRAction(LRAction::Type::ACCEPT);
}
LRAction REDUCE(Production const * const i_production)
{
return LRAction(i_production);
}
LRAction SHIFT(LRState const i_state)
{
return LRAction(i_state);
}
/**************************************************/