MarpaX::Simple::Rules - Simple definition language for rules
MarpaX::Simple::Rules depends on a deprecated module called Marpa::XS. That module will be (or is already) removed from CPAN.
MarpaX::Simple::Rules served as an inspiration to a new interface called Marpa::R2::Scanless (SLIF), which features similar syntax and more features. Where MarpaX::Simple::Rules only parsed BNF rules, SLIF will also tokenize your input. SLIF is the way forward for all new projects.
use Marpa::XS; use MarpaX::Simple::Rules 'parse_rules'; sub numbers { my (undef, @numbers) = @_; return \@numbers; } my $rules = parse_rules(<<"RULES"); parser ::= number+ => numbers RULES my $grammar = Marpa::XS::Grammar->new({ start => 'parser', rules => $rules, actions => __PACKAGE__, }); $grammar->precompute(); # Read tokens my $rec = Marpa::XS::Recognizer->new({grammar => $grammar }); $rec->read('number', 1); $rec->read('number', 2); # Get the return value my $val = ${$rec->value()}; print @{$val} . "\n";
MarpaX::Simple::Rules is a specification language that allows us to write the parameter for the rules argument of Marpa::XS grammar as a string.
Parses the argument and returns a values that can be used as the rules argument in Marpa::XS::Grammar constructor.
rules
A rule is a line that consists of two or three parts. These parts are called the left-hand side (LHS), the right-hand side (RHS) and the action. Every rule should contain a LHS and RHS. The action is optional.
The LHS and RHS are separated by the declare operator ::=. A LHS begins with a Name. A name is anything that matches the following regex: \w+.
::=
\w+
The RHS can be specified in four ways: multiple names, a name with a plus +, a name with a star *, or Null.
+
*
Null
This is a list of the patterns that can be specified. On the left of becomes we see the rule as used in the grammar string and on the right we see perl data structure that it becomes.
becomes
A ::= B becomes { lhs => 'A', rhs => [ qw/B/ ] } A ::= B C becomes { lhs => 'A', rhs => [ qw/B C/ ] } A ::= B+ becomes { lhs => 'A', rhs => [ qw/B/ ], min => 1 } A ::= B* becomes { lhs => 'A', rhs => [ qw/B/ ], min => 0 } A ::= B* => return_all becomes { lhs => 'A', rhs => [ qw/B/ ], min => 0, action => 'return_all', }
MarpaX::Simple::Rules doesn't help you getting from a stream to tokens. See MarpaX::Simple::Lexer for that or MarpaX::Simple::Rules, which contains a very simple lexer.
Marpa::XS, MarpaX::Simple::Lexer
http://github.com/pstuifzand/MarpaX-Simple-Rules
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Peter Stuifzand <peter@stuifzand.eu>
Copyright (c) 2012-2014 Peter Stuifzand. All rights reserved.
To install MarpaX::Simple::Rules, copy and paste the appropriate command in to your terminal.
cpanm
cpanm MarpaX::Simple::Rules
CPAN shell
perl -MCPAN -e shell install MarpaX::Simple::Rules
For more information on module installation, please visit the detailed CPAN module installation guide.