我一直在掌握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 1\x{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 wheezy)
Marpa版本2.068000
(我看到有一个全新的Marpa 2.069我尚未尝试过)
这是我在语法上做错了吗?
答案 0 :(得分:6)
在Marpa Scanless中,你的语法有两个层次:主要的高级语法,你可以归因于动作等等,以及低级别的lexing语法。它们是独立执行的(如果你使用传统的解析器/词法分析器,这是预期的,但是当你从正则表达式转到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'