User João Aragão wrote:
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???
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
--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)