سؤال

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

هل كانت مفيدة؟

المحلول

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.
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top