Some doubts about BNF grammars and Prolog's DCG grammars -


i studying grammars in prolog , have litle doubt conversions classic bnf grammars prolog dcg grammars form.

for example have following bnf grammar:

<s> ::= b <s> ::= <s> b 

that, rewriting, generates strings of type:

ab aabb aaabbb aaaabbbb ..... ..... a^n b^n 

looking on ivan bratko book programming artificial intelligence convert bnf grammar dcg grammar in way:

s --> [a],[b]. s --> [a],s,[b]. 

at first seems me similar classic bnf grammar form have doubt related , symbol used in dcg

this not symbol of logical or in prolog separator character in generated sequence.

is right?

you can read , in dcgs "and then"/"concatenated with":

s -->     [a],    [b]. 

and

t -->   [a,b]. 

is same:

?- phrase(s,x). x = [a, b].  ?- phrase(t,x). x = [a, b]. 

it different , in prolog rule means logical conjunction:

a. b.  u :-  a,  b.  ?- u. true. 

i.e. u true if , b true(which case here).

another difference predicate s/0 not exist:

?- s. error: undefined procedure: s/0 error:     however, there definitions for: error:         s/2 false. 

the reason grammar rule s translated prolog predicate, needs additional arguments. intended way evaluate grammar rule use phrase/2 above(phrase(startrule,list)). if like, can add explanations translation dcg prolog, dont know if not confusing if beginner in prolog.

addendum: better example have been define t as:

t -->    [b],   [a]. 

where evaluation phrase results in list [b,a] (which different [a,b]):

?- phrase(t,x). x = [b, a]. 

but if reorder goals in rule, cases in predicate true never changes(*), in our case, defining

v :-   b,   a. 

is equivalent u.

(*) because prolog uses depth-first search find solution, might case might need try infinitely many candidates before find solution after reordering. (in more technical terms, solutions don't change search might not terminate if reorder goals).


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -