Nick Maclaren wrote:> I have been thinking about a programming language, and have good> reasons to abandon context-free grammars completely. So what I am> asking is what reasons are there to favour them - I know of the> following:>> 1) Dogma. Let's skip that.>> 2) Efficient, easy to write parsers. That is no longer a major> issue, and I can deliver that in other ways.>> 3) Diagnosability of errors. I can deliver that in other ways.>> So WHY should I use a context-free grammar? Good reasons appreciated.

The language your compiler accepts /already/ isn't context free, if it
checks that variables are bound and that expressions typecheck -- ie,
the set of strings that the compiler will do other than emit an error
on is not context free.

Splitting the task of parsing into phases -- a) lexing, b) parsing,
and c) type-checking -- is just plain old good engineering. Each phase
creates stronger invariants on the things it passes on to the next, so
that writing them is easier. It's easier to form an AST when you know
what's a keyword and what's not. It's easier to typecheck a program
when you know your input is an abstract syntax tree -- your
typechecker doesn't have to have cases for what to do if a paren
doesn't have a closing brace. Making each phase simpler, makes it
easier to understand and debug, and yields more modular and
maintainable code.