adamcrussell

Perl Weekly Challenge 010

As you'll see below I took a bit of an over engineered approach to Part 1 by using  Parse::YYLex with byacc. That was a fun rabbit hole to dive into and something that I'll continue to explore even after this week's Perl Weekly Challenge concludes.

As with the previous weekly challenges the problem statements are short and are included in the first comment of the code. The code blocks  shown  link to GitHub Gists.

Part 1

Sample Run

$ ./perl-byacc1.8.2/byacc -d -P RomanParser roman.y
$ perl -I. ch-1.pl
MMI  
2001
CDIV
404

The code for this look unexpectedly short because all the work is being done by the lexer and parser! By running byacc (with the -d option) we generate the following files based on the input file roman.y

  • RomanParser.pm — the parser itself
  • y.tab.ph — numerical token list (required)

The file roman.y is the bulk of the effort for this part of the challenge. It is fairly large so check it out at the link above. It contains the actions to take for each Roman numeral generated by the tokens given to it from the yylex() function implemented within that file.

Part 2

After spending so much over engineering energy on Part 1 this second part of the challenge was much more straightforward! Well, somewhat. I was recently reading the Vandevoorde and Josuttis C++ Template book and had the idea of doing as much during compilation as possible stuck in my head! Whence the somewhat more prolific use of use constant than usual.

Comments for this post were locked by the author