Skip to content

Commit

Permalink
implement offline recovery
Browse files Browse the repository at this point in the history
  • Loading branch information
bachish committed May 21, 2024
1 parent b85f892 commit a25b64a
Show file tree
Hide file tree
Showing 14 changed files with 193 additions and 134 deletions.
55 changes: 31 additions & 24 deletions benchmarks/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -36,35 +36,42 @@ fun getArgs(strFolder: String): Array<String> {
val files = resourcesDir.listFiles()!!
return files.map { it.toString() }.sorted().toTypedArray()
}

benchmark {
configurations {
named("main") {
val dataset = "dataset"
if (!hasProperty(dataset)) {
throw Exception("Error! Set dataset folder by property '$dataset'")
}
param("fileName", *getArgs(property(dataset).toString()))
this.reportFormat = "csv"
iterations = 15
iterationTime = 2000
iterationTimeUnit = "ms"
warmups = 5
outputTimeUnit = "ms"
mode = "avgt"
val tools = "toolName"
if (hasProperty(tools)) {
println("Run benchmarks for: .*${property(tools)}.*")
include(".*${property(tools)}.*")
}

named("main") {}
targets {
register("main")
}
}
targets {
register("main")
}

}
//benchmark {
// configurations {
// named("main") {
// val dataset = "dataset"
// if (!hasProperty(dataset)) {
// println("Error! Set dataset folder by property '$dataset'")
// throw Exception("Error! Set dataset folder by property '$dataset'")
// }
// param("fileName", *getArgs(property(dataset).toString()))
// this.reportFormat = "csv"
// iterations = 15
// iterationTime = 1000
// iterationTimeUnit = "ms"
// warmups = 5
// outputTimeUnit = "ms"
// mode = "avgt"
// val tools = "toolName"
// if (hasProperty(tools)) {
// println("Run benchmarks for: .*${property(tools)}.*")
// include(".*${property(tools)}.*")
// }
//
// }
// }
// targets {
// register("main")
// }
//}


allOpen {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,10 @@ import org.antlr.Java8Lexer
import org.antlr.Java8Parser
import org.antlr.v4.runtime.CharStreams
import org.antlr.v4.runtime.CommonTokenStream
import java.io.File


@State(Scope.Benchmark)
class AntlrBench {

@Param("Throwables.java")
var fileName: String = ""

lateinit var fileContents: String

@Setup
fun prepare() {
fileContents = File(fileName).readText()
}
class Antlr : BaseBench(){

@Benchmark
fun measureAntlr(blackhole: Blackhole) {
Expand Down
17 changes: 17 additions & 0 deletions benchmarks/src/main/kotlin/org/BaseBench.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org

import kotlinx.benchmark.*
import java.io.File
@State(Scope.Benchmark)
abstract class BaseBench {
@Param("Throwables.java")
var fileName: String = ""

lateinit var fileContents: String

@Setup
open fun prepare() {
fileContents = File(fileName).readText()
}

}
15 changes: 10 additions & 5 deletions benchmarks/src/main/kotlin/org/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,18 @@ fun getAnrltTokens(file: String): Int{
return stream.numberOfOnChannelTokens
}
fun calculateTokens(){
val pth = Main::class.java.getResource("/junit")
val resourcesDir = File(pth.toURI())
val stat = File("junit-tokens.csv").printWriter()
val resourcesDir = File("/home/olga/gllgen/dataset_black_box/too_little")
val stat = File("too-little-tokens.csv").printWriter()
stat.println("Param: fileName,Line Count")
for (file in resourcesDir.listFiles()!!){
// stat.println("${file.name},${getTokenStream(file.readText()).edges.size}")
stat.println("${file.name},${getAnrltTokens(file.readText())}")
try {
stat.println("${file.path},${getTokenStream(file.readText()).edges.size}")
// stat.println("${file.path},${getAnrltTokens(file.readText())}")

}
catch(e: Error){
println(file.name)
}
}
stat.close()
}
Expand Down
16 changes: 16 additions & 0 deletions benchmarks/src/main/kotlin/org/OfflineGll.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org

import kotlinx.benchmark.*
import org.ucfs.input.LinearInputLabel


@State(Scope.Benchmark)
class OfflineGll : BaseBench() {

@Benchmark
fun measureGll(blackhole: Blackhole) {
val parser = org.ucfs.Java8Parser<Int, LinearInputLabel>()
parser.input = getTokenStream(fileContents)
blackhole.consume(parser.parse())
}
}
37 changes: 0 additions & 37 deletions benchmarks/src/main/kotlin/org/OfflineGllBench.kt

This file was deleted.

29 changes: 29 additions & 0 deletions benchmarks/src/main/kotlin/org/OnlineGll.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org

import kotlinx.benchmark.*
import org.junit.Before
import org.ucfs.Java8
import org.ucfs.input.LinearInput
import org.ucfs.input.LinearInputLabel
import org.ucfs.parser.Gll


@State(Scope.Benchmark)
class OnlineGll : BaseBench(){

val startState = Java8().rsm
lateinit var tokens: LinearInput<Int, LinearInputLabel>

@Setup
@Before
override fun prepare() {
super.prepare()
tokens = getTokenStream(fileContents)
}

@Benchmark
fun measureGll(blackhole: Blackhole) {
val gll = Gll.gll(startState, getTokenStream(fileContents))
blackhole.consume(gll.parse())
}
}
49 changes: 0 additions & 49 deletions benchmarks/src/main/kotlin/org/OnlineGllBench.kt

This file was deleted.

31 changes: 31 additions & 0 deletions benchmarks/src/main/kotlin/org/RecoveryOfflineGll.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org

import kotlinx.benchmark.*
import org.ucfs.Java8
import org.ucfs.input.LinearInputLabel
import org.ucfs.parser.Gll


@State(Scope.Benchmark)
class RecoveryOfflineGll : BaseBench() {

@Benchmark
fun measureGll(blackhole: Blackhole) {
val parser = org.ucfs.Java8Parser<Int, LinearInputLabel>()
parser.input = getRecoveryTokenStream(fileContents)
blackhole.consume(parser.parse())
}
}
//fun main(){
// val inp = getRecoveryTokenStream(
// """ public clas HellowWorldCTY{
// }
//
// """.trimIndent()
// )
// val p = Gll.recoveryGll(Java8().rsm, inp)
// assert(p.parse().first != null)
// val parser = org.ucfs.Java8Parser<Int, LinearInputLabel>()
// parser.input = inp
// assert(parser.parse().first != null)
//}
29 changes: 29 additions & 0 deletions benchmarks/src/main/kotlin/org/RecoveryOnlineGll.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org

import kotlinx.benchmark.*
import org.junit.Before
import org.ucfs.Java8
import org.ucfs.input.LinearInput
import org.ucfs.input.LinearInputLabel
import org.ucfs.parser.Gll


@State(Scope.Benchmark)
class RecoveryOnlineGll : BaseBench(){

val startState = Java8().rsm
lateinit var tokens: LinearInput<Int, LinearInputLabel>

@Setup
@Before
override fun prepare() {
super.prepare()
tokens = getRecoveryTokenStream(fileContents)
}

@Benchmark
fun measureGll(blackhole: Blackhole) {
val gll = Gll.recoveryGll(startState, getRecoveryTokenStream(fileContents))
blackhole.consume(gll.parse())
}
}
22 changes: 18 additions & 4 deletions generator/src/main/kotlin/org/ucfs/parser/GeneratedParser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import org.ucfs.grammar.combinator.Grammar
import org.ucfs.input.Edge
import org.ucfs.input.IInputGraph
import org.ucfs.input.ILabel
import org.ucfs.input.IRecoveryInputGraph
import org.ucfs.parser.context.Context
import org.ucfs.parser.context.RecoveryContext
import org.ucfs.rsm.RsmState
import org.ucfs.rsm.symbol.ITerminal
import org.ucfs.rsm.symbol.Nonterminal
Expand All @@ -20,7 +22,11 @@ abstract class GeneratedParser<VertexType, LabelType : ILabel> :
return ctx.input
}
set(value) {
ctx = Context(grammar.rsm, value)
ctx = if(value is IRecoveryInputGraph){
RecoveryContext(grammar.rsm, value)
} else {
Context(grammar.rsm, value)
}
}

//protected abstract val ntFuncs: HashMap<String, (Descriptor<VertexType>, SppfNode<VertexType>?) -> Unit>
Expand All @@ -37,18 +43,17 @@ abstract class GeneratedParser<VertexType, LabelType : ILabel> :
// val handleEdges = ntFuncs[nt] ?: throw ParsingException("Nonterminal ${nt.name} is absent from the grammar!")

val pos = descriptor.inputPosition

ctx.descriptors.addToHandled(descriptor)
val curSppfNode = descriptor.sppfNode
val epsilonSppfNode = ctx.sppf.getEpsilonSppfNode(descriptor)

val leftExtent = curSppfNode?.leftExtent
val rightExtent = curSppfNode?.rightExtent

if (state.isFinal) {
pop(descriptor.gssNode, curSppfNode ?: epsilonSppfNode, pos)
}

ctx.descriptors.addToHandled(descriptor)

if (state.isStart && state.isFinal) {
checkAcceptance(
epsilonSppfNode,
Expand All @@ -60,6 +65,15 @@ abstract class GeneratedParser<VertexType, LabelType : ILabel> :
checkAcceptance(curSppfNode, leftExtent, rightExtent, nt)

callNtFuncs(nt, descriptor, curSppfNode)
val input = ctx.input
if(input is IRecoveryInputGraph){
val errorRecoveryEdges = input.createRecoveryEdges(descriptor)
input.handleRecoveryEdges(
errorRecoveryEdges,
this::handleTerminalOrEpsilonEdge,
descriptor
)
}
// ntFuncs[nt.name]!!(descriptor, curSppfNode)
}

Expand Down
9 changes: 9 additions & 0 deletions run_bench.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
for dataset in /home/olga/gllgen/dataset_black_box/too_little #/home/olga/gllgen/java7
do
for tool in Antlr Online Offline
do
echo "running $tool on $dataset, start at $(date)"
gradle benchmark -PtoolName=$tool -Pdataset=$dataset >> stdout_$tool.txt 2>> stderr_$tool.txt
done
done

Loading

0 comments on commit a25b64a

Please sign in to comment.