8.7.5 Using the new libraries

Using the function MakeParser offered by our new library, we are going to implement both the naive recognizer and the naive parser producing parse trees. First, we acquire the library module from its abstract URI:

<Using the refactored library>=
declare 
[Parser] = {Module.link ['x-ozlib://oz-kurs/naive/NaiveParser.ozf']}
<Grammar Rules> 
<Instantiating the naive recognizer> 
<Instantiating the naive parser>

For the recognizer, IsPrefix is the library function for lists and MakeTree simply returns the Rule's cat.

<Instantiating the naive recognizer>=
%% recognizer
declare 
IsPrefix1 = List.isPrefix
fun {MakeTree1 Rule Cats} Rule.cat end 
NaiveRecognizer = {Parser.makeParser IsPrefix1 MakeTree1 RULES}
 
{Inspect {NaiveRecognizer [john sees the man with the telescope]}}

For the parser, IsPrefix must look at the labels of the record representing the parse trees. You already did this as an exercise. MakeTree simply packs the recognized subsequence into a new tree using List.toTuple:

<Instantiating the naive parser>=
%% parser
declare 
fun {IsPrefix2 Cats Trees}
   case Cats
   of nil then true 
   [] Cat|Cats then 
      case Trees
      of Tree|Trees then 
         Cat=={Label Tree} andthen {IsPrefix2 Cats Trees}
      else false end 
   end 
end 
fun {MakeTree2 Rule Trees}
   {List.toTuple Rule.cat Trees}
end 
NaiveParser = {Parser.makeParser IsPrefix2 MakeTree2 RULES}
 
{Inspect {NaiveParser [john sees the man with the telescope]}}


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