Dcg interpreter

Description

A definite clause grammar (DCG) interpreter

Source: The Art of Prolog

Download

Program source code: dcg_interpreter.pl

Listing

/*
        parse(Start,Tokens) :-
            The sequence of tokens Tokens represented as a difference-list
            can be reached by applying the grammar rules defined by -->/2,
            starting from Start.
*/
 
:- op(40,xfx,\).
:-  op( 800, xfx, -->).
 
        parse(A,Tokens) :-
            nonterminal(A), A --> B, parse(B,Tokens).
        parse((A,B),Tokens\Xs) :-
            parse(A,Tokens\Tokens1), parse(B,Tokens1\Xs).
 
        parse(A,Tokens) :-  terminals(A), connect(A,Tokens).
        parse({A},Xs\Xs) :-  A.
 
	terminals(Xs) :- list(Xs).
 
	list([]).
	list([X|Xs]) :- list(Xs).
 
        connect([ ],Xs\Xs).
        connect([W|Ws],[W|Xs]\Ys) :-  connect(Ws,Xs\Ys).
 
%     Program 19.4:  A definite clause grammar (DCG) interpreter

Comments

pl/prolog/pllib/dcg_interpreter.txt · ostatnio zmienione: 2017/07/17 08:08 (edycja zewnętrzna)
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0