Skip to content

Commit

Permalink
Fix issues
Browse files Browse the repository at this point in the history
  • Loading branch information
bachish committed Nov 9, 2023
1 parent 5485c87 commit 596a0f3
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 23 deletions.
28 changes: 22 additions & 6 deletions src/main/kotlin/org/kotgll/grammar/combinator/Grammar.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,31 @@ object GlobalState {

open class Grammar {
val nonTerms = ArrayList<NT>()
private lateinit var startState: NT
fun setStart(state: Regexp) {
if (state is NT) {
startState = state

private var startState: RSMState? = null
private lateinit var startNt: NT
fun setStart(expr: Regexp) {
if (expr is NT) {
startNt = expr
} else throw IllegalArgumentException("Only NT object can be start state for Grammar")
}

/**
* Builds or returns a Rsm built earlier for the grammar
*/
fun getRsm(): RSMState {
if (startState == null) {
buildRsm()
}
return startState as RSMState
}

fun toRsm(): RSMState {
/**
* Builds a new Rsm for the grammar
*/
fun buildRsm(): RSMState {
nonTerms.forEach { it.buildRsmBox() }
return startState.nonTerm.startState
startState = startNt.getNonterminal()?.startState
return startState as RSMState
}
}
13 changes: 12 additions & 1 deletion src/main/kotlin/org/kotgll/grammar/combinator/Many.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,15 @@ data class Many(val exp: Regexp) : Regexp() {
else -> Concat(newReg, Many(exp))
}
}
}
}

fun Many(termValue: String): Many {
return Many(Term(termValue))
}

val String.many: Many
get() = Many(this)

val Regexp.many: Many
get() = Many(this)

8 changes: 5 additions & 3 deletions src/main/kotlin/org/kotgll/grammar/combinator/NT.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import java.util.*
import kotlin.reflect.KProperty

open class NT : Symbol() {
lateinit var nonTerm: Nonterminal
private lateinit var nonTerm: Nonterminal
private lateinit var rsmDescription: Regexp

private fun getNewState(regex: Regexp): RSMState {
Expand All @@ -34,14 +34,16 @@ open class NT : Symbol() {
if (!regexpToRsmState.containsKey(newState)) {
regexpToProcess.add(newState)
}
val toState = if (newState is Epsilon) getNewState(newState)
else regexpToRsmState.getOrPut(newState) { getNewState(newState) }
val toState = regexpToRsmState.getOrPut(newState) { getNewState(newState) }
when (symbol) {
is Term -> {
state?.addTerminalEdge(RSMTerminalEdge(symbol.terminal, toState))
}

is NT -> {
if (!symbol::nonTerm.isInitialized) {
throw IllegalArgumentException("Not initialized NT used in description of \"${nonTerm.name}\"")
}
state?.addNonterminalEdge(RSMNonterminalEdge(symbol.nonTerm, toState))
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/main/kotlin/org/kotgll/grammar/combinator/Term.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package org.kotgll.grammar.combinator

import org.kotgll.rsm.grammar.symbol.Terminal

open class Term(val text: String) : Symbol() {
open class Term(private val text: String) : Symbol() {
val terminal: Terminal = Terminal(text)
}

val String.t: Term
get() = Term(this)
12 changes: 8 additions & 4 deletions src/test/kotlin/grammar/combinator/dsl/AManyTest.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package grammar.combinator.dsl

import org.junit.jupiter.api.Test
import org.kotgll.grammar.combinator.*
import org.kotgll.grammar.combinator.Grammar
import org.kotgll.grammar.combinator.Many
import org.kotgll.grammar.combinator.NT
import org.kotgll.grammar.combinator.t
import org.kotgll.rsm.grammar.toString

class AManyTest : DslTest{
class AManyTest : DslTest {
class AStar : Grammar() {
var A by NT()
var S by NT()

init {
A = Term("a")
A = "a".t
S = Many(A)
setStart(S)
}
Expand All @@ -18,7 +22,7 @@ class AManyTest : DslTest{

@Test
fun someTest() {
val rsm = AStar().toRsm()
val rsm = AStar().buildRsm()
generateOutput("aMany.txt", toString(rsm))
}
}
4 changes: 2 additions & 2 deletions src/test/kotlin/grammar/combinator/dsl/ManyTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ class ManyTest : DslTest {
var S by NT()

init {
S = Many(Term("a")) * Many(Term("a") * Term("b"))
S = Many("a") * Many("a" * "b")
setStart(S)
}
}

@Test
fun someTest() {
val rsm = StarTest().toRsm()
val rsm = StarTest().buildRsm()
generateOutput("many.txt", toString(rsm))
}
}
2 changes: 1 addition & 1 deletion src/test/kotlin/grammar/combinator/dsl/MultiDyckTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class MultiDyckTest : DslTest {

@Test
fun someTest() {
val rsm = MultiDyckGrammar().toRsm()
val rsm = MultiDyckGrammar().buildRsm()
generateOutput("MultiDyckGenerated.txt", toString(rsm))
}
}
16 changes: 11 additions & 5 deletions src/test/kotlin/grammar/combinator/dsl/WhileTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package grammar.combinator.dsl

import org.junit.jupiter.api.Test
import org.kotgll.grammar.combinator.*
import org.kotgll.grammar.combinator.Many
import org.kotgll.rsm.grammar.toString

class WhileTest : DslTest {
Expand All @@ -16,14 +17,14 @@ class WhileTest : DslTest {
var Id by NT()
var NumVar by NT()
var BoolVar by NT()
var Lit = Term("a..z")
var Num = Term("0..9")
var Text = Term("...any..text...")
var Lit by NT()
var Num by NT()
var Text by NT()

init {
Program = SeqStatement
SeqStatement = Statement or Statement * ";" * SeqStatement
Statement = Id * ":=" or "skip" or (
Statement = Id * ":=" * NumExpr or "skip" or (
"print" * Text) or (
"print" * NumExpr) or (
"{" * SeqStatement * "}") or (
Expand All @@ -37,14 +38,19 @@ class WhileTest : DslTest {
Id = Lit or Lit * Id
NumVar = Num or Num * NumVar
BoolVar = "false" or "true"
Lit =
"a" * "b" * "c" * "d" * "e" * "f" * "g" * "h" * "i" * "j" * "k" * "l" * "m" * "n" * "o" * "p" * "q" * "r" * "s" * "t" * "u" * "v" * "w" * "x" * "y" * "z"
Num = "1" * "2" * "3" * "4" * "5" * "6" * "7" * "8" * "9" * "0"
setStart(Program)
//todo correctly present text
Text = "\"" * Many(Lit or Num) * "\""
}
}


@Test
fun someTest() {
val rsm = MultiDyckGrammar().toRsm()
val rsm = MultiDyckGrammar().buildRsm()
generateOutput("while.txt", toString(rsm))
}
}

0 comments on commit 596a0f3

Please sign in to comment.