Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
Algorithms Library Toolkit Core
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Container Registry
Model registry
Monitor
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Algorithms Library Toolkit
Algorithms Library Toolkit Core
Commits
ccfa95a7
Commit
ccfa95a7
authored
8 years ago
by
Martin Kočička
Committed by
Jan Trávníček
8 years ago
Browse files
Options
Downloads
Patches
Plain Diff
added universal LR parsing algorithm
parent
c69d56e5
No related branches found
No related tags found
1 merge request
!18
Bp kocicma3
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
alib2algo/src/grammar/parsing/LRParser.cpp
+43
-0
43 additions, 0 deletions
alib2algo/src/grammar/parsing/LRParser.cpp
alib2algo/src/grammar/parsing/LRParser.h
+4
-0
4 additions, 0 deletions
alib2algo/src/grammar/parsing/LRParser.h
with
47 additions
and
0 deletions
alib2algo/src/grammar/parsing/LRParser.cpp
+
43
−
0
View file @
ccfa95a7
...
@@ -85,6 +85,49 @@ LR0Items LRParser::getNextStateItems ( LR0Items items, alphabet::Symbol symbol,
...
@@ -85,6 +85,49 @@ LR0Items LRParser::getNextStateItems ( LR0Items items, alphabet::Symbol symbol,
return
getClosure
(
transitionItems
,
originalGrammar
);
return
getClosure
(
transitionItems
,
originalGrammar
);
}
}
bool
LRParser
::
parse
(
LRActionTable
actionTable
,
LRGotoTable
gotoTable
,
automaton
::
State
initialState
,
std
::
vector
<
alphabet
::
Symbol
>
input
)
{
std
::
stack
<
automaton
::
State
>
states
;
states
.
push
(
initialState
);
unsigned
currentPosition
=
0
;
while
(
true
)
{
LRActionTable
::
iterator
actionIterator
=
actionTable
.
find
(
{
states
.
top
(
),
input
[
currentPosition
]
}
);
if
(
actionIterator
==
actionTable
.
end
(
)
)
{
return
false
;
}
switch
(
actionIterator
->
second
.
first
)
{
case
LRAction
::
Shift
:
states
.
push
(
actionIterator
->
second
.
second
.
get
<
automaton
::
State
>
(
)
);
++
currentPosition
;
break
;
case
LRAction
::
Reduce
:
{
std
::
pair
<
alphabet
::
Symbol
,
std
::
vector
<
alphabet
::
Symbol
>
>
reduceBy
=
actionIterator
->
second
.
second
.
get
<
std
::
pair
<
alphabet
::
Symbol
,
std
::
vector
<
alphabet
::
Symbol
>
>
>
(
);
for
(
unsigned
i
=
0
;
i
<
reduceBy
.
second
.
size
(
);
++
i
)
{
states
.
pop
(
);
}
LRGotoTable
::
iterator
nextStateIterator
=
gotoTable
.
find
(
{
states
.
top
(
),
reduceBy
.
first
}
);
if
(
nextStateIterator
==
gotoTable
.
end
())
{
return
false
;
}
states
.
push
(
nextStateIterator
->
second
);
break
;
}
case
LRAction
::
Accept
:
return
true
;
break
;
}
}
return
false
;
}
}
/* namespace parsing */
}
/* namespace parsing */
}
/* namespace grammar */
}
/* namespace grammar */
This diff is collapsed.
Click to expand it.
alib2algo/src/grammar/parsing/LRParser.h
+
4
−
0
View file @
ccfa95a7
...
@@ -12,6 +12,8 @@
...
@@ -12,6 +12,8 @@
#include <grammar/ContextFree/CFG.h>
#include <grammar/ContextFree/CFG.h>
#include <grammar/parsing/LRParserTypes.h>
#include <grammar/parsing/LRParserTypes.h>
#include <vector>
namespace grammar {
namespace grammar {
namespace parsing {
namespace parsing {
...
@@ -25,6 +27,8 @@ public:
...
@@ -25,6 +27,8 @@ public:
static LR0Items getClosure ( LR0Items items, grammar::CFG originalGrammar );
static LR0Items getClosure ( LR0Items items, grammar::CFG originalGrammar );
static LR0Items getNextStateItems ( LR0Items items, alphabet::Symbol symbol, grammar::CFG originalGrammar );
static LR0Items getNextStateItems ( LR0Items items, alphabet::Symbol symbol, grammar::CFG originalGrammar );
static bool parse ( LRActionTable actionTable, LRGotoTable gotoTable, automaton::State initialState, std::vector < alphabet::Symbol > input );
};
};
} /* namespace parsing */
} /* namespace parsing */
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment