Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Alexandru Dura
SPPF Earley Parser
Commits
f27f8dd6
Commit
f27f8dd6
authored
Mar 09, 2020
by
Alexandru Dura
Browse files
Fix a bug in the EarleyRule implementation of Comparable
parent
eb33214a
Changes
3
Hide whitespace changes
Inline
Side-by-side
src/main/java/se/lth/sep/EarleyParser.java
View file @
f27f8dd6
...
...
@@ -90,6 +90,17 @@ public class EarleyParser {
int
start
=
grammar
.
getInternalSymbol
(
startSymbol
);
StateSet
[]
state
=
internalParseScott
(
symbols
,
start
);
if
(
DEBUG
)
{
for
(
int
i
=
0
;
i
<
s
.
length
+
1
;
++
i
)
{
System
.
out
.
println
(
"=== Item set at position "
+
i
+
" ==="
);
for
(
EarleyItem
item
:
state
[
i
])
{
System
.
out
.
println
(
item
.
prettyPrint
(
grammar
));
}
}
System
.
out
.
println
(
"==========================="
);
}
StateSet
finalState
=
state
[
s
.
length
];
for
(
EarleyItem
item
:
finalState
)
{
...
...
src/main/java/se/lth/sep/EarleyRule.java
View file @
f27f8dd6
...
...
@@ -30,7 +30,7 @@ class EarleyRule implements Comparable<EarleyRule> {
}
else
if
(
this
.
body
.
length
<
other
.
body
.
length
)
{
return
-
1
;
}
else
{
return
0
;
return
1
;
}
}
...
...
src/test/java/EarleyParserTest.java
View file @
f27f8dd6
...
...
@@ -436,4 +436,78 @@ public class EarleyParserTest {
assertEquals
(
3
*
3
*
3
*
3
*
3
*
3
,
pts
.
size
());
Util
.
dumpParseTrees
(
"testJava5-parse-tree"
,
pts
);
}
@Test
public
void
testJava6
()
{
Grammar
g
=
new
Grammar
();
Java15Grammar
.
addRules
(
g
);
EarleyParser
parser
=
new
EarleyParser
(
g
);
/* enum E { ID } */
Category
str
[]
=
{
Java15Grammar
.
t_ENUM
,
Java15Grammar
.
t_IDENTIFIER
,
Java15Grammar
.
t_LBRACE
,
Java15Grammar
.
t_IDENTIFIER
,
Java15Grammar
.
t_RBRACE
};
SPPFNode
root
=
parser
.
parse
(
str
,
Java15Grammar
.
n_enum_declaration
);
assertNotNull
(
root
);
// remove trivial productions
SPPFTrivialProductionRemover
tpr
=
new
SPPFTrivialProductionRemover
(
g
)
{
@Override
public
boolean
isBubleUpChild
(
Category
p
,
Category
c
)
{
if
(
c
.
getName
().
equals
(
"METAVARID"
))
return
true
;
if
(
c
.
getName
().
equals
(
"GAP"
))
return
true
;
return
false
;
}
};
List
<
ParseTree
>
pts
=
Util
.
enumerateParseTrees
(
root
,
g
,
tpr
);
Util
.
dumpParseTrees
(
"testJava6-parse-tree"
,
pts
);
assertEquals
(
1
,
pts
.
size
());
}
@Test
public
void
testJava7
()
{
Grammar
g
=
new
Grammar
();
Java15Grammar
.
addRules
(
g
);
EarleyParser
parser
=
new
EarleyParser
(
g
);
System
.
out
.
println
(
g
);
/* enum E { .., MetaVarID,..; } */
Category
str
[]
=
{
Java15Grammar
.
t_ENUM
,
Java15Grammar
.
t_IDENTIFIER
,
Java15Grammar
.
t_LBRACE
,
Java15Grammar
.
t_GAP
,
Java15Grammar
.
t_COMMA
,
Java15Grammar
.
t_IDENTIFIER
,
Java15Grammar
.
t_COMMA
,
Java15Grammar
.
t_GAP
,
Java15Grammar
.
t_SEMICOLON
,
Java15Grammar
.
t_INT
,
Java15Grammar
.
t_IDENTIFIER
,
Java15Grammar
.
t_SEMICOLON
,
Java15Grammar
.
t_RBRACE
};
SPPFNode
root
=
parser
.
parse
(
str
,
Java15Grammar
.
n_enum_declaration
);
assertNotNull
(
root
);
// remove trivial productions
SPPFTrivialProductionRemover
tpr
=
new
SPPFTrivialProductionRemover
(
g
)
{
@Override
public
boolean
isBubleUpChild
(
Category
p
,
Category
c
)
{
if
(
c
.
getName
().
equals
(
"METAVARID"
))
return
true
;
if
(
c
.
getName
().
equals
(
"GAP"
))
return
true
;
return
false
;
}
};
List
<
ParseTree
>
pts
=
Util
.
enumerateParseTrees
(
root
,
g
,
tpr
);
Util
.
dumpParseTrees
(
"testJava7-parse-tree"
,
pts
);
assertEquals
(
1
,
pts
.
size
());
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment