in reply to Rec::Descent Woes - Parsing JavaScript Files and Other Issues
Just looking at your grammar, it seems to me that statement and function_method are infinitely recursive with respect to brace_statement, paren_statement and bracket_statement (which themselves are defined in terms of statement).
Since you are not trying to actually parse the contents of functions, but simply return the contents wholesale (in order to associate it with the function name in a hash), why not make it simple for yourself?
For each function, there are two things you need: the contents of the parentheses and those of the curly braces, either of which may be nonexistent (empty). In order to recognize the end of a particular set of parens (or braces), you need to be able to identify any embedded matching pairs (or parens, braces or square brackets), which is why you should identify symbols, quoted strings, etc. as well.
Okay, you have these basic building blocks. Now you need to combine them in ways in which they might actually appear within programs without overcomplicating your task.
For instance, I think in your grammar, the statement rule is over-used (incorrectly). I'm uncertain, but I don't think the parameter list of a function can consist of arbitrarily complex statements; likewise, for the contents of expressions within square brackets. Perhaps you should describe expression in a rule. Then, your definition of statement could be comprised of several different kinds of expressions (e.g., assignment).
It would be interesting to know where you are taking this; are you building a Javascript debugger in Perl?
dmm
If you GIVE a man a fish you feed him for a day
|
|---|