Re: Is C++ LL(k)?

"Mike Dimmick" <>
27 Jul 2001 02:55:10 -0400

          From comp.compilers

Related articles
Re: Is C++ LL(k)? (Lin Gu) (2001-07-23)
Re: Is C++ LL(k)? (2001-07-27)
Re: Is C++ LL(k)? (Mike Dimmick) (2001-07-27)
Re: Is C++ LL(k)? (2001-08-06)
| List of all articles for this month |

From: "Mike Dimmick" <>
Newsgroups: comp.lang.c++,comp.compilers
Date: 27 Jul 2001 02:55:10 -0400
Organization: Compilers Central
References: <9jh5uu$olr$> <mSZ67.20253$> 01-07-138
Keywords: C++, parse
Posted-Date: 27 Jul 2001 02:55:09 EDT

"Lin Gu" <> wrote in message
> Thanks for the clear elumination.
> Your example also reminds me the 'if...if...else' ambiguation. Surely it is
> not LR(k).
> However, I may have to use an LL(k) compiler generator (Antlr) to
> write a compiler for it. Is it difficult? It is expected that I need
> to add some rules to disambiguate, but I want to know whether this is
> feasible.

If it's not LR(k), it can't possibly be LL(k).

My own experiences, with PCCTS (ANTLR v1.x - the whole tool set is now
incorporated into one command-line tool with ANTLR 2.x), suggest that C++ is
not LL(k). I don't think it's LR(k) either. I may be wrong - I don't have
an LR(k) tool to test with. I've been considering building one. I was
personally attempting to get it down to LL(1) but this was before I
understood exactly how PCCTS k>1 parsing works. Don't try, you'll just
exhaust yourself for little visible benefit. My feeling is that you need at
least LL(3), although the largest lookahead I tried was 2 tokens.

For example, the steering logic around the nested-name-specifier suggests
that at least three, possibly more, tokens of lookahead are necessary.
However, the main problem with C++ is that user-defined types are used as
keywords - this is perfectly intellegible to a human, but means you need to
disambiguate identifiers in some contexts. You can do this in PCCTS with
semantic predicates, but you still need to navigate the symbol table at
parse time - yuck.

There is a moderately successful C++ parser based around a rewritten version
of PCCTS by John Lilley; you can find links to it on

Now, if you only need a cut-down C++ (no nested classes, namespaces), then
you can probably use ANTLR. The NeXT-derived grammar at is
probably sufficient for this.


Mike Dimmick

Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.