Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

too slow DCG!

20 views
Skip to first unread message

João Aragão

unread,
Sep 19, 2022, 3:20:02 PM9/19/22
to
Hey. I'm trying to make a program with DCG that recognizes a list of variables as such:
x1, x9, a, b, ..., z

it's getting like this:

var -->
[X], var, {char_type(X, alnum)}
| [X], var, {X='_'}.

var -->
[X], {char_type(X, alnum)}
| [X], {X='_'}.


var-list -->
var, [,], var-list, !.

var-list -->
var.

the problem is that when i try to run phrase(var-list, X), if X is a fairly lengthily list like [a,,,b,,,c,,,d,,,e,,,f,,,g,,,h,,,i,,,j,,,k, ')'] it backtracks too much before returning "false"! ( ')' is not part of list of variables)

any suggestions???

Alex Grabowski

unread,
Sep 21, 2022, 9:11:26 AM9/21/22
to
Hi,

I think the main issue is that var-list has the base case at the last
position and not the first, that's why it loops, also var//2 isn't too
clear for me, it has unnecessary recursive definition.

If you insist on using char_type/2 then I would've written something like
this (tested in SWI):

:- set_prolog_flag(double_quotes, chars).
variable_list --> variable.
variable_list --> variable, variable_list_aux.
variable_list_aux --> ",", variable_list.
variable --> ("_" | alpha), alnums.
alnums --> [] | ("_" | alnum), alnums.
alnum --> [X], { char_type(X, alnum) }.
alpha --> [X], { char_type(X, alpha) }.

Please try to avoid cut operator in simple DCGs like yours and also try
not to use var-list, but use var_list instead, because the first one will
be parsed as -(var, list).

--
Best wishes
0 new messages