I'm confused on how to parse this grammar using LR(1):
S -> A
A -> A(A) | empty
I'm aware there is left recursion but I was told it isn't necessary to remove it for LR(1). My item sets look like this: (the comma separates the grammar from the lookaheads)
item set 0:
s -> .A, $
A -> .A(A), $(
A -> ., $(
item set 1:
S -> A., $
A -> A.(A), $(
Now here is where I get confused:
item set 2:
A -> A(.A), $(
A -> .A(A), )(
A -> ., )(
item set 3:
A -> A(A.), $(
A -> A. (A), )(
item set 4:
A -> A(A)., $(
I was told to parse the string "(())". When I did this i realized state 4 needed to have a right parenthesis ")" as a lookahead, which makes sense. Now I traced this back and figured out that this right parenthesis should have originally came from the first statement in item set 2.
A -> A(.A), $()
This would cause the right parenthesis to be carried over to the next 2 states and my grammar would parse perfectly. However, what I'm confused about is WHY a right parenthesis is supposed to be here? Shouldn't the $( be carried over from item set 1. Where does this right parenthesis come from? Can somebody please explain. Thanks