| 1 |
#!perl |
| 2 |
# Copyright 2022 Jeffrey Kegler |
| 3 |
# This file is part of Marpa::R2. Marpa::R2 is free software: you can |
| 4 |
# redistribute it and/or modify it under the terms of the GNU Lesser |
| 5 |
# General Public License as published by the Free Software Foundation, |
| 6 |
# either version 3 of the License, or (at your option) any later version. |
| 7 |
# |
| 8 |
# Marpa::R2 is distributed in the hope that it will be useful, |
| 9 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 |
# Lesser General Public License for more details. |
| 12 |
# |
| 13 |
# You should have received a copy of the GNU Lesser |
| 14 |
# General Public License along with Marpa::R2. If not, see |
| 15 |
# http://www.gnu.org/licenses/. |
| 16 |
|
| 17 |
# This test uses the thin interface. It tests the behavior of |
| 18 |
# ambiguous nulls. These occur if two nullable rules have the same |
| 19 |
# LHS. |
| 20 |
|
| 21 |
use 5.010001; |
| 22 |
use strict; |
| 23 |
use warnings; |
| 24 |
|
| 25 |
use Test::More tests => 3; |
| 26 |
|
| 27 |
use lib 'inc'; |
| 28 |
use Marpa::R2::Test; |
| 29 |
use English qw( -no_match_vars ); |
| 30 |
use Fatal qw( close open ); |
| 31 |
use Data::Dumper; |
| 32 |
use Marpa::R2; |
| 33 |
|
| 34 |
sub myDump { |
| 35 |
my $v = shift; |
| 36 |
return Data::Dumper->new( [$v] )->Indent(0)->Terse(1)->Dump; |
| 37 |
} |
| 38 |
|
| 39 |
my $grammar = Marpa::R2::Thin::G->new( { if => 1 } ); |
| 40 |
$grammar->force_valued(); |
| 41 |
my @symbol = (); |
| 42 |
my $symbol_S = $grammar->symbol_new(); |
| 43 |
$symbol[$symbol_S] = "S"; |
| 44 |
$grammar->start_symbol_set($symbol_S); |
| 45 |
my $symbol_amb = $grammar->symbol_new(); |
| 46 |
$symbol[$symbol_amb] = "AMB"; |
| 47 |
my $symbol_a = $grammar->symbol_new(); |
| 48 |
$symbol[$symbol_a] = "A"; |
| 49 |
my $symbol_b = $grammar->symbol_new(); |
| 50 |
$symbol[$symbol_b] = "B"; |
| 51 |
my $symbol_x = $grammar->symbol_new(); |
| 52 |
$symbol[$symbol_x] = "X"; |
| 53 |
my $symbol_y = $grammar->symbol_new(); |
| 54 |
$symbol[$symbol_y] = "Y"; |
| 55 |
|
| 56 |
my @rule = (); |
| 57 |
my $start_rule_id = |
| 58 |
$grammar->rule_new( $symbol_S, [ $symbol_x, $symbol_amb, $symbol_y ] ); |
| 59 |
$rule[$start_rule_id] = 'start'; |
| 60 |
my $amb1_rule_id = $grammar->rule_new( $symbol_amb, [ $symbol_a, $symbol_b ] ); |
| 61 |
$rule[$amb1_rule_id] = 'amb1'; |
| 62 |
my $amb2_rule_id = $grammar->rule_new( $symbol_amb, [ $symbol_b, $symbol_a ] ); |
| 63 |
$rule[$amb2_rule_id] = 'amb2'; |
| 64 |
my $a_rule_id = $grammar->rule_new( $symbol_a, [] ); |
| 65 |
$rule[$a_rule_id] = 'a'; |
| 66 |
my $b_rule_id = $grammar->rule_new( $symbol_b, [] ); |
| 67 |
$rule[$b_rule_id] = 'b'; |
| 68 |
|
| 69 |
$grammar->precompute(); |
| 70 |
|
| 71 |
my $recce = Marpa::R2::Thin::R->new($grammar); |
| 72 |
$recce->start_input(); |
| 73 |
|
| 74 |
# The numbers from 1 to 3 are themselves -- |
| 75 |
# that is, they index their own token value. |
| 76 |
# Important: zero cannot be itself! |
| 77 |
|
| 78 |
my @token_values = ( 0 .. 3 ); |
| 79 |
my $x_token_value = -1 + push @token_values, "x"; |
| 80 |
my $y_token_value = -1 + push @token_values, "y"; |
| 81 |
|
| 82 |
$recce->alternative( $symbol_x, $x_token_value, 1 ); |
| 83 |
$recce->earleme_complete(); |
| 84 |
$recce->alternative( $symbol_y, $x_token_value, 1 ); |
| 85 |
$recce->earleme_complete(); |
| 86 |
|
| 87 |
my %expected_value = ( q{\['start:',['X:','x'],['AMB:'],['Y:','x']]} => 1, ); |
| 88 |
|
| 89 |
my $actual_values = evalIt($recce); |
| 90 |
my $i = 0; |
| 91 |
for my $actual_value (@$actual_values) { |
| 92 |
my $dumped_value = myDump( \$actual_value ); |
| 93 |
if ( defined $expected_value{$dumped_value} ) { |
| 94 |
delete $expected_value{$dumped_value}; |
| 95 |
Test::More::pass("Expected Value $i: $dumped_value"); |
| 96 |
} |
| 97 |
else { |
| 98 |
Test::More::fail("Unexpected Value $i: $dumped_value"); |
| 99 |
} |
| 100 |
$i++; |
| 101 |
} |
| 102 |
my @not_found = keys %expected_value; |
| 103 |
my $not_found_count = scalar @not_found; |
| 104 |
if ($not_found_count) { |
| 105 |
Test::More::fail("$not_found_count expected value(s) not found"); |
| 106 |
for my $value (@not_found) { |
| 107 |
Test::More::diag("$value"); |
| 108 |
} |
| 109 |
} |
| 110 |
else { |
| 111 |
Test::More::ok("All expected values found"); |
| 112 |
} |
| 113 |
|
| 114 |
my @rule_data = ( |
| 115 |
[ 'AMB1', $amb1_rule_id ], |
| 116 |
[ 'AMB2', $amb2_rule_id ], |
| 117 |
[ 'A', $a_rule_id ], |
| 118 |
[ 'B', $b_rule_id ], |
| 119 |
); |
| 120 |
|
| 121 |
sub evalIt { |
| 122 |
my ($recce) = @_; |
| 123 |
my $latest_earley_set_ID = $recce->latest_earley_set(); |
| 124 |
my $bocage = Marpa::R2::Thin::B->new( $recce, $latest_earley_set_ID ); |
| 125 |
my $metric = $bocage->ambiguity_metric(); |
| 126 |
Test::More::is( $metric, 1, "Ambiguity metric" ); |
| 127 |
my $order = Marpa::R2::Thin::O->new($bocage); |
| 128 |
my $tree = Marpa::R2::Thin::T->new($order); |
| 129 |
my @actual_values = (); |
| 130 |
|
| 131 |
while ( $tree->next() ) { |
| 132 |
my $valuator = Marpa::R2::Thin::V->new($tree); |
| 133 |
my @stack = (); |
| 134 |
STEP: while (1) { |
| 135 |
my ( $type, @step_data ) = $valuator->step(); |
| 136 |
last STEP if not defined $type; |
| 137 |
if ( $type eq 'MARPA_STEP_TOKEN' ) { |
| 138 |
|
| 139 |
# say STDERR "TOKEN: ", join " ", @step_data; |
| 140 |
my ( $sym_id, $token_value_ix, $arg_n ) = @step_data; |
| 141 |
my $tag = $symbol[$sym_id] . ':'; |
| 142 |
$stack[$arg_n] = [ $tag, $token_values[$token_value_ix] ]; |
| 143 |
|
| 144 |
# say STDERR "Stack: ", join " ", myDump( \@stack ); |
| 145 |
next STEP; |
| 146 |
} |
| 147 |
if ( $type eq 'MARPA_STEP_NULLING_SYMBOL' ) { |
| 148 |
|
| 149 |
# say STDERR "NULLING: ", join " ", @step_data; |
| 150 |
my ( $sym_id, $arg_n ) = @step_data; |
| 151 |
$stack[$arg_n] = [ $symbol[$sym_id] . ':' ]; |
| 152 |
|
| 153 |
# say STDERR "Stack: ", join " ", myDump( \@stack ); |
| 154 |
next STEP; |
| 155 |
} |
| 156 |
if ( $type eq 'MARPA_STEP_RULE' ) { |
| 157 |
|
| 158 |
# say STDERR "RULE: ", join " ", @step_data; |
| 159 |
my ( $rule_id, $arg_0, $arg_n ) = @step_data; |
| 160 |
my $rule_name = $rule[$rule_id]; |
| 161 |
die "Unknown rule $rule_id" unless defined $rule_name; |
| 162 |
my $elements = [ $rule_name . ':' ]; |
| 163 |
for my $i ( $arg_0 ... $arg_n ) { |
| 164 |
push @$elements, $stack[$i]; |
| 165 |
} |
| 166 |
$stack[$arg_0] = $elements; |
| 167 |
|
| 168 |
# say STDERR "Stack: ", join " ", myDump( \@stack ); |
| 169 |
next STEP; |
| 170 |
} ## end if ( $type eq 'MARPA_STEP_RULE' ) |
| 171 |
die "Unexpected step type: $type"; |
| 172 |
} ## end STEP: while (1) |
| 173 |
push @actual_values, $stack[0]; |
| 174 |
} ## end while ( $tree->next() ) |
| 175 |
return \@actual_values; |
| 176 |
} |
| 177 |
|
| 178 |
# vim: expandtab shiftwidth=4: |