According to the dragon book, the algorithm for LR(1) state machine is:
initialize C to Closure([S` -> .S, $])
repeat
for each state in C
for each grammar symbol X
if Goto(state, X) is not empty and is not in C
add Goto(state, X) to C
until no new states are added
How can the time complexity of this algorithm can be analysed, in terms of the number of terminals, non-terminals and productions in the grammar.
Also, assuming this algorithm is changed to construct LALR(1) (such that a state is considered in C
if there is a state with the same kernel), only the function in C
changes (but not in terms of complexity). So how can their run times be expressed differently mathematically?
I know Closure is O(n * k) where:
- n – number of terminals
- k – number of production rules
(Obviously this bound is the very worst case and the average bound is probably lower)
But how can the complexity of the repeat until no new states are added
be analysed?