# Can an LL(k) parser parse any grammar without left recursion or common prefixes?

I'm being asked to create a "top down grammar" for a certain language (I'm pretty sure there's no such thing as a "top down grammar" but I think it means write a grammar that an LL(k) parser can parse).

I'm pretty sure the language is not LL(1), but I can remove left-recursion and common prefixes from it and still have an unambiguous grammar. But I'm a little confused about the significance of that. If I successfully removed left-recursion and common prefixes and the grammar is still unambiguous, is the language LL(k)? And does that mean it can be parsed by a top-down parser?

• What exactly do you mean by 'no common prefixes'? The definition of LL(k)-ness is some variant of that. – reinierpost Oct 9 '12 at 10:38

A 'top down parser' is usually understood to be a hand-written $LL(1)$ parser, or $LL(k)$ for some low $k$, which would explain the use of this terminology. These two concepts are therefore essentially the same.

I'm unsure what you mean exactly by 'common prefixes' - I guess you are talking about common lookaheads. If you ensure that you can always uniquely distinguish which production you need to expand by looking at the next letter, then your language is $LL(1)$ (and also contains no left recursion). Doing that usually means you change your grammar structure a bit so your parser is never confused by 'k letter prefixes' of the part of the input that you haven't looked at yet.

If you are really talking about common prefixes in the language-theoretic sense, then all your words would have to start with a unique letter, which means your language is finite and therefore $LL(1)$.

• I'm pretty sure that by removing common prefixes the OP means left factoring. – AProgrammer Oct 9 '12 at 19:13

This grammar

<stmt> ::= <label> <unlabeled stmt>
<label> ::=
::= <name> ":"
<name>  ::= id <qual>
<qual>  ::=
::= "." id <qual>
<unlabeled stmt> ::= <name> "=" <exp>


is not $LL(k)$ for any $k$ (you need unbound look ahead to be able to decide if there is a label or not), has no left recursion and probably has no common prefixes if I correctly get what you mean. You can get a $LL(1)$ grammar for the same language, but with a generalization of left factoring but involving productions of several non terminals (AFAIK left factoring is usually taken as applying to the productions of one non terminal, an alternative view is that you apply left factoring after having inlined <label> and <unlabeled stmt> in <stmt>):

<stmt> ::= <name> <name suffix>
<name suffix> ::= ":" <unlabeled stmt>
::= "=" <exp>
<name>  ::= id <qual>
<qual>  ::=
::= "." id <qual>
<unlabeled stmt> ::= <name> "=" <exp>