Wednesday, February 27, 2008

Update: Context-free in Scala

After reviewing my implementation in Scala I discovered, that both Scala and Java implementation allow method chains like a().a().b().a().b().b(). In fact the presented grammar only allows words starting with n calls of a() and ending with n calls of b().

Here is a patched version of the code:
object AbLanguage {  
def a = new AfterABase

protected class AfterBBase {
def b = {}
}

protected class AfterABase extends AfterBBase {
def a = new AfterA[AfterBBase](this)
}

protected class AfterB[T](sourroundingExpression : T) {
def b = sourroundingExpression
}

protected class AfterA[T](sourroundingExpression : T) extends AfterB[T](sourroundingExpression) {
def a = new AfterA[AfterB[T]](this)
}

def main(args : Array[String]) : Unit = {
a b;
((a a) b) b;
a.a.b.b;
}
}

The return type of method a() itself allows a further call of a(), instead of that the enclosed type does not have a method a().

No comments: