0

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"?

Guy Coder
  • 24,501
  • 8
  • 71
  • 136
ivt
  • 301
  • 3
  • 9

1 Answers1

1

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.
gusbro
  • 22,357
  • 35
  • 46
  • Thanks! That's exactly what I just did! non_digit --> [C], {not(code_type(C,digit))}, non_digit. non_digit --> !. I think it's the same. – ivt Apr 12 '13 at 19:20