Parse::RecDescent vs. YACC

January 29th, 2004 at 3:08 pm

Parse::RecDescent (RD) looks like the best parsing option in Perl for me, for two reasons. First, it is very lightweight – only one .pm file to carry around. Second, I like recursive descent parsing :-) RD parsing is, IMHO, easier to visualize and understand. Looking at the grammar (BNF) it is immediately obvious how each rule will be parsed given the input. This is very nice for grammar debugging.

Yesterday was my first serious experience with the RD module (historically, I did a lot of Yacc (in C), and coded some simple recursive descent parsers by hand). The module works nicely, and is easy to learn and understand. Some notable differences from Yacc:

  1. Integrated lexing. Very nice ! It looks much more natural this way, and there’s no need for extra headache with Lex linkage. Tokens are defined as simple regex rules in the grammar itself.
  2. Some little things that make life easier and more pleasant. For example, the rule quantifiers (s), (s?) etc.
  3. Left recursion problem. Hits blatantly when arithmetic expressions must be parsed. A different mindset must be employed when comparing with Yacc.

Additionally, RD has a very useful trace option, that traces parsing and allows to see where things went wrong with the grammar.

Related posts:

  1. a VHDL parser in Perl
  2. Recursive descent, LL and predictive parsers
  3. Some problems of recursive descent parsers
  4. The context sensitivity of C’s grammar
  5. How Clang handles the type / variable name ambiguity of C/C++

Comments are closed.