% ab/2 % Write a DCG with the starting symbol ab for the language a^mb^n where m,n≥0. % % Example words: [], a, aab, abbb, bbb. % % Hint: to generate words of increasing length, use the query % ?- conc(Word,_,_), ab(Word,[]). ab --> [a], ab. ab --> t. t --> [b], t. t --> []. % flower/2 % Write a DCG with the starting symbol flower for the language +^n-^m+^n, where % m>0 and n≥0. % % Example words: -, ++-++, +---+. flower --> iflower. flower --> [+], flower, [+]. iflower --> [-]. iflower --> [-], iflower. % digit/2 % Write a DCG with the starting symbol digit for the language defined by the % set of words {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}. digit --> [N], { memb(N, [0,1,2,3,4,5,6,7,8,9]) }. % number/2 % Write a DCG with the starting symbol number for the language of non-negative % integers. The numbers may include leading zeros. % % Example words: 123, 54, 0122, 0001221, 0. number --> digit. number --> digit, number. % number_proper/2 % Write a DCG with the starting symbol number_proper for the language of % non-negative integers. The numbers should not contain leading zeros. % % Example words: 123, 54, 122, 1221, 0. number_proper --> digit. number_proper --> nzdigit, num_next. num_next --> digit. num_next --> digit, num_next. nzdigit --> [N], { memb(N, [1,2,3,4,5,6,7,8,9]) }. % number/3 % Write a DCG with the starting symbol number for the language of non-negative % integers. The numbers may contain leading zeros. The meaning of a word in % this language is the numeric value of the represented number. % % ?- number(N, [1,2,3,4], []). % N = 1234. digit(N) --> [N], { memb(N, [0,1,2,3,4,5,6,7,8,9]) }. number(N) --> digit(N). number(N) --> number(N1), digit(D), { N is 10*N1 + D }. % paren/2 % Write a DCG with the starting symbol paren for the language of properly % nested sequences of parentheses. The terminal symbols in the grammar should % be written like this: ['('] and [')']. % % Example words: (), (()), ()(()), (()())(). % Example non-words: )(, (((), )). paren --> []. paren --> ['('], paren, [')'], paren. % paren/3 % Write a DCG with the starting symbol paren for the language of properly % nested sequences of parentheses. The meaning of a word in this language is % the maximum depth of the nested parentheses. % % ?- paren(D, ['(','(',')',')','(',')'], []). % (())() % D = 2. paren(0) --> []. paren(D) --> ['('], paren(D1), [')'], paren(D2), { D1 >= D2, D is D1 + 1 ; D1 < D2 , D is D2 }.