Debian Codegraph @

t/leo.t

raw
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 # The example from p. 168-169 of Leo's paper.
17 #
18 # Make sure I have a CHAF example!
19 #
20
21 use 5.010001;
22 use strict;
23 use warnings;
24
25 use Test::More tests => 17;
26
27 use lib 'inc';
28 use Marpa::R2::Test;
29 use Marpa::R2;
30
31 ## no critic (Subroutines::RequireArgUnpacking)
32
33 sub generate_action {
34 my ($lhs) = @_;
35 return sub {
36 shift;
37 my $v_count = scalar @_;
38 return q{-} if $v_count <= 0;
39 my @vals = map { $_ // q{-} } @_;
40 return $lhs . '(' . ( join q{;}, @vals ) . ')';
41 }
42 } ## end sub generate_action
43
44 my $C_action = generate_action('C');
45 my $S_action = generate_action('S');
46 my $default_action = generate_action(q{?});
47
48 ## use critic
49
50 my $grammar = Marpa::R2::Grammar->new(
51 { start => 'S',
52 rules => [
53 [ 'S', [qw/a S/], 'S_action', ],
54 [ 'S', [qw/C/], 'S_action', ],
55 [ 'C', [qw(a C b)], 'C_action', ],
56 [ 'C', [], ],
57 ],
58 terminals => [qw(a b)],
59 default_action => 'default_action',
60 }
61 );
62
63 $grammar->precompute();
64
65 Marpa::R2::Test::is( $grammar->show_symbols(),
66 <<'END_OF_STRING', 'Leo168 Symbols' );
67 0: a, terminal
68 1: b, terminal
69 2: S
70 3: C
71 END_OF_STRING
72
73 Marpa::R2::Test::is( $grammar->show_rules,
74 <<'END_OF_STRING', 'Leo168 Rules' );
75 0: S -> a S
76 1: S -> C
77 2: C -> a C b
78 3: C -> /* empty !used */
79 END_OF_STRING
80
81 Marpa::R2::Test::is( $grammar->show_ahms, <<'END_OF_STRING', 'Leo168 AHMs' );
82 AHM 0: postdot = "a"
83 S ::= . a S
84 AHM 1: postdot = "S"
85 S ::= a . S
86 AHM 2: completion
87 S ::= a S .
88 AHM 3: postdot = "a"
89 S ::= . a S[]
90 AHM 4: completion
91 S ::= a S[] .
92 AHM 5: postdot = "C"
93 S ::= . C
94 AHM 6: completion
95 S ::= C .
96 AHM 7: postdot = "a"
97 C ::= . a C b
98 AHM 8: postdot = "C"
99 C ::= a . C b
100 AHM 9: postdot = "b"
101 C ::= a C . b
102 AHM 10: completion
103 C ::= a C b .
104 AHM 11: postdot = "a"
105 C ::= . a C[] b
106 AHM 12: postdot = "b"
107 C ::= a C[] . b
108 AHM 13: completion
109 C ::= a C[] b .
110 AHM 14: postdot = "S"
111 S['] ::= . S
112 AHM 15: completion
113 S['] ::= S .
114 END_OF_STRING
115
116 my %expected = (
117 'a' => q{S(a;-)},
118 'ab' => q{S(C(a;-;b))},
119 'aa' => q{S(a;S(a;-))},
120 'aab' => q{S(a;S(C(a;-;b)))},
121 'aabb' => q{S(C(a;C(a;-;b);b))},
122 'aaa' => q{S(a;S(a;S(a;-)))},
123 'aaab' => q{S(a;S(a;S(C(a;-;b))))},
124 'aaabb' => q{S(a;S(C(a;C(a;-;b);b)))},
125 'aaabbb' => q{S(C(a;C(a;C(a;-;b);b);b))},
126 'aaaa' => q{S(a;S(a;S(a;S(a;-))))},
127 'aaaab' => q{S(a;S(a;S(a;S(C(a;-;b)))))},
128 'aaaabb' => q{S(a;S(a;S(C(a;C(a;-;b);b))))},
129 'aaaabbb' => q{S(a;S(C(a;C(a;C(a;-;b);b);b)))},
130 'aaaabbbb' => q{S(C(a;C(a;C(a;C(a;-;b);b);b);b))},
131 );
132
133 for my $a_length ( 1 .. 4 ) {
134 for my $b_length ( 0 .. $a_length ) {
135
136 my $string = ( 'a' x $a_length ) . ( 'b' x $b_length );
137 my $recce = Marpa::R2::Recognizer->new(
138 { grammar => $grammar,
139 closures => {
140 'C_action' => $C_action,
141 'S_action' => $S_action,
142 'default_action' => $default_action,
143 }
144 }
145 );
146 for (1 .. $a_length ) {
147 $recce->read( 'a', 'a' );
148 }
149 for (1 .. $b_length ) {
150 $recce->read( 'b', 'b' );
151 }
152 my $value_ref = $recce->value();
153 my $value = $value_ref ? ${$value_ref} : 'No parse';
154 Marpa::R2::Test::is( $value, $expected{$string}, "Parse of $string" );
155
156 } ## end for my $b_length ( 0 .. $a_length )
157 } ## end for my $a_length ( 1 .. 4 )
158
159 1; # In case used as "do" file
160
161 # Local Variables:
162 # mode: cperl
163 # cperl-indent-level: 4
164 # fill-column: 100
165 # End:
166 # vim: expandtab shiftwidth=4: