Generating string of symbols(sentence) for a given context free grammar?

Consider this DCG code: s-->. S-->a,s,b to run a predicate you defined by DCG you should add two more arguments at the end: the "input" and what it's left. If you want to recognize the whole list you simply put .

So, when you run it you get: 38? - s(C,). C = ; C = a, be ; C = a, a, b, be ; C = a, a, a, b, b, be ; C = a, a, a, a, b, b, b, be If you wanted some sort of "return" string you could add it as an extra arg.To write prolog code in a dcg clause you use {}: s('')-->.

S(S)--> a,s(SI),b, { atomic_list_concat(a,SI,b,S)} and you get: 40? - s(R,X,). R = '', X = ; R = ab, X = a, be ; R = aabb, X = a, a, b, be ; R = aaabbb, X = a, a, a, b, b, be ; R = aaaabbbb, X = a, a, a, a, b, b, b, be ; R = aaaaabbbbb we generated all the strings that are recognized by this grammar; usually you just want to check if a string is recognized by the grammar.

To do that you simply put it as input: 41? - s(a,b,). True 42?

- s(a,b,b,). False note that we put the S::= rule first otherwise prolog would fall in a infinite loop if you asked to generate all the solutions. This problem might not be trivial to solve in more complex grammars.

To get the solutions you can use length/2:? - length(X,_),s(X,). X = ; X = a, be ; X = a, a, b, be ; X = a, a, a, b, b, be ; X = a, a, a, a, b, b, b, be even if your code is: s-->.

S-->a,s,b.

Consider this DCG code: s-->. S-->a,s,b. To run a predicate you defined by DCG you should add two more arguments at the end: the "input" and what it's left.

If you want to recognize the whole list you simply put . So, when you run it you get: 38? - s(C,).

C = ; C = a, be ; C = a, a, b, be ; C = a, a, a, b, b, be ; C = a, a, a, a, b, b, b, be ; ... If you wanted some sort of "return" string you could add it as an extra arg. To write prolog code in a dcg clause you use {}: s('')-->. S(S)--> a,s(SI),b, { atomic_list_concat(a,SI,b,S)}.

And you get: 40? - s(R,X,). R = '', X = ; R = ab, X = a, be ; R = aabb, X = a, a, b, be ; R = aaabbb, X = a, a, a, b, b, be ; R = aaaabbbb, X = a, a, a, a, b, b, b, be ; R = aaaaabbbbb, ... we generated all the strings that are recognized by this grammar; usually you just want to check if a string is recognized by the grammar.To do that you simply put it as input: 41?

- s(a,b,). True 42? - s(a,b,b,).

False. Note that we put the S::= rule first otherwise prolog would fall in a infinite loop if you asked to generate all the solutions. This problem might not be trivial to solve in more complex grammars.

To get the solutions you can use length/2:? - length(X,_),s(X,). X = ; X = a, be ; X = a, a, b, be ; X = a, a, a, b, b, be ; X = a, a, a, a, b, b, b, be even if your code is: s-->.

S-->a,s,b.

I understood a bit about what you explained. But frankly speaking how to code the parser which generates all the possible strings? Can you give me the entire code?

I'm really finding prolog very tuff and confused? – bharath kumar Nov 17 at 23:28 1 The 'entire code' it's these 2 lines at start. Awesome, isn't it?

– chac Nov 17 at 23:42 yup, it's the first 2 lines. Write them in a file and consult it (cannot use assert/1 for DCG in swi-prolog atm) – thanosQR Nov 18 at 6:46 2 Very nice! One detail: It's best to use the official phrase/2 (or phrase/3) interface to DCGs, since other implementations may translate DCG rules differently to plain Prolog code.

Using phrase/2 keeps your program portable across implementations, for example, instead of s(Ls, ), you can write phrase(s, Ls). – mat Nov 18 at 8:09 1 yup, it's a built-in predicate. The first arg is a list of atoms and the second is their concatenation.

I think that it was recently added in swi-prolog so maybe you will have to replace it with atom_concat(a,SI,Temp), atom_concat(Temp,b,S). – thanosQR Nov 18 at 14:44.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions