Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re^2: Simple Parser Combinator Implementation

by withering (Sexton)
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.


Comment on Re^2: Simple Parser Combinator Implementation
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1052863]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2014-09-21 17:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (173 votes), past polls