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
fdc4af04
Commit
fdc4af04
authored
Jan 29, 2020
by
Alexandru Dura
Browse files
Restrict visibility of some classes, members and methods
parent
dbcee794
Changes
6
Hide whitespace changes
Inline
Side-by-side
src/main/java/DottedRule.java
View file @
fdc4af04
public
class
DottedRule
{
class
DottedRule
{
final
EarleyRule
r
;
final
int
dot
;
...
...
src/main/java/EarleyItem.java
View file @
fdc4af04
public
class
EarleyItem
{
class
EarleyItem
{
final
int
start
;
// 0 means beginning of input
final
DottedRule
rule
;
SPPFNode
sppf
;
...
...
src/main/java/EarleyParser.java
View file @
fdc4af04
import
javax.swing.plaf.nimbus.State
;
import
java.io.File
;
import
java.io.FileNotFoundException
;
import
java.io.PrintStream
;
import
java.util.ArrayList
;
import
java.util.Collection
;
import
java.util.HashMap
;
import
java.util.Map
;
import
java.util.TreeSet
;
import
java.util.HashSet
;
import
java.util.Iterator
;
import
java.util.
LinkedLis
t
;
import
java.util.
TreeSe
t
;
public
class
EarleyParser
{
private
boolean
DEBUG
=
true
;
...
...
@@ -31,7 +24,7 @@ public class EarleyParser {
return
grammar
.
toString
();
}
class
StateSet
extends
HashSet
<
EarleyItem
>
{
static
class
StateSet
extends
HashSet
<
EarleyItem
>
{
public
StateSet
(
Collection
<
EarleyItem
>
c
)
{
super
(
c
);
}
...
...
@@ -44,7 +37,6 @@ public class EarleyParser {
Iterator
<
EarleyItem
>
it
=
iterator
();
if
(
it
.
hasNext
())
{
EarleyItem
item
=
it
.
next
();
// the following is optional, let's see if it works
it
.
remove
();
return
item
;
}
...
...
@@ -65,8 +57,7 @@ public class EarleyParser {
for
(
int
i
=
0
;
i
<
s
.
length
+
1
;
++
i
)
{
System
.
out
.
println
(
"=== Item set at position "
+
i
+
" ==="
);
for
(
EarleyItem
item
:
state
[
i
])
{
if
(
true
||
item
.
isComplete
())
System
.
out
.
println
(
item
.
prettyPrint
(
grammar
));
System
.
out
.
println
(
item
.
prettyPrint
(
grammar
));
}
}
}
...
...
src/main/java/EarleyRule.java
View file @
fdc4af04
public
class
EarleyRule
implements
Comparable
<
EarleyRule
>
{
class
EarleyRule
implements
Comparable
<
EarleyRule
>
{
final
int
body
[];
final
int
head
;
...
...
src/main/java/SPPFNode.java
View file @
fdc4af04
import
java.io.PrintStream
;
import
java.util.ArrayList
;
import
java.util.HashMap
;
import
java.util.HashSet
;
import
java.util.List
;
public
class
SPPFNode
{
static
class
FamilyNode
{
SPPFNode
child1
;
SPPFNode
child2
;
private
SPPFNode
[]
child
;
FamilyNode
(
SPPFNode
child1
,
SPPFNode
child2
)
{
this
.
child1
=
child1
;
this
.
child2
=
child2
;
FamilyNode
(
SPPFNode
child0
,
SPPFNode
child1
)
{
assert
child0
!=
null
||
child1
==
null
;
child
=
new
SPPFNode
[
2
];
child
[
0
]
=
child0
;
child
[
1
]
=
child1
;
}
public
SPPFNode
getChild
(
int
i
)
{
return
child
[
i
];
}
@Override
public
int
hashCode
()
{
final
int
prime
=
31
;
int
result
=
1
;
result
=
prime
*
result
+
((
child
1
==
null
)
?
0
:
child
1
.
hashCode
());
result
=
prime
*
result
+
((
child
2
==
null
)
?
0
:
child
2
.
hashCode
());
result
=
prime
*
result
+
((
child
[
0
]
==
null
)
?
0
:
child
[
0
]
.
hashCode
());
result
=
prime
*
result
+
((
child
[
1
]
==
null
)
?
0
:
child
[
1
]
.
hashCode
());
return
result
;
}
...
...
@@ -32,15 +33,15 @@ public class SPPFNode {
if
(
getClass
()
!=
obj
.
getClass
())
return
false
;
FamilyNode
other
=
(
FamilyNode
)
obj
;
if
(
child
1
==
null
)
{
if
(
other
.
child
1
!=
null
)
if
(
child
[
0
]
==
null
)
{
if
(
other
.
child
[
0
]
!=
null
)
return
false
;
}
else
if
(!
child
1
.
equals
(
other
.
child
1
))
}
else
if
(!
child
[
0
]
.
equals
(
other
.
child
[
0
]
))
return
false
;
if
(
child
2
==
null
)
{
if
(
other
.
child
2
!=
null
)
if
(
child
[
1
]
==
null
)
{
if
(
other
.
child
[
1
]
!=
null
)
return
false
;
}
else
if
(!
child
2
.
equals
(
other
.
child
2
))
}
else
if
(!
child
[
1
]
.
equals
(
other
.
child
[
1
]
))
return
false
;
return
true
;
}
...
...
@@ -65,8 +66,8 @@ public class SPPFNode {
children
.
add
(
new
FamilyNode
(
child
,
null
));
}
public
void
addChildren
(
SPPFNode
child
1
,
SPPFNode
child
2
)
{
children
.
add
(
new
FamilyNode
(
child
1
,
child
2
));
public
void
addChildren
(
SPPFNode
child
0
,
SPPFNode
child
1
)
{
children
.
add
(
new
FamilyNode
(
child
0
,
child
1
));
}
public
void
addEpsilon
()
{
...
...
src/main/java/Util.java
View file @
fdc4af04
...
...
@@ -45,22 +45,22 @@ class DotVisitor implements SPPFNodeVisitor {
int
currentID
=
nodeID
++;
visitedFamilies
.
put
(
f
,
currentID
);
if
(
f
.
c
hild
1
==
null
)
{
if
(
f
.
getC
hild
(
0
)
==
null
)
{
// epsilon
ps
.
print
(
currentID
+
" [label=\u03b5];\n"
);
}
else
{
ps
.
print
(
currentID
+
" [shape=circle];\n"
);
f
.
c
hild
1
.
accept
(
this
);
if
(
f
.
c
hild
2
!=
null
)
{
f
.
c
hild
2
.
accept
(
this
);
f
.
getC
hild
(
0
)
.
accept
(
this
);
if
(
f
.
getC
hild
(
1
)
!=
null
)
{
f
.
getC
hild
(
1
)
.
accept
(
this
);
}
}
if
(
f
.
c
hild
1
!=
null
)
ps
.
print
(
currentID
+
" -> "
+
visitedNodes
.
get
(
f
.
c
hild
1
)
+
";\n"
);
if
(
f
.
c
hild
2
!=
null
)
ps
.
print
(
currentID
+
" -> "
+
visitedNodes
.
get
(
f
.
c
hild
2
)
+
";\n"
);
if
(
f
.
getC
hild
(
0
)
!=
null
)
ps
.
print
(
currentID
+
" -> "
+
visitedNodes
.
get
(
f
.
getC
hild
(
0
)
)
+
";\n"
);
if
(
f
.
getC
hild
(
1
)
!=
null
)
ps
.
print
(
currentID
+
" -> "
+
visitedNodes
.
get
(
f
.
getC
hild
(
1
)
)
+
";\n"
);
}
@Override
...
...
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