dcg

Tree path in a List of Lists

回眸只為那壹抹淺笑 提交于 2019-12-08 06:40:20
问题 I want to build a predicate (Prolog) that takes a tree and returns a list of lists and each list is a tree path. The tree is defined as tree(Root,LeftTree,RightTree). Do you have any suggestions, please? 回答1: This is quite unusual (it is more common to ask for example for a relation between a tree and a flat list of all its nodes, for which DCGs are a good fit), maybe like this: tree_list(nil, []). tree_list(tree(Node,Left,Right), [Lefts,Node,Rights]) :- tree_list(Left, Lefts), tree_list

How do I specify a DCG for a valid number?

心已入冬 提交于 2019-12-07 16:21:35
问题 I'm trying to specify a DCG for a valid number that would be used like so: value(Number) --> valid_number(Number). Basically checking if a specified value is numeric, (it could also be a variable, so it's necessary to check). I don't know how to build this valid_number DCG/predicate though. Right now I just have: valid_number('1') --> ['1']. valid_number('2') --> ['2']. ... Which works but it obviously terrible. Trying something like: valid_number(Number) --> { integer(Number), Number =

How to do a parser in Prolog?

丶灬走出姿态 提交于 2019-12-07 14:39:16
问题 I would like to do a parser in prolog. This one should be able to parse something like this: a = 3 + (6 * 11); For now I only have this grammar done. It's working but I would like to improve it in order to have id such as (a..z)+ and digit such as (0..9)+. parse(-ParseTree, +Program, []):- parsor(+Program, []). parsor --> []. parsor --> assign. assign --> id, [=], expr, [;]. id --> [a] | [b]. expr --> term, (add_sub, expr ; []). term --> factor, (mul_div, term ; []). factor --> digit | (['(']

Prolog - Palindrome Functor

别等时光非礼了梦想. 提交于 2019-12-07 04:46:58
问题 I am trying to write a predicate palindrome/1 in Prolog that is true if and only if its list input consists of a palindromic list. for example: ?- palindrome([1,2,3,4,5,4,3,2,1]). is true. Any ideas or solutions? 回答1: A palindrome list is a list which reads the same backwards, so you can reverse the list to check whether it yields the same list: palindrome(L):- reverse(L, L). 回答2: Looks that everybody is voting for a reverse/2 based solution. I guess you guys have a reverse/2 solution in mind

Prolog predicate calling

二次信任 提交于 2019-12-06 09:12:10
In the following tutorial: http://www.csupomona.edu/~jrfisher/www/prolog_tutorial/7_3.html There is the part: test_parser :- repeat, write('?? '), read_line(X), ( c(F,X,[]) | q(F,X,[]) ), nl, write(X), nl, write(F), nl, fail. Now I'm extremely confused about the c(F,X,[]) and q(F,X,[]) part because it doesn't seem to match any thing that I have seen, c only takes one parameter from what I can tell and these parameters don't seem to make sense for q. Please help me understand what is going on here. c is defined with --> , which actually adds two hidden arguments to it. The first of these is a

how to split a sentence in swi-prolog

瘦欲@ 提交于 2019-12-06 03:35:46
问题 I am trying my hands on SWI-Prolog in win xp. I am trying to understand how to split a sentence in Prolog into separate atoms. Ex : Say I have a sentence like this : "this is a string" Is there any way to get individual words to get stored in a variable? like : X = this Y = is .... and so forth. Can anyone please explain how this works? Thanks. 回答1: I would use atomic_list_concat/3. See http://www.swi-prolog.org/pldoc/man?predicate=atomic_list_concat%2F3 Normally it is meant to insert a

Removing left recursion in DCG - Prolog

自闭症网瘾萝莉.ら 提交于 2019-12-06 02:14:01
问题 I've got a small problem with left recursion in this grammar. I'm trying to write it in Prolog, but I don't know how to remove left recursion. <expression> -> <simple_expression> <simple_expression> -> <simple_expression> <binary_operator> <simple_expression> <simple_expression> -> <function> <function> -> <function> <atom> <function> -> <atom> <atom> -> <number> | <variable> <binary_operator> -> + | - | * | / expression(Expr) --> simple_expression(SExpr), { Expr = SExpr }. simple_expression

Parse Variables Using DCG

左心房为你撑大大i 提交于 2019-12-05 20:12:45
I am having trouble parsing sequences that begin with capital letters into variables using Prolog's DCG notation. For instance, if I have the string f a X y Z X and a DCG that parses this string, is there any way to parse each capitalized letter into a unique Prolog variable. E.g., parse Y to a variable and each X to a variable? The intended application would be to build the functor T = f(a,X,y,Z,X) via a DCG rule ending with the statement {T =.. [Head|Args]} Maybe you are looking for term_to_atom/3 : ?- term_to_atom(Term, 'f(a,X,y,Z,X)'). Term = f(a, _G304, y, _G306, _G304). If you are using

How to do a parser in Prolog?

放肆的年华 提交于 2019-12-05 18:33:00
I would like to do a parser in prolog. This one should be able to parse something like this: a = 3 + (6 * 11); For now I only have this grammar done. It's working but I would like to improve it in order to have id such as (a..z)+ and digit such as (0..9)+. parse(-ParseTree, +Program, []):- parsor(+Program, []). parsor --> []. parsor --> assign. assign --> id, [=], expr, [;]. id --> [a] | [b]. expr --> term, (add_sub, expr ; []). term --> factor, (mul_div, term ; []). factor --> digit | (['('], expr, [')'] ; []). add_sub --> [+] | [-]. mul_div --> [*] | [/]. digit --> [0] | [1] | [2] | [3] | [4

Flatting a list

爷,独闯天下 提交于 2019-12-05 11:55:47
I need to write a function that flat a list. For example: flat([ c , [[[]]] , [] , k] , X). X=[c,k] this is what I did: flat([],[]). flat([[A] |B] , R) :- flat([A|B],R). flat([[]|L],L1) :- flat(L,L1).! flat([[A|L]|W],R) :- flat([A|L],U), flat(W,W1), append(U,W1,R). flat([A|L], [A|L1]) :- flat(L,L1). I know why it is not true but I do not know how to do that. thanks. EDIT: almost work: flat([],[]). flat([[]|L],L1) :- flat(L,L1). --- i think something here missing flat([[A|L]|W],R) :- flat([A|L],U), flat(W,W1), append(U,W1,R). flat([A|L], [A|L1]) :- flat(L,L1). ?- flat([c , [[[]]] , [] , k],C).