| 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 |
# Testing an ambiguous equation |
| 18 |
# using the thin interface |
| 19 |
|
| 20 |
use 5.010001; |
| 21 |
use strict; |
| 22 |
use warnings; |
| 23 |
|
| 24 |
use Test::More tests => 12; |
| 25 |
|
| 26 |
use lib 'inc'; |
| 27 |
use Marpa::R2::Test; |
| 28 |
use English qw( -no_match_vars ); |
| 29 |
use Fatal qw( close open ); |
| 30 |
use Marpa::R2; |
| 31 |
|
| 32 |
# Marpa::R2::Display |
| 33 |
# name: Thin example |
| 34 |
|
| 35 |
my $grammar = Marpa::R2::Thin::G->new( { if => 1 } ); |
| 36 |
$grammar->force_valued(); |
| 37 |
my $symbol_S = $grammar->symbol_new(); |
| 38 |
my $symbol_E = $grammar->symbol_new(); |
| 39 |
$grammar->start_symbol_set($symbol_S); |
| 40 |
my $symbol_op = $grammar->symbol_new(); |
| 41 |
my $symbol_number = $grammar->symbol_new(); |
| 42 |
my $start_rule_id = $grammar->rule_new( $symbol_S, [$symbol_E] ); |
| 43 |
my $op_rule_id = |
| 44 |
$grammar->rule_new( $symbol_E, [ $symbol_E, $symbol_op, $symbol_E ] ); |
| 45 |
my $number_rule_id = $grammar->rule_new( $symbol_E, [$symbol_number] ); |
| 46 |
$grammar->precompute(); |
| 47 |
|
| 48 |
my $recce = Marpa::R2::Thin::R->new($grammar); |
| 49 |
$recce->start_input(); |
| 50 |
|
| 51 |
# The numbers from 1 to 3 are themselves -- |
| 52 |
# that is, they index their own token value. |
| 53 |
# Important: zero cannot be itself! |
| 54 |
|
| 55 |
my @token_values = ( 0 .. 3 ); |
| 56 |
my $zero = -1 + push @token_values, 0; |
| 57 |
my $minus_token_value = -1 + push @token_values, q{-}; |
| 58 |
my $plus_token_value = -1 + push @token_values, q{+}; |
| 59 |
my $multiply_token_value = -1 + push @token_values, q{*}; |
| 60 |
|
| 61 |
$recce->alternative( $symbol_number, 2, 1 ); |
| 62 |
$recce->earleme_complete(); |
| 63 |
$recce->alternative( $symbol_op, $minus_token_value, 1 ); |
| 64 |
$recce->earleme_complete(); |
| 65 |
$recce->alternative( $symbol_number, $zero, 1 ); |
| 66 |
$recce->earleme_complete(); |
| 67 |
$recce->alternative( $symbol_op, $multiply_token_value, 1 ); |
| 68 |
$recce->earleme_complete(); |
| 69 |
$recce->alternative( $symbol_number, 3, 1 ); |
| 70 |
$recce->earleme_complete(); |
| 71 |
$recce->alternative( $symbol_op, $plus_token_value, 1 ); |
| 72 |
$recce->earleme_complete(); |
| 73 |
$recce->alternative( $symbol_number, 1, 1 ); |
| 74 |
$recce->earleme_complete(); |
| 75 |
|
| 76 |
my $latest_earley_set_ID = $recce->latest_earley_set(); |
| 77 |
my $bocage = Marpa::R2::Thin::B->new( $recce, $latest_earley_set_ID ); |
| 78 |
my $order = Marpa::R2::Thin::O->new($bocage); |
| 79 |
my $tree = Marpa::R2::Thin::T->new($order); |
| 80 |
my @actual_values = (); |
| 81 |
while ( $tree->next() ) { |
| 82 |
my $valuator = Marpa::R2::Thin::V->new($tree); |
| 83 |
my @stack = (); |
| 84 |
STEP: while ( 1 ) { |
| 85 |
my ( $type, @step_data ) = $valuator->step(); |
| 86 |
last STEP if not defined $type; |
| 87 |
if ( $type eq 'MARPA_STEP_TOKEN' ) { |
| 88 |
my ( undef, $token_value_ix, $arg_n ) = @step_data; |
| 89 |
$stack[$arg_n] = $token_values[$token_value_ix]; |
| 90 |
next STEP; |
| 91 |
} |
| 92 |
if ( $type eq 'MARPA_STEP_RULE' ) { |
| 93 |
my ( $rule_id, $arg_0, $arg_n ) = @step_data; |
| 94 |
if ( $rule_id == $start_rule_id ) { |
| 95 |
my ( $string, $value ) = @{ $stack[$arg_n] }; |
| 96 |
$stack[$arg_0] = "$string == $value"; |
| 97 |
next STEP; |
| 98 |
} |
| 99 |
if ( $rule_id == $number_rule_id ) { |
| 100 |
my $number = $stack[$arg_0]; |
| 101 |
$stack[$arg_0] = [ $number, $number ]; |
| 102 |
next STEP; |
| 103 |
} |
| 104 |
if ( $rule_id == $op_rule_id ) { |
| 105 |
my $op = $stack[ $arg_0 + 1 ]; |
| 106 |
my ( $right_string, $right_value ) = @{ $stack[$arg_n] }; |
| 107 |
my ( $left_string, $left_value ) = @{ $stack[$arg_0] }; |
| 108 |
my $value; |
| 109 |
my $text = '(' . $left_string . $op . $right_string . ')'; |
| 110 |
if ( $op eq q{+} ) { |
| 111 |
$stack[$arg_0] = [ $text, $left_value + $right_value ]; |
| 112 |
next STEP; |
| 113 |
} |
| 114 |
if ( $op eq q{-} ) { |
| 115 |
$stack[$arg_0] = [ $text, $left_value - $right_value ]; |
| 116 |
next STEP; |
| 117 |
} |
| 118 |
if ( $op eq q{*} ) { |
| 119 |
$stack[$arg_0] = [ $text, $left_value * $right_value ]; |
| 120 |
next STEP; |
| 121 |
} |
| 122 |
die "Unknown op: $op"; |
| 123 |
} ## end if ( $rule_id == $op_rule_id ) |
| 124 |
die "Unknown rule $rule_id"; |
| 125 |
} ## end if ( $type eq 'MARPA_STEP_RULE' ) |
| 126 |
die "Unexpected step type: $type"; |
| 127 |
} ## end while ( my ( $type, @step_data ) = $valuator->step() ) |
| 128 |
push @actual_values, $stack[0]; |
| 129 |
} ## end while ( $tree->next() ) |
| 130 |
|
| 131 |
# Marpa::R2::Display::End |
| 132 |
|
| 133 |
my %expected_value = ( |
| 134 |
'(2-(0*(3+1))) == 2' => 1, |
| 135 |
'(((2-0)*3)+1) == 7' => 1, |
| 136 |
'((2-(0*3))+1) == 3' => 1, |
| 137 |
'((2-0)*(3+1)) == 8' => 1, |
| 138 |
'(2-((0*3)+1)) == 1' => 1, |
| 139 |
); |
| 140 |
|
| 141 |
my $i = 0; |
| 142 |
for my $actual_value (@actual_values) { |
| 143 |
if ( defined $expected_value{$actual_value} ) { |
| 144 |
delete $expected_value{$actual_value}; |
| 145 |
Test::More::pass("Expected Value $i: $actual_value"); |
| 146 |
} |
| 147 |
else { |
| 148 |
Test::More::fail("Unexpected Value $i: $actual_value"); |
| 149 |
} |
| 150 |
$i++; |
| 151 |
} ## end for my $actual_value (@actual_values) |
| 152 |
|
| 153 |
# For the error methods, start clean, |
| 154 |
# with a new, trivial grammar |
| 155 |
$grammar = $recce = $bocage = $order = $tree = undef; |
| 156 |
$grammar = Marpa::R2::Thin::G->new( { if => 1 } ); |
| 157 |
$grammar->force_valued(); |
| 158 |
|
| 159 |
# Marpa::R2::Display |
| 160 |
# name: Thin throw_set() example |
| 161 |
|
| 162 |
$grammar->throw_set(0); |
| 163 |
|
| 164 |
# Marpa::R2::Display::End |
| 165 |
|
| 166 |
# Turn it right back on, for safety's sake |
| 167 |
$grammar->throw_set(1); |
| 168 |
|
| 169 |
# Marpa::R2::Display |
| 170 |
# name: Thin grammar error methods |
| 171 |
|
| 172 |
my ( $error_code, $error_description ) = $grammar->error(); |
| 173 |
my @error_names = Marpa::R2::Thin::error_names(); |
| 174 |
my $error_name = $error_names[$error_code]; |
| 175 |
|
| 176 |
# Marpa::R2::Display::End |
| 177 |
|
| 178 |
Test::More::is( $error_code, 0, 'Grammar error code' ); |
| 179 |
Test::More::is( $error_name, 'MARPA_ERR_NONE', 'Grammar error name' ); |
| 180 |
Test::More::is( $error_description, 'No error', 'Grammar error description' ); |
| 181 |
|
| 182 |
$symbol_S = $grammar->symbol_new(); |
| 183 |
my $symbol_a = $grammar->symbol_new(); |
| 184 |
my $symbol_sep = $grammar->symbol_new(); |
| 185 |
$grammar->start_symbol_set($symbol_S); |
| 186 |
|
| 187 |
# Marpa::R2::Display |
| 188 |
# name: Thin sequence_new() example |
| 189 |
|
| 190 |
my $sequence_rule_id = $grammar->sequence_new( |
| 191 |
$symbol_S, |
| 192 |
$symbol_a, |
| 193 |
{ separator => $symbol_sep, |
| 194 |
proper => 0, |
| 195 |
min => 1 |
| 196 |
} |
| 197 |
); |
| 198 |
|
| 199 |
# Marpa::R2::Display::End |
| 200 |
|
| 201 |
$grammar->precompute(); |
| 202 |
my @events; |
| 203 |
my $event_ix = $grammar->event_count(); |
| 204 |
while ( $event_ix-- ) { |
| 205 |
|
| 206 |
# Marpa::R2::Display |
| 207 |
# name: Thin event() example |
| 208 |
|
| 209 |
my ( $event_type, $value ) = $grammar->event( $event_ix++ ); |
| 210 |
|
| 211 |
# Marpa::R2::Display::End |
| 212 |
|
| 213 |
} |
| 214 |
|
| 215 |
$recce = Marpa::R2::Thin::R->new($grammar); |
| 216 |
|
| 217 |
# Marpa::R2::Display |
| 218 |
# name: Thin ruby_slippers_set() example |
| 219 |
|
| 220 |
$recce->ruby_slippers_set(1); |
| 221 |
|
| 222 |
# Marpa::R2::Display::End |
| 223 |
|
| 224 |
$recce->start_input(); |
| 225 |
$recce->alternative( $symbol_a, 1, 1 ); |
| 226 |
$recce->earleme_complete(); |
| 227 |
|
| 228 |
# Marpa::R2::Display |
| 229 |
# name: Thin terminals_expected() example |
| 230 |
|
| 231 |
my @terminals = $recce->terminals_expected(); |
| 232 |
|
| 233 |
# Marpa::R2::Display::End |
| 234 |
|
| 235 |
Test::More::is( (scalar @terminals), 1, 'count of terminals expected' ); |
| 236 |
Test::More::is( $terminals[0], $symbol_sep, 'expected terminal' ); |
| 237 |
|
| 238 |
my $report; |
| 239 |
|
| 240 |
# Marpa::R2::Display |
| 241 |
# name: Thin progress_item() example |
| 242 |
|
| 243 |
my $ordinal = $recce->latest_earley_set(); |
| 244 |
$recce->progress_report_start($ordinal); |
| 245 |
ITEM: while (1) { |
| 246 |
my ($rule_id, $dot_position, $origin) = $recce->progress_item(); |
| 247 |
last ITEM if not defined $rule_id; |
| 248 |
push @{$report}, [$rule_id, $dot_position, $origin]; |
| 249 |
} |
| 250 |
$recce->progress_report_finish(); |
| 251 |
|
| 252 |
# Marpa::R2::Display::End |
| 253 |
|
| 254 |
Test::More::is( ( join q{ }, map { @{$_} } @{$report} ), |
| 255 |
'0 -1 0', 'progress report' ); |
| 256 |
|
| 257 |
$recce->alternative( $symbol_sep, 1, 1 ); |
| 258 |
$recce->earleme_complete(); |
| 259 |
$recce->alternative( $symbol_a, 1, 1 ); |
| 260 |
$recce->earleme_complete(); |
| 261 |
$recce->alternative( $symbol_sep, 1, 1 ); |
| 262 |
$recce->earleme_complete(); |
| 263 |
$recce->alternative( $symbol_a, 1, 1 ); |
| 264 |
$recce->earleme_complete(); |
| 265 |
$latest_earley_set_ID = $recce->latest_earley_set(); |
| 266 |
$bocage = Marpa::R2::Thin::B->new( $recce, $latest_earley_set_ID ); |
| 267 |
$order = Marpa::R2::Thin::O->new($bocage); |
| 268 |
$tree = Marpa::R2::Thin::T->new($order); |
| 269 |
$tree->next(); |
| 270 |
my $valuator = Marpa::R2::Thin::V->new($tree); |
| 271 |
my $locations_report = q{}; |
| 272 |
STEP: for ( ;; ) { |
| 273 |
my ( $type, @step_data ) = $valuator->step(); |
| 274 |
last STEP if not defined $type; |
| 275 |
|
| 276 |
# Marpa::R2::Display |
| 277 |
# name: Thin location() example |
| 278 |
|
| 279 |
$type = $valuator->step_type(); |
| 280 |
my ( $start, $end ) = $valuator->location(); |
| 281 |
if ( $type eq 'MARPA_STEP_RULE' ) { |
| 282 |
my ($rule_id) = @step_data; |
| 283 |
$locations_report .= "Rule $rule_id is from $start to $end\n"; |
| 284 |
} |
| 285 |
if ( $type eq 'MARPA_STEP_TOKEN' ) { |
| 286 |
my ($token_id) = @step_data; |
| 287 |
$locations_report .= "Token $token_id is from $start to $end\n"; |
| 288 |
} |
| 289 |
if ( $type eq 'MARPA_STEP_NULLING_SYMBOL' ) { |
| 290 |
my ($symbol_id) = @step_data; |
| 291 |
$locations_report |
| 292 |
.= "Nulling symbol $symbol_id is from $start to $end\n"; |
| 293 |
} |
| 294 |
|
| 295 |
# Marpa::R2::Display::End |
| 296 |
|
| 297 |
} ## end STEP: for ( ;; ) |
| 298 |
|
| 299 |
Test::More::is( $locations_report, <<'EXPECTED', 'Step locations' ); |
| 300 |
Token 1 is from 0 to 1 |
| 301 |
Token 2 is from 1 to 2 |
| 302 |
Token 1 is from 2 to 3 |
| 303 |
Token 2 is from 3 to 4 |
| 304 |
Token 1 is from 4 to 5 |
| 305 |
Rule 0 is from 0 to 5 |
| 306 |
EXPECTED |
| 307 |
|
| 308 |
# Local Variables: |
| 309 |
# mode: cperl |
| 310 |
# cperl-indent-level: 4 |
| 311 |
# fill-column: 100 |
| 312 |
# End: |
| 313 |
# vim: expandtab shiftwidth=4: |