3

I know that it is possible to define definite clause grammars in Picat, but the syntax is much more verbose than in Prolog. In Prolog, definite clause grammars can be written more concisely:

pronoun --> him,her,it.

Surprisingly, Picat's official tutorial doesn't mention a syntax for definite clause grammars. Would it be possible to use Prolog's DCG notation in Picat?

false
  • 10,264
  • 13
  • 101
  • 209
Anderson Green
  • 30,230
  • 67
  • 195
  • 328

2 Answers2

3

I have an idea. This is simple DCG converter for Picat. This code is example file of DCG like format.

dcg([sentence, -->, noun_phrase, verb_phrase]).
dcg([noun_phrase, -->, det, noun]).
dcg([verb_phrase, -->, verb, noun_phrase]).
dcg([det, -->, [the]]).
dcg([det, -->, [a]]).
dcg([det, -->, [an]]).
dcg([noun, -->, [cat]]).
dcg([noun, -->, [bat]]).
dcg([verb, -->, [eats]]).

Next code is main.pi file.

import util, io.

main([IN]) =>
  writeln([IN.to_atom()]),
  rw_dcg(IN.to_atom()).

rw_dcg(RF) =>
  RD = open(RF),
  I = 1,
  MP = new_map(),
  L = read_term(RD),
  while (L != end_of_file)
    DCG=cnv_dcg(I,MP,L),
    cl_facts(DCG),
    println(L),
    println(DCG),
    L := read_term(RD),
    I := I+1
  end,
  close(RD).
cnv_dcg(_,MP,dcg([HD,-->,[a]])) =DCG,
  Q=check_lhs(MP,HD) =>
  % I don't know this code problem of "a", and why?.
  DCG = HD.to_string()++"([a|X],X2)"++Q++"=> X2=X.".
cnv_dcg(_,MP,dcg([HD,-->,T])) =DCG,
  list(T),
  Q=check_lhs(MP,HD) =>
  append(T2,[_],T.to_string()),
  DCG = HD.to_string()++"("++T2++"|X],X2)"++Q++"=> X2=X.".
cnv_dcg(_,MP,dcg([HD,-->|T])) =DCG =>
  LN = (T.length()+1).to_string(),
  DCG = HD.to_string()++"(S1,S"++LN++") => ",
  foreach(I in 1..T.length())
    E=cond(I==T.length(),".",",").to_string(),
    N1 = I.to_string(),
    N2 = (I+1).to_string(),
    DCG := DCG++T[I].to_string()++"(S"++N1++",S"++N2++")"++E.

check_lhs(MP,KY)= Q =>
  (MP.has_key(KY)-> Q=" " ; Q=" ?", MP.put(KY,Q)).

Next is converted code. Thank you for deleted and comments.

sentence(S1,S3) => noun_phrase(S1,S2),verb_phrase(S2,S3).
noun_phrase(S1,S3) => det(S1,S2),noun(S2,S3).
verb_phrase(S1,S3) => verb(S1,S2),noun_phrase(S2,S3).
det([the|X],X2) ?=> X2=X.
det([a|X],X2) => X2=X.
det([an|X],X2) => X2=X.
noun([cat|X],X2) ?=> X2=X.
noun([bat|X],X2) => X2=X.
verb([eats|X],X2) ?=> X2=X.
1

Since Version 3.0 (September 26, 2020), Picat supports DCG rules natively: http://picat-lang.org/updates.txt

Sergii Dymchenko
  • 6,890
  • 1
  • 21
  • 46