3

All I managed to get working is STRING, PARAMS, VARIABLE and FUNCNAME There seems to be a problem with FUNCTION, but I just can't see it.

use strict;
use Parse::RecDescent;

$::RD_ERRORS = 1; # Make sure the parser dies when it encounters an error
$::RD_WARN   = 1; # Enable warnings. This will warn on unused rules &c.
$::RD_HINT   = 1; # Give out hints to help fix problems.

my $grammar = <<'_GRAMMAR_';
            SCRIPT : INSTRUCTION(s /;/)

            INSTRUCTION: FUNCTION | VARIABLE "=" FUNCTION

            FUNCTION : FUNCNAME "[" PARAMETERS "]"

            FUNCNAME : "print" | "add2" | "save" # ok

            PARAMETERS : STRING | STRING /\s*\,\s*/ PARAMETERS # ok

            VARIABLE : /\w[A-Za-z-0-9]{0,10}/i # ok

            STRING : /'(?:[^'\\]|\\.)*'/ # ok
    _GRAMMAR_
my $parser2 = Parse::RecDescent->new($grammar);
#i test every rule here
print "STRING OK\n" if $parser2->STRING("'asd'");
print "PARAMS OK\n" if $parser2->PARAMETERS("'asd','XCV','vfgdg'");
print "SCRIPT OK\n" if $parser2->SCRIPT(q(print['hola','asd'];save['hello'];));
print "VARIABLE OK \n" if $parser2->VARIABLE("a1");
print "FUNCTION OK\n" if $parser2->FUNCTION(q(print['hola','asd']));
print "FUNCNAME OK\n" if $parser2->FUNCNAME(q(print));
print "INSTRUCTION OK\n" if $parser2->FUNCTION(q(a0=print['hola','asd'];));

Any help? BTW my target is this:

variable=functioname['param1','param2','paramN'];
function2['param1'];
etc etc
cjm
  • 61,471
  • 9
  • 126
  • 175
AlfredoVR
  • 4,069
  • 3
  • 25
  • 33

1 Answers1

3

This works for me (particularly, look at [A-Za-z0-9]):

my $grammar = <<'_GRAMMAR_';
            SCRIPT : INSTRUCTION(s /;/) /\Z/

            INSTRUCTION: VARIABLE EQ FUNCTION | FUNCTION

            FUNCTION : FUNCNAME '[' PARAMETERS ']'

            FUNCNAME : "print" | "add2" | "save" 

            PARAMETERS : STRING(s /,/) 

            VARIABLE : /(\w[A-Za-z0-9]{0,10})/i 

            EQ: '=' 

            STRING : /'(?:[^'\\]|\\.)*'/ # ok
_GRAMMAR_
ikegami
  • 367,544
  • 15
  • 269
  • 518
perreal
  • 94,503
  • 21
  • 155
  • 181
  • i think it's working, can you please explain what i did wrong? – AlfredoVR Jul 22 '12 at 09:28
  • `[A-Za-z-0-9]` has an extra '-' between z and 0. also, I changed `/\s*,\s*/` in parameters. – perreal Jul 22 '12 at 09:38
  • Are the parentheses around the variable name matching pattern significant too? – Jonathan Leffler Jul 22 '12 at 15:45
  • No they are leftovers from debugging you can remove them. – perreal Jul 22 '12 at 15:46
  • Added `/\Z/` to forbid trailing junk – ikegami Jul 22 '12 at 18:14
  • 1
    Generally speaking, `FUNCNAME : "print" | "add2" | "save"` is bad because it matches the start of `printf` (for example). The proper way is to match an identifier, and check if that identifier is valid. In this particular grammar, it's not an issue. – ikegami Jul 22 '12 at 18:17
  • I don't understand what you mean with matching an identifier, could you please exemplify this? thanks. – AlfredoVR Jul 22 '12 at 18:43
  • @alfa64, he means, for example, having `FUNCNAME : VARIABLE`, and later checking if the variable is a valid function name such as print, add2.... – perreal Jul 22 '12 at 19:03