in reply to Parse:RecDescent grammar help
1.) Can you explain the following code? How does$lang->$method(@$item); know which sub-routine to call?
2.) From looking at the output of Data::Dumper for the array and hash subroutines, it looks like this is stored in arrays or nested arrays. Im having difficulty understanding how to extract this data. It's nice that Data::dumper is smart enough to print the structure but how it does it Im not sure.
3.) Can this grammar be modified to allow the following entries?
$a = "hello"
$b = "world"
@array = $a, $b
%hash = key=>@array
etc...
Once again, thank you for your help!
-Phillip
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Parse:RecDescent grammar help
by tachyon (Chancellor) on Oct 26, 2004 at 06:42 UTC | |
1) The first element of @item (ie $item[0]) is the PRD rule name. You will notice that in a lot of cases I am grabbing item 1+ and ignoring item 0 because we want the matched data, not the name of the rule. In the three 'method' rules we select 0,2,4/5 from @item which gives us the rule_name, var_name, assign_data. The rule name is the same as the method name. Get it? The rule we match also tells us what function to call to deal with the data. 2) The data is stored as array_refs or array_refs of array refs. See perlreftut. I have given you an example of how to access a typical value. The parse tree is an array ref, that hold more array refs, which probably hold yet more array refs. The first level of array refs if what we iterate over. We assign that to $item and this is the result of one successful rule parse. @$ syntax gets us an array from our array ref. $ref->[0] gets us item 0, rather than the whole list. 3) You can modify the grammar to your hearts content. The more complexity you add the more problems you are going to find. You have what looks a hell of a lot like Perl5 syntax and Perl is a bitch to parse. Why not just use a real language and let its parser generate a parse tree for you? I am not sure you have considered just how complex a project what you propose is. cheers tachyon | [reply] [d/l] |
by pip9ball (Acolyte) on Oct 26, 2004 at 13:34 UTC | |
Your right about not considering how complex this project is...the generic languge can be anything, not necessarily what I proposed. Im not quite sure what you mean when you say why not use a real language? Do you have any examples? I was trying to come up with a generic language that allows the basic data structures (element, array, hash) that will eventually get parsed and translated to a tool specific language...this way variables only need to be specified in one place and converted if needed. If you have any suggestions, please don't hesitate to share them. Once again, I really appreciate all of your knowledge and help!! regards, Phillip | [reply] |
|
Re^2: Parse:RecDescent grammar help
by thekestrel (Friar) on Oct 26, 2004 at 10:28 UTC | |
Hi Phillip, Firstly, as Tachyon says these rules can get really funky really quickly, especialy when you want to try and model nested things of conditional instructions (as I'm finding out for with my tinkering).
"fluffy" # encased text I call an 'identifier' $cuddles # this is a 'variable' @animals # this is an 'array' %stuff # this is a hash Now if you follow the rules you'll see that you can pretty much have any cobination of these...so you can do seksi things like this.. (put this in the text section from before as an example) %stuff = { animal => @pets, age => 5, name => "fluffy", colour => $col };
Replace all the bits in my 'rules' section from before with the following and that should spice things up...
....and the output for the example I gave you...
Have phun... Regards Paul | [reply] [d/l] [select] |
by pip9ball (Acolyte) on Oct 26, 2004 at 13:39 UTC | |
Thank-you for the reply...I will try these changes out after Jury Duty :-( From the examples yourself and tachyon provided, I realize that the output of what's parsed can become very complex and get nasty real quick. Perhaps I'll need to set some limitations on how many nested statments are allowed...otherwise retreiving this data is going to be a nightmare. Thanks again! -Phillip | [reply] |
by thekestrel (Friar) on Oct 27, 2004 at 03:49 UTC | |
Phillip,
Regards Paul | [reply] |
by pip9ball (Acolyte) on Oct 27, 2004 at 02:11 UTC | |
Thanks, -Phillip | [reply] [d/l] |
by thekestrel (Friar) on Oct 27, 2004 at 03:41 UTC | |
Phillip,
your example @dogs = [ "dollar", "mack" ]; My method doesn't support the brackets yet. Its hard when you're making it from scratch and it looks like perl not to auto-assume that it would auto perform therse things but you would have to tell it how to do them... I removed the brackets and ran with the following... @dogs = "dollar", "mack"; and it now runs but gives me gumby output see below:
ok so mistake on my part =P replace the 'array' rule with this.. array : arrayname EQUAL arraelement(s?) { [ @item[1,3] ] } now we add both lines back in...
A few things to note .... =) - The reference to '@dogs' in the myHash is just stored as text that is told to be of 'array' in the hash. It would be your job to search the tree so see if you had a valid array called 'dogs' in the tree to get data from, it doesn't auto put the dogs array inside the hash or make any kewl little pointers or anything. - I mentioned in my last note that you could pretty much use any combination or literal, itentifier, array or hash, but you can't actually have hashes embedded i.e. hashes of hashes. This is an easy change...just see what I did to the 'array' and 'variable' types. - You can easily make is so that you array rule did accomodate brackets too....think along the line of adding something like..
Regards Paul =) p.s. I'm using v5.8.4 | [reply] [d/l] [select] |