Skip to content

Commit 5c62b34

Browse files
committed
bugfix and change epsilon represition
bug: 1.when the automata all states are acceptable then it would out of index 2.fix state marking when start state and accept state is the same one 3.fix mimizing algorithm when all state are accepteable
1 parent 14149e6 commit 5c62b34

File tree

13 files changed

+86
-28
lines changed

13 files changed

+86
-28
lines changed
File renamed without changes.

Automata.h renamed to Automata/Automata.h

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@
1212
#include <iterator>
1313
#include <string>
1414
#include <exception>
15-
#include "third_part/ATMPL/optional/optional.hpp"
16-
#include "Set.h"
15+
#include "../third_part/ATMPL/optional/optional.hpp"
16+
#include "../util/Set.h"
1717
template<typename _State = int, typename _Driver = char, typename _Sigma = std::string>
1818
class Automata
1919
{
2020
public:
21-
enum { epsilon = 0 };
21+
enum { epsilon = 1 };
2222
enum { invalid_state = -1 };
2323
//the set must impl unique insert().
2424
using state_set = Set<_State>;
@@ -151,8 +151,21 @@ void Automata<_State, _Driver, _Sigma>::minimize()
151151
}
152152
throw AutomataException("unexpected state" + std::to_string(s_));
153153
};
154-
equivalence.push_front(accept_);
155-
equivalence.push_front(origin - accept_);
154+
if (!accept_.empty())
155+
{
156+
equivalence.push_front(accept_);
157+
158+
}
159+
if (!(origin - accept_).empty())
160+
{
161+
equivalence.push_front(origin - accept_);
162+
}
163+
else
164+
{
165+
//in this consequence it should not use minimizing algorithm
166+
return;
167+
}
168+
156169
bool done = false;
157170
while (!done)//calculate equivalence state set
158171
{
@@ -229,14 +242,15 @@ void Automata<_State, _Driver, _Sigma>::minimize()
229242
}
230243
auto eq_state_i = equivalence_state.begin();
231244
std::advance(eq_state_i, std::distance(equivalence.begin(), eq));
232-
if (eq->find(start_) != eq->end())
245+
if (!(*eq & accept_).empty())
233246
{
234-
ato.set_start(*eq_state_i);
247+
ato.set_accept(*eq_state_i);
235248
}
236-
else if (!(*eq & accept_).empty())
249+
if (eq->find(start_) != eq->end())
237250
{
238-
ato.set_accept(*eq_state_i);
251+
ato.set_start(*eq_state_i);
239252
}
253+
240254

241255
}
242256
*this = ato;
File renamed without changes.
File renamed without changes.

CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ cmake_minimum_required(VERSION 3.6)
22
project(ERE)
33

44
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
5-
6-
set(SOURCE_FILES FA.cpp Automata.cpp BREParser.cpp Set.cpp ERE.cpp)
5+
set(SOURCE_FILES Automata/FA.cpp Automata/Automata.cpp Parser/BREParser.cpp util/Set.cpp ERE.cpp)
76
add_executable(ERE ${SOURCE_FILES})
87
target_link_libraries(ERE libboost_program_options-mt.a)

ERE.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#include "Set.h"
2-
#include "Automata.h"
3-
#include "BREParser.h"
1+
#include "util/Set.h"
2+
#include "Automata//Automata.h"
3+
#include "Parser/BREParser.h"
44
#include "boost/program_options/options_description.hpp"
55
#include "boost/program_options/parsers.hpp"
66
#include "boost/program_options/variables_map.hpp"
@@ -31,15 +31,15 @@ int main(int argc, char* argv[])
3131
("help,h", "produce help message")
3232
("expr,e", po::value<std::string>(), "set regular expression")
3333
("string,s", po::value<std::string>(), "string to check by regular expression")
34-
("alphabet,a", po::value<std::string>()->default_value(FA::alphabet), "set regular expression alphabet");
34+
("alphabet,a", po::value<std::string>()->default_value(FA::alphabet.data()), "set regular expression alphabet");
3535
po::variables_map vm;
3636
po::store(po::parse_command_line(argc, argv, desc), vm);
3737
po::notify(vm);
3838
if (vm.count("expr") && vm.count("string"))
3939
{
4040
try
4141
{
42-
FA::set_alphabet(vm["alphabet"].as<std::string>());
42+
FA::set_alphabet(vm["alphabet"].as<std::string>().c_str());
4343
BREParser bre_parser;
4444
bre_parser.register_event(BREParser::event::NFA_complete, event_handler);
4545
bre_parser.register_event(BREParser::event::NFA_determinated, event_handler);

ERE.vcxproj

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,17 @@
8888
</Link>
8989
</ItemDefinitionGroup>
9090
<ItemGroup>
91-
<ClInclude Include="Automata.h" />
92-
<ClInclude Include="BREParser.h" />
93-
<ClInclude Include="FA.h" />
94-
<ClInclude Include="Set.h" />
91+
<ClCompile Include="Automata\Automata.cpp" />
92+
<ClCompile Include="Automata\FA.cpp" />
93+
<ClCompile Include="ERE.cpp" />
94+
<ClCompile Include="Parser\BREParser.cpp" />
95+
<ClCompile Include="util\Set.cpp" />
9596
</ItemGroup>
9697
<ItemGroup>
97-
<ClCompile Include="BREParser.cpp" />
98-
<ClCompile Include="FA.cpp" />
99-
<ClCompile Include="ERE.cpp" />
98+
<ClInclude Include="Automata\Automata.h" />
99+
<ClInclude Include="Automata\FA.h" />
100+
<ClInclude Include="Parser\BREParser.h" />
101+
<ClInclude Include="util\Set.h" />
100102
</ItemGroup>
101103
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
102104
<ImportGroup Label="ExtensionTargets">

ERE.vcxproj.filters

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup>
4+
<ClCompile Include="ERE.cpp" />
5+
<ClCompile Include="Parser\BREParser.cpp">
6+
<Filter>Parser</Filter>
7+
</ClCompile>
8+
<ClCompile Include="util\Set.cpp">
9+
<Filter>util</Filter>
10+
</ClCompile>
11+
<ClCompile Include="Automata\Automata.cpp">
12+
<Filter>Automata</Filter>
13+
</ClCompile>
14+
<ClCompile Include="Automata\FA.cpp">
15+
<Filter>Automata</Filter>
16+
</ClCompile>
17+
</ItemGroup>
18+
<ItemGroup>
19+
<Filter Include="Parser">
20+
<UniqueIdentifier>{86ed5819-5bb7-496c-a427-cb4a0c39ad6e}</UniqueIdentifier>
21+
</Filter>
22+
<Filter Include="util">
23+
<UniqueIdentifier>{9515bada-3a17-4266-99ae-2299b657bff1}</UniqueIdentifier>
24+
</Filter>
25+
<Filter Include="Automata">
26+
<UniqueIdentifier>{9425ca9f-6075-4a9f-9c79-ec244024ac86}</UniqueIdentifier>
27+
</Filter>
28+
</ItemGroup>
29+
<ItemGroup>
30+
<ClInclude Include="Parser\BREParser.h">
31+
<Filter>Parser</Filter>
32+
</ClInclude>
33+
<ClInclude Include="util\Set.h">
34+
<Filter>util</Filter>
35+
</ClInclude>
36+
<ClInclude Include="Automata\Automata.h">
37+
<Filter>Automata</Filter>
38+
</ClInclude>
39+
<ClInclude Include="Automata\FA.h">
40+
<Filter>Automata</Filter>
41+
</ClInclude>
42+
</ItemGroup>
43+
</Project>

ERE.vcxproj.user

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
4-
<LocalDebuggerCommandArguments>-e "a(b|c)*" -s abbbccc -a a</LocalDebuggerCommandArguments>
4+
<LocalDebuggerCommandArguments> -e "a*|(b|c)*" -s aaaa -a abc</LocalDebuggerCommandArguments>
55
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
66
</PropertyGroup>
77
</Project>

BREParser.cpp renamed to Parser/BREParser.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#include "BREParser.h"
2-
2+
#include <algorithm>
33
BREParser::BREParser()
44
:m_pos(-1)
55
{
@@ -94,7 +94,7 @@ FA BREParser::item()
9494
next();
9595
return _fa;
9696
}
97-
else if (FA::alphabet.find(peek_next()) != std::string::npos)
97+
else if (std::find(FA::alphabet.begin(), FA::alphabet.end(),peek_next()) != FA::alphabet.end())
9898
{
9999
return sym();
100100
}

0 commit comments

Comments
 (0)