NAME

MarpaX::Simple::Rules - Simple definition language for rules

WARNING

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.

SYNOPSYS

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";

DESCRIPTION

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.

FUNCTION

parse_rules(GRAMMAR-STRING)

Parses the argument and returns a values that can be used as the rules argument in Marpa::XS::Grammar constructor.

SYNTAX

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+.

The RHS can be specified in four ways: multiple names, a name with a plus +, a name with a star *, or Null.

TRANSFORMATION

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.

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',
                                       }

TOKENS

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.

SEE ALSO

Marpa::XS, MarpaX::Simple::Lexer

HOMEPAGE

http://github.com/pstuifzand/MarpaX-Simple-Rules

LICENSE

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

AUTHOR

Peter Stuifzand <peter@stuifzand.eu>

COPYRIGHT

Copyright (c) 2012-2014 Peter Stuifzand. All rights reserved.