Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re^3: converting context free grammar to BNF

by haukex (Archbishop)
on May 31, 2016 at 18:42 UTC ( [id://1164632]=note: print w/replies, xml ) Need Help??


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

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

Replies are listed 'Best First'.
Re^4: converting context free grammar to BNF
by nido203 (Novice) on Jun 03, 2016 at 17:57 UTC

    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

        Hi Hauke D

        Thank you so much for clearing that up. I will do research about actions.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1164632]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-04-23 07:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found