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!
views:
958answers:
2
A:
The wikipedia article on Left Recursion gives pretty good info on how to pull this off.
Mark P Neyer
2009-06-10 20:55:55
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
2009-06-10 23:14:59
+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
2009-06-11 02:23:35