home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Simtel MSDOS 1992 September
/
Simtel20_Sept92.cdr
/
msdos
/
turbopas
/
spoc88.arc
/
DCG.ARC
/
GRAMMAR.PRO
< prev
next >
Wrap
Text File
|
1988-06-17
|
3KB
|
109 lines
/* Simple DCG parser
Barbara Clinger, 1988
This program illustrates the expansion of a simple DCG.
Its vocabulary consists of:
Nouns: John, Mary, man, dog;
Determiners: the, a
verbs: likes, sees
Sample input: The man sees a dog.
Output: True or False, for success or failure of parsing.
*/
domains
toklist = string*
predicates
reader(string,toklist) /* the reader */
remove_period(toklist,toklist)
append(toklist,toklist,toklist)
do
/* The grammar */
sentence(toklist,toklist,toklist) /* the parser */
noun_phrase(toklist)
verb_phrase(toklist)
determiner(string)
noun(string)
verb(string)
goal
do.
clauses
/* The clause do parses a sentence and returns true or false. Its
writing is informational only. */
do :-
nl,write("Enter a sentence --> "),
readln(S),nl,nl,
reader(S,List), /* use the reader */
write("Output of the reader: ",List),nl,nl,
remove_period(List, List_in),
sentence(List_in,Noun_phrase,Verb_phrase),
write(" Noun phrase: ",Noun_phrase),nl,
write("Verb phrase: ",Verb_phrase),nl.
/*
Using append to split the List_in into possible noun phrases and
verb phrases is not efficient, but for simple grammars it is
adequate.
*/
/* expansion of:
sentence --> noun_phrase, verb_phrase
*/
sentence(List_in,Noun_list_out,Verb_list_out) :-
append(Noun_list_out,Verb_list_out,List_in),
noun_phrase(Noun_list_out),!,verb_phrase(Verb_list_out).
/* expansion of:
noun_phrase --> determiner, noun
noun_phrase --> noun
*/
noun_phrase([A,B]) :- determiner(A),noun(B).
noun_phrase([A]) :- noun(A).
/* expansion of:
verb_phrase --> verb, noun_phrase
verb_phrase --> verb, noun
verb_phrase --> verb
*/
verb_phrase([A|B]) :- verb(A), noun_phrase(B).
verb_phrase([A,B]) :- verb(A),noun(B).
verb_phrase([A]) :- verb(A).
/* the dictionary */
determiner("the").
determiner("a").
noun("man").
noun("john").
noun("mary").
noun("dog").
verb("likes").
verb("sees").
/* end of dictionary */
/* reader
(1) the empty string returns the empty list,
(2) if the string is not empty, it recursively takes the front
token, converts it to lower case, then reads the rest of the
list, until the string is empty.
*/
reader("",[]) :- !.
reader(Str,[Token|Rest]) :-
fronttoken(Str,Tok,Str1),
upper_lower(Tok,Token),
reader(Str1,Rest),!.
/* removes the period from list of tokens, if it exists */
remove_period(L1,L2) :-
append(L2,["."],L1).
remove_period(L1,L1).
append([],List,List).
append([H|T],L,[H|T2]) :-
append(T,L,T2).