DCG question. 
Author Message
 DCG question.

I want to write a DCG that can take in a list of words
and convert some of the words to other words,
as shown here :

?- parse( [a, b, c, twelve, d, e], Out, [] ).
Out = [a, b, c, one, two, d, e]

Yes
?- parse( [a, b, thirty, six, c, d], Out, [] ).
Out = [a, b, three, six, c, d]

Yes
?- parse( [five, hundred, and, twenty, four, a, b, c], Out, [] ).
Out = [five, two, four, a, b, c]

Yes
?-

Note that many strings of words can't be converted to just one term.
They have to be converted to two or more words (e.g "three six" )
which are inserted in the output list, (e.g. "[a, b, three, six, c, d]" ).

I know a bit about DCG, but this task is beyond me.

All the examples of DCG grammars that I've seen
take one of more words in a list and convert them into a single term.
I don't know how to convert those words into two or three terms.

(If you can point me towards a website that says how this can be done,
that would be fine.)

--
Martin Sondergaard,
London.



Fri, 24 Jun 2005 23:12:03 GMT  
 DCG question.

Quote:

> I want to write a DCG that can take in a list of words
> and convert some of the words to other words,
> as shown here :
> ?- parse( [a, b, c, twelve, d, e], Out, [] ).
> Out = [a, b, c, one, two, d, e]
> Note that many strings of words can't be converted to just one term.
> They have to be converted to two or more words (e.g "three six" )
> which are inserted in the output list, (e.g. "[a, b, three, six, c, d]" ).
> I know a bit about DCG, but this task is beyond me.
> All the examples of DCG grammars that I've seen
> take one of more words in a list and convert them into a single term.
> I don't know how to convert those words into two or three terms.

But a list *is* a single term, right? So we'll need another pair of
variables for the *output* list.

parse(Input,Output) :-
    parse(Output,[],Input,[]).

parse(R0,R) -->
    parse_one(R0,R1),
    parse(R1,R).
parse(R,R) --> [].

parse_one([a|R],R) --> [a].

parse_one([one,two|R]) --> [twelve].

Gj

--
Gertjan van Noord Alfa-informatica, RUG,  Postbus 716, 9700 AS Groningen
vannoord at let dot rug dot nl            http://www.let.rug.nl/~vannoord



Sat, 25 Jun 2005 00:13:48 GMT  
 DCG question.

Quote:

> > All the examples of DCG grammars that I've seen
> > take one of more words in a list and convert them into a single term.
> > I don't know how to convert those words into two or three terms.

> But a list *is* a single term, right? So we'll need another pair of
> variables for the *output* list.

> parse(Input,Output) :-
>     parse(Output,[],Input,[]).

> parse(R0,R) -->
>     parse_one(R0,R1),
>     parse(R1,R).
> parse(R,R) --> [].

> parse_one([a|R],R) --> [a].

> parse_one([one,two|R]) --> [twelve].

> Gj

    I don't really understand how that code works.
I tried to mentally trace through it, but couldn't quite
do it.

    But with a bit of trial and error I did get it working correctly.
The second clause of "parse_one" had a parameter missing, "R".

    I changed "parse_one" to this :

parse_one([a1|R], R) --> [a].
parse_one([one,two|R], R) --> [twelve].
% New clause.  Returns the word unchanged :
parse_one([X|R], R) --> [X].

    It can also be coded like this :

parse_one( [a1|A], A, [a|B], B ).
parse_one( [one, two|A],A, [twelve|B], B ).
parse_one( [A|B], B, [A|C], C ).

    Now it works as desired :

?- parse( Out, [], [a, twelve, a, b, c, a, a], [] ).

Out = [a1, one, two, a1, b, c, a1, a1]

Yes
?-

    Thank you Gj.

--
Martin Sondergaard,
London.



Sun, 26 Jun 2005 22:55:37 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. A DCG question.

2. Logical Form question for DCG

3. Question about DCG's and natural language

4. DCG troubles

5. DCG problem

6. DCG help ASAP!!!

7. DCG help

8. DCG's compared to Haskell monads

9. DCG Limitations?

10. Parsing C++ with dcg?

11. dcg

12. DCG grammer result manipulation

 

 
Powered by phpBB® Forum Software