Hacker News new | past | comments | ask | show | jobs | submit login

The wikipedia article has a few examples. For instance:

  sentence --> noun_phrase, verb_phrase.
  noun_phrase --> det, noun.
  verb_phrase --> verb, noun_phrase.
  det --> [the].
  det --> [a].
  noun --> [cat].
  noun --> [bat].
  verb --> [eats].
And here are some example queries:

  % Running as a recogniser
  ?- sentence([the,cat,eats,a,cat], []).
  true.
  
  % With a string not in the grammar
  ?- sentence([the,cat,eats,'YeGoblynQuenne'], []).
  false.

  % Running as a generator
  ?- sentence(P, []).
  P = [the, cat, eats, the, cat] ;
  P = [the, cat, eats, the, bat] ;
  P = [the, cat, eats, a, cat] ;
  P = [the, cat, eats, a, bat] ;
  P = [the, bat, eats, the, cat] ;
  P = [the, bat, eats, the, bat] ;
  P = [the, bat, eats, a, cat] ;
  % ... 
  % More results generated on backtracking
To prevent any confusion - the "A --> B" syntax is syntactic sugar. Such statements are expanded to ordinary Prolog terms by the Prolog equivalent of a macro (an expansion hook). You can inspect the ordinary Prolog definitions saved in the program database:

  ?- listing(sentence).
  sentence(A, B) :-
    noun_phrase(A, C),
    verb_phrase(C, B).
Indeed, it's perfectly possible to write these by hand - as you can see above the ordinary Prolog definition is not that complicated, the macro expanding the DCG notation adds two variables to each nonterminal and relieves you of the need to add this boilerplate yourself.



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: