#include "ParsingAutomaton.h"
namespace
{
namespace
{
using
namespace
collections
;
using
namespace
definitions
;
namespace
{
void
(
<
>
,
<
>
,
<
*>&
)
{
while
(
true
)
{
=
0
;
for
(
=
.
()-
1
;
i
>=
0
;
i
--)
{
*
=
i
];
if
(
newState
->
.
()==
0
)
{
if
(
newState
!=
&& !
newState
->
)
{
(
newState
);
.
(
i
);
}
}
}
if
(
deleteCount
==
0
)
{
break
;
}
}
}
bool
(
*
,
<
>
)
{
if
(
==
||
==
||
==
)
{
return
false
;
}
return
true
;
}
#define COMPARE_SYMBOL(S1, S2)\
if (S1 && S2)\
{\
if (S1->GetType() < S2->GetType()) return -1;\
if (S1->GetType() > S2->GetType()) return 1;\
if (S1->GetName() < S2->GetName()) return -1;\
if (S1->GetName() > S2->GetName()) return 1;\
}\
else if (S1)\
{\
return 1;\
}\
else if (S2)\
{\
return -1;\
}\
CompareTransitionForRearranging
(
*
,
*
)
{
if
(
->
<
->
)
return
-
1
;
if
(
->
>
->
)
return
1
;
COMPARE_SYMBOL(t1->transitionSymbol, t2->transitionSymbol);
if
(
->
&&
->
) {
if
(
->
->
() <
->
->
())
return
-
1
;
if
(
->
->
() >
->
->
())
return
1
;
if
(
->
->
()
->
->
())
return
-
1
;
if
(
->
->
()
->
->
())
return
1
; }
else
if
(
->
) {
return
1
; }
else
if
(
->
) {
return
-
1
; };
return
0
;
}
CompareActionForRearranging
(
<
>
,
<
>
)
{
if
(
<
)
return
-
1
;
if
(
>
)
return
1
;
COMPARE_SYMBOL(a1->actionSource, a2->actionSource);
if
(
&&
) {
if
(
->
() <
->
())
return
-
1
;
if
(
->
() >
->
())
return
1
;
if
(
->
()
->
())
return
-
1
;
if
(
->
()
->
())
return
1
; }
else
if
(
) {
return
1
; }
else
if
(
) {
return
-
1
; };
COMPARE_SYMBOL(a1->actionTarget, a2->actionTarget);
if
(
&&
) {
if
(
->
() <
->
())
return
-
1
;
if
(
->
() >
->
())
return
1
;
if
(
->
()
->
())
return
-
1
;
if
(
->
()
->
())
return
1
; }
else
if
(
) {
return
1
; }
else
if
(
) {
return
-
1
; };
return
0
;
}
#undef COMPARE_SYMBOL
void
(
*
,
<
*>&
)
{
if
(!
.
(
))
{
FOREACH(Transition*, transition, state->transitions)
if
(
bool
=
true
)
for
(
const
::
vl
::
collections
::
<
*>&
= ::
vl
::
collections
::
(
->
);
__scope_variable_flag__
;
__scope_variable_flag__
=
false
)
for
(
*
;
__foreach_iterator__
.
(
transition
);)
{
(
transition
->
,
(
transition
->
).OrderBy(&
CompareActionForRearranging
));
}
(
->
,
(
->
).OrderBy(&
CompareTransitionForRearranging
));
.
(
);
}
}
void
(
*
)
{
<
<
>>
;
for
(
=
->
.
()-
1
;
i
>=
0
;
i
--)
{
switch
(
->
i
]
)
{
case
::
:
case
::
:
break
;
default
:
movableActions
.
(
->
i
]);
->
.
(
i
);
}
}
FOREACH(Transition*, t, transition->source->inputs)
if
(
bool
=
true
)
for
(
const
::
vl
::
collections
::
<
*>&
= ::
vl
::
collections
::
(
->
->
);
__scope_variable_flag__
;
__scope_variable_flag__
=
false
)
for
(
*
;
__foreach_iterator__
.
(
t
);)
{
(
t
->
,
movableActions
,
true
);
}
}
bool
IsMergableBecauseTransitions
(
*
,
*
)
{
if
(
->
.
()!=
->
.
())
return
false
;
if
(
->
.
()==
1
&&
->
.
()==
1
)
{
*
=
->
0
];
*
=
->
0
];
if
(
CompareTransitionForRearranging
(
t1
,
t2
)==
0
&& !
::
(
t1
,
t2
,
false
) &&
t1
->
==
t2
->
)
{
MoveActionsForMergingState
(
t1
);
MoveActionsForMergingState
(
t2
);
}
}
for
(
=
0
;
i
<
->
.
();
i
++)
{
*
=
->
i
];
*
=
->
i
];
if
(!
::
(
t1
,
t2
,
false
) ||
t1
->
!=
t2
->
)
{
return
false
;
}
}
return
true
;
}
bool
(
*
,
*
)
{
if
(
->
.
()!=
->
.
())
return
false
;
for
(
=
0
;
i
<
->
.
();
i
++)
{
*
=
->
i
];
*
=
->
i
];
if
(!
::
(
t1
,
t2
,
false
) ||
t1
->
!=
t2
->
)
{
return
false
;
}
}
return
true
;
}
void
MergeState2ToState1BecauseTransitions
(
<
>
,
*
,
*
)
{
->
+=
L"\r\n"
+
->
;
for
(
=
->
.
()-
1
;
i
>=
0
;
i
--)
{
*
=
->
i
];
bool
=
true
;
FOREACH(Transition*, t1, state1->inputs)
if
(
bool
=
true
)
for
(
const
::
vl
::
collections
::
<
*>&
= ::
vl
::
collections
::
(
->
);
__scope_variable_flag__
;
__scope_variable_flag__
=
false
)
for
(
*
;
__foreach_iterator__
.
(
t1
);)
{
if
(
::
(
t1
,
t2
,
false
) &&
t1
->
==
t2
->
)
{
add
=
false
;
break
;
}
}
if
(
add
)
{
->
.
(
t2
);
t2
->
=
;
->
.
(
i
);
}
}
(
);
}
void
(
<
>
,
*
,
*
)
{
->
+=
L"\r\n"
+
->
;
for
(
=
->
.
()-
1
;
i
>=
0
;
i
--)
{
*
=
->
i
];
bool
=
true
;
FOREACH(Transition*, t1, state1->transitions)
if
(
bool
=
true
)
for
(
const
::
vl
::
collections
::
<
*>&
= ::
vl
::
collections
::
(
->
);
__scope_variable_flag__
;
__scope_variable_flag__
=
false
)
for
(
*
;
__foreach_iterator__
.
(
t1
);)
{
if
(
::
(
t1
,
t2
,
false
) &&
t1
->
==
t2
->
)
{
add
=
false
;
break
;
}
}
if
(
add
)
{
->
.
(
t2
);
t2
->
=
;
->
.
(
i
);
}
}
(
);
}
void
(
<
>
,
<
>
,
<
*>&
)
{
<
*>
;
while
(
true
)
{
for
(
=
0
;
i
<
.
();
i
++)
{
*
=
i
];
if
(
(
state1
,
))
{
for
(
=
i
+
1
;
j
<
.
();
j
++)
{
*
=
j
];
if
(
state1
!=
state2
&&
(
state2
,
))
{
(
state1
,
stateContentSorted
);
(
state2
,
stateContentSorted
);
if
(
IsMergableBecauseTransitions
(
state1
,
state2
))
{
MergeState2ToState1BecauseTransitions
(
,
state1
,
state2
);
.
(
j
);
goto
MERGED_STATES_PAIR;
}
else
if
(
(
state1
,
state2
))
{
MergeState2ToState1BecauseInputs
(
,
state1
,
state2
);
.
(
j
);
goto
MERGED_STATES_PAIR;
}
}
}
}
}
break
;
MERGED_STATES_PAIR:
continue
;
}
}
}
}
}