I am trying to find a way to write the classic a^nb^nc^n Context Sensitive Grammar (CSG) from Hopcroft & Ullman in DCG notation. The book gives the productions of the grammar as follows:
- S \rightarrow aSBC
- S \rightarrow aBC
- CB \rightarrow BC
- aB \rightarrow ab
- bB \rightarrow bb
- bC \rightarrow bc
- cC \rightarrow cc
First, note that I’m not simply interested in DCG or a Prolog program that can generate strings of a’s, b’s and c’s of the same length one after the other: I know how to do that. What I’m trying to understand is how I can represent a CSG in DCG notation.
More specifically, I guess I’m trying to understand what is the best way to denote that the left-hand side of a DCG production has a “context”. Recall that a CSG has productions of the form \alpha A\beta \rightarrow \alpha\gamma\beta where A is a non-terminal symbol and \alpha, \beta, \gamma are a mix of terminals and non-terminals. In the left-hand side of a production \alpha A\beta \rightarrow \alpha\gamma\beta, the \alpha and \beta form the “context” of the rule so that A is replaced by the string \gamma if and only if it is surrounded by \alpha and \beta in an input string.
The practical difficulty is that it’s not clear how to represent a rule that has only non-terminals on the left-hand side, like production 3 in the a^nb^nc^n listing above:
- CB \rightarrow BC
The problem is that the grammar must process a string of both terminals and non-terminals but when we parse a string with a DCG, the grammar only consumes terminals from its input.
To make up for that I tried the following:
s([a,'S','B','C'|Ss]) --> ['S'], s(Ss).
s([a,'B','C'|Ss]) --> ['S'], s(Ss).
s(['B','C'|Ss]) --> ['C','B'], s(Ss).
s([a,b|Ss]) --> [a,'B'], s(Ss).
s([b,b|Ss]) --> [b,'B'], s(Ss).
s([b,c|Ss]) --> [b,'C'], s(Ss).
s([c,c|Ss]) --> [c,'C'], s(Ss).
s(['B'|Ss]) --> ['B'], s(Ss).
s(['C'|Ss]) --> ['C'], s(Ss).
s([a|Ss]) --> [a], s(Ss).
s([b|Ss]) --> [b], s(Ss).
s([c|Ss]) --> [c], s(Ss).
s([]) --> [].
That is to say, I tried to generate a string of terminals and non-terminals that is progressively whittled down to terminals during parsing and placed in the “output” of the grammar.
This requires a dedicated interpreter, which I defined as follows:
csg(_S,Os,0,Os):-
!.
csg(S,Is,N,Os):-
S_ =.. [S,Ss]
,phrase(S_, Is)
,N_ is N - 1
,csg(S,Ss,N_,Os).
This is based on some earlier work on L-Systems I won’t bore you with, but that’s where the counting-down comes from (L-systems have a concept of “generation” that requires counting).
This setup allows me to generate strings like the following:
248 ?- csg(s,['S'],3,Ps).
Ps = [a, a, a, 'S', 'B', 'C', 'B', 'B', 'C', 'C'] ;
Ps = [a, a, a, 'S', 'B', 'C', 'B', 'C', 'B', 'C'] ;
Ps = [a, a, a, 'B', 'C', 'B', 'B', 'C', 'C'] ;
Ps = [a, a, a, 'B', 'C', 'B', 'C', 'B', 'C'] ;
Ps = [a, a, b, 'B', 'C', 'C'] ;
Ps = [a, a, b, 'C', 'B', 'C'] ;
Ps = [a, a, 'B', 'B', 'C', 'C'] ;
Ps = [a, a, 'B', 'C', 'B', 'C'] ;
Ps = [a, b, c] ;
Ps = [a, b, 'C'] ;
Ps = [a, b, 'C'] ;
Ps = [a, 'B', 'C'] ;
Where [a,b,c] is the target. The others are really partial parses on the way to [a,b,c]. It’s possible to filter the output to keep only the strings composed exclusively of terminals, or just keep parsing until only terminals are left, but I don’t think this is really on the right track.
Note that if I try to restrict the output of csg/4 to a certain length that blows up the stacks, as expected (CSG parsing is in PSPACE). So if you try ?- csg(s,['S'],3,[A,B,C]). that dies a death, as does ?- csg(s,['S'],3,[a,b,c])..
So my question to the community is: does anyone know a better way to do this?
Is there a better way to define Hopcroft & Ullman’s a^nb^nc^n CSG as a DCG? Btw that CSG is Example 2.2 on page 13 of my copy of Formal Languages and their Relation to Automata.
What is the best way to represent CSGs with rules like production 3 above in DCG notation?
Thanks in advance.