dcg

What makes a DCG predicate expensive?

纵然是瞬间 提交于 2019-12-11 01:57:09
问题 I'm building a Definite Clause Grammar to parse 20,000 pieces of semi-natural text. As the size of my database of predicates grows (now up to 1,200 rules), parsing a string can take quite a long time -- particularly for strings that are not currently interpretable by the DCG, due to syntax I haven't yet encoded. The current worst-case is 3 minutes for a string containing 30 words. I'm trying to figure out how I can optimize this, or if I should just start researching cloud computing. I'm

Writing DCG for strings containing same number of specific digits - Prolog

时光总嘲笑我的痴心妄想 提交于 2019-12-11 00:53:26
问题 I'm working on writing a DCG that would take strings in the form of u2v where u and v do not have to be of equal length and the number of 0 's in u must be the same as the number of 1 's in v . So far I've been able to write several grammars that work on paper but when I code them and try a query I typically end up with a loop somewhere. This is the closest I've been able to get: s-->[2]. s-->u,[0],n. s-->u,s,v. n-->s,[1],v. u-->[1],u. u-->[]. v-->[0],v. v-->[]. I can get the correct answers

DCG and left recursion

天大地大妈咪最大 提交于 2019-12-10 22:55:40
问题 I am trying to implement a dcg that takes a set of strings of the form {a,b,c,d}*.The problem i have is if I have a query of the form s([a,c,b],[]),It returns true which is the right answer but when i have a query of the form s([a,c,f],[]),It does not return an answer and it runs out of local stack. s --> []. s --> s,num. num --> [a]. num--> [b]. num--> [c]. num--> [d]. 回答1: Use phrase/2 Let's try phrase(s,[a,b,c]) in place of s([a,b,c],[]) . The reason is very simple: In this manner we are

Removing whitespace from strings in Prolog

柔情痞子 提交于 2019-12-10 21:33:06
问题 I wrote parser in Prolog. I haven't finished yet. It is a part of code. The next step is killing all whitespace in string. parse(Source, Tree) :- kill_whitespace(Source, CleanInput), % remove whitespaces actual_parse(CleanInput, Tree). actual_parse(CleanInput, Tree):- phrase(expr(Tree),CleanInput). expr(Ast) --> term(Ast1), expr_(Ast1,Ast). expr_(Acc,Ast) --> " + ", !, term(Ast2), expr_(plus(Acc,Ast2), Ast). expr_(Acc,Ast) --> " - ", !, term(Ast2), expr_(minus(Acc,Ast2), Ast). expr_(Acc,Acc)

Avoid linear cost of append/3 in Prolog

♀尐吖头ヾ 提交于 2019-12-10 21:25:36
问题 Let's assume that we're reading from standard input and building a list of all the lines that have been read. In the end, we need to display those lines, separated by commas. go:- prompt(_, ''), processInput([ ], Lines), findall(_, (member(L, Lines), write(L), write(',')), _), nl. processInput(LinesSoFar, Lines):- read_line_to_codes(current_input, Codes), processInput(Codes, LinesSoFar, Lines). processInput(Codes, LinesSoFar, Lines):- ( Codes \= end_of_file -> atom_codes(Line, Codes), append

Given a substitution S and list Xs, how to apply S to Xs

十年热恋 提交于 2019-12-10 21:03:39
问题 Suppose I have a substitution S and list Xs , where each variable occurring in Xs also occurs in S . How would I find the list S(Xs) , i.e., the list obtained by applying the substitution S to the list Xs . More concretely, I have a set of predicates and DCG rules that look something like pat(P) --> seg(_), P, seg(_). seg(X,Y,Z) :- append(X,Z,Y). If I attempt to match a pattern P with variables against a list, I receive a substitution S : ?- pat([a,X,b,Y],[d,a,c,b,e,d],[]). X = c, Y = e I

Handling prolog context free grammar

我的梦境 提交于 2019-12-10 19:33:25
问题 Given a CFG S --> a S b | c | d I wanna write a predicate like, grammar('S', sentence) which generates all possible sentences like sentence=acb, sentence=acd, sentence=c, sentence=ab...................... Using left most derivation , if the encountered symbol is terminal it should print out that terminal, and if the encountered symbol is non terminal 'S' , it should backtrack and substitute and one of the grammar a S b or c or d and repeat the process. I dont want any code...just help me with

Using a prolog DCG to find & replace - code review

北战南征 提交于 2019-12-10 17:17:10
问题 I came up w/ the following code to replace all occurences of Find w/ Replace in Request & put the answer in Result . This is using a DCG, so they are all lists of character codes. The predicate that client code would use is substitute . findReplace(_, _, [], []) --> []. % The end. findReplace(Find, Replace, Result, ResultRest) --> Find, % Found Find. { append(Replace, Intermediate, Result) }, % Put in Replace in Find's place. !, % Make sure we don't backtrack & interpret Find as the next case

Checking if a string is contained in a language (Prolog)

耗尽温柔 提交于 2019-12-10 16:52:58
问题 This is the CFG: S -> T | V T -> UU U -> aUb | ab V -> aVb | aWb W -> bWa | ba so this will accept some form of: {a^n b^n a^m b^m | n,m >= 1} U {a^n b^m a^m b^n | n,m >= 1} And here is the code I'm working with: in_lang([]). in_lang(L) :- mapS(L), !. mapS(L) :- mapT(L) ; mapV(L),!. mapT(L) :- append(L1, mapU(L), L), mapU(L1), !. mapU([a|T]) :- ((append(L1,[b],T), mapU(L1)) ; (T = b)),!. mapV([a|T]) :- ((append(L1,[b],T), mapV(L1)) ; (append(L1,[b],T), mapW(L1))), !. mapW([b|T]) :- ((append(L1

Tree postorder traversal in Prolog

孤街浪徒 提交于 2019-12-10 16:43:58
问题 Tree traversal refers to the process of visiting each node in a tree data structure in a systematic way. The postorder traversal in the following image Sorted_binary_tree returns A, C, E, D, B, H, I, G, F (left, right, root) . The Prolog code for PREORDER traversal is preorder(tree(X,L,R),Xs) :- preorder(L,Ls), preorder(R,Rs), append([X|Ls],Rs,Xs). preorder(void,[]). I would like to modify the above code to implement postorder traversal. 回答1: In case of a postorder you have to traverse the