http://www.perlmonks.org?node_id=1061876

three18ti has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks

We have a sudoers file that is a mess. I'm attempting to parse it out, and after a few attempts to basically roll my own parser, I found Marpa. I've read through the tutorial, and I thought I understood, so I build my grammar spec based on the Sudoers Manual... I was getting errors so I am attempting to break down my grammar into little pieces and build the grammar back up to the full spec.

I'm definitely missing something because I don't see anything wrong, but I'm getting an error...

Here's my small sample code:

#!/usr/bin/env perl use strict; use warnings; use warnings; use Marpa::R2; my $grammar_spec = get_grammar(); my $test_input = test_input(); my $grammar = Marpa::R2::Scanless::G->new({ source => \$grammar_spec, +}); my $recce = Marpa::R2::Scanless::R->new({ grammar => $grammar }); $recce->read(\$test_input); my $val = $recce->value; sub get_grammar { return <<'END_GRAMMAR'; :start ::= Alias :discard ~ ws Alias ::= 'User_Alias' User_Alias (':' User_Alias)* User_Alias ::= NAME '=' User_List User_List ::= User | User ',' User_List User ::= <user name> NAME ~ [A-Z]([A-Z][0-9]_)* ws ~ [\s]+ <user name> ~ [a-z]([a-z][0-9]_)* END_GRAMMAR } sub test_input { return <<'END_INPUT'; User_Alias FOO = abc, def END_INPUT }

The error I get is:

Error in SLIF parse: No lexemes accepted at line 4, column 70 * String before error: ::= 'User_Alias' User_Alias (':' User_Ali +as) * The error was at line 4, column 70, and at character 0x002a '*', ... * here: *\n User_Alias ::= NAME '=' User_List\n Marpa::R2 exception at test.pl line 10.

This seems to indicate the error is in the line:

Alias ::= 'User_Alias' User_Alias (':' User_Alias)*

rewriting the Alias definition:

Alias ::= 'User_Alias' User_Alias

Returns a new error:

Error in SLIF parse: No lexemes accepted at line 10, column 45 * String before error: ame>\n NAME ~ [A-Z]([A-Z][0-9 +]_) * The error was at line 10, column 45, and at character 0x002a '*', .. +. * here: *\n ws ~ [\\s]+\n <user nam Marpa::R2 exception at test.pl line 10.

So I'm thinking the problem lies in the second iteration of "User_Alias" i.e.: (':' User_Alias)* ... I'm just not sure what the problem is.

Also, the second error seems to indicate an issue with my NAME definition. column 40 is the dash between the 0 and 9 in "(A-Z0-9_)*"

I appreciate any thoughts or assistance.

Thanks!

Replies are listed 'Best First'.
Re: [Marpa::R2] Help with EBNF Grammar Formatting (SLIF is more BNF than EBNF)
by Anonymous Monk on Nov 10, 2013 at 01:49 UTC

    I was under the impression that its more BNF than EBNF ... whatever that means :)

    meaning you don't use * to specify OPTIONAL rule, or zero or more times ... or whatever you mean by it :) see http://blogs.perl.org/users/jeffrey_kegler/2013/01/making-dsls-even-simpler.html#comment-311460 and http://stackoverflow.com/questions/18634749/marpa-parser-cant-seem-to-cope-with-optional-first-symbol

    With that in mind I come up with something which doesn't croak on the SLIF; I've no idea what it does :)

    sub get_grammar { return q{ :start ::= Alias :discard ~ ws Alias ::= 'User_Alias' User_Alias repeatUser_Alias | 'User_Alias' User_Alias repeatUser_Alias ::= manyColonUser_Alias ### repeated OPTIONALLY repeatUser_Alias ::= ### repeated many times manyColonUser_Alias ::= colonUser_Alias* colonUser_Alias ::= ':' User_Alias User_Alias ::= NAME '=' User_List User_List ::= User | User ',' User_List User ::= <user name> ws ~ [\s]+ NAME ~ <name_firstchar><name_restchars> name_firstchar ~ [A-Z] name_restchars ~ [A-Z0-9_]* <user name> ~ <username_firstchar><username_restchars> username_firstchar ~ [a-z] username_restchars ~ [a-z0-9_]* };

    I'm eager to learn what you learn about this :)

        Awesome! Thanks for the links. Looks like I have a bit more reading to do :)

        I certainly know about feeling overwhelmed with Marpa.

        I swear I read somewhere that Marpa supported EBNF but looking at the docs, maybe that isn't true...

        I've got a bit more digesting to do, but I'll post back my results.