OCTAGRAM
OCTAGRAM

Reputation: 706

How to turn Prolog into preprocessor (kinda m4, gpp)?

m4 can be used with different programming languages because its default policy on unrecognised text is to output and it has a quoting mechanism required for convenient preprocessor usage. I mean, I can write preprocessed code in such a way that my IDE thinks it's Ada. m4 macro invocations (include, define) are hidden in Ada comments. Ada comments start with --, Prolog comments start with % or inside /* */. I can't mix Ada and Prolog easily.

m4 does preprocessing well enough, but its programming side sucks. I thought: why don't me try a general purpose language instead? Prolog looks like being appropriate for my tasks. Prolog has ISO standard, and logic programming looks being useful for reasoning about source generation.

So here we are: we have an arbitrary output language (Ada, then JavaScript, then maybe something else), and it would be nice to write source code in native IDEs for corresponding languages most of the time. I need preprocessor to have a proper parser (like m4 has). This parser shouldn't have Ada syntax knowledge, instead it should only know Ada lexical, and it should be possible to make its macro invocations look like Ada functions invocations.

Its command line interface should look similar to m4 or gcc -E. It should be possible to do both kinds of includes: pure Prolog source code and preprocessed code.

If you know how can I shortly achieve these goals, please write here. I can probably accept non-Prolog solutions, but among the others I prefer standard languages. m4 is part of POSIX, for example.

Upvotes: 2

Views: 477

Answers (3)

Ciper
Ciper

Reputation: 11

I use DCGs to generate python scripts for FontForge (to create and maintain candlestick fonts for forex trading); I tried M4 scripts but they are not easily amenable to programming (as you mention) and difficult to merge with my glyph definitions that reside in a Prolog database.

DCGs are perceived to be mainly for parsing; but I find they are like a templating engine on their own.

Upvotes: 0

Nicholas Carey
Nicholas Carey

Reputation: 74267

Have you looked at StringTemplate? It's a part of ANTLR (compiler construction toolket). It's written in Java (though there's a C# port, among others). An ANTLR grammar can be written to target many different languages. StringTemplate is what it uses to generate code for the different target languages.

Check it out.

Upvotes: 0

CapelliC
CapelliC

Reputation: 60014

Prolog is an unusual choice for this task. Indeed, SWI-Prolog used to have the possibility to invoke C preprocessor - should has been removed now, because of its little usefulness.

Anyway, a DCG could be used. I would advice against it if you are not really proficient in Prolog, because debugging can be difficult:

prep(I, O) :-
    phrase(exp, I, O).

exp --> mac, !, exp.
exp, [C] --> [C], exp.
exp --> [].

% macro definitions, use 'pushback' argument to change text

mac, "AAA" --> "a".
mac, "G" --> "goal".

example

?- prep("my goal is mars", X).
X = "my G is mAAArs" .

edit: from SWI-Prolog mailing list, WRT the latest release 6.3.18

MODIFIED: Deleted preprocessor/2 after discussion on the mailinglist. Code relying on this (we hope none) can use the hook user:prolog_load_file/2 to achieve the same result.

I think the functionality available is more or less that of #include <...>. For macro expansion (or better, term rewrite) in Prolog there is goal_expansion or term_expansion

edit: latest release brings quasiquotations. Together with a parser in DCG could help to achieve the transformation.

Upvotes: 3

Related Questions