本文介绍了Parse :: RecDescent语法无法按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我设法上班的只有STRING,PARAMS,VARIABLE和FUNCNAME功能似乎有问题,但我看不到.
All I managed to get working is STRING, PARAMS, VARIABLE and FUNCNAMEThere 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
推荐答案
这对我有用(特别是查看[A-Za-z0-9]
):
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_
这篇关于Parse :: RecDescent语法无法按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!