:- module(_,_).
% :- use_package(sr/bfall).

%% * Parsing: DCGs + normal Prolog calls + additional arguments
%    ----------------------------------------------------------

% - Other actions can be interspersed with the grammar.
% - Plain Prolog can be called between ``{ ... }''
% - Additional arguments can be added; they go before the implicit ones. 

% Example: we add an additional argument to count number
% of characters parsed: 

myphrase(N) --> article(AC), spaces(S1), noun(NC), spaces(S2), 
                  verb(VC), { N is AC + S1 + NC + S2 + VC }.
% Translates to: 
% myphrase(N,X1,X6) :- article(AC,X1,X2), spaces(S1,X2,X3), noun(NC,X3,X4),
%                      spaces(S2,X4,X5), verb(VC,X5,X6), N is AC+S1+NC+S2+VC.

article(1) --> "a".
article(3) --> "the".

spaces(1) --> " ".
spaces(N) --> " ", spaces(N1), { N is N1+1 }.

noun(3) --> "car".
noun(5) --> "plane".

verb(5) --> "flies".
verb(6) --> "drives".

% Try:
% ?- set_prolog_flag(write_strings,on).
% ?- myphrase(NChars, "the plane flies", []).
% Or, using the phrase/2 built-in:
% ?- phrase(myphrase(NChars), "the plane flies").
% ?- myphrase(NChars, "the plane", []).
% ?- myphrase(NChars, S, []).

% Try also uncommenting :- use_pakage(sr/bfall).
% above, reloading, and, again:
% ?- myphrase(NChars,S,[]).
