views:

958

answers:

2

I'm trying to write a regular expression engine. I'd like to write a recursive descent parser by hand. What would a context-free grammar without left recursion for the language of regular expressions (not the languages that can be described by regular expressions) look like? Would it be easiest to re-factor out the syntactic sugar, i.e. change a+ to aa* ? Thanks in advance!

A: 

The wikipedia article on Left Recursion gives pretty good info on how to pull this off.

Mark P Neyer
It's not that I need to re-factor a grammar with left recursion, but rather that I'm trying to get a feel for what the grammar should look like in general. While I've read about them a lot, I've never actually used a context-free grammar 'in the wild' so to speak.
wkf
+4  A: 

Left recursion:

Expression = Expression '|' Sequence
           | Sequence
           ;

Sequence = Sequence Repetition
         | <empty>
         ;

Right recursion:

Expression = Sequence '|' Expression
           | Sequence
           ;

Sequence = Repetition Sequence
         | <empty>
         ;

Ambiguous form:

Expression = Expression '|' Expression
           | Sequence
           ;

Sequence = Sequence Sequence
         | Repetition
         | <empty>
         ;
MizardX
Right on man; you've answered all my questions this evening. Thanks!
wkf