nido203 has asked for the wisdom of the Perl Monks concerning the following question:

Hello! I have a problem with converting this grammar to BNF version in Perl. I am using Parse RecDescent

$grammar = q{ start: seq seq: '(' seqstr(s) ')' seqstr: seq | tagstr tagstr: OZN ( seq | rijec ) OZN: /[A-Z.,?'*:`*]+ / rijec: /[\w-?,:<*`*]+/ };

i tried this but it is not working.

use Grammar::BNF; my $g = Grammar::BNF.generate(Q:to<END> <start> => <seq> <seq> => '(' <seqstr(s)> ')' <seqstr> => <seq> | <tagstr> <tagstr> => <OZN> '(' <seq> | <rijec> ')' <OZN> => /[A-Z.,?'*:`*]+ / <rijec> => /[\w-?,:<*`*]+/ END );

Replies are listed 'Best First'.
Re: converting context free grammar to BNF
by Corion (Patriarch) on May 28, 2016 at 21:08 UTC

    How is it failing for you? Do you get an error message?

    Note that you don't use Parse::RecDescent but seem to be using a module "Grammar::BNF", which doesn't exist on CPAN.

    Also, your syntax seems weird for Perl 5:

    my $g = Grammar::BNF.generate(Q:to<END>

    This seems to be closer to Perl 6 syntax, but with the little information you've given us, that's hard to tell.

    Please help us help you better by providing the version of Perl you're using and the exact error message(s) you get. Also, please provide us a small sample of code you're trying to parse using your BNF.

Re: converting context free grammar to BNF
by hexcoder (Curate) on May 28, 2016 at 21:34 UTC
    Hello nido203,

    like Corion said, it would have been helpful to mention where this module can be found (monks assume by default CPAN, which is not the case here) and that you are using Perl6.

    I found the module finally here on github.

    The grammar rules need to be specified in the notation of BNF. You used something else. So it can't work. I would try with some simple examples from the github or the wikipedia page first. Then understand and convert the Parse RecDescent grammar version.

    use Grammar::BNF; my $g = Grammar::BNF.generate(Q:to<END> <seq> ::= ... END );
    I hope this helps, hexcoder

      Hi! Sorry for so little information. I'm relatively new to this so I'm still discovering things. I'm using perl5. Bellow is the entire code. I'm trying to translate grammar to BNF

      use Parse::RecDescent; use Data::Dumper; $::RD_AUTOACTION = q { [@item] }; $grammar = q{ start: seq seq: '(' seqstr(s) ')' seqstr: seq | tagstr tagstr: OZN ( seq | rijec ) OZN: /[A-Z.,?'*:`*]+ / rijec: /[\w-?,:<*`*]+/ }; my $parser=Parse::RecDescent->new($grammar); my $result = $parser->start("(SBARQ (WHNP (WP Who))(SQ (VP (VBZ says)( +, ,)(S (SBAR (`` ``)(IN If)(S (NP (PRP you))(VP (VBP do)(RB n<t)(VP ( +VB look)(ADJP (JJ good)))))(, ,)(S (NP (PRP we))(VP (VBP do)(RB n<t)( +VP (VB look)(ADJP (JJ good)('' <<)))))))))(? ?))"); print Dumper($result);

      I found something similar to code bellow on github and tried to modify it

      use Grammar::BNF; use Parse::RecDescent; use Data::Dumper; $::RD_AUTOACTION = q { [@item] }; my $g = Grammar::BNF.generate(Q:to<END> <start> ::= <seq> <seq> ::= '(' <seqstr(s)> ')' <seqstr> ::= <seq> | <tagstr> <tagstr> ::= <OZN> '(' <seq> | <rijec> ')' <OZN> ::= /[A-Z.,?'*:`*]+ / <rijec> ::= /[\w-?,:<*`*]+/ END ); my $parser=Parse::RecDescent->new($g); my $result = $parser->start("(SBARQ (WHNP (WP Who))(SQ (VP (VBZ says)( +, ,)(S (SBAR (`` ``)(IN If)(S (NP (PRP you))(VP (VBP do)(RB n<t)(VP ( +VB look)(ADJP (JJ good)))))(, ,)(S (NP (PRP we))(VP (VBP do)(RB n<t)( +VP (VB look)(ADJP (JJ good)('' <<)))))))))(? ?))"); print Dumper($result);

      This is the message that shows: "Can't locate Grammar/BNF.pm in @INC (@INC contains: /etc/perl /usr/local/lib/perl/5.14.2 /usr/local/share/perl/5.14.2 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.14 /usr/share/perl/5.14 /usr/local/lib/site_perl .) at gramatika_bnf line 1. BEGIN failed--compilation aborted at gramatika_bnf line 1." It seems that i have been using wrong syntax but what shoud i'do to fix it? Should i have perl6? Thank you for your time!

        Hi nido203,

        I'm using perl5.

        Grammar::BNF is a module for Perl 6 (see modules.perl6.org) which won't work under Perl 5.

        A quick search of CPAN (= Perl 5 modules) doesn't seem to show me any modules which directly support BNF (Update: choroba showed how to use a form of BNF with Marpa::R2 here), modules for ABNF and EBNF appear to be available though. May I ask why you need to convert to BNF instead of just continuing to use Parse::RecDescent?

        Should i have perl6?

        Perl 6 is strongly related to, but not directly compatible with, Perl 5. So whether you want to switch to Perl 6 depends on several factors, like if you have the time to spend learning it, whether this script is intended for production use (as Perl 6 is not yet as widespread and stable as Perl 5), whether you already have a Perl 5 codebase, etc.

        Maybe if you could describe the bigger picture of what you're trying to do, that would allow us to give some better advice.

        Regards,
        -- Hauke D

Re: converting context free grammar to BNF
by choroba (Cardinal) on May 29, 2016 at 16:48 UTC
    I switched to Marpa::R2 instad of Parse::RecDescent. I had to change the grammar a bit to make it work:
    #!/usr/bin/perl use warnings; use strict; use Marpa::R2; use Data::Dumper; my $dsl = << '__DSL__'; :default ::= action => [values] lexeme default = latm => 1 seq ::= '(' seqstrs ')' seqstrs ::= seqstr seqstrs | seqstr seqstr ::= seq | tagstr tagstr ::= OZN seq | OZN rijec OZN ~ [A-Z.,?'*:`]+ rijec ~ [\w\-?,:<*`*]+ space ~ [\s]+ :discard ~ space __DSL__ my $grammar = 'Marpa::R2::Scanless::G'->new({ source => \$dsl }); my $input = "(SBARQ (WHNP (WP Who))(SQ (VP (VBZ says)(, ,)(S (SBAR (`` + ``)(IN If)(S (NP (PRP you))(VP (VBP do)(RB n<t)(VP (VB look)(ADJP (J +J good)))))(, ,)(S (NP (PRP we))(VP (VBP do)(RB n<t)(VP (VB look)(ADJ +P (JJ good)('' <<)))))))))(? ?))"; my $result = $grammar->parse(\$input); print Dumper($result);

    It seems you confused ( and '(' . Marpa doesn't know the (s) , you have to enumerate the alternatives.

    Output:

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

      Thank you so much guys, I learned some things that were confusing me and turns out that it is ok to just use Parse::RecDescent without BNF. This really helps. I have just one question. As I must use Parse::RecDescent and not Marpa, I need to parse Abstract syntax tree and copy data from dumper to array and then export it to JSON format. The code is this:

      use Parse::RecDescent; use Data::Dumper; $::RD_AUTOACTION = q { [ @item ] }; $grammar = q{ start: seq seq: '(' seqstr(s) ')' seqstr: seq | tagstr tagstr: OZN ( seq | rijec ) OZN: /[A-Z.,?'*:`*]+ / rijec: /[\w-?,:<*`*]+/ }; my $parser=Parse::RecDescent->new($grammar); my $result = $parser->start("(SBARQ (WHADVP (WRB Where))(SQ (VBZ is)(N +P (NNP Inoco))(VP (VBN based)))(. ?))"); print Dumper($result);

        Hi nido203,

        export it to JSON format

        With one of the JSON modules this is as simple as:

        use JSON::MaybeXS; print encode_json($result);

        Hope this helps,
        -- Hauke D