Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re^2: Simple Parser Combinator Implementation

by withering (Monk)
on Sep 08, 2013 at 03:23 UTC ( #1052863=note: print w/replies, xml ) Need Help??


in reply to Re: Simple Parser Combinator Implementation
in thread Simple Parser Combinator Implementation

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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1052863]
help
Chatterbox?
[Discipulus]: php does not suck anymore?
[ambrus]: Discipulus: I'm not sure, but it certainly doesn't suck as much as it's used to. it's like C++, it sucks because people still recursively learn from twenty year old PHP examples,
[ambrus]: and they try to use the obsolete features that PHP has to support only for compatibility with old scripts. C++ and PHP both have the problem that people can't forget the past, because when they google "PHP" plus the problme they want to solve, they find b
[ambrus]: ad code examples.
[ambrus]: I'm not trying to recommend PHP, but I think it has way too bad a name because of its past.
[ambrus]: This is different from MS Word, which was already a good editor in the pre-unicode days (in word for windows versions 2 and 6, which ran on windows 3 but also on windows 95), only it wasn't trying to solve the task of writing maths papers back then.
[Discipulus]: ah ok, sounds reasonable; with no fear: Perl all life long
[ambrus]: Mind you, LaTeX is currently still useful for writing math paper or snippet content without styling in such a way that the
[ambrus]: formatting conventions of a journal or website can be quickly applied to it, and MS Office and LibreOffice has not quite solved this (although it's better for this than it used to be),
[ambrus]: which is sort of a drawback compared to the ages of typewritten manuscripts representing content only to which the typesetter applies formatting, but that process required much more manual labor.

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (11)
As of 2017-09-26 11:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    During the recent solar eclipse, I:









    Results (293 votes). Check out past polls.

    Notices?