Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Re: Simple Parser Combinator Implementation

by roboticus (Chancellor)
on Sep 07, 2013 at 22:10 UTC ( #1052846=note: print w/replies, xml ) Need Help??

in reply to Simple Parser Combinator Implementation


That looks like it might be very interesting--I like playing with parsing, so I typically read all nodes that talk about it. Unfortunately, I'm not conversant with functional programming, nor familiar with the Hutton paper. So I can't really dig into it unless I'm willing to do a bit of digging.

If you want people to take a closer look at it you might need to add a couple of links to your post to the relevent background information. A sentence or two describing why and how we would use such a thing would be even better.

Having said that, I find the first block of code to be mostly clear and easy to read. If I were going to try to work on it, it doesn't look like it would be too difficult to maintain. There are a few odd variable names, but the way you're using other variable names, I'm guessing that they're relatively obvious contractions to someone familiar with the problem domain.

The only criticisms I can offer at this time are:

  • In some functions the variable names are far too short to be meaningful. Giving more meaningful names to the variables here and there would make the code even easier to read.
  • I may be missing something, but it appears that you're trying to bless some code references, but not providing the class to bless the code reference into. I don't do much object-oriented coding in perl, so it could easily be that I'm simply missing something.

That's the best I can do. Without enough background, I'm afraid the second code block is rather difficult for me to read. I can decipher bits of the syntax, but I can't see why or how you're tying it all together.

I hope you find this somewhat helpful.


When your only tool is a hammer, all problems look like your thumb.

  • Comment on Re: Simple Parser Combinator Implementation

Replies are listed 'Best First'.
Re^2: Simple Parser Combinator Implementation
by withering (Monk) on Sep 08, 2013 at 03:23 UTC

    Thanks a lot for your criticisms!

    Though the paper can be found through Google Scholar, I will add some simple introduction for the combinators later -- try to make them short :D

    The odd or short variable names, as you said, are just mathematic-style names, whose meaning is clear to people familiar with the parser combinator theory. I will rename them in the later version of my local clone.

    As for the blessings, please refer to perldoc bless. There is the second form 'bless REF', where CLASSNAME is omitted and the current package is used.

    The former test case I used is a expression calculator:

    my ($expn, $term, $factor, $num); wraith_rule->makerules(\$expn, \$term, \$factor, \$num); $expn = ( (\$term >> $wraith::token->('\+') >> \$expn) ** sub { [ $_[0 +]->[0] + $_[0]->[2] ] } ) | ( (\$term >> $wraith::token->('-') >> \$expn) ** sub { [ $_[0] +->[0] - $_[0]->[2] ] } ) | ( \$term ); $term = ( (\$factor >> $wraith::token->('\*') >> \$term) ** sub { [ $_ +[0]->[0] * $_[0]->[2] ] } ) | ( (\$factor >> $wraith::token->('\/') >> \$term) ** sub { $_[0]->[2] ? [ $_[0]->[0] / $_[0]->[2] ] : [] } ) | ( \$factor ); $factor = ( (\$num) ** sub { my $args = $_[0]; my $val = undef; for my + $elt (@$args) { $val .= $elt; } [ $val ] } ) | ( ( $wraith::token->('\(') >> \$expn >> $wraith::token->('\) +') ) ** sub { my $args = $_[0]; [ $args->[1] ] } ); $num = $wraith::token->('[1-9][0-9]*'); print $expn->('2 + (4 - 1) * 3 + 4 -2')->[0]->[0]->[0], "\n";

    The corresponding BNFs are

    E -> T + E | T - E | T,

    T -> F * T | F / T | F,

    F -> num | ( E ),

    where num is a terminal symbol (a natural number).

    The overloaded operator >> means sequence, i.e, A >> B means the concatenation of A and B. Operator | has the same meaning with BNF operator | (alternative). Perl operator ** is overloaded for semantic action, e.g, ALPHA ** sub { ... } means that when product ALPHA is correctly matched, the second operand of ** is executed, with its only argument being a reference to a list of return values of each term (terminal or nonterminal symbol) in product ALPHA. Just like what we use in YAPP except there is only one argument to the semantic action sub.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1052846]
[Corion]: I haven't found a good way to include/use the source SVGs I use for creating the PNGs directly as animations
[ambrus]: Presentations come in many different shapes, and so slides do as well.
[Corion]: Doing that in Powerpoint or Ooxml would be nice(r) but I'm much quicker doing the outline of a presentation and the code as Pod
[Corion]: Hurrr - on a machine that is behaving weirdly, I have two processes CROND running. I guess that is the source of unattended jobs sometimes not finding their files anymore...
[ambrus]: Most of the time if I make slides, they're just a formatted document with a medium level of formality (between a well written article and an informal draft), with usually the page breaks chosen carefully and possibly some content repeated between pages.
[Corion]: ambrus: My slides are mostly a list of things I want to talk about. But sometimes I want to point out interesting parts, or build up a larger image from small components. This is where animations/ highlights would come in well
[choroba]: what I like about slides in HTML + CSS is I can use templates to produce them, which means all code in slides is tested, and I can version them in git
[ambrus]: Btw, a request to everyone who organizes international conferences where presenters will show slides.
[ambrus]: In the document where you tell people how long the presentations are supposed to be, please also tell the aspect ratio of your projectors, because that's not obvious in advance.
[Corion]: ambrus: A more important point is that when constructing slides, you should respect a margin around your slide content of maybe 10% where no important information should be ;)

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (12)
As of 2017-09-26 10:08 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (293 votes). Check out past polls.