DCGs are a sublanguage hosted in Prolog, then it's easy to count occurrences of a terminal T.
count(T, N) --> [T], count(T, M), {N is M+1}.
count(_, 0) --> [].
?- phrase(count(a, C), [a,a,a]).
C = 3 ;
false.
Of course, you should find this can solve your question easily.
EDIT: I missed the generation part. count//2 will not work for generation, because of builtin arithmetic. Using the successors notation the code can work as a generator too:
count(T, s(N)) --> [T], count(T, N).
count(_, 0) --> [].