Reputation: 13915
While parsing list of strings the current rules allow comma to be at the end of the sentence and not allow comma to be between words, which is obviously not correct. How to modify current rules?
complexsentence --> [] ; simplesentence, complexsentence;
simplesentence, delimiter, complexsentence.
simplesentence --> noun, verb.
delimiter --> [,].
noun --> [sun].
verb --> [shines].
% TEST: phrase(complexsentence, [sun,shines]). %must be TRUE
% TEST: phrase(complexsentence, [sun,',',shines]). %must be TRUE
% TEST: phrase(complexsentence, [sun,shines,',']). %must be FALSE
Update: Tried to solve it like this. It works
complexsentence --> [] ; simplesentence,complexsentence.
simplesentence --> noun,verb;
noun,verb,delimiter,noun;
noun,verb,delimiter,noun,verb.
delimiter -->[,].
noun-->[sun].
verb-->[shines].
% TEST: phrase(complexsentence, [sun,shines]). % TRUE
% TEST: phrase(complexsentence, [sun,',',shines]). % FALSE
% TEST: phrase(complexsentence, [sun,shines,',']). %FALSE
Upvotes: 0
Views: 131
Reputation: 58324
Suggested change to DCG:
complexsentence --> simplesentence. %was []
complexsentence --> simplesentence, complexsentence.
complexsentence --> simplesentence, delimiter, complexsentence.
simplesentence --> noun, verb.
delimiter --> [,].
noun --> [sun].
verb --> [shines].
Then:
% TEST: phrase(complexsentence, [sun,shines]). %will be TRUE
% TEST: phrase(complexsentence, [sun,',',shines]). %will be FALSE (as it should)
% TEST: phrase(complexsentence, [sun,shines,sun,shines]). %will be TRUE
% TEST: phrase(complexsentence, [sun,shines,',',sun,shines]). %will be TRUE
% TEST: phrase(complexsentence, [sun,shines,',']). %will be FALSE
I'm suggesting the second test should be FALSE since the problem description indicates, ...and not allow comma to be between words.
Although the original clause (complexsentence --> [].
) provided a termination case for the recursive definition of complexsentence
, it had two issues: (1) it defined the empty sentence as the simplest complex sentence, and (2) it allowed a trailing delimiter on a valid complex sentence (via the 3rd clause, since you could get simplesentence, delimiter, []
. Using complexsentence --> simplesentence.
as the base case makes more logical sense and fixes the second issue as a bonus, since your other cases are logical in that context.
Upvotes: 3