dacceptor.tcl
Go to the documentation of this file.00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 package require snit        ; 
00017 package require struct:: ; # Tcllib =  | Extended  operations = .
00018 
00019 
00020 
00021 
00022 snit::type ::grammar::fa::dacceptor {
00023     
00024     
00025 
00026     
00027     
00028 
00029     
00030     
00031 
00032     ret  accept? (type symbolstring) {}
00033 
00034     option -any     {}
00035 
00036     
00037     
00038 
00039     
00040     
00041     
00042     
00043     
00044 
00045     variable start ; 
00046     variable final ; 
00047     variable trans ; 
00048     variable sym   ; 
00049     variable any   ; 
00050     
00051     
00052     variable stop  ; 
00053 
00054     
00055     
00056 
00057     constructor {fa args} {
00058      any =  {}
00059     $self configurelist $args
00060 
00061     if {![$fa is deterministic]} {
00062         return -code error "Source FA is not deterministic"
00063     }
00064     if {($any ne "") && ![$fa symbol exists $any]} {
00065         return -code error "Chosen any symbol \"$any\" does not exist"
00066     }
00067 
00068     if {![$fa is complete]} {
00069          istmp =  1
00070          tmp =  [grammar::fa ${selfns}::fa = $fa]
00071          before =  [$tmp states]
00072         $tmp complete
00073         
00074          stop =  [struct:: difference =  [$tmp states] $before]
00075     } else {
00076          istmp =  0
00077          tmp =  $fa
00078         
00079          stop =  {}
00080     }
00081 
00082      start =  [lindex [$tmp startstates] 0]
00083     foreach s [$tmp finalstates]        { final = ($s) .}
00084     foreach s [ syms =  [$tmp symbols]] { sym = ($s) .}
00085 
00086     foreach s [$tmp states] {
00087         foreach sy $syms {
00088          trans = ($s,$sy) [lindex [$tmp next $s $sy] 0]
00089         }
00090     }
00091 
00092     if {$istmp} {$tmp destroy}
00093     return
00094     }
00095 
00096     
00097 
00098     onconfigure -any {value} {
00099      options = (-any) $value
00100      any =            $value
00101     return
00102     }
00103 
00104     
00105 
00106     ret  accept? (type symbolstring) {
00107     set state $start
00108 
00109     ## puts "\n====================== ($symbolstring)"
00110 
00111     if {$any eq ""} {
00112         # No any mapping of unknown symbols.
00113 
00114         foreach sy $symbolstring {
00115         if {![info exists sym($sy)]} {
00116             # Bad symbol in input. String is not accepted,
00117             # abort immediately.
00118             ## puts " \[$state\] -- Unknown symbol ($sy)"
00119             return 0
00120         }
00121 
00122         ## puts " \[$state\] --($sy)--> "
00123 
00124         set state $trans($state,$sy)
00125         # state == "" cannot happen, as our FA is complete.
00126         if {$state eq $stop} {
00127             # This is a known sink, we can stop processing input now.
00128             ## puts " \[$state\] FULL STOP"
00129             return 0
00130         }
00131         }
00132 
00133     } else {
00134         # Mapping of unknown symbols to any.
00135 
00136         foreach sy $symbolstring {
00137         if {![info exists sym($sy)]} {set sy $any}
00138         ## puts " \[$state\] --($sy)--> "
00139         set state $trans($state,$sy)
00140         # state == "" cannot happen, as our FA is complete.
00141         if {$state eq $stop} {
00142             # This is a known sink, we can stop processing input now.
00143             ## puts " \[$state\] FULL STOP"
00144             return 0
00145         }
00146         }
00147     }
00148 
00149     ## puts " \[$state\][expr {[info exists final($state)] ? " ACCEPT" : ""}]"
00150 
00151     return [info exists final($state)]
00152     }
00153 
00154     
00155     
00156 
00157     
00158     
00159 
00160     
00161 }
00162 
00163 
00164 
00165 
00166 package provide grammar::fa::dacceptor 0.1.1
00167