Using a Parser for a DSL in Prolog

Given Prolog’s strengths in pattern matching, and unifying arbitrarily nested structures, and meta-interpretation, I’m wondering if DCGs/parsing would ever have a place in implementing an in-Prolog DSL.
Any insights are appreciated.

This is actually very common.
Search for the predicates that make use of DCGS, e.g.

  • phrase/2
  • phrase/3
  • phrase_from_file/2
  • phrase_from_file/3
  • phrase_from_stream/2
  • phrase_from_quasi_quotation/2

One of the better places to search for such code is on GitHub but even using GitHub search for this is tedious.

Over the years I have found that downloading quality GitHub repositories such as these and searching them with find in files options of editors like NotePad++ is often much more effective.

1 Like

The other way in Prolog is to define custom operators, use existing operators, and write your DSL in augmented prolog syntax. While you are limited by the basics of how Prolog looks, you can nevertheless get something quite readable which has the bonuses of
a) no need to write a parser, and
b) can use DSL code intermixed with Prolog code for your development purposes.

3 Likes

Since the introduction of quasi-quotations this is somewhat less of an issue. Meaning, you can embed the DSL within the Prolog source code and it works just fine. I haven’t been bothered however to figure out how to provide proper IDE support for the embedded language, surely it is doable with emacs.