我已经掌握了Marpa解析器,当第一个符号是可选的时遇到了一个问题。这里有一个例子:
use strict;
use warnings;
use 5.10.0;
use Marpa::R2;
use Data::Dump;
my $grammar = Marpa::R2::Scanless::G->new({source => <<'END_OF_GRAMMAR'});
:start ::= Rule
Rule ::= <optional a> 'X'
<optional a> ~ a *
a ~ 'a'
END_OF_GRAMMAR
my $recce = Marpa::R2::Scanless::R->new({grammar => $grammar});
dd $recce->read("X");
当我运行这个时,我得到以下错误:
Error in SLIF parse: No lexemes accepted at line 1, column 1
* String before error:
* The error was at line 1, column 1, and at character 0x0058 'X', ...
* here: X
Marpa::R2 exception at small.pl line 20
at /usr/local/lib/perl/5.14.2/Marpa/R2.pm line 126
Marpa::R2::exception('Error in SLIF parse: No lexemes accepted at line 1, column 1x{a}...') called at /usr/local/lib/perl/5.14.2/Marpa/R2/Scanless.pm line 1545
Marpa::R2::Scanless::R::read_problem('Marpa::R2::Scanless::R=ARRAY(0x95cbfd0)', 'no lexemes accepted') called at /usr/local/lib/perl/5.14.2/Marpa/R2/Scanless.pm line 1345
Marpa::R2::Scanless::R::resume('Marpa::R2::Scanless::R=ARRAY(0x95cbfd0)', 0, -1) called at /usr/local/lib/perl/5.14.2/Marpa/R2/Scanless.pm line 926
Marpa::R2::Scanless::R::read('Marpa::R2::Scanless::R=ARRAY(0x95cbfd0)', 'SCALAR(0x95aeb1c)') called at small.pl line 20
Perl版本5.14.2(debian喘息)
Marpa版本2.068000
(我看到有一款全新的Marpa 2.069,我还没有试过)
这是我语法错误的地方吗?
在Marpa Scanless中,您的语法有两个级别:主要的高级语法,您可以在其中属性动作等,以及低级词汇语法。它们是独立执行的(如果您使用了传统的解析器/lexer,这是意料之中的事,但当您从regex到Marpa时,这会非常令人困惑)。
现在在低级语法中,Marpa将您的输入识别为单个X
,而不是"零个a
s,然后是一个X
"。但是,高级语法要求存在optional a
符号。
最好的方法是在高级语法中使a
可选:
<optional a> ::= <many a>
<optional a> ::= # empty
<many a> ~ a* # would work the same here with "a+"
a ~ 'a'