Debian Codegraph @

t/syn_engine.t

raw
1 #!/usr/bin/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 # Engine Synopsis
18
19 use 5.010001;
20 use strict;
21 use warnings;
22
23 use Test::More tests => 3;
24
25 use lib 'inc';
26 use Marpa::R2::Test;
27
28 ## no critic (ErrorHandling::RequireCarping);
29
30 # Marpa::R2::Display
31 # name: Engine Synopsis Unambiguous Parse
32
33 use Marpa::R2;
34
35 my $grammar = Marpa::R2::Grammar->new(
36 { start => 'Expression',
37 actions => 'My_Actions',
38 default_action => 'first_arg',
39 rules => [
40 { lhs => 'Expression', rhs => [qw/Term/] },
41 { lhs => 'Term', rhs => [qw/Factor/] },
42 { lhs => 'Factor', rhs => [qw/Number/] },
43 { lhs => 'Term', rhs => [qw/Term Add Term/], action => 'do_add' },
44 { lhs => 'Factor',
45 rhs => [qw/Factor Multiply Factor/],
46 action => 'do_multiply'
47 },
48 ],
49 }
50 );
51
52 $grammar->precompute();
53
54 my $recce = Marpa::R2::Recognizer->new( { grammar => $grammar } );
55
56 $recce->read( 'Number', 42 );
57 $recce->read('Multiply');
58 $recce->read( 'Number', 1 );
59 $recce->read('Add');
60 $recce->read( 'Number', 7 );
61
62 sub My_Actions::do_add {
63 my ( undef, $t1, undef, $t2 ) = @_;
64 return $t1 + $t2;
65 }
66
67 sub My_Actions::do_multiply {
68 my ( undef, $t1, undef, $t2 ) = @_;
69 return $t1 * $t2;
70 }
71
72 sub My_Actions::first_arg { shift; return shift; }
73
74 my $value_ref = $recce->value;
75 my $value = $value_ref ? ${$value_ref} : 'No Parse';
76
77 # Marpa::R2::Display::End
78
79 # Ambiguous, Array Form Rules
80
81 # Marpa::R2::Display
82 # name: Engine Synopsis Ambiguous Parse
83
84 use Marpa::R2;
85
86 my $ambiguous_grammar = Marpa::R2::Grammar->new(
87 { start => 'E',
88 actions => 'My_Actions',
89 rules => [
90 [ 'E', [qw/E Add E/], 'do_add' ],
91 [ 'E', [qw/E Multiply E/], 'do_multiply' ],
92 [ 'E', [qw/Number/], ],
93 ],
94 default_action => 'first_arg',
95 }
96 );
97
98 $ambiguous_grammar->precompute();
99
100 my $ambiguous_recce =
101 Marpa::R2::Recognizer->new( { grammar => $ambiguous_grammar } );
102
103 $ambiguous_recce->read( 'Number', 42 );
104 $ambiguous_recce->read('Multiply');
105 $ambiguous_recce->read( 'Number', 1 );
106 $ambiguous_recce->read('Add');
107 $ambiguous_recce->read( 'Number', 7 );
108
109 my @values = ();
110 while ( defined( my $ambiguous_value_ref = $ambiguous_recce->value() ) ) {
111 push @values, ${$ambiguous_value_ref};
112 }
113
114 # Marpa::R2::Display::End
115
116 Test::More::is( $value, 49, 'Unambiguous Value' );
117 Test::More::is_deeply( [ sort @values ], [ 336, 49 ], 'Ambiguous Values' );
118
119 # An example of "Ruby Slippers" lexing, using
120 # the unambiguous grammar.
121
122 sub fix_things {
123 my ($recce, $tokens, $token_ix) = @_;
124 die qq{Don't know how to fix things at $token_ix};
125 }
126
127 # Marpa::R2::Display
128 # name: Engine Synopsis Interactive Parse
129
130 $recce = Marpa::R2::Recognizer->new( { grammar => $grammar } );
131
132 my @tokens = (
133 [ 'Number', 42 ],
134 ['Multiply'], [ 'Number', 1 ],
135 ['Add'], [ 'Number', 7 ],
136 );
137
138 TOKEN: for ( my $token_ix = 0; $token_ix <= $#tokens; $token_ix++ ) {
139 defined $recce->read( @{ $tokens[$token_ix] } )
140 or fix_things( $recce, $token_ix, \@tokens )
141 or die q{Don't know how to fix things};
142 }
143
144 # Marpa::R2::Display::End
145
146 $value_ref = $recce->value;
147 $value = $value_ref ? ${$value_ref} : 'No Parse';
148 Test::More::is( $value, 49, 'Interactive Value' );
149
150 1; # In case used as "do" file
151
152 # Local Variables:
153 # mode: cperl
154 # cperl-indent-level: 4
155 # fill-column: 100
156 # End:
157 # vim: expandtab shiftwidth=4: