<< Prev | - Up - |
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:
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
.
%% 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
:
%% 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]}}
<< Prev | - Up - |