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

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);

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

    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

      Hi! thanks for the reply but now there is another problem. It works now and it exports to json but it looks like this:

      ["start",["seq","(",[["seqstr",["tagstr",["OZN","SBARQ "],["_alternati +on_1_of_production_1_of_rule_tagstr",["seq","(",[["seqstr",["tagstr", +["OZN","WHADVP "],["_alternation_1_of_production_1_of_rule_tagstr",[" +seq","(",[["seqstr",["tagstr",["OZN","WRB "],["_alternation_1_of_prod +uction_1_of_rule_tagstr",["rijec","Where"]]]]],")"]]]]],")"]]]],["seq +str",["seq","(",[["seqstr",["tagstr",["OZN","SQ "],["_alternation_1_o +f_production_1_of_rule_tagstr",["seq","(",[["seqstr",["tagstr",["OZN" +,"VBZ "],["_alternation_1_of_production_1_of_rule_tagstr",["rijec","i +s"]]]]],")"]]]],["seqstr",["seq","(",[["seqstr",["tagstr",["OZN","NP +"],["_alternation_1_of_production_1_of_rule_tagstr",["seq","(",[["seq +str",["tagstr",["OZN","NNP "],["_alternation_1_of_production_1_of_rul +e_tagstr",["rijec","Inoco"]]]]],")"]]]]],")"]],["seqstr",["seq","(",[ +["seqstr",["tagstr",["OZN","VP "],["_alternation_1_of_production_1_of +_rule_tagstr",["seq","(",[["seqstr",["tagstr",["OZN","VBN "],["_alter +nation_1_of_production_1_of_rule_tagstr",["rijec","based"]]]]],")"]]] +]],")"]]],")"]],["seqstr",["seq","(",[["seqstr",["tagstr",["OZN",". " +],["_alternation_1_of_production_1_of_rule_tagstr",["rijec","?"]]]]], +")"]]],")"]]

      It should actually look like this example bellow, I mean with curly brackets (not square) and colons instead of comma:

      {"firstName": "John", "lastName" : "Smith", "age" : 25, "address" : {"streetAdr ” : "21 2nd Street", "city" : "New York", "state" : "NY", ” zip" : "10021"}, "phoneNumber": [{"type" : "home", "number": "212 555 - 1234"}, {"type" : "fax", "number ” : "646 555 - 4567"}] }

      I tried using <autotree> directive and it won't even export to json (shows message:"encountered object 'start=HASH(0x89d0d08)', but neither allow_blessed nor convert_blessed settings are enabled at gramatika.txt line 31.") Is there another way to get desired shape? Thanks!

        Hi nido203,

        The reason your result looks like that is because of the $::RD_AUTOACTION = q { [ @item ] }; directive, which creates the arrayrefs. Also, <autotree> does create objects (blessed references), which are not always easy to convert to JSON. Parse::RecDescent does provide a hash for each item, so you could put this at the top of your grammar: <autoaction: { \%item } > and you'll get hashrefs. However, those will still contain all of the raw parse results. If you want to customize it, you'll have to look into writing custom actions that return only the values you are interested in. There's of course the Parse::RecDescent docs (especially the section "Actions"), but also this: Some Parse::RecDescent Tutorials

        Hope this helps,
        -- Hauke D