22.2 A Concurrent Chart Parser

<Test the Concurrent Parser>=
declare   
<RunAgenda> 
<MakeAgenda> 
local  
   Dir='www.ps.uni-sb.de/~niehren/vorlesung/Programs/Functors/'   
   URL='http://'#Dir
   [GrammarMod] = {Module.link [URL#'Grammar.ozf']}
in 
   Rules = GrammarMod.rules
   Lexicon = GrammarMod.lexicon
end 
 
fun{Parse Words}
    Agenda = {MakeAgenda Rules Lexicon Words}
    Edges = {RunAgenda Agenda}  
    {Browse Edges}
    FullEdges ={Filter Edges fun{$ E}  
                                {And E.left==1  
                                 E.right=={Length Words}+1}
                             end}
in 
    {Map FullEdges fun{$ E}  
                      {E.pred}.head
                   end}
end 
 
/*
 
declare Words = [john saw the man with the telescope]
{Browse {Parse Words}}
 
*/

<MakeAgenda>=
fun{MakeAgenda Rules Lexicon Words}
   fun{MoveDot Rule}
      case Rule of rule(head:Head done:Done todo:Next|Todo dot:Index)
      then rule(head:Head done:Next|Done todo:Todo dot:Index+1) end 
   end 
       
   fun{ProcessInfo Agent Edge}% computes new a info item or a new agent  
                              % whenever possible by applying the
                              % dotted rule of the Agent to the Edge
      fun{Create Pred Right}
         if Agent.rule.todo.2==nil then 
            info(edge(left: Agent.left
                      right  : Right
                      cat    : Agent.rule.head
                      pred   : Pred))
         else 
            agent(edge(left:Agent.left
                       right:Right
                       rule:{MoveDot Agent.rule}
                       pred:Pred))
         end 
      end 
   in 
      if Edge.left==Agent.right andthen 
         Edge.cat==Agent.rule.todo.1
      then 
         {Map
          {Search.allP
           proc {$ Tree}
              {Agent.pred Tree}
              {Edge.pred}.head = Tree.args.(Agent.rule.dot)
           end 1 _}
          fun{$ Pred} {Create Pred Edge.right} end}
      else 
         nil
      end 
   end  
 
   proc{Initialize Board}  
      {List.forAllInd Words
       proc {$ I Word}
          {ForAll {Lexicon Word}
           proc {$ Cat#Fun}
              fun {Pred} root(head:{Fun} args:args) end 
           in 
              {Board.post info(edge(left:I right:I+1
                                    cat:Cat pred:Pred))} end}
       end}
      {ForAll Rules
       proc {$ Rule}
          %% -- for each rule
          case Rule of rule(Head Body Fun) then 
             N = {Length Body}
             R = rule(head:Head done:nil todo:Body dot:1)
             fun {Pred}
                Args = {Tuple.make args N}
             in 
                root(head:{Fun Args} args:Args)
             end 
          in 
             {List.forAllInd Words
              proc {$ I _}
                 %% -- for each position
                 {Board.startAgent edge(left:I right:I rule:R pred:Pred)}
              end}
          end 
       end}
   end 
 
in 
   agenda(initialize:Initialize
          processInfo:ProcessInfo)
end   


Denys Duchier, Claire Gardent and Joachim Niehren
Version 1.3.99 (20050412)