I was going to fix your code but found it easier to just write it from scratch.
Sorry I don’t have time at the moment to explain how it works.
% parens_0//0 - Example DCG that only parses for validation. Does not return a structure.
parens_0 -->
"(",
parens_0, !.
parens_0 -->
")",
parens_0, !.
parens_0 -->
[_],
parens_0, !.
parens_0 --> [].
% parens//1 DCG that returns only parenthesis from input.
parens(["("|T]) -->
"(",
parens(T), !.
parens([")"|T]) -->
")",
parens(T), !.
parens(L) -->
[_],
parens(L), !.
parens([]) --> [].
parens_diff(List,Result) :-
parens_diff(List,0,Result).
parens_diff(["("|T],Count0,Result) :-
Count is Count0 + 1,
parens_diff(T,Count,Result).
parens_diff([")"|T],Count0,Result) :-
Count is Count0 - 1,
parens_diff(T,Count,Result).
parens_diff([],Count,Count).
:- begin_tests(parens).
parens_0_test_generator("()()" ).
parens_0_test_generator("(())" ).
parens_0_test_generator("(((" ).
parens_0_test_generator("(()(()(").
parens_0_test_generator("))(((((").
parens_0_test_generator("())" ).
parens_0_test_generator("))(" ).
parens_0_test_generator(")))" ).
parens_0_test_generator(")())())").
parens_0_test_generator("a(b)c(d)e" ).
parens_0_test_generator("a(b(c)d)e" ).
parens_0_test_generator("a(b(c(d" ).
parens_0_test_generator("a(b(c)d(e(f)g(h").
parens_0_test_generator("a)b)c(d(e(f(g(h").
parens_0_test_generator("a(b)c)d" ).
parens_0_test_generator("a)b)c(d" ).
parens_0_test_generator("a)b)c)d" ).
parens_0_test_generator("a)b(c)d)e(f)g)h").
test(001,[forall(parens_0_test_generator(Input))]) :-
string_codes(Input,Codes),
phrase(parens_0,Codes,Rest),
assertion( Rest == [] ).
parens_test_generator("()()" ,["(",")","(",")"]).
parens_test_generator("(())" ,["(","(",")",")"]).
parens_test_generator("(((" ,["(","(","("]).
parens_test_generator("(()(()(",["(","(",")","(","(",")","("]).
parens_test_generator("))(((((",[")",")","(","(","(","(","("]).
parens_test_generator("())" ,["(",")",")"]).
parens_test_generator("))(" ,[")",")","("]).
parens_test_generator(")))" ,[")",")",")"]).
parens_test_generator(")())())",[")","(",")",")","(",")",")"]).
test(002,[forall(parens_test_generator(Input,Expected_result))]) :-
string_codes(Input,Codes),
phrase(parens(Parens),Codes,Rest),
assertion( Parens == Expected_result ),
assertion( Rest == [] ).
parens_diff_test_generator(["(",")","(",")"] ,0).
parens_diff_test_generator(["(","(",")",")"] ,0).
parens_diff_test_generator(["(","(","("] ,3).
parens_diff_test_generator(["(","(",")","(","(",")","("],3).
parens_diff_test_generator([")",")","(","(","(","(","("],3).
parens_diff_test_generator(["(",")",")"] ,-1).
parens_diff_test_generator([")",")","("] ,-1).
parens_diff_test_generator([")",")",")"] ,-3).
parens_diff_test_generator([")","(",")",")","(",")",")"],-3).
test(003,[forall(parens_diff_test_generator(Input,Expected_result))]) :-
parens_diff(Input,Result),
assertion( Result == Expected_result ).
:- end_tests(parens).
Example run
?- make.
% c:/swi-discourse_027 compiled 0.00 sec, 0 clauses
% PL-Unit: parens .................................... done
% All 36 tests passed
true.