can a context free grammar be left and right recursive?
ex.
S-> S + T | 开发者_如何学PythonT
T-> U - T | U
U -> ID | N
associativity is obviously not preserved. But I can't see it being ambiguous in anyways.. So is this a non-ambiguous cfg?
A grammar can have both left and right recursions, like you show, but that doesn't mean much. Any grammar can be rewritten so all the recursions are either left or right (but not consistently the same one unless the grammar is regular):
A -> B A C
becomes:
A -> B X
X -> A C
You now have a mutual recursion which is on the left in one rule and on the right in another. Your grammar in the question does appear to be unambiguous, but that really doesn't have anything to do with left- or right-recursion.
精彩评论