jeffreykegler/Marpa--R2

name of nulling rule is added to value even if there is no input for it

rns opened this issue · 4 comments

rns commented

If [ name, value ] is changed to [ value ], empty array is added.

At MARPA_STEP_NULLING_SYMBOL valuator step, a constant (the rule's lhs or empty array) is pushed as shown by trace_values => 99 output of the script below.

use 5.010;
use strict;
use warnings;

use Data::Dumper;
$Data::Dumper::Indent = 1;
$Data::Dumper::Terse = 1;
$Data::Dumper::Deepcopy = 1;

use Carp::Always; # force stack trace

use Marpa::R2;

my $g = Marpa::R2::Scanless::G->new( { source => \(<<'END_OF_SOURCE'),

    :default ::= action => [ name, value ]
    lexeme default = action => [ name, value ] latm => 1

        s ::= req opt

        req ::= 'req'

        opt ::=
        opt ::= 'opt'

    :discard ~ whitespace
    whitespace ~ [\s]+

END_OF_SOURCE
} );

my $input = <<EOI;
req
EOI

say Dumper $g->parse( 
    \$input, 
    { trace_terminals => 99, 
      trace_values => 99 
    } 
);
Setting trace_terminals option
Setting trace_values option
Expecting "[Lex-0]" at earleme 0
Lexer "L0" registering character U+0072 'r' as symbol 9: [r]
Lexer "L0" registering character U+0065 'e' as symbol 5: [e]
Lexer "L0" registering character U+0071 'q' as symbol 8: [q]
Attempting to read lexeme L1c1-3 e1: 'req'; value="req"
Lexer "L0" accepted lexeme L1c1-3 e1: 'req'; value="req"
Lexer "L0" registering character U+000a as symbol 4: [\s]
Lexer "L0" discarded lexeme L1c4: whitespace

# excessive output omitted ...
value event: starting op MARPA_STEP_NULLING_SYMBOL push_constant
value event: starting op MARPA_STEP_NULLING_SYMBOL result_is_array
Stack position   1:
 \[
    'opt'
  ]
Stack position   0:
 \[
    'req',
    'req'
  ]
# excessive output omitted ...

\[
    's',
    [
      'req',
      'req'
    ],
    [
      'opt'
    ]
  ]

Unless I am missing something, this is Marpa behaving as documented and expected. The [ 'opt' ] is there because an <opt> symbol was found, one which is nulled. Was your expected output different?

rns commented

Actually, yes -- "nulled symbol is always the LHS of a rule instance", so a
feature, not a bug.

The thing was, I just saw in the AST what's not in the input and that was
unexpected, in a sense, and I found no way to define its semantics to 'just
prune it away', because even action => ::undef puts undef in the value
array and that requires pruning ast manually -- not a big deal, but an
extra step.

So, I caught myself missing smth. like

action => prune

or even

{ nulled_symbols => prune } or
:default ::= action => [ name, value ] nulled_symbols => prune
lexeme default = action => [ name, value ] nulled_symbols => prune

On Thu, Feb 5, 2015 at 11:45 AM, Jeffrey Kegler notifications@github.com
wrote:

Unless I am missing something, this is Marpa behaving as documented and
expected. The [ 'opt' ] is there because an symbol was found, one
which is nulled. Was your expected output different?


Reply to this email directly or view it on GitHub
#243 (comment)
.

Your "pruned" version is actually the semantics of a different grammar: s ::= req opt | req.

rns commented

Good point. This one can be closed, I think.