1

I need a way of generating parsers for all deterministic context-free grammars.

I know that every deterministic context-free grammar can be parsed by some LR(k) parser. The problem is that I need to generate parsers for grammars of unknown k. So, to handle every deterministic context-free grammar, k would need to be infinite.

I also know that GLR parsers can parse all context-free grammars, deterministic or not. But I need to reject non-deterministic grammars. I'm not sure if GLR can detect that property from an input grammar.

Is there a type of parser generator that can handle all deterministic context-free grammars, while rejecting non-deterministic grammars, without needing a k input? (The only input is the grammar itself)

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
Joshua Wise
  • 613
  • 4
  • 15

1 Answers1

1

The problem of “given a CFG, decide whether it’s LR(k) for any k” is, surprisingly, undecidable! This means that it’s not possible for any parser generator to always be able to take an arbitrary grammar and determine which choice of k to use, or even if such a choice of k exists.

In practice, most grammars that we care about are fairly close to LR(1), for some definition of “fairly close,” which is why most parser generators focus on that simpler case.

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
  • How can this be so? Any CFG can be converted into a PDA, right? According to wikipedia (https://en.wikipedia.org/wiki/Deterministic_pushdown_automaton), "A deterministic pushdown automaton has at most one legal transition for the same combination of input symbol, state, and top stack symbol." So, couldn't I just convert the CFG into a PDA, then check the PDA to see if it meets the above criteria? If it does, I know it's a deterministic PDA, which means the CFG was deterministic, which means it's LR(k). What's wrong with my thinking here? – Joshua Wise May 14 '21 at 16:08
  • 1
    @joshua: Any CFG can be converted to an NPDA. Sometimes that results in a DPDA. But the fact that the algorithm didn't happen to produce a DPDA doesn't indicate that one doesn't exist. – rici May 14 '21 at 16:29
  • Ahh okay. Thanks for the explanation. – Joshua Wise May 14 '21 at 16:30
  • Actually, I think there are more GLR parser generators than LR(2) generators. But if you know of a horde of LR(2) generators (or even one good one) do let me know :-) – rici May 14 '21 at 16:44