$grammar = q { statement: 'var' ';' { $return = $item[2] } expressions: var_name ('=' expression)(?) { $return = $item[1] } expression: conditional_operation | arithmetic_operation | equality | function_call | object_declaration | numeric_value | array_reference | array_value | object_reference | escapedRegex | escapedQuote | var_name operand: var_name | numeric_value arithmetic_operation: operand arithmetic_operator '(' arithmetic_operation ')' | operand arithmetic_operator arithmetic_operation | '(' operand arithmetic_operator operand ')' | operand arithmetic_operator operand | unary_negation_operator operand # -12 | incremental_operator operand # --j | operand incremental_operator # i++ conditional_operation: equality '?' expression ':' expression | '(' equality '?' expression ':' expression ')' comma_values: | array_reference: array_name '(' expression ')' | array_name '()' array_value: array_name '[' array_item ']' ('.' expression)(?) | '[' array_list ']' array_list: array_item: var_name | integer object_reference: object_declaration: 'new' object_name '()' | 'new' object_name '(' comma_values ')' | 'new' object_name object_name: 'Array' | 'Object' | 'Date' | /\w+/ function_call: function_name '()' | function_name '(' comma_values ')' function_name: /\w+/ condition: 'true' | '1' | 'false' | '0' | equality equality: '(' expression ')' | '(' expression equality_operator expression ')' var_name: /\w+/ array_name: /\w+/ numeric_value: real_number | integer integer: /\d+/ real_number: /\d+\.?\d*/ escapedRegex: '__REGEX__' escapedQuote: '__QUOTE__' # ARITHMETIC OPERATORS arithmetic_operator: '+' | '-' | '*' | '/' | '%' incremental_operator: '++' | '--' unary_negation_operator: '-' # OTHER OPERATORS string_operator: '+' | '+=' logical_operator: '&&' | '||' | '!' bitwise_operator: '&' | '^' | '|' | '~' | '<<' | '>>' | '>>>' equality_operator: '===' | '!==' | '==' | '!=' | '>' | '<' | '>=' | '<=' assignment_operator: '+' | '-' | '*' | '/' | '%' | '<<' | '>>' | '>>>' | '&' | '^' | '|' assignshort_operator: '+=' | '-=' | '*=' | '/=' | '%=' | '<<=' | '>>=' | '>>>=' | '&=' | '^=' | '|=' }; $parser = new Parse::RecDescent ($grammar) or die "*** Bad grammar!\n"; foreach my $localDeclaredVar (@localDeclaredVars) { my $refParsedValues = $parser->statement($localDeclaredVar) || print "*** $localDeclaredVar\n"; if (ref($refParsedValues) eq 'ARRAY') { foreach my $parsedValue (@$refParsedValues) { push (@localVariables, $parsedValue) if ($parsedValue); #print "==> [$parsedValue]\n"; } } else { push (@localVariables, $refParsedValues) if ($refParsedValues); #print "==> [$refParsedValues]\n"; } }