1
votes

I have little to no knowledge of what I'm about to ask, so I would like a suggestion based on the level of skills required to implemented a parser for the given grammar ( since I'm a beginner in this kind of formal approach to parsers and languages ).

Just by going back of a couple of years, this situation reminds me a little of Pascal grammar vs C/C++ grammar, this left vs right stuff.

But I'm not going to do any of that, my purpose is to implement a simple parser for a markup language for documents like Markdown.

So considering that I'm starting with a markup language in mind, I want to keep things simple, which is the easiest one to handle between this 2 options and why . Another kind of grammar could be an easier option for me ? If yes which one do you suggest ?

3

3 Answers

4
votes

Right recursive vs. left recursive mostly comes down to how you're going to implement the parser.

If you're going to do a top-down (e.g., recursive descent) parser, you normally want to use right recursion in the grammar (and for pure recursive descent, that's the only option).

If you're going to do a bottom-up parser, you normally want to use left recursion. Bottom up parsers are normally generated with a parser generator like Yacc or Bison, most of which can handle right recursion if needed, but can handle left recursion more efficiently, so it's preferred as long as it doesn't adversely affect semantics.

2
votes

It depends upon what type of parser you are using. If you use an LL type, you have to use right recursion.

If you use an LR type, you can use either left or right recursion. However, left recursion minimizes stack depth.

1
votes

Well there are two basic types of grammars:

LL(k) = Left to tight, leftmost derivation, k symbols look ahead
LR(k) = Left to right, rightmost derivation, k symbols look ahead

LR parsers are much more difficult to write by hand, however, they are more powerful then LL parsers. If you are looking at some popular parser generator tools:

ANTLR : a LL parser
BISON:  a LALR(1) parser (a type of LR parser but a bit less powerful)
CUP:    a LALR(1) parser (outputs Java / C# code)