G.I.S. Prolog DCG grammar parser

The Definite Clause Grammar (DCG) Parser inside G.I.S. Prolog is invisibly called whenever a new clause of the form "A --> B,..." is asserted. G.I.S. Prolog uses a very efficient DCG grammar parser, written and optimised from scratch (even though similar source code for such a parser exists in many Prolog textbooks). If you want to take a closer look at the source-code of this parser, use the command "listing($)".

Notes:

1) The original P.I.E. inference engine, which formed the basis for G.I.S. Prolog as well, did not include a DCG grammar parser.

2) In accordance with traditional Prolog practices, the DCG grammar parser treats any sections of code surrounded by curly brackets ({...}) as code that should be passed on to the parser's output without any changes. In G.I.S. Prolog, any expression surrounded by curly brackets { EXPR... } is translated into the form 'k$'(EXPR...), and if this appears inside a Grammar Rule such as "A --> B,...{EXPR}...", then it will be preserved intact inside the parser's output code.

Examples:

1) sentence( s(N,V) ) --> noun_phrase( N ), verb_phrase( V ).

DCG Parser Output code (asserted):

sentence(s(_1,_2),C,D):-

noun_phrase(_1,C,_3),

verb_phrase(_2,_3,D).

2) num(X) --> [C], {nonvar(C),"0" =< C, C =< "9", X is C - "0"}.

DCG Parser Output code (asserted):

num(_1,C,D):-

'C'(C,_2,D),

nonvar(_2),

"0" =< _2,

_2 =< "9",

_1 is _2 - "0".

3) Natural Language DCG parsing:

sent( s(N,V) ) --> noun_phr( N ), verb_phr( V ).

det( det(the) ) --> [the].

n( noun(house) ) --> [house].

n( noun(boy) ) --> [boy].

v( verb(likes) ) --> [likes].

noun_phr( np(D,N) ) --> det( D ), n( N ).

verb_phr( vp(V,N) ) --> v( V ), noun_phr( N ).

test:- SentenceL = [the,boy,likes,house],

sent(ParserOut,SentenceL,[]), write(ParserOut).

RESULT (from G.I.S. Prolog):

test.

s(np(det(the),noun(boy)),vp(verb(likes),np(det(the),noun(house))))

True

1 Solution