in reply to Re: converting context free grammar to BNF
in thread converting context free grammar to BNF

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!

Replies are listed 'Best First'.
Re^3: converting context free grammar to BNF
by haukex (Archbishop) on May 29, 2016 at 15:28 UTC

    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