The Trinc-Prolog compiler translates grammar rules in the DCG notation to Prolog predicates which can be executed immediately. The grammar rule
head --> body
is translated to the Prolog clause:
head(Start, End) :- body(Start, End).
The extra arguments 'Start' and 'End' are necessary for passing the sentences of symbols as list arguments. The compiler of Trinc-Prolog automatically creates unique variable names when there are already variables with the same name in the grammar rule. If the 'house' example of the previous section is compiled then the following goals can be proven:
| :- house( [first_floor, second_floor], [] ). no. :- house( [foundation, first_floor, second_floor, roof], [] ). :- house( [foundation, first_floor, roof], [] ). |
The first goal has a sentence of symbols as the first argument then cannot be recognized by the grammar because the 'house' has no 'foundation. The last two examples contain correct sentences of symbols that can be recognized by the grammar. The source of the Prolog program that the Trinc-Prolog compiler produces is:
| house( Start, End ) :- (Start = [foundation | Var0]), floor(Var0, Var1), Var1 = [roof | End]. floor( Start, End ) :- floor( Start, End ) :- |
It is possible to add extra arguments to grammar rules. The DCG variables automatically added will always be the last two variables. The following example extends the 'house' grammar with an extra argument that will contain the parse tree of the sentence that was examined.
| house( house(foundation, F, roof) ) -->
[foundation], floor(F), [roof]. floor( floor(first, second) ) --> [first_floor], [second_floor]. floor( floor(first) ) --> [first_floor]. |
Examples of using this grammar are:
| :- house( X, [foundation, first_floor, second_floor,
roof], [] ). X=house(foundation, floor(first, second), roof) :- house( X,
[foundation, first_floor, roof], [] ). |