in reply to converting context free grammar to BNF

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

Replies are listed 'Best First'.
Re^2: converting context free grammar to BNF
by nido203 (Novice) on May 29, 2016 at 14:56 UTC

    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