Reputation: 301
I was going through this example on DCG
integer(I) -->
digit(D0),
digits(D),
{ number_codes(I, [D0|D])
}.
digits([D|T]) -->
digit(D), !,
digits(T).
digits([]) -->
[].
digit(D) -->
[D],
{ code_type(D, digit)
}.
But this example parses an integer only if it's in the beginning of the string (because digit(D0) fails is D0 is not a number code). How do I go about parsing an integer anywhere in the string, e.g. "abc123def"?
Upvotes: 0
Views: 390
Reputation: 22585
You might add something like this:
non_digits--> [D], {not(code_type(D, digit))}, !, non_digits.
non_digits-->[].
and then add a call to non_digits
to skip non digits, e.g.:
integer_skip(I) -->
non_digits,
digit(D0),
digits(D),
{
number_codes(I, [D0|D])
},
non_digits.
Upvotes: 1