dcg

prolog dcg restriction

不想你离开。 提交于 2021-02-07 13:23:36
问题 I would like to use DCGs as a generator. As of now, the syntax is s-->a,b. a-->[]. a-->a,c. c-->[t1]. c-->[t2]. b-->[t3]. b-->[t4]. I would like to generate all s where the length of a is < someNumber . Using ?- phrase(a,X),length(X,Y),Y<4. i can get all a with less than 4 items. However, when all combinations are exhausted, the system (SWI-Prolog 6.2.5) seems to stall. Sometimes ago, a similar question was asked here. However, being new to Prolog i am not able to make it work with the

DCG : zero-or-more, zero-or-one , one-or-more occurrences?

守給你的承諾、 提交于 2021-01-27 21:33:54
问题 In DCG how do you implement : zero-or-more, zero-or-one , one-or-more occurrences ? I'm talking about the following in pseudo code : sentence --> word+ float --> int+, ['.'], int+ nilORa --> a? nilORaaaa --> a* 回答1: You use the or-nondeterminism offered by the clause set of a predicate (or, in this case, the set of DCG productions for the same DCG "nonterminal" - the DCG production is an alternative notation for a Horn clause) Move the production that should be performed first to the top. For

About building a list until it meets conditions

Deadly 提交于 2021-01-27 06:13:06
问题 I wanted to solve "the giant cat army riddle" by Dan Finkel using Prolog. Basically you start with [0] , then you build this list by using one of three operations: adding 5 , adding 7 , or taking sqrt . You successfully complete the game when you have managed to build a list such that 2 , 10 and 14 appear on the list, in that order, and there can be other numbers between them. The rules also require that all the elements are distinct, they're all <=60 and are all only integers. For example,

About building a list until it meets conditions

非 Y 不嫁゛ 提交于 2021-01-27 06:12:29
问题 I wanted to solve "the giant cat army riddle" by Dan Finkel using Prolog. Basically you start with [0] , then you build this list by using one of three operations: adding 5 , adding 7 , or taking sqrt . You successfully complete the game when you have managed to build a list such that 2 , 10 and 14 appear on the list, in that order, and there can be other numbers between them. The rules also require that all the elements are distinct, they're all <=60 and are all only integers. For example,

How do I create a DCG rule inverse to another in Prolog?

a 夏天 提交于 2021-01-04 06:40:43
问题 I am writing a Commodore BASIC interpreter in Prolog, and I am writing some DCGs to parse it. I have verified the DCGs below to work except for the variable one. My goal is this: for anything which isn't a boolean, integer, float, or a string, it's a variable. However, anything that I give it via phrase just results in no . bool --> [true]. bool --> [false]. integer --> [1]. % how to match nums? float --> [0.1]. string --> [Str], {atom_chars(Str, ['"' | Chars]), last(Chars, '"')}. literal -->

How do I create a DCG rule inverse to another in Prolog?

烈酒焚心 提交于 2021-01-04 06:39:29
问题 I am writing a Commodore BASIC interpreter in Prolog, and I am writing some DCGs to parse it. I have verified the DCGs below to work except for the variable one. My goal is this: for anything which isn't a boolean, integer, float, or a string, it's a variable. However, anything that I give it via phrase just results in no . bool --> [true]. bool --> [false]. integer --> [1]. % how to match nums? float --> [0.1]. string --> [Str], {atom_chars(Str, ['"' | Chars]), last(Chars, '"')}. literal -->

How do I create a DCG rule inverse to another in Prolog?

。_饼干妹妹 提交于 2021-01-04 06:39:26
问题 I am writing a Commodore BASIC interpreter in Prolog, and I am writing some DCGs to parse it. I have verified the DCGs below to work except for the variable one. My goal is this: for anything which isn't a boolean, integer, float, or a string, it's a variable. However, anything that I give it via phrase just results in no . bool --> [true]. bool --> [false]. integer --> [1]. % how to match nums? float --> [0.1]. string --> [Str], {atom_chars(Str, ['"' | Chars]), last(Chars, '"')}. literal -->

How to find out if Prolog performs Tail Call Optimization

假如想象 提交于 2020-12-08 07:33:14
问题 Using the development version of SWI Prolog (Win x64), I wrote a DCG predicate for a deterministic lexer (hosted on github) (thus all external predicates leave no choice points): read_token(parser(Grammar, Tables), lexer(dfa-DFAIndex, last_accept-LastAccept, chars-Chars0), Token) --> ( [Input], { dfa:current(Tables, DFAIndex, DFA), char_and_code(Input, Char, Code), dfa:find_edge(Tables, DFA, Code, TargetIndex) } -> { table:item(dfa_table, Tables, TargetIndex, TargetDFA), dfa:accept(TargetDFA,

How to find out if Prolog performs Tail Call Optimization

泄露秘密 提交于 2020-12-08 07:33:02
问题 Using the development version of SWI Prolog (Win x64), I wrote a DCG predicate for a deterministic lexer (hosted on github) (thus all external predicates leave no choice points): read_token(parser(Grammar, Tables), lexer(dfa-DFAIndex, last_accept-LastAccept, chars-Chars0), Token) --> ( [Input], { dfa:current(Tables, DFAIndex, DFA), char_and_code(Input, Char, Code), dfa:find_edge(Tables, DFA, Code, TargetIndex) } -> { table:item(dfa_table, Tables, TargetIndex, TargetDFA), dfa:accept(TargetDFA,

How to find out if Prolog performs Tail Call Optimization

Deadly 提交于 2020-12-08 07:32:15
问题 Using the development version of SWI Prolog (Win x64), I wrote a DCG predicate for a deterministic lexer (hosted on github) (thus all external predicates leave no choice points): read_token(parser(Grammar, Tables), lexer(dfa-DFAIndex, last_accept-LastAccept, chars-Chars0), Token) --> ( [Input], { dfa:current(Tables, DFAIndex, DFA), char_and_code(Input, Char, Code), dfa:find_edge(Tables, DFA, Code, TargetIndex) } -> { table:item(dfa_table, Tables, TargetIndex, TargetDFA), dfa:accept(TargetDFA,