dcg

Applying semicontext notation for passing additional arguments

拜拜、爱过 提交于 2020-01-21 12:17:08
问题 This is a follow-on question from an earlier question from Mat's answer Starting with this e([number(0)] , t1 , Uc0 , Uc0, Bc0 , Bc0) --> []. e([number(1)] , t2 , Uc0 , Uc0, Bc0 , Bc0) --> []. e([number(2)] , t3 , Uc0 , Uc0, Bc0 , Bc0) --> []. e([op(neg),[Arg]] , u1(E) , [_|Uc0], Uc1, Bc0 , Bc1) --> [_], e(Arg , E , Uc0, Uc1, Bc0, Bc1). e([op(ln),[Arg]] , u2(E) , [_|Uc0], Uc1, Bc0 , Bc1) --> [_], e(Arg , E , Uc0, Uc1, Bc0, Bc1). e([op(add),[Left,Right]], b1(E0,E1) , Uc0 , Uc2, [_|Bc0], Bc2) -

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

不想你离开。 提交于 2020-01-21 07:15:33
问题 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

What does the “-” symbol mean in Prolog when dealing with lists?

喜夏-厌秋 提交于 2020-01-14 07:21:46
问题 I was reading the answer to this question, p(X) :- read(A), q(A,X-[]). q(end,X-X) :- !. q(A,[A|X]-Y) :- read(B), q(B,X-Y). The code above uses the syntax List-List . I somewhat understand what is going on, but I want to know what exactly what the "-" symbol/predicate does here. Also, is this SWI specific? 回答1: The (-)/2 to represent difference lists is a rather uncommon convention. In older books, another operator (\)/2 was used too. Many prefer to use two separate arguments instead. There

Prolog DCG set_prolog_flag double_quotes source code directive location matters; documentation?

北慕城南 提交于 2020-01-13 09:22:09
问题 I learned the hard way that with SWI-Prolog the location for the Prolog directive set_prolog_flag matters in a source code file. The only documentation I found of value about loading source code files with directives was in Loading Prolog source files A directive is an instruction to the compiler. Directives are used to set (predicate) properties (see section 4.15), set flags (see set_prolog_flag/2) and load files (this section). Directives are terms of the form :- <term>. Is there

right linear context free grammar

好久不见. 提交于 2020-01-07 06:38:28
问题 I've a problem. I have to write right linear context free grammar with alphapet={0,1} where the numbers of 0 will be even and the numbers od 1 will be odd. I tried write sth. but it's doesn't work. s --> [1],a. s --> [0],b. a --> []. a --> [1],c. a --> [0],b. c --> [1],k. c --> [0],b. b --> [0],k. b --> [1],d. d --> [1],b. d --> [0],c. k --> []. k --> s. Grammar should accept even amount of 0s and odd amount of 1s. Grammar context free is right linear when A->wB or A->w where w is any word

Reading a file in prolog [duplicate]

荒凉一梦 提交于 2019-12-31 02:57:29
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: Read a file line by line in Prolog I found the following prolog code which reads one character at a time and prints out. process(File) :- open('C:/Users/BHARAT/Desktop/a.txt', read, In), get_char(In, Char1), process_stream(Char1, In), close(In). process_stream(end_of_file, _) :- !. process_stream(Char, In) :- print(Char), get_char(In, Char2), process_stream(Char2, In). But if the file has multiple lines is there

Get elements from list of lists

好久不见. 提交于 2019-12-28 03:10:17
问题 is it possible to get all elements from list of lists in Prolog? Something like: We have getElements([[[a,b,[c]],d,e],f,g,[h,[i,j]]],S) and the result is: S = [a,b,c,d,e,f,g,h,i,j] ... Thanks for help. 回答1: In SWI-Prolog (and maybe others), you can use flatten/2 : ?- flatten([[[a,b,[c]],d,e],f,g,[h,[i,j]]], S). S = [a, b, c, d, e, f, g, h, i|...]. Note that the SWI-Prolog manual page for flatten/2 includes the following statement: Ending up needing flatten/3 often indicates, like append/3 for

Write a Prolog DCG grammar that handle the mean of a sentence and also build its parse tree

萝らか妹 提交于 2019-12-25 18:25:27
问题 I have this DCG grammar that understand and agree phrases like: [john, paints] and [john, likes, mary] managing the semantic meaning directly into the DCG grammar by the use of parameters sentence2(VP) --> noun_phrase2(Actor), verb_phrase2(Actor, VP). noun_phrase2(Name) --> properName(Name). verb_phrase2(Actor, VP) --> intrans_verb(Actor, VP). verb_phrase2(Somebody, VP) --> trans_verb(Somebody, Something, VP), noun_phrase2(Something). properName(john) --> [john]. properName(mary) --> [mary].

definite clause grammar (dcg) Prolog (homework)

孤街浪徒 提交于 2019-12-24 18:13:15
问题 i tried to write a predicate and N={Expression,Number,Digit,Operator,Variable} T={1,2,3,+,-,*,(,),X,Y,Z} and S is expression and program p defines as Expression-->Number **Expression-->(Expression) Operator (Expression)** Number-->Digit **Number --> Digit Number** Digit-->1 Digit-->2 Digit-->3 Operator-->+ Operator-->- Operator-->* Variable-->X Variable-->Y Variable-->Z I think that i implemented many parts however could not implement bold parts!! my prolog code that describes the terminals

I can't get my Prolog DCG working with atom concat

一曲冷凌霜 提交于 2019-12-24 14:16:28
问题 I can't get this Prolog DCG code working: String1=" ",string_codes(String1,Codes),phrase(spaces(Output),Codes). spaces(XXs) --> [X], {X=32}, spaces(Xs), {char_code(Ch,X), atom_concat(Ch,Xs,XXs)}, !. %%Space spaces([]) --> []. 回答1: I solved this by changing [] in the base case to ''. spaces(XXs) --> [X], {X=32}, spaces(Xs), {char_code(Ch,X), atom_concat(Ch,Xs,XXs)}, !. %% Space spaces('') --> []. String1 = " ", Codes = [32, 32, 32], Output = ' '. 回答2: I feel like an improved solution would