dcg

Prolog Roman Numerals (Attribute Grammars)

柔情痞子 提交于 2019-12-12 12:24:30
问题 I am working on an assignment in prolog that scans a list of numerals and should return whether the list is a valid roman numeral and the decimal value of the numerals. Ex) 1 ?- roman(N, ['I'], []). N = 1 true. 2 ?- When I run the program that I feel should work, the decimal value is always right, so I'm guessing I got the synthesized attributes part right, but it always returns false for numeral lists that should return true. I'd also like to add that it aborts when it is supposed to if more

prolog - bst tree to list

本小妞迷上赌 提交于 2019-12-12 06:36:47
问题 I am pracising dcg technique, but I have a problem. I am going to implement converting BST tree to list (each possible) and in reversed side - list ot bst (each possible). (List means set in real here). However, this grammar should be ok, but it generates very much duplicates. I know cause ( nil ), but I can't deal with it. bstToList(nil) --> []. bstToList(t(Root, L, R)) --> [Root], bstToList(L), bstToList(R). bstToList(t(Root, L, R)) --> [Root], bstToList(R), bstToList(L). bstToList(t(Root,

Prolog solving prefix arithmetic expression with unknown variable

烂漫一生 提交于 2019-12-12 05:50:08
问题 I want to make an arithmetic solver in Prolog that can have +,-,*,^ operations on numbers >= 2. It should also be possible to have a variable x in there. The input should be a prefix expression in a list. I have made a program that parses an arithmetic expression in prefix format into a syntax tree. So that: ?- parse([+,+,2,9,*,3,x],Tree). Tree = plus(plus(num(2), num(9)), mul(num(3), var(x))) . (1) At this stage, I want to extend this program to be able to solve it for a given x value. This

Prolog: pattern matching within a list

一世执手 提交于 2019-12-11 13:37:06
问题 Define a relation xyz(X) that is true if X is a xyz sequence. A xyz sequence is a sequence that consists of either the number 0, or the number 1 followed by two other xyz sequences. Some xyz sequences: xyz([0]). xyz([1,0,1,0,0]). And, the following are not considered xyz sequences: xyz([1,1,0,0]). xyz([0,1,0]). xyz([1,1,0]). xyz([1,0,1,1,1,1,1,0,1]). Can someone help me with how to approach this problem? 回答1: The easiest is to write a DCG. See this tutorial for a thorough introduction. You

Removing left recursion grammar using DCG

夙愿已清 提交于 2019-12-11 10:48:14
问题 The following Grammar has left-recursion Grammar : Expr ::= E1|E2|E3|E4|E5|E6|E7 E1 ::= "(" Expr ")" E2 ::= "not""(" Expr ")" E3 ::= Expr "=>" Expr E4 ::= Expr "=/=" Expr E5 ::= Expr "*" Expr E6 ::= Func "=>" Func Func ::= Ter (Ters)+"," ... and I'm trying to remove the LR in this manner ; Expr ::= E1|... E1 ::= Expr "*" Expr ==> E1 ::= Expr Expr' Expr'::= *Expr Expr' but the problem still exists, How to fix it to get this program working? example query and test | ?- phrase(e(T),"not((2+3)=/

Definite Clause Grammar - Prolog

最后都变了- 提交于 2019-12-11 06:59:18
问题 Below I have a Definite Clause Grammar that should accept the string aabccc , however when I tested the grammar, the only string I was able to get accepted was abc . I'm pretty sure I've gotten rid of left-hand recursion, so I'm not sure what's going wrong. s --> x, y, z. x --> [a], x. x --> [a]. y --> [b], y. y --> [b]. z --> [c], z. z --> [c]. Also, would I be able to define the above grammar as... s --> x, y, z. x --> [a], x; [a]. y --> [b], y; [b]. z --> [c], z; [c]. 回答1: Both version of

Dcg state implementation of algorithm

浪子不回头ぞ 提交于 2019-12-11 05:57:37
问题 The distance between a long sequence and a short sequence, is the minimum distance between the short sequence and any subsequence of the long sequence that is the same length as the short sequence. The distance I am using is I think the Manhattan distance . (But this should be unimportant as I would like to be able to change distance functions). This first version shows a naive implementation without early abandon. I generate all subsequence of the same length, map these to find the distance

Calling facts from database in prolog

眉间皱痕 提交于 2019-12-11 04:53:57
问题 I've inserted the given context free grammar into the database using assert(....) If the grammar is something like S-->a,S,b S-->c This grammar is inserted into the database. I have to write a dcg to generate sentences for the cfg in the database. For example if i define the dcg in this way myDcg('S',str) , the 'S' (non terminal) should be called or substituted by aSb or c|d or so. The problem is how can i call/substitute 'S' by facts from the database each time a non terminal('S') is

Using a Prolog DCG to split a string

a 夏天 提交于 2019-12-11 03:49:03
问题 I'm trying to use a DCG to split a string into two parts separated by spaces. E.g. 'abc def' should give me back "abc" & "def". The program & DCG are below. main:- prompt(_, ''), repeat, read_line_to_codes(current_input, Codes), ( Codes = end_of_file -> true ; processData(Codes), fail ). processData(Codes):- ( phrase(data(Part1, Part2), Codes) -> format('~s, ~s\n', [ Part1, Part2 ]) ; format('Didn''t recognize data.\n') ). data([ P1 | Part1 ], [ P2 | Part2 ]) --> [ P1 | Part1 ], spaces(_), [

Prolog - Numbers greater than x

你。 提交于 2019-12-11 03:29:10
问题 I'm working with Prolog arithmetic and have a program that generates an abstract syntax tree, such as plus(num(1),num(2)) which simply is 1+2 . This is done by using DCG. In this example plus(num(1),num(2)) is the same as the prefix list representation [+,1,2] . My problem is that I only want to allow num(x) greater than 3. For example num(4) is allowed but not num(1) . I'm doing this by: num(num(4)) --> [4]. num(num(5)) --> [5]. num(num(6)) --> [6]. num(num(7)) --> [7]. etc. but would like