| 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 |
# the example grammar in Aycock/Horspool "Practical Earley Parsing", |
| 18 |
# _The Computer Journal_, Vol. 45, No. 6, pp. 620-630, |
| 19 |
# in its "NNF" form |
| 20 |
|
| 21 |
use 5.010001; |
| 22 |
use strict; |
| 23 |
use warnings; |
| 24 |
|
| 25 |
use English qw( -no_match_vars ); |
| 26 |
use Test::More tests => 6; |
| 27 |
use lib 'inc'; |
| 28 |
use Marpa::R2::Test; |
| 29 |
use Marpa::R2; |
| 30 |
|
| 31 |
my $source = <<'END_OF_GRAMMAR'; |
| 32 |
:start ::= script |
| 33 |
reduce_op ::= |
| 34 |
op_plus action => do_arg0 |
| 35 |
| op_minus action => do_arg0 |
| 36 |
| op_divide action => do_arg0 |
| 37 |
| op_star action => do_arg0 |
| 38 |
script ::= e action => do_arg0 |
| 39 |
script ::= script op_semicolon e action => do_arg2 |
| 40 |
e ::= |
| 41 |
NUM action => do_arg0 |
| 42 |
| VAR action => do_is_var |
| 43 |
| op_lparen e op_rparen action => do_arg1 assoc => group |
| 44 |
|| op_minus e action => do_negate |
| 45 |
|| e op_caret e action => do_power assoc => right |
| 46 |
|| e op_star e action => do_multiply |
| 47 |
| e op_divide e action => do_divide |
| 48 |
|| e op_plus e action => do_addition |
| 49 |
| e op_minus e action => do_subtract |
| 50 |
|| e op_comma e action => do_array |
| 51 |
|| reduce_op op_reduce e action => do_reduce |
| 52 |
|| VAR op_assign e action => do_set_var |
| 53 |
END_OF_GRAMMAR |
| 54 |
|
| 55 |
my $grammar = Marpa::R2::Grammar->new( |
| 56 |
{ |
| 57 |
actions => __PACKAGE__, |
| 58 |
source => \$source, |
| 59 |
} |
| 60 |
); |
| 61 |
$grammar->precompute; |
| 62 |
|
| 63 |
# Order matters !! |
| 64 |
my @terminals = ( |
| 65 |
[ op_reduce => qr/reduce\b/xms ], |
| 66 |
[ NUM => qr/\d+/xms ], |
| 67 |
[ VAR => qr/\w+/xms ], |
| 68 |
[ op_assign => qr/[=]/xms ], |
| 69 |
[ op_semicolon => qr/[;]/xms ], |
| 70 |
[ op_star => qr/[*]/xms ], |
| 71 |
[ op_divide => qr/[\/]/xms ], |
| 72 |
[ op_plus => qr/[+]/xms ], |
| 73 |
[ op_minus => qr/[-]/xms ], |
| 74 |
[ op_caret => qr/[\^]/xms ], |
| 75 |
[ op_lparen => qr/[(]/xms ], |
| 76 |
[ op_rparen => qr/[)]/xms ], |
| 77 |
[ op_comma => qr/[,]/xms ], |
| 78 |
); |
| 79 |
|
| 80 |
my %symbol_table = (); |
| 81 |
|
| 82 |
sub do_is_var { |
| 83 |
my ( undef, $var ) = @_; |
| 84 |
my $value = $symbol_table{$var}; |
| 85 |
die qq{Undefined variable "$var"} if not defined $value; |
| 86 |
return $value; |
| 87 |
} ## end sub do_is_var |
| 88 |
|
| 89 |
sub do_set_var { |
| 90 |
my ( undef, $var, undef, $value ) = @_; |
| 91 |
return $symbol_table{$var} = $value; |
| 92 |
} |
| 93 |
|
| 94 |
sub do_negate { |
| 95 |
return -$_[2]; |
| 96 |
} |
| 97 |
|
| 98 |
sub do_arg0 { return $_[1]; } |
| 99 |
sub do_arg1 { return $_[2]; } |
| 100 |
sub do_arg2 { return $_[3]; } |
| 101 |
|
| 102 |
sub do_array { |
| 103 |
my ( undef, $left, undef, $right ) = @_; |
| 104 |
my @value = (); |
| 105 |
my $ref; |
| 106 |
if ( $ref = ref $left ) { |
| 107 |
die "Bad ref type for array operand: $ref" if $ref ne 'ARRAY'; |
| 108 |
push @value, @{$left}; |
| 109 |
} |
| 110 |
else { |
| 111 |
push @value, $left; |
| 112 |
} |
| 113 |
if ( $ref = ref $right ) { |
| 114 |
die "Bad ref type for array operand: $ref" if $ref ne 'ARRAY'; |
| 115 |
push @value, @{$right}; |
| 116 |
} |
| 117 |
else { |
| 118 |
push @value, $right; |
| 119 |
} |
| 120 |
return \@value; |
| 121 |
} ## end sub do_array |
| 122 |
|
| 123 |
sub do_power { my ( undef, $left, undef, $right ) = @_; return $left**$right; } |
| 124 |
sub do_multiply { my ( undef, $left, undef, $right ) = @_; return $left*$right; } |
| 125 |
sub do_divide { my ( undef, $left, undef, $right ) = @_; return $left/$right; } |
| 126 |
sub do_addition { my ( undef, $left, undef, $right ) = @_; return $left+$right; } |
| 127 |
sub do_subtract { my ( undef, $left, undef, $right ) = @_; return $left-$right; } |
| 128 |
|
| 129 |
my %binop_closure = ( |
| 130 |
'*' => \&do_multiply, |
| 131 |
'/' => \&do_divide, |
| 132 |
'+' => \&do_addition, |
| 133 |
'-' => \&do_subtract, |
| 134 |
'^' => \&do_power, |
| 135 |
); |
| 136 |
|
| 137 |
sub do_reduce { |
| 138 |
my ( undef, $op, undef, $args ) = @_; |
| 139 |
my $closure = $binop_closure{$op}; |
| 140 |
die qq{Do not know how to perform binary operation "$op"} |
| 141 |
if not defined $closure; |
| 142 |
$args = [$args] if ref $args eq ''; |
| 143 |
my @stack = @{$args}; |
| 144 |
OP: while (1) { |
| 145 |
return $stack[0] if scalar @stack <= 1; |
| 146 |
my $result = $closure->( undef, $stack[-2], undef, $stack[-1] ); |
| 147 |
splice @stack, -2, 2, $result; |
| 148 |
} |
| 149 |
die; # Should not get here |
| 150 |
} ## end sub do_reduce |
| 151 |
|
| 152 |
# For debugging |
| 153 |
sub add_brackets { |
| 154 |
my ( undef, @children ) = @_; |
| 155 |
return $children[0] if 1 == scalar @children; |
| 156 |
my $original = join q{}, grep {defined} @children; |
| 157 |
return '[' . $original . ']'; |
| 158 |
} ## end sub add_brackets |
| 159 |
|
| 160 |
sub calculate { |
| 161 |
my ($string) = @_; |
| 162 |
|
| 163 |
%symbol_table = (); |
| 164 |
|
| 165 |
my $recce = Marpa::R2::Recognizer->new( { grammar => $grammar } ); |
| 166 |
|
| 167 |
my $length = length $string; |
| 168 |
my $last_position = 0; |
| 169 |
pos $string = $last_position; |
| 170 |
TOKEN: while ( 1 ) { |
| 171 |
|
| 172 |
$last_position = pos $string; |
| 173 |
last TOKEN if $last_position >= $length; |
| 174 |
|
| 175 |
# skip whitespace |
| 176 |
next TOKEN if $string =~ m/\G\s+/gcxms; |
| 177 |
|
| 178 |
# read other tokens |
| 179 |
TOKEN_TYPE: for my $t (@terminals) { |
| 180 |
next TOKEN_TYPE if not $string =~ m/\G($t->[1])/gcxms; |
| 181 |
my $token_value = $1; |
| 182 |
next TOKEN if defined $recce->read( $t->[0], $token_value ); |
| 183 |
# say STDERR $recce->show_progress() or die "say failed: $ERRNO"; |
| 184 |
die q{Token rejected, "}, $t->[0], qq{", "$token_value"\n}, |
| 185 |
qq{Problem near position $last_position: "}, |
| 186 |
( substr $string, $last_position, 40 ), "\n"; |
| 187 |
} ## end TOKEN_TYPE: for my $t (@terminals) |
| 188 |
|
| 189 |
die q{No token at "}, ( substr $string, pos $string, 40 ), |
| 190 |
q{", position }, pos $string; |
| 191 |
} ## end TOKEN: while ( pos $string < $length ) |
| 192 |
|
| 193 |
my $value_ref = $recce->value; |
| 194 |
|
| 195 |
if ( !defined $value_ref ) { |
| 196 |
say $recce->show_progress() or die "say failed: $ERRNO"; |
| 197 |
die 'Parse failed'; |
| 198 |
} |
| 199 |
return ${$value_ref}; |
| 200 |
|
| 201 |
} ## end sub calculate |
| 202 |
|
| 203 |
sub report_calculation { |
| 204 |
my ($string) = @_; |
| 205 |
my $output = qq{Input: "$string"\n}; |
| 206 |
my $result = calculate($string); |
| 207 |
$result = join q{,}, @{$result} if ref $result eq 'ARRAY'; |
| 208 |
$output .= " Parse: $result\n"; |
| 209 |
for my $symbol ( sort keys %symbol_table ) { |
| 210 |
$output .= qq{"$symbol" = "} . $symbol_table{$symbol} . qq{"\n}; |
| 211 |
} |
| 212 |
return $output; |
| 213 |
} ## end sub report_calculation |
| 214 |
|
| 215 |
if (@ARGV) { |
| 216 |
my $result = calculate( join ';', grep {/\S/} @ARGV ); |
| 217 |
$result = join q{,}, @{$result} if ref $result eq 'ARRAY'; |
| 218 |
say "Result is ", $result; |
| 219 |
for my $symbol ( sort keys %symbol_table ) { |
| 220 |
say qq{"$symbol" = "} . $symbol_table{$symbol} . qq{"}; |
| 221 |
} |
| 222 |
exit 0; |
| 223 |
} ## end if (@ARGV) |
| 224 |
|
| 225 |
my @tests = ( |
| 226 |
[ '4 * 3 + 42 / 1', <<'END_OF_OUTPUT'], |
| 227 |
Input: "4 * 3 + 42 / 1" |
| 228 |
Parse: 54 |
| 229 |
END_OF_OUTPUT |
| 230 |
[ '4 * 3 / (a = b = 5) + 42 - 1', <<'END_OF_OUTPUT'], |
| 231 |
Input: "4 * 3 / (a = b = 5) + 42 - 1" |
| 232 |
Parse: 43.4 |
| 233 |
"a" = "5" |
| 234 |
"b" = "5" |
| 235 |
END_OF_OUTPUT |
| 236 |
[ '4 * 3 / 5 - - - 3 + 42 - 1', <<'END_OF_OUTPUT'], |
| 237 |
Input: "4 * 3 / 5 - - - 3 + 42 - 1" |
| 238 |
Parse: 40.4 |
| 239 |
END_OF_OUTPUT |
| 240 |
[ 'a=1;b = 5; - a - b', <<'END_OF_OUTPUT'], |
| 241 |
Input: "a=1;b = 5; - a - b" |
| 242 |
Parse: -6 |
| 243 |
"a" = "1" |
| 244 |
"b" = "5" |
| 245 |
END_OF_OUTPUT |
| 246 |
[ '1 * 2 + 3 * 4 ^ 2 ^ 2 ^ 2 * 42 + 1', <<'END_OF_OUTPUT'], |
| 247 |
Input: "1 * 2 + 3 * 4 ^ 2 ^ 2 ^ 2 * 42 + 1" |
| 248 |
Parse: 541165879299 |
| 249 |
END_OF_OUTPUT |
| 250 |
['+ reduce 1 + 2, 3,4*2 , 5', <<'END_OF_OUTPUT'], |
| 251 |
Input: "+ reduce 1 + 2, 3,4*2 , 5" |
| 252 |
Parse: 19 |
| 253 |
END_OF_OUTPUT |
| 254 |
); |
| 255 |
|
| 256 |
for my $test (@tests) { |
| 257 |
my ( $input, $expected_output ) = @{$test}; |
| 258 |
my $actual_output = report_calculation($input); |
| 259 |
Marpa::R2::Test::is( $actual_output, $expected_output, |
| 260 |
qq{Parsing "$input"} ); |
| 261 |
} ## end for my $test (@tests) |
| 262 |
|
| 263 |
# vim: expandtab shiftwidth=4: |