Here's a bit of a kludge to use alternate start rules in a single Parse::Yapp grammar. If someone knows a better way, please clue me in. This fully functional snippet also shows how to put all of the necessary P::Y ingredients into a single file for easy debugging.
#!perl -w use strict; my $grammar = <<''; %% greeting : hi | hello ; farewell : bye | goodbye ; %% # create two parsers with same grammar, different start rules my $g_parser = MyParser->new($grammar,'greeting'); my $f_parser = MyParser->new($grammar,'farewell'); die if $g_parser->parse('bye'); # 'bye' is not a greeting die if $f_parser->parse('hi'); # 'hi' is not a farewell print "ok!\n" if $g_parser->parse('hi'); # 'hi' is a greeting print "ok!\n" if $f_parser->parse('bye'); # 'bye' is a farewell package MyParser; use Parse::Yapp; sub new { my ($class,$grammar,$start,$debug)=@_; %Parser:: = () if defined %Parser::; if ($start){ $grammar =~ s/^%start.*$/%start $start/m or $grammar =~ s/^(%%\n)/%start $start\n$1/m; } my $py = new Parse::Yapp(input => $grammar); eval $py->Output; die "ERROR IN GRAMMAR\n$@\n" if $@; return bless {parser => new Parser, debug => $debug}, $class; } sub lexer { for(shift->YYData->{INPUT}) { s/^(\S+)// and return($1,$1) } } sub parse { my $self = shift; my $parser = $self->{parser}; $parser->YYData->{INPUT}=shift; my $rv = $parser->YYParse ( yylex => \&lexer , yyerror => sub{} # we just need a yes or no , yydebug => $self->{debug} # set this to 0x0F for a good time ); return undef if $parser->YYNberr; return $rv; } __END__

In reply to Alternate start rules in Parse::Yapp by jZed

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.