dcg

How do I read in a text file and print them out to a file in Prolog?

拜拜、爱过 提交于 2019-12-24 10:18:08
问题 I have a text file, and I want to read it in and print them out in screen and write them into a new output file. So what I have done so far is main :- open('text.txt', read, ID), % open a stream repeat, % try again forever read(ID, X), % read from the stream write(X), nl, % write to current output stream X == end_of_file, % fail (backtrack) if not end of !, close(ID). But I only received an error message like, ERROR: text.txt:1:0: Syntax error: Operator expected What should I do? 回答1: read/2

A DCG that matches the rest of the input

喜你入骨 提交于 2019-12-24 04:41:25
问题 This is the predicate that does what it should, namely, collect whatever is left on input when part of a DCG: rest([H|T], [H|T], []). rest([], [], []). but I am struggling to define this as a DCG... Or is it at all doable? This of course is not the same (although it does the same when used in the same manner): rest([H|T]) --> [H], !, rest(T). rest([]) --> []. The reason I think I need this is that the rest//1 is part of a set of DCG rules that I need to parse the input. I could do phrase(foo

Consecutive elements in a list

匆匆过客 提交于 2019-12-23 20:18:10
问题 I'm blocking on a predicate to code in Prolog . I need to code that two predicates: If I call : u([a,b,c,d,e,f], X). it will give X=[a,b], X=[b,c], X=[c,d] ... If I call : v([a,b,c,d,e,f], X). it will give X=[a,b], X=[c,d], X=[e,f] ... Thanks a lot! 回答1: Although false's answer is more elegant, here is a solution more appropriate for beginners for your predicate u/2 . u([X,Y|_], [X,Y]). u([_|Tail], XY):- u(Tail,XY). The first rule says that [X,Y] represent two consecutive elements in a list

Is there a BNF with arguments for non-terminal symbols?

会有一股神秘感。 提交于 2019-12-23 13:05:36
问题 In working with Prolog DCG to parse input it is nice to have an accompaning BNF of the grammar. For example: BNF <Sentence> ::= <Noun_phrase> <Verb_phrase> <Noun_phrase> ::= <Determiner> <Noun> <Verb_phrase> ::= <Verb> <Phrase> <Determiner> ::= a <Determiner> ::= the <Noun> ::= cat <Noun> ::= mouse <Verb> ::= scares <Verb> ::= hates as Prolog DCG sentence --> noun_phrase, verb_phrase. verb_phrase --> verb, noun_phrase. noun_phrase --> determiner, noun. determiner --> [a]. determiner --> [the]

arithmetic computer

纵饮孤独 提交于 2019-12-23 01:22:13
问题 I need some help in prolog, which is pretty new to me. I have to design a small arithmetic computer. The expression to be evaluated will be represented as a list for example: ?-evaluate([2,+,4,*,5,+,1,*,2,*,3],R). I am trying to do this by designing two predicates one called parse to transform my list for example: ?-parse([1,+,2,*,3],PF). PF=[+,1,[*,2,3]] and another one to evaluate the new expression. ?-evpf([+,1,[*,2,3]],R). R=7 I have problems with the first part, can anyone help my with

Parse Variables Using DCG

廉价感情. 提交于 2019-12-22 09:30:25
问题 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]} 回答1: Maybe you are looking for

What does the operator `-->` in Prolog do?

霸气de小男生 提交于 2019-12-21 19:22:19
问题 What does the --> operator do in Prolog and what is the difference between it and :- ? I'm using SWI Prolog. 回答1: It is used to define a DCG ( D efinite C lause G rammar) rule as opposed to a normal predicate. See this tutorial for a very nice explanation of DCG rules. There are many examples here on Stack Overflow. See the DCG tag. Here is one very simple solution, showing both the DCG and the normal predicate. Note that you have to use phrase/2 or phrase/3 to evaluate a DCG. Make sure to

Very basic dcg prolog syntax

Deadly 提交于 2019-12-20 01:45:15
问题 I am trying to understand prolog and definite clause grammar but I am having a very hard time understanding both of them. I am really trying to understand how to use the dcg syntax... Here I have two examples: The first is actually the code from another question on this forum but with an additional question: The code looked like this: s --> first, operator, second. first --> [X]. operator --> ['+']. second --> [X]. And when Prolog is asked about this, it returns true/false but I can't for the

Parsing numbers with multiple digits in Prolog

被刻印的时光 ゝ 提交于 2019-12-19 05:29:45
问题 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? 回答1: Use accumulator variables, and pass

Extension to CFG, what is it?

限于喜欢 提交于 2019-12-19 02:22:19
问题 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.