dcg

Is there a way or an algorithm to convert DCG into normal definite clauses in Prolog?

别等时光非礼了梦想. 提交于 2019-12-01 06:06:51
I am a newbie in Prolog, and I am trying to understand how a grammar can be translated into a normal definite clause from a DCG. I understood that DCG notation is just syntactic sugar for normal definite clauses in Prolog. I started to depict some similarities between the normal definite grammars, and the DCGs, but failed to apply the same pattern, so I am asking is there some rules that I am missing or an algorithm of conversion that might work. Here is the grammar that I am working on, and here is what I did in order to translate that grammar: expr --> term, addterm. addterm --> []. addterm

How to avoid using assert and retractall in Prolog to implement global (or state) variables

限于喜欢 提交于 2019-12-01 04:42:01
I often end up writing code in Prolog which involves some arithmetic calculation (or state information important throughout the program), by means of first obtaining the value stored in a predicate, then recalculating the value and finally storing the value using retractall and assert because in Prolog we cannot assign values to variable twice using is (thus making almost every variable that needs modification, global). I have come to know that this is not a good practice in Prolog. In this regard I would like to ask: Why is it a bad practice in Prolog (though i myself don't like to go through

Parsing numbers with multiple digits in Prolog

坚强是说给别人听的谎言 提交于 2019-12-01 03:40:47
I have the following simple expression parser: expr(+(T,E))-->term(T),"+",expr(E). expr(T)-->term(T). term(*(F,T))-->factor(F),"*",term(T). term(F)-->factor(F). factor(N)-->nat(N). factor(E)-->"(",expr(E),")". nat(0)-->"0". nat(1)-->"1". nat(2)-->"2". nat(3)-->"3". nat(4)-->"4". nat(5)-->"5". nat(6)-->"6". nat(7)-->"7". nat(8)-->"8". nat(9)-->"9". However this only supports 1-digit numbers. How can I parse numbers with multiple digits in this case? Use accumulator variables, and pass those in recursive calls. In the following, A and A1 are the accumulator. digit(0) --> "0". digit(1) --> "1". %

How to avoid using assert and retractall in Prolog to implement global (or state) variables

北慕城南 提交于 2019-12-01 02:17:57
问题 I often end up writing code in Prolog which involves some arithmetic calculation (or state information important throughout the program), by means of first obtaining the value stored in a predicate, then recalculating the value and finally storing the value using retractall and assert because in Prolog we cannot assign values to variable twice using is (thus making almost every variable that needs modification, global). I have come to know that this is not a good practice in Prolog. In this

Read input in prolog and print result

亡梦爱人 提交于 2019-12-01 01:33:02
I am learning Prolog ideas and here is what I want to practice: I want to write a Prolog program that can work like this: ?- input([apple,is,fruit]). ?- input([chicken,is,meat]). ?- input([Is,apple,meat]). No, it is a fruit ?- input[(Is,chicken,meat]) Yes. And when I was trying to implement this program, I got some problem: (1) I used this code trying to read the input and distinguish between questions and assertions, but it fails: input([]). input([X|R]) :- X is 'Is', write('test code read question'); write("test code read assertion"). (2) I am still confused about How I can filter the useful

Parsing an expression in Prolog and returning an abstract syntax

╄→гoц情女王★ 提交于 2019-12-01 01:03:31
I have to write parse(Tkns, T) that takes in a mathematical expression in the form of a list of tokens and finds T, and return a statement representing the abstract syntax, respecting order of operations and associativity. For example, ?- parse( [ num(3), plus, num(2), star, num(1) ], T ). T = add(integer(3), multiply(integer(2), integer(1))) ; No I've attempted to implement + and * as follows parse([num(X)], integer(X)). parse(Tkns, T) :- ( append(E1, [plus|E2], Tkns), parse(E1, T1), parse(E2, T2), T = add(T1,T2) ; append(E1, [star|E2], Tkns), parse(E1, T1), parse(E2, T2), T = multiply(T1,T2)

How is this context free grammar using difference lists in Prolog functioning?

时光毁灭记忆、已成空白 提交于 2019-11-30 20:41:10
I'm reading this tutorial on context free grammars in Prolog, and they mention at the bottom of the page implementing a context free grammar in Prolog using difference lists, with the following code block included: s(X,Z):- np(X,Y), vp(Y,Z). np(X,Z):- det(X,Y), n(Y,Z). vp(X,Z):- v(X,Y), np(Y,Z). vp(X,Z):- v(X,Z). det([the|W],W). det([a|W],W). n([woman|W],W). n([man|W],W). v([shoots|W],W). It mentions: Consider these rules carefully. For example, the s rule says: I know that the pair of lists X and Z represents a sentence if (1) I can consume X and leave behind a Y , and the pair X and Y

Extension to CFG, what is it?

耗尽温柔 提交于 2019-11-30 19:44:15
Consider the following extension to context-free grammars that permits rules to have in the left-hand side, one (or more) terminal on the right side of the non-terminal. That is, rules of the form: A b -> ... The right-hand side may be anything, like in context-free grammars. In particular, it is not required, that the right-hand side will have exactly the same terminal symbol at the end. In that case, this extension would be context-sensitive. But the terminal is not just a context. Sometimes, this terminal is called "pushback". Clearly, this is no longer CFG (type-2). It includes type-1. But

Prolog : Combining DCG grammars with other restrictions

倖福魔咒の 提交于 2019-11-30 18:28:13
I'm very impressed by Prolog's DCG and how quickly I can produce all the possible structures that fit a particular grammar. But I'd like to combine this search with other constraints. For example, define a complex grammar and ask Prolog to generate all sentences with not more than 10 words. Or all sentences that don't repeat the same word twice. Is it possible to add extra constraints like this to a DCG grammer? Or do I basically have to translate the DCG back into normal Prolog clauses and start modifying them? If you only want to see all sentences that are generated, it is very convenient to

Prolog : Combining DCG grammars with other restrictions

拟墨画扇 提交于 2019-11-30 16:51:48
问题 I'm very impressed by Prolog's DCG and how quickly I can produce all the possible structures that fit a particular grammar. But I'd like to combine this search with other constraints. For example, define a complex grammar and ask Prolog to generate all sentences with not more than 10 words. Or all sentences that don't repeat the same word twice. Is it possible to add extra constraints like this to a DCG grammer? Or do I basically have to translate the DCG back into normal Prolog clauses and