| 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 |
# An ambiguous equation |
| 17 |
|
| 18 |
use 5.010001; |
| 19 |
use strict; |
| 20 |
use warnings; |
| 21 |
|
| 22 |
use Test::More tests => 1; |
| 23 |
|
| 24 |
use lib 'inc'; |
| 25 |
use Marpa::R2::Test; |
| 26 |
use English qw( -no_match_vars ); |
| 27 |
use Fatal qw(open close); |
| 28 |
use Marpa::R2; |
| 29 |
|
| 30 |
## no critic (InputOutput::RequireBriefOpen) |
| 31 |
open my $original_stdout, q{>&STDOUT}; |
| 32 |
## use critic |
| 33 |
|
| 34 |
sub save_stdout { |
| 35 |
my $save; |
| 36 |
my $save_ref = \$save; |
| 37 |
close STDOUT; |
| 38 |
open STDOUT, q{>}, $save_ref; |
| 39 |
return $save_ref; |
| 40 |
} ## end sub save_stdout |
| 41 |
|
| 42 |
sub restore_stdout { |
| 43 |
close STDOUT; |
| 44 |
open STDOUT, q{>&}, $original_stdout; |
| 45 |
return 1; |
| 46 |
} |
| 47 |
|
| 48 |
# Marpa::R2::Display |
| 49 |
# name: Null Value Example |
| 50 |
|
| 51 |
sub do_L { |
| 52 |
shift; |
| 53 |
return 'L(' . ( join q{;}, map { $_ // '[ERROR!]' } @_ ) . ')'; |
| 54 |
} |
| 55 |
|
| 56 |
sub do_R { |
| 57 |
return 'R(): I will never be called'; |
| 58 |
} |
| 59 |
|
| 60 |
sub do_S { |
| 61 |
shift; |
| 62 |
return 'S(' . ( join q{;}, map { $_ // '[ERROR!]' } @_ ) . ')'; |
| 63 |
} |
| 64 |
|
| 65 |
sub do_X { return 'X(' . $_[1] . ')'; } |
| 66 |
sub do_Y { return 'Y(' . $_[1] . ')'; } |
| 67 |
|
| 68 |
## no critic (Variables::ProhibitPackageVars) |
| 69 |
our $null_A = 'null A'; |
| 70 |
our $null_B = 'null B'; |
| 71 |
our $null_L = 'null L'; |
| 72 |
our $null_R = 'null R'; |
| 73 |
our $null_X = 'null X'; |
| 74 |
our $null_Y = 'null Y'; |
| 75 |
## use critic |
| 76 |
|
| 77 |
my $grammar = Marpa::R2::Grammar->new( |
| 78 |
{ start => 'S', |
| 79 |
actions => 'main', |
| 80 |
rules => [ |
| 81 |
[ 'S', [qw/L R/], 'do_S' ], |
| 82 |
[ 'L', [qw/A B X/], 'do_L' ], |
| 83 |
[ 'L', [], 'null_L' ], |
| 84 |
[ 'R', [qw/A B Y/], 'do_R' ], |
| 85 |
[ 'R', [], 'null_R' ], |
| 86 |
[ 'A', [], 'null_A' ], |
| 87 |
[ 'B', [], 'null_B' ], |
| 88 |
[ 'X', [], 'null_X' ], |
| 89 |
[ 'X', [qw/x/], 'do_X' ], |
| 90 |
[ 'Y', [], 'null_Y' ], |
| 91 |
[ 'Y', [qw/y/], 'do_Y' ], |
| 92 |
], |
| 93 |
} |
| 94 |
); |
| 95 |
|
| 96 |
$grammar->precompute(); |
| 97 |
|
| 98 |
my $recce = Marpa::R2::Recognizer->new( { grammar => $grammar } ); |
| 99 |
|
| 100 |
$recce->read( 'x', 'x' ); |
| 101 |
|
| 102 |
# Marpa::R2::Display::End |
| 103 |
|
| 104 |
## use critic |
| 105 |
|
| 106 |
# Marpa::R2::Display |
| 107 |
# name: Null Value Example Output |
| 108 |
# start-after-line: END_OF_OUTPUT |
| 109 |
# end-before-line: '^END_OF_OUTPUT$' |
| 110 |
|
| 111 |
chomp( my $expected = <<'END_OF_OUTPUT'); |
| 112 |
S(L(null A;null B;X(x));null R) |
| 113 |
END_OF_OUTPUT |
| 114 |
|
| 115 |
# Marpa::R2::Display::End |
| 116 |
|
| 117 |
my $value = $recce->value(); |
| 118 |
Marpa::R2::Test::is( ${$value}, $expected, 'Null example' ); |
| 119 |
|
| 120 |
1; # In case used as "do" file |
| 121 |
|
| 122 |
# Local Variables: |
| 123 |
# mode: cperl |
| 124 |
# cperl-indent-level: 4 |
| 125 |
# fill-column: 100 |
| 126 |
# End: |
| 127 |
# vim: expandtab shiftwidth=4: |