Norswap
Norswap

Reputation: 12202

Converting a context-free grammar into a LL(1) grammar

First off, I have checked similar questions and none has quite the information I seek.

I want to know if, given a context-free grammar, it is possible to:

If the answer to those questions are yes, are such algorithms or tools available somewhere ? My own researches have been fruitless.

Another answer mentions that the Dragon Book has an algorithms to eliminate left recursion and to left factor a context-free grammar. I have access to the book and checked it but it is unclear to me if the grammar is guaranteed to be LL(1). The restriction imposed on the context-free grammar (no null production and no cycle) are agreeable to me.

Upvotes: 2

Views: 7095

Answers (1)

jJ'
jJ'

Reputation: 3078

From university compiler courses I took I remember that LL(1) grammar is context free grammar, but context free grammar is much bigger than LL(1). There is no general algorithm (meaning not NP hard) to check and convert from context-free (that can be transformed to LL(1)) to LL(1).

Applying the bag of tricks like removing left recursion, removing first-follow conflict, left-factoring, etc. are similar to mathematical transformation when you want to integrate a function... You need experience that is sometimes very close to an art. The transformations are often inverse of each other.

One reason why LR type grammars are being used now a lot for generated parsers is that they cover much wider spectrum of context free grammars than LL(1).

Btw. e.g. C grammar can be expressed as LL(1), but C# cannot (e.g. lambda function x -> x + 1 comes to mind, where you cannot decide if you are seeing a parameter of lambda or a known variable).

Upvotes: 2

Related Questions