From 60f606940566e8cd0112374c7c72ae1d5ccb81a1 Mon Sep 17 00:00:00 2001 From: buraq Date: Tue, 18 May 2004 18:13:16 +0000 Subject: added some stuff for matching, and CaseClass to... added some stuff for matching, and CaseClass to library --- config/list/library.lst | 1 + sources/scala/runtime/matching/Grammar.scala | 90 ++++------------------------ sources/scala/runtime/matching/NonTerm.scala | 47 +++++++++------ sources/scala/runtime/matching/Rule.scala | 13 ++-- 4 files changed, 52 insertions(+), 99 deletions(-) diff --git a/config/list/library.lst b/config/list/library.lst index 1de6d2db62..69314a30c0 100644 --- a/config/list/library.lst +++ b/config/list/library.lst @@ -8,6 +8,7 @@ Array.java Boolean.java BufferedIterator.scala Byte.java +CaseClass.scala Cell.scala Char.java Console.scala diff --git a/sources/scala/runtime/matching/Grammar.scala b/sources/scala/runtime/matching/Grammar.scala index 69a7c10a27..04536b2094 100644 --- a/sources/scala/runtime/matching/Grammar.scala +++ b/sources/scala/runtime/matching/Grammar.scala @@ -6,85 +6,21 @@ import scala.collection.immutable; import scala.collection.mutable; //val ruleOrder = new Order( rule_smaller, rule_eq ); +/** runtime representation of patterns. This class treats all variable + * indices as sequence variables + */ +abstract class Grammar( theTreeTransitions:Map[Int,Set[Rule]], + theHedgeTransitions:Map[Int,Set[Rule]], + caseVars:Array[Int] ) { -case class PatternGrammar( treeRules:Set[Rule], - treeTransitions:Map[Int,Set[Rule]], - hedgeRules:Set[Rule], - hedgeTransitions:Map[Int,Set[Rule]], - thVars:mutable.Map[Int,List[Int]], - typeMapping:immutable.Map[Int,String/*Type*/], - make:NonTermFactory) { + final val treeTransitions = theTreeTransitions ; + final val hedgeTransitions = theHedgeTransitions ; + /** for cases 1..n, holds max. variable index */ + final val vars = caseVars; - val isSequenceType = false; + val treeInitials:Set[TreeNT] ; + val hedgeInitials:Set[HedgeNT] ; - /** this mapping is only for debugging purposes. Given a variable index, - ** it prints out the string representation of this variable - */ - val debugVarMapping:mutable.Map[Int,String] = - new mutable.HashMap[Int,String](); + def test( test:int, inp:Any ):boolean ; - def hedgeRulesToString:String = { - hedgeRules.foldLeft ("") { - (s:String,r:Rule) => s + (r match { - case HedgeRule( h, _ ) if ( make.hedgeInitials contains h ) => "*" - case _ => " " - }) + r.toString() + "\n" - } ; - } - - def treeRulesToString:String = { - treeRules.foldLeft ("") { - (s:String,r:Rule) => s + r.toString() + "\n" - } ; - } - - def toString:String = { - val sb = new StringBuffer(); - sb.append( { if( treeRules.isEmpty ) - "Set of rules is EMPTY.\n" else treeRulesToString }); - sb.append( { if( hedgeRules.isEmpty ) - "Set of rules is EMPTY.\n" else hedgeRulesToString }); - /* - sb.append( { if( bindMap.isEmpty ) - "Binding mapping is EMPTY.\n" else mapsToString }); - */ - sb.append( "hedgeInitials:"+make.hedgeInitials ); - sb.append( "treeInitials:"+make.treeInitials ); - sb.append( "\ntreeTransitions:"+treeTransitions ); - sb.append( "\nhedgeTransitions:"+hedgeTransitions ); - sb.append( "\nvariables :"+( thVars( 0 ).map { debugVarMapping }) ); - sb.append( "\ntypeMapping :"+ typeMapping.toString() ); - sb.toString() - } -} - - - -/* -def followChainRules( nset:Set[HedgeNT], - gram:Set[Rule] ):immutable.Set[HedgeNT] = { - //Console.println("followChainRules"); - var dirty = false; -var newnset = immutable.ListSet.Empty[HedgeNT].incl( nset ); -for( val nt <- nset ) { - for( val rule <- gram ) { - val NT = nt; -rule match { - case HedgeChainRule( NT, h ) => { - if( !newnset.contains( h ) ) { - dirty = true; -newnset = newnset + h; - } - } - case _ => -} - } -} -if( dirty ) { - followChainRules( newnset, gram ); -} else { - //Console.println("followChainRules output" + newnset); - newnset } - } -*/ diff --git a/sources/scala/runtime/matching/NonTerm.scala b/sources/scala/runtime/matching/NonTerm.scala index 1f79ae7a79..6480e3659c 100644 --- a/sources/scala/runtime/matching/NonTerm.scala +++ b/sources/scala/runtime/matching/NonTerm.scala @@ -6,39 +6,57 @@ abstract class NonTerm { override def toString() = this match { case TreeNT( i ) => "t"+i; - case HedgeNT( i ) => "h"+i; + case h @ HedgeNT( i ) => "h"+i+{ if( h.nullable )"~" else "" }; } + } +/** tree nonterminal. holds set of binding variable indices. + */ case class TreeNT(i:int) extends NonTerm with Ordered[TreeNT] { + + var vset:immutable.Set[Int] = new immutable.TreeSet[Int]() ; + + /** vset should be sorted to allow fast lookup */ + def this( i:int, vset:immutable.Set[Int] ) = { + this( i ); + this.vset = new immutable.TreeSet[Int]() incl vset ; + } + def compareTo [b >: TreeNT <% Ordered[b]](y: b): int = y match { case TreeNT( y1 ) => i compareTo y1; case _ => -(y compareTo this) } - var vset:immutable.Set[Int] = immutable.ListSet.Empty ; }; + +/** hedge nonterminals. holds nullable property. + */ case class HedgeNT(i:int) extends NonTerm with Ordered[HedgeNT] { + + var nullable:boolean = false; + + def this( i:int, nullable:boolean ) = { + this( i ); + this.nullable = nullable; + } + def compareTo [b >: HedgeNT <% Ordered[b]](y: b): int = y match { case HedgeNT( y1 ) => i compareTo y1; case _ => -(y compareTo this) } - var nullable:boolean = false; - //val vset:immutable.Set[Int] = immutable.ListSet.Empty ; }; -object EmptySeqNT extends HedgeNT(0) { - override val nullable = true; - }; -object AnyHedgeNT extends HedgeNT(1); // for the "anytree-rule": (A,("",AnyTreeNT)) -object ANYTREE extends TreeNT(1); -// for the "anynode-rule": (A,("",HedgeNT B)) +object EMPTYHEDGE extends HedgeNT( 0, true ) ; +object ANYHEDGE extends HedgeNT( 1, true ) ; +object ANYTREE extends TreeNT( 1 ); +/** factory for nonterminals. maintains list of initial nonterminals + */ class NonTermFactory { var treeInitials:immutable.Set[TreeNT] = new immutable.TreeSet[TreeNT](); var hedgeInitials:immutable.Set[HedgeNT] = new immutable.TreeSet[HedgeNT](); - //private var counter = 4; private var treeCounter = 2; private var hedgCounter = 2; @@ -49,12 +67,7 @@ class NonTermFactory { } def TreeNT( vs:immutable.Set[Int] ): TreeNT = { - /* - val x = new TreeNT( treeCounter ) { - override var vset = vs; - }; - */ - val x = new TreeNT( treeCounter ); + val x = new TreeNT( treeCounter, vs ); x.vset = vs; treeCounter = treeCounter + 1; x diff --git a/sources/scala/runtime/matching/Rule.scala b/sources/scala/runtime/matching/Rule.scala index 1ac1991022..5d33f2d8f9 100644 --- a/sources/scala/runtime/matching/Rule.scala +++ b/sources/scala/runtime/matching/Rule.scala @@ -80,6 +80,9 @@ abstract class Rule with Ordered[Rule] { } } + +abstract class TRule extends Rule; +abstract class HRule extends Rule; /* a tree rule is of the from A -> s(B) where A,B are TreeNTs and s is an identifier (string). @@ -87,15 +90,15 @@ where A,B are TreeNTs and s is an identifier (string). If s is the empty string, then the node label is arbitrary If HedgeNT is AnyHedgeNT, then the tree is arbitrary */ -case class HedgeChainRule( n: HedgeNT, rhs: HedgeNT ) extends Rule; -case class TreeRule( n:TreeNT, rhs:Pair[Int,HedgeNT] ) extends Rule { +case class HedgeChainRule( n: HedgeNT, rhs: HedgeNT ) extends HRule; +case class TreeRule( n:TreeNT, rhs:Pair[Int,HedgeNT] ) extends TRule { def this(i:int, s:Int, n:int ) = { this( new TreeNT(i), new Pair(s, new HedgeNT(n))); } }; -case class AnyTreeRule( n:TreeNT ) extends Rule { +case class AnyTreeRule( n:TreeNT ) extends TRule { } -case class AnyNodeRule( n:TreeNT, h:HedgeNT ) extends Rule { +case class AnyNodeRule( n:TreeNT, h:HedgeNT ) extends TRule { } -case class HedgeRule( n:HedgeNT, rhs:Pair[TreeNT,HedgeNT] ) extends Rule; +case class HedgeRule( n:HedgeNT, rhs:Pair[TreeNT,HedgeNT] ) extends HRule; -- cgit v1.2.3