I want to write a program for creating presentations. The presentation style I have in mind is very simple: few lines per slide, few words per line with a font size as large as possible, occasionally an image. (This style is close to what's called the Takahashi style).

LaTeX with the beamer class is a good candidate to make such presentations, but writing \begin{frame}...\end{frame} every time is tedious. So I wanted a program that takes the bare outline (with just the lines I want displayed) and produces the latex source for me.

One required feature of this program would be that, by default, the font size for a particular slide should be chosen so that the longest line fills the available screen width. However, it should be possible to override this mechanism and set absolute font sizes for individual lines.

Here is an example of an outline file:

First slide Test line Second line Line with explicit font size @20

The "@20", or more generally the "@" character and an integer at the end of the line is the font size override.


I actually wrote the program that does all this, but it's became an unmaintainable mess, so this time I want to do it the Right Way(TM).

I figured that when it comes to Perl and parsing text files with a pre-determined structure, the Right Way is Parse::RecDescent. (I also have the ulterior motive that I want to learn P::RD.)

As a first step I set out to write a grammar that parses outline files with a structure similar to the example above.

Here is what I have so far:

#!/usr/bin/perl use strict; use warnings; use Parse::RecDescent; use Data::Dumper; $::RD_ERRORS = 1; $::RD_WARN = 1; $::RD_HINT = 1; $::RD_TRACE = 1; my $grammar = <<'END_GRAMMAR'; #<autotree> startrule: slide(s) slide: <skip: qr/[ \t]*/> line(s) line_end(s) line: text fontspec(?) line_end text: /[^@\n]+(?:\b@[^@\n]*)*/ fontspec: <skip: ''> "@" fontspec_size fontspec_size: /\d+/ | <error: Invalid fontspec or unescaped '\@' at:\n$text.> line_end: "\n" END_GRAMMAR my $good_text = <<'SAMPLE_TEXT'; Test Line @20 Even more test address@test.com @20 Escaped \@text SAMPLE_TEXT my $bad_text = <<'SAMPLE_TEXT'; Bad line @invalid Also bad @foo @20 SAMPLE_TEXT my $parser = new Parse::RecDescent ($grammar); #$parser->startrule($text); $parser->startrule($good_text); print STDERR "\n" x 3; $parser->startrule($bad_text);

I've ran into problems with the font size specifiers. I want to allow "@" characters within the text if they are not at word-initial positions (for e-mail addresses and whatnot), but I want an error to be generated if there is an unescaped or non-word-initial "@", or the font size specifier is invalid (not an integer). The version above fails at "\@", even though I want to allow that, and it does not print the error text on failure, just fails silently, as seen from the P::RD trace.

How to rephrase the grammar to make it pass the lines in $good_text, but fail on the lines in $bad_text, printing the error message?

Eventually I want to collect the parsed lines into a data structure for further processing, so I'd appreciate tips on how to that, too.


In reply to Parse::RecDescent: problem with grammar and error reporting by kikuchiyo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.