-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #80 from uuverifiers/automaton-parser
Automaton parser
- Loading branch information
Showing
6 changed files
with
175 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package ostrich | ||
|
||
import fastparse.Parsed | ||
import ostrich.automata.{AtomicStateAutomaton, AtomicStateAutomatonAdapter, Automaton, BricsAutomaton, BricsAutomatonBuilder} | ||
|
||
import scala.collection.mutable | ||
import fastparse._ | ||
import MultiLineWhitespace._ | ||
|
||
case class Transition(from: String, to: String, range: (Int, Int)) | ||
case class AutomatonFragment(name: String, initStates: String, transitions: Seq[Transition], acceptingStates: Seq[String]) | ||
|
||
|
||
class AutomatonParser { | ||
def identifier[_: P]: P[String] = P(CharsWhileIn("a-zA-Z_0-9").!) | ||
|
||
def number[_: P]: P[Int] = P(CharsWhileIn("0-9").!).map(_.toInt) | ||
|
||
def range[_: P]: P[(Int, Int)] = P("[" ~/ number ~ "," ~ number ~ "]").map { case (start, end) => (start, end) } | ||
|
||
def transition[_: P]: P[Transition] = P(identifier ~ "->" ~ identifier ~ range ~ ";").map { | ||
case (from, to, range) => Transition(from, to, range) | ||
} | ||
def init[_: P]: P[String] = P("init" ~/ identifier ~ ";") | ||
def accepting[_: P]: P[Seq[String]] = P("accepting" ~/ identifier.rep(1, sep = ",") ~ ";").map(_.toSeq) | ||
|
||
def automaton[_: P]: P[AutomatonFragment] = P("automaton" ~/ identifier ~ "{" ~ init ~ transition.rep ~ accepting ~ "}").map { | ||
case (name, initStates, transitions, acceptingStates) => AutomatonFragment(name, initStates, transitions, acceptingStates) | ||
} | ||
|
||
def parseAutomaton(input: String): Either[String, Automaton] = parse(input, automaton(_)) match { | ||
case Parsed.Success(value, _) => Right(automatonFromFragment(value)) | ||
case f: Parsed.Failure => Left(f.msg) | ||
} | ||
|
||
def automatonFromFragment(fragment: AutomatonFragment) : Automaton = { | ||
val builder = new BricsAutomatonBuilder() | ||
val nameToState = new mutable.HashMap[String, BricsAutomaton#State]() | ||
val init = builder.getNewState | ||
nameToState.put(fragment.initStates, init) | ||
builder.setInitialState(init) | ||
|
||
for (transition <- fragment.transitions) { | ||
val source_state = nameToState.getOrElseUpdate(transition.from, builder.getNewState) | ||
val destination_state = nameToState.getOrElseUpdate(transition.to, builder.getNewState) | ||
builder.addTransition(source_state, builder.LabelOps.interval(transition.range._1.toChar,transition.range._2.toChar), destination_state) | ||
} | ||
|
||
for (accepting <- fragment.acceptingStates) { | ||
val accepting_state = nameToState.getOrElseUpdate(accepting, builder.getNewState) | ||
builder.setAccept(accepting_state, isAccepting = true) | ||
} | ||
|
||
|
||
val result = builder.getAutomaton | ||
Console.err.println("Result at \n ", result) | ||
result | ||
} | ||
} | ||
|
||
|
||
/* | ||
// Example usage | ||
val exampleInput = """automaton value_0 { | ||
init s0; | ||
s0 -> s0 [0, 31]; | ||
s0 -> s1 [32, 32]; | ||
s0 -> s0 [33, 65535]; | ||
s1 -> s1 [0, 65535]; | ||
accepting s0,s1; | ||
};""" | ||
val result = AutomatonParser.parseAutomaton(exampleInput) | ||
println(result)*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package ostrich.automata | ||
|
||
import dk.brics.automaton.{Transition, Automaton => BAutomaton} | ||
import org.scalacheck.Prop._ | ||
import org.scalacheck.Properties | ||
import ostrich.AutomatonParser | ||
|
||
object AutomatonParserTest | ||
extends Properties("AutomatonParser"){ | ||
|
||
val anyStringAut = BricsAutomaton.makeAnyString() | ||
|
||
val aPlusbPlusAut = { | ||
// Transducer encoding the capture group (a*)a* | ||
val builder = new BricsAutomatonBuilder() | ||
val init = builder.getNewState | ||
val aplus = builder.getNewState | ||
val bplus = builder.getNewState | ||
builder.setInitialState(init) | ||
|
||
builder.addTransition(init,builder.LabelOps singleton 'a', aplus) | ||
builder.addTransition(aplus,builder.LabelOps singleton 'a', aplus) | ||
|
||
builder.addTransition(aplus,builder.LabelOps singleton 'b', bplus) | ||
builder.addTransition(bplus,builder.LabelOps singleton 'b', bplus) | ||
builder.setAccept(bplus, isAccepting = true) | ||
builder.getAutomaton | ||
} | ||
|
||
property("parse success") = { | ||
|
||
val exampleInput = "automaton value_0 {init s0; s0 -> s0 [0, 31]; s0 -> s1 [32, 32]; s0 -> s0 [33, 65535]; s1 -> s1 [0, 65535]; accepting s0,s1;};" | ||
val result = new AutomatonParser().parseAutomaton(exampleInput) | ||
println(result) | ||
result.isRight | ||
} | ||
property("parse two initial states") = { | ||
// exactly one initial state | ||
val exampleInput = "automaton value_0 {init s0,s1; s0 -> s0 [0, 31]; s0 -> s1 [32, 32]; s0 -> s0 [33, 65535]; s1 -> s1 [0, 65535]; accepting s0,s1;};" | ||
val result = new AutomatonParser().parseAutomaton(exampleInput) | ||
result.isLeft | ||
} | ||
|
||
property("parse no initial state") = { | ||
// exactly one initial state | ||
val exampleInput = "automaton value_0 {init; s0 -> s0 [0, 31]; s0 -> s1 [32, 32]; s0 -> s0 [33, 65535]; s1 -> s1 [0, 65535]; accepting s0,s1;};" | ||
val result = new AutomatonParser().parseAutomaton(exampleInput) | ||
result.isLeft | ||
} | ||
|
||
property("parse equiv check") = { | ||
val exampleInput = "automaton value_0 {init s0; s0 -> s0 [0, 65535]; accepting s0;};" | ||
val result = new AutomatonParser().parseAutomaton(exampleInput) | ||
val res1 = result.isRight | ||
val aut = result.right.get | ||
val aut2 = !aut & anyStringAut | ||
val res2 = aut2.isEmpty | ||
val aut3 = aut & !anyStringAut | ||
val res3 = aut3.isEmpty | ||
res1 & res2 & res3 | ||
} | ||
|
||
property("parse equiv check 2") = { | ||
val exampleInput = "automaton value_0 {init s0; s0 -> s1 [97, 97]; s1 -> s1 [97, 97]; s1 -> s2 [98, 98]; s2 -> s2 [98, 98];accepting s2;};" | ||
val result = new AutomatonParser().parseAutomaton(exampleInput) | ||
val res1 = result.isRight | ||
val aut = result.right.get | ||
|
||
val aut2 = !aut & aPlusbPlusAut | ||
val res2 = aut2.isEmpty | ||
|
||
val aut3 = aut & !aPlusbPlusAut | ||
val res3 = aut3.isEmpty | ||
|
||
res1 & res2 & res3 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
(declare-const x String) | ||
(assert (= x "ab")) | ||
(assert (str.in.re x (re.from_automaton "automaton value_0 {init s0; s0 -> s1 [0, 100]; s1 -> s1 [0, 65535];accepting s1;};"))) |