in reply to Regex for stripping variable names from a JavaScript file
I'm not sure I'd use a regex for this. Think about Parse::RecDescent or its ilk.
dmm
If you GIVE a man a fish you feed him for a day
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Regex for stripping variable names from a JavaScript file
by Incognito (Pilgrim) on Feb 25, 2002 at 20:51 UTC | |
Okay, so I have tried using Parse::RecDescent and built a small parser for var statements... I'm having only one problem...
How do we grab the matched var_name? My goal is to match each variable name that was matched... but I've read the FAQ as much as I could handle and cannot determine that small fact.... The Input
My Output
As you can see, all that I get is the darn semicolon - the string that was left after all matching was successful... this is of course not what I want... Does anyone know how to solve this? | [reply] [d/l] [select] |
by dmmiller2k (Chaplain) on Feb 26, 2002 at 14:49 UTC | |
There are two problems here. First, your grammar is not quite right. And secondly, you aren't setting the return in your starting rule. I'm not an expert with Parse::RecDescent, or with constructing grammars for YACC, Bison, etc.(far from it, actually); but IMHO, you probably don't want the { $return = $item[1] } on the 'var_name:' rule. Instead, I think you want it on the 'statement:' AND 'varStatement:' rules (see below). Also, removing the 'comma:' rule and replacing its use with literal commas, prevents getting commas in the output (apologies for not using the lingo correctly). Here's my attempt:
and here is the output:
This still misses array_names and variables within parenthesized expressions, but hey, it's a step in the right direction, I suppose. How would I be helping you if I solved your whole problem for you? :) At least you now have a debuggable chuknk of code. dmm If you GIVE a man a fish you feed him for a dayBut, TEACH him to fish and you feed him for a lifetime | [reply] [d/l] [select] |
by Incognito (Pilgrim) on Feb 26, 2002 at 19:08 UTC | |
Thank-you very much for leading me in the right direction... ++ to you. Just 2 days ago I didn't know anything about Parse:RecDescent... and now I wrote my first simple grammar to it... I don't know much about grammars, but I'm learning. That's why it's nice that someone can take a look at what I've done and give me pointers, etc. One thing I haven't figured out, but now have some working code to play with, are the $return value 'stuff' (I was just guessing/hacking at it). Also, what you've done here does exactly what I want, return just the variable names (which I will use somewhere else in my code)... I'm not sure what you mean by missing "array_names" and "variables within parenthesized expressions", so I think I'll have a look into that as well. I appreciate your time. What references do you use for this sort of thing? I've been reading the .pod files... | [reply] |
by dmmiller2k (Chaplain) on Feb 27, 2002 at 16:35 UTC | |