Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring gll: fix error recovery #15

Merged
merged 19 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/main/kotlin/org/srcgll/Example.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ package org.srcgll
import org.srcgll.grammar.combinator.Grammar
import org.srcgll.grammar.combinator.regexp.*
import org.srcgll.input.Edge
import org.srcgll.input.IGraph
import org.srcgll.input.IInputGraph
import org.srcgll.input.ILabel
import org.srcgll.parser.Gll
import org.srcgll.parser.context.Context
import org.srcgll.rsm.symbol.Terminal
import org.srcgll.sppf.node.SppfNode

Expand Down Expand Up @@ -78,10 +77,11 @@ class SimpleInputLabel(

/**
* Simple Realisation of IGraph interface as Directed Graph
* @param VertexType = Int
* @param LabelType = SimpleInputLabel
* VertexType = Int
* LabelType = SimpleInputLabel
*/
class SimpleGraph : IGraph<Int, SimpleInputLabel> {

class SimpleGraph : IInputGraph<Int, SimpleInputLabel> {
override val vertices: MutableMap<Int, Int> = HashMap()
override val edges: MutableMap<Int, MutableList<Edge<Int, SimpleInputLabel>>> = HashMap()

Expand Down Expand Up @@ -200,9 +200,9 @@ fun main() {

// result = (root of SPPF, set of reachable vertices)
val resultAnBn: Pair<SppfNode<Int>?, HashMap<Pair<Int, Int>, Int>> =
Gll(Context(rsmAnBnStartState, inputGraphAnBn)).parse()
Gll.gll(rsmAnBnStartState, inputGraphAnBn).parse()
val resultStack: Pair<SppfNode<Int>?, HashMap<Pair<Int, Int>, Int>> =
Gll(Context(rsmStackStartState, inputGraphStack)).parse()
Gll.gll(rsmStackStartState, inputGraphStack).parse()

println("AnBn Language Grammar")
println("Reachability pairs : ")
Expand Down
7 changes: 3 additions & 4 deletions src/main/kotlin/org/srcgll/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ import kotlinx.cli.ArgParser
import kotlinx.cli.ArgType
import kotlinx.cli.default
import kotlinx.cli.required
import org.srcgll.input.LinearInput
import org.srcgll.input.LinearInputLabel
import org.srcgll.input.RecoveryLinearInput
import org.srcgll.lexer.JavaGrammar
import org.srcgll.lexer.JavaLexer
import org.srcgll.lexer.JavaToken
import org.srcgll.parser.Gll
import org.srcgll.parser.context.RecoveryContext
import org.srcgll.rsm.symbol.Terminal
import org.srcgll.rsm.writeRsmToDot
import org.srcgll.sppf.writeSppfToDot
Expand Down Expand Up @@ -48,9 +47,9 @@ fun main(args: Array<String>) {

val input = File(pathToInput).readText().replace("\n", "").trim()
val grammar = JavaGrammar().getRsm()
val inputGraph = LinearInput<Int, LinearInputLabel>()
val inputGraph = RecoveryLinearInput<Int, LinearInputLabel>()
val lexer = JavaLexer(StringReader(input))
val gll = Gll(RecoveryContext(grammar, inputGraph))
val gll = Gll.recoveryGll(grammar, inputGraph)
var vertexId = 0
var token: JavaToken

Expand Down
11 changes: 4 additions & 7 deletions src/main/kotlin/org/srcgll/benchmarks/Benchmarks.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import kotlinx.cli.ArgParser
import kotlinx.cli.ArgType
import kotlinx.cli.default
import kotlinx.cli.required
import org.srcgll.input.LinearInput
import org.srcgll.input.LinearInputLabel
import org.srcgll.input.RecoveryLinearInput
import org.srcgll.lexer.GeneratedLexer
import org.srcgll.lexer.SymbolCode
import org.srcgll.parser.Gll
import org.srcgll.parser.context.RecoveryContext
import org.srcgll.rsm.readRsmFromTxt
import org.srcgll.rsm.symbol.Terminal
import org.srcgll.sppf.writeSppfToDot
Expand Down Expand Up @@ -70,11 +69,9 @@ fun runRsmWithSppf(
val resultPath = getResultPath(pathToOutput, inputName, "rsm", rsmName, "with_sppf")
File(resultPath).writeText("")

val inputGraph = LinearInput<Int, LinearInputLabel>()
val inputGraph = RecoveryLinearInput<Int, LinearInputLabel>()
val lexer = GeneratedLexer(StringReader(input))
val gll = Gll(
RecoveryContext(rsm, inputGraph)
)
val gll = Gll.recoveryGll(rsm, inputGraph)
var token: SymbolCode
var vertexId = 1

Expand Down Expand Up @@ -106,7 +103,7 @@ fun runRsmWithSppf(

for (benchmarkAttempt in 1..benchmarkRounds) {
val elapsedRecovery = measureNanoTime {
Gll(RecoveryContext(rsm, inputGraph)).parse()
Gll.recoveryGll(rsm, inputGraph).parse()
}

val elapsedRecoverySeconds = elapsedRecovery.toDouble() / 1_000_000_000.0
Expand Down
81 changes: 81 additions & 0 deletions src/main/kotlin/org/srcgll/input/IInputGraph.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package org.srcgll.input

import org.srcgll.descriptors.Descriptor
import org.srcgll.parser.context.IContext
import org.srcgll.rsm.RsmState
import org.srcgll.rsm.symbol.Nonterminal
import org.srcgll.rsm.symbol.Terminal
import org.srcgll.sppf.node.SppfNode

interface IInputGraph<VertexType, LabelType : ILabel> {
val vertices: MutableMap<VertexType, VertexType>
val edges: MutableMap<VertexType, MutableList<Edge<VertexType, LabelType>>>
val startVertices: MutableSet<VertexType>

fun getInputStartVertices(): MutableSet<VertexType>
fun getVertex(vertex: VertexType?): VertexType?
fun addStartVertex(vertex: VertexType)
fun addVertex(vertex: VertexType)
fun removeVertex(vertex: VertexType)

/**
* Get all outgoing edges
*/
fun getEdges(from: VertexType): MutableList<Edge<VertexType, LabelType>>
fun addEdge(from: VertexType, label: LabelType, to: VertexType)
fun removeEdge(from: VertexType, label: LabelType, to: VertexType)
fun isStart(vertex: VertexType): Boolean
fun isFinal(vertex: VertexType): Boolean

fun handleEdges(
handleTerminalOrEpsilonEdge: (
curDescriptor: Descriptor<VertexType>,
curSppfNode: SppfNode<VertexType>?,
terminal: Terminal<*>?,
targetState: RsmState,
targetVertex: VertexType,
targetWeight: Int,
) -> Unit,
handleNonterminalEdge: (
descriptor: Descriptor<VertexType>,
nonterminal: Nonterminal,
targetStates: HashSet<RsmState>,
curSppfNode: SppfNode<VertexType>?
) -> Unit,
ctx: IContext<VertexType, LabelType>,
curDescriptor: Descriptor<VertexType>,
curSppfNode: SppfNode<VertexType>?
) {
val state = curDescriptor.rsmState
val pos = curDescriptor.inputPosition
val gssNode = curDescriptor.gssNode
val terminalEdges = state.getTerminalEdges()
val nonterminalEdges = state.getNonterminalEdges()
for (inputEdge in ctx.input.getEdges(pos)) {
if (inputEdge.label.terminal == null) {
val descriptor = Descriptor(
state, gssNode, ctx.sppf.getParentNode(
state, curSppfNode, ctx.sppf.getOrCreateTerminalSppfNode(
terminal = null, pos, inputEdge.head
)
), inputEdge.head
)
ctx.addDescriptor(descriptor)
continue
}
for ((edgeTerminal, targetStates) in terminalEdges) {
if (inputEdge.label.terminal == edgeTerminal) {
for (target in targetStates) {
handleTerminalOrEpsilonEdge(curDescriptor, curSppfNode, edgeTerminal, target, inputEdge.head, 0)
}
}
}
}

for ((edgeNonterminal, targetStates) in nonterminalEdges) {
handleNonterminalEdge(curDescriptor, edgeNonterminal, targetStates, curSppfNode)
}
}


}
145 changes: 145 additions & 0 deletions src/main/kotlin/org/srcgll/input/IRecoveryInputGraph.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
package org.srcgll.input

import org.srcgll.descriptors.Descriptor
import org.srcgll.parser.context.IContext
import org.srcgll.rsm.RsmState
import org.srcgll.rsm.symbol.Nonterminal
import org.srcgll.rsm.symbol.Terminal
import org.srcgll.sppf.TerminalRecoveryEdge
import org.srcgll.sppf.node.SppfNode

interface IRecoveryInputGraph<VertexType, LabelType : ILabel> : IInputGraph<VertexType, LabelType> {
override fun handleEdges(
handleTerminalOrEpsilonEdge: (
curDescriptor: Descriptor<VertexType>,
curSppfNode: SppfNode<VertexType>?,
terminal: Terminal<*>?,
targetState: RsmState,
targetVertex: VertexType,
targetWeight: Int,
) -> Unit,
handleNonterminalEdge: (
descriptor: Descriptor<VertexType>,
nonterminal: Nonterminal,
targetStates: HashSet<RsmState>,
curSppfNode: SppfNode<VertexType>?
) -> Unit,
ctx: IContext<VertexType, LabelType>,
curDescriptor: Descriptor<VertexType>,
curSppfNode: SppfNode<VertexType>?
) {
super.handleEdges(handleTerminalOrEpsilonEdge, handleNonterminalEdge, ctx, curDescriptor, curSppfNode)
val errorRecoveryEdges = createRecoveryEdges(curDescriptor)
handleRecoveryEdges(
errorRecoveryEdges,
handleTerminalOrEpsilonEdge,
curDescriptor,
curDescriptor.rsmState.getTerminalEdges()
)
}

private fun createRecoveryEdges(curDescriptor: Descriptor<VertexType>): HashMap<Terminal<*>?, TerminalRecoveryEdge<VertexType>> {
val pos = curDescriptor.inputPosition
val state = curDescriptor.rsmState
val terminalEdges = state.getTerminalEdges()

val errorRecoveryEdges = HashMap<Terminal<*>?, TerminalRecoveryEdge<VertexType>>()
val currentEdges = getEdges(pos)

if (currentEdges.isNotEmpty()) {
addTerminalRecoveryEdges(terminalEdges, errorRecoveryEdges, pos, state, currentEdges)
} else {
addEpsilonRecoveryEdges(terminalEdges, errorRecoveryEdges, pos, state)
}

return errorRecoveryEdges
}

private fun addEpsilonRecoveryEdges(
terminalEdges: HashMap<Terminal<*>, HashSet<RsmState>>,
errorRecoveryEdges: HashMap<Terminal<*>?, TerminalRecoveryEdge<VertexType>>,
pos: VertexType,
state: RsmState
) {
for (terminal in state.errorRecoveryLabels) {
if (!terminalEdges[terminal].isNullOrEmpty()) {
errorRecoveryEdges[terminal] = TerminalRecoveryEdge(pos, weight = 1)
}
}
}

/**
* Trying to reach states that were previously inaccessible using recovery terminal
*/
private fun addTerminalRecoveryEdges(
terminalEdges: HashMap<Terminal<*>, HashSet<RsmState>>,
errorRecoveryEdges: HashMap<Terminal<*>?, TerminalRecoveryEdge<VertexType>>,
pos: VertexType,
state: RsmState,
currentEdges: MutableList<Edge<VertexType, LabelType>>
) {
for (currentEdge in currentEdges) {
if (currentEdge.label.terminal == null) continue
val currentTerminal = currentEdge.label.terminal!!

val coveredByCurrentTerminal: HashSet<RsmState> = terminalEdges[currentTerminal] ?: hashSetOf()

for (terminal in state.errorRecoveryLabels) {
//accessible states
val coveredByTerminal = HashSet(terminalEdges[terminal] as HashSet<RsmState>)

coveredByCurrentTerminal.forEach { coveredByTerminal.remove(it) }

if (terminal != currentTerminal && coveredByTerminal.isNotEmpty()) {
errorRecoveryEdges[terminal] = TerminalRecoveryEdge(pos, weight = 1)
}
}

errorRecoveryEdges[null] = TerminalRecoveryEdge(currentEdge.head, weight = 1)
}
}

private fun handleRecoveryEdges(
errorRecoveryEdges: HashMap<Terminal<*>?, TerminalRecoveryEdge<VertexType>>,
handleTerminalOrEpsilonEdge: (
curDescriptor: Descriptor<VertexType>,
curSppfNode: SppfNode<VertexType>?,
terminal: Terminal<*>?,
targetState: RsmState,
targetVertex: VertexType,
targetWeight: Int,
) -> Unit,
curDescriptor: Descriptor<VertexType>,
terminalEdges: HashMap<Terminal<*>, HashSet<RsmState>>
) {
for ((terminal, errorRecoveryEdge) in errorRecoveryEdges) {
if (terminal == null) {
handleTerminalOrEpsilonEdge(
curDescriptor,
curDescriptor.sppfNode,
null,
curDescriptor.rsmState,
errorRecoveryEdge.head,
errorRecoveryEdge.weight
)
} else {

if (terminalEdges.containsKey(terminal)) {
for (targetState in terminalEdges.getValue(terminal)) {
handleTerminalOrEpsilonEdge(
curDescriptor,
curDescriptor.sppfNode,
terminal,
targetState,
errorRecoveryEdge.head,
errorRecoveryEdge.weight
)
}
}
}
}

}


}
18 changes: 0 additions & 18 deletions src/main/kotlin/org/srcgll/input/InputGraph.kt

This file was deleted.

2 changes: 1 addition & 1 deletion src/main/kotlin/org/srcgll/input/LinearInput.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.srcgll.input

class LinearInput<VertexType, LabelType : ILabel> : IGraph<VertexType, LabelType> {
open class LinearInput<VertexType, LabelType : ILabel> : IInputGraph<VertexType, LabelType> {
override val vertices: MutableMap<VertexType, VertexType> = HashMap()
override val edges: MutableMap<VertexType, MutableList<Edge<VertexType, LabelType>>> = HashMap()

Expand Down
4 changes: 4 additions & 0 deletions src/main/kotlin/org/srcgll/input/RecoveryLinearInput.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package org.srcgll.input

class RecoveryLinearInput<VertexType, LabelType : ILabel> : LinearInput<VertexType, LabelType>(),
IRecoveryInputGraph<VertexType, LabelType>
Loading
Loading