3

I've began to read about LR(k) parse table construction, and all texts explaining the algorithm for k > 0 suggest that the lookaheads should be calculated for every symbol before generating the item sets, then when all item sets are generated, the redundant ones should be merged to generate a minimal parse table.

Consider the following pseudo state/itemset construction routine:

  1. Start by assuming a state transition can be determined without lookaheads(k = 0)
  2. Calculate the entire itemset for the current state
  3. Try to determine the current state action:
    • If there's only one item in the set and it has consumed all input(marker to the right of the rhs, the action is reduce to the lhs of the item.
    • If every item in the set expects an input, the action is to shift and go to the next state
    • If some items expect input and some don't this is a shift/reduce conflict
    • If two or more items with different lhs reached end of input, this is a reduce /reduce conflict. If one of the last two cases occurred, it means we need to look ahead before deciding the state action. Increase k by 1 and go back to step 2.
  4. If the action is to shift, proceed to create the follow up states by simulating combinations of inputs(and lookaheads if k > 0) and going back to step 1 for each new state.

Would it be possible/viable to construct tables for arbitrary LR(k) grammars using the above steps? If not, what am I missing?

Thiago Padilha
  • 4,590
  • 5
  • 44
  • 69
  • You can't "compute the itemset for the current state". States aren't picked off trees; they are computed. In a very real sense, the state and its itemset/lookahead are indistinguishable. – rici Oct 19 '13 at 00:10
  • Thats just the way I'm thinking on how to calculate the states: beginning with the start rule, compute its itemset and transitions(if any) at the same time. Thats what I meant by the 'current state': at this point you know almost everything about the state), all you need is to compute its follow-up states recursively using the current itemset then associate the transitions – Thiago Padilha Oct 19 '13 at 10:06
  • I think if you continue with this line of thought, you will rediscover some variety of chart parsing, or perhaps glr parsing. These are powerful techniques, which are once again becoming popular. I'm sure there are still discoveries to be made so I encourage you to continue, but it's more a question for cs.stackexchange.com than stackoverflow. – rici Oct 19 '13 at 16:16
  • Pager's lane-tracing algorithm might've been what you were looking for (n.b. it's different from the lane-table algorithm that also comes up when you search for Pager). – user Jul 10 '18 at 16:16

0 Answers0