#include "ParsingAutomaton.h"
namespace
{
namespace
{
using
namespace
collections
;
using
namespace
definitions
;
namespace
{
<
>
CreateJointPDAFromNondeterministicPDA
(
<
>
)
{
<
>
=
new
(
);
<
,
ParsingDefinitionRuleDefinition
*>
;
<
*,
*>
;
FOREACH(ParsingDefinitionRuleDefinition*, rule, nondeterministicPDA->orderedRulesDefs)
if
(
bool
=
true
)
for
(
const
::
vl
::
collections
::
<
ParsingDefinitionRuleDefinition
*>&
= ::
vl
::
collections
::
(
);
__scope_variable_flag__
;
__scope_variable_flag__
=
false
)
for
(
ParsingDefinitionRuleDefinition
*
;
__foreach_iterator__
.
(
rule
);)
{
<
>
=
rule
];
<
>
=
new
;
automaton
(
rule
,
newRuleInfo
);
ruleMap
.
(
rule
->
,
rule
);
newRuleInfo
=
automaton
(
rule
);
newRuleInfo
=
automaton
(
rule
);
newRuleInfo
=
automaton
(
rule
);
oldNewStateMap
.
(
ruleInfo
,
newRuleInfo
);
oldNewStateMap
.
(
ruleInfo
,
newRuleInfo
);
oldNewStateMap
.
(
ruleInfo
,
newRuleInfo
);
newRuleInfo
->
ruleInfo
->
;
newRuleInfo
->
ruleInfo
->
;
newRuleInfo
->
ruleInfo
->
;
}
FOREACH(Ptr<State>, oldState, nondeterministicPDA->states)
if
(
bool
=
true
)
for
(
const
::
vl
::
collections
::
<
<
>>&
= ::
vl
::
collections
::
(
);
__scope_variable_flag__
;
__scope_variable_flag__
=
false
)
for
(
<
>
;
__foreach_iterator__
.
(
oldState
);)
{
if
((
oldState
.
()>
0
||
oldState
.
()>
0
) && !
oldNewStateMap
.
().
(
oldState
.
()))
{
*
=
automaton
(
oldState
.
());
oldNewStateMap
.
(
oldState
.
(),
newState
);
newState
->
oldState
;
}
}
FOREACH(ParsingDefinitionRuleDefinition*, rule, nondeterministicPDA->orderedRulesDefs)
if
(
bool
=
true
)
for
(
const
::
vl
::
collections
::
<
ParsingDefinitionRuleDefinition
*>&
= ::
vl
::
collections
::
(
);
__scope_variable_flag__
;
__scope_variable_flag__
=
false
)
for
(
ParsingDefinitionRuleDefinition
*
;
__foreach_iterator__
.
(
rule
);)
{
<
>
=
rule
];
<
>
=
automaton
rule
];
FOREACH(State*, endState, ruleInfo->endStates)
if
(
bool
=
true
)
for
(
const
::
vl
::
collections
::
<
*>&
= ::
vl
::
collections
::
(
ruleInfo
);
__scope_variable_flag__
;
__scope_variable_flag__
=
false
)
for
(
*
;
__foreach_iterator__
.
(
endState
);)
{
newRuleInfo
.
(
oldNewStateMap
endState
]);
}
<
*>
;
=
0
;
scanningStates
.
(
ruleInfo
);
while
(
currentStateIndex
<
scanningStates
.
())
{
*
=
scanningStates
currentStateIndex
++];
*
=
oldNewStateMap
currentOldState
];
FOREACH(Transition*, oldTransition, currentOldState->transitions)
if
(
bool
=
true
)
for
(
const
::
vl
::
collections
::
<
*>&
= ::
vl
::
collections
::
(
currentOldState
->
);
__scope_variable_flag__
;
__scope_variable_flag__
=
false
)
for
(
*
;
__foreach_iterator__
.
(
oldTransition
);)
{
*
=
oldTransition
->
;
*
=
oldTransition
->
;
*
=
oldNewStateMap
oldSource
];
*
=
oldNewStateMap
oldTarget
];
if
(!
scanningStates
.
(
oldSource
))
scanningStates
.
(
oldSource
);
if
(!
scanningStates
.
(
oldTarget
))
scanningStates
.
(
oldTarget
);
if
(
oldTransition
->
==
::
&&
oldTransition
->
->
()==
::
)
{
ParsingDefinitionRuleDefinition
*
=
ruleMap
oldTransition
->
->
()];
<
>
=
rule
];
{
*
=
automaton
(
newSource
,
oldNewStateMap
oldRuleInfo
]);
<
>
=
new
;
action
=
::
;
action
=
newSource
;
action
=
newTarget
;
action
=
shiftTransition
->
->
;
shiftTransition
->
.
(
action
);
}
FOREACH(State*, oldEndState, oldRuleInfo->endStates)
if
(
bool
=
true
)
for
(
const
::
vl
::
collections
::
<
*>&
= ::
vl
::
collections
::
(
oldRuleInfo
);
__scope_variable_flag__
;
__scope_variable_flag__
=
false
)
for
(
*
;
__foreach_iterator__
.
(
oldEndState
);)
{
*
=
automaton
(
oldNewStateMap
oldEndState
],
newTarget
);
<
>
=
new
;
action
=
::
;
action
=
newSource
;
action
=
newTarget
;
action
=
reduceTransition
->
->
;
reduceTransition
->
.
(
action
);
(
reduceTransition
->
,
oldTransition
->
,
true
);
}
}
else
{
*
=
automaton
(
newSource
,
newTarget
,
oldTransition
);
(
newTransition
->
,
oldTransition
->
);
}
}
}
}
return
automaton
;
}
::
ShiftReduceCompactClosure
(
*
)
{
return
->
!=
::
?
::
:
->
!=
::
?
::
:
::
;
}
void
(
<
>
)
{
FOREACH(Ptr<State>, state, jointPDA->states)
if
(
bool
=
true
)
for
(
const
::
vl
::
collections
::
<
<
>>&
= ::
vl
::
collections
::
(
);
__scope_variable_flag__
;
__scope_variable_flag__
=
false
)
for
(
<
>
;
__foreach_iterator__
.
(
state
);)
{
*
=
state
.
();
<
>
;
(&
ShiftReduceCompactClosure
,
currentState
,
closure
);
FOREACH(ClosureItem, closureItem, closure)
if
(
bool
=
true
)
for
(
const
::
vl
::
collections
::
<
>&
= ::
vl
::
collections
::
(
closure
);
__scope_variable_flag__
;
__scope_variable_flag__
=
false
)
for
(
;
__foreach_iterator__
.
(
closureItem
);)
{
*
=
closureItem
.
(
closureItem
.
()-
1
);
::
=
::
;
::
=
lastTransition
->
;
if
(
closureItem
.
&&
lastTransition
->
==
::
)
{
bool
=
false
;
bool
=
false
;
FOREACH(Transition*, pathTransition, *closureItem.transitions.Obj())
if
(
bool
=
true
)
for
(
const
::
vl
::
collections
::
<
*>&
= ::
vl
::
collections
::
(*
closureItem
.
.
());
__scope_variable_flag__
;
__scope_variable_flag__
=
false
)
for
(
*
;
__foreach_iterator__
.
(
pathTransition
);)
{
FOREACH(Ptr<Action>, action, pathTransition->actions)
if
(
bool
=
true
)
for
(
const
::
vl
::
collections
::
<
<
>>&
= ::
vl
::
collections
::
(
pathTransition
->
);
__scope_variable_flag__
;
__scope_variable_flag__
=
false
)
for
(
<
>
;
__foreach_iterator__
.
(
action
);)
{
if
(
action
==
::
)
containsShift
=
true
;
if
(
action
==
::
)
containsReduce
=
true
;
}
}
if
(
containsShift
&& !
containsReduce
)
{
if
(
closureItem
.
==
currentState
)
{
stackOperationType
=
::
;
}
}
else
if
(!
containsShift
&&
containsReduce
)
{
}
}
else
if
(
closureItem
.
()>
1
)
{
stackOperationType
=
::
;
}
if
(
stackOperationType
!=
::
)
{
*
=
(
currentState
,
lastTransition
->
,
lastTransition
);
transition
->
=
transitionType
;
transition
->
=
stackOperationType
;
FOREACH(Transition*, pathTransition, *closureItem.transitions.Obj())
if
(
bool
=
true
)
for
(
const
::
vl
::
collections
::
<
*>&
= ::
vl
::
collections
::
(*
closureItem
.
.
());
__scope_variable_flag__
;
__scope_variable_flag__
=
false
)
for
(
*
;
__foreach_iterator__
.
(
pathTransition
);)
{
(
transition
->
,
pathTransition
->
,
true
);
}
}
}
}
for
(
=
.
()-
1
;
i
>=
0
;
i
--)
{
*
=
i
].
();
if
(
transition
->
==
::
&&
transition
->
==
::
)
{
(
transition
);
}
}
}
void
MarkLeftRecursiveInJointPDA
(
<
>
,
collections
::
<
<
>>&
)
{
=
.
();
<
<
*,
*>>
;
FOREACH(Ptr<State>, state, jointPDA->states)
if
(
bool
=
true
)
for
(
const
::
vl
::
collections
::
<
<
>>&
= ::
vl
::
collections
::
(
);
__scope_variable_flag__
;
__scope_variable_flag__
=
false
)
for
(
<
>
;
__foreach_iterator__
.
(
state
);)
{
for
(
=
state
.
()-
1
;
i
>=
0
;
i
--)
{
*
=
state
i
];
if
(
transition
->
==
::
)
{
<
>
;
FOREACH(Ptr<Action>, action, transition->actions)
if
(
bool
=
true
)
for
(
const
::
vl
::
collections
::
<
<
>>&
= ::
vl
::
collections
::
(
transition
->
);
__scope_variable_flag__
;
__scope_variable_flag__
=
false
)
for
(
<
>
;
__foreach_iterator__
.
(
action
);)
{
if
(
action
==
::
)
{
if
(
shiftAction
)
{
.
(
new
(
state
,
L"Indirect left recursive transition in rule \""
+
state
->
+
L"\" is not allowed."
));
goto
FOUND_INDIRECT_LEFT_RECURSIVE_TRANSITION;
}
else
{
shiftAction
action
;
}
}
}
if
(
shiftAction
)
{
leftRecursiveShifts
.
(
<
*,
*>(
shiftAction
,
shiftAction
));
}
FOUND_INDIRECT_LEFT_RECURSIVE_TRANSITION:
(
transition
);
}
}
}
if
(
errorCount
!=
.
())
{
return
;
}
FOREACH(Ptr<State>, state, jointPDA->states)
if
(
bool
=
true
)
for
(
const
::
vl
::
collections
::
<
<
>>&
= ::
vl
::
collections
::
(
);
__scope_variable_flag__
;
__scope_variable_flag__
=
false
)
for
(
<
>
;
__foreach_iterator__
.
(
state
);)
{
FOREACH(Transition*, transition, state->transitions)
if
(
bool
=
true
)
for
(
const
::
vl
::
collections
::
<
*>&
= ::
vl
::
collections
::
(
state
);
__scope_variable_flag__
;
__scope_variable_flag__
=
false
)
for
(
*
;
__foreach_iterator__
.
(
transition
);)
{
for
(
=
transition
->
.
()-
1
;
i
>=
0
;
i
--)
{
<
>
=
transition
->
i
];
if
(
action
==
::
)
{
<
*,
*>
(
action
,
action
);
if
(
leftRecursiveShifts
.
(
shift
))
{
if
(
transition
->
==
::
)
{
transition
->
=
::
;
<
>
=
new
;
newAction
=
::
;
newAction
=
action
;
newAction
=
action
;
newAction
=
action
;
newAction
=
action
;
newAction
=
action
;
newAction
=
shift
.
->
;
transition
->
i
]
newAction
;
}
else
{
.
(
new
(
state
,
L"Left recursive reduce action in non-normal-reduce found in rule \""
+
state
->
+
L"\" is not allowed."
));
}
}
}
}
}
}
FOREACH(Ptr<State>, state, jointPDA->states)
if
(
bool
=
true
)
for
(
const
::
vl
::
collections
::
<
<
>>&
= ::
vl
::
collections
::
(
);
__scope_variable_flag__
;
__scope_variable_flag__
=
false
)
for
(
<
>
;
__foreach_iterator__
.
(
state
);)
{
while
(
true
)
{
bool
=
false
;
FOREACH(Transition*, t1, state->transitions)
if
(
bool
=
true
)
for
(
const
::
vl
::
collections
::
<
*>&
= ::
vl
::
collections
::
(
state
);
__scope_variable_flag__
;
__scope_variable_flag__
=
false
)
for
(
*
;
__foreach_iterator__
.
(
t1
);)
FOREACH(Transition*, t2, state->transitions)
if
(
bool
=
true
)
for
(
const
::
vl
::
collections
::
<
*>&
= ::
vl
::
collections
::
(
state
);
__scope_variable_flag__
;
__scope_variable_flag__
=
false
)
for
(
*
;
__foreach_iterator__
.
(
t2
);)
if
(
t1
!=
t2
)
{
if
(
::
(
t1
,
t2
,
true
))
{
(
t2
);
deleted
=
true
;
goto
TRANSITION_DELETED;
}
}
TRANSITION_DELETED:
if
(!
deleted
)
break
;
}
}
}
}
}
}