| 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 |
# Synopsis for Stuizand interface |
| 18 |
|
| 19 |
use 5.010001; |
| 20 |
use strict; |
| 21 |
use warnings; |
| 22 |
use Test::More tests => 1; |
| 23 |
|
| 24 |
use lib 'inc'; |
| 25 |
use Marpa::R2::Test; |
| 26 |
|
| 27 |
## no critic (ErrorHandling::RequireCarping); |
| 28 |
|
| 29 |
# Marpa::R2::Display |
| 30 |
# name: Stuifzand Synopsis |
| 31 |
|
| 32 |
use Marpa::R2; |
| 33 |
|
| 34 |
my $grammar = Marpa::R2::Grammar->new( |
| 35 |
{ |
| 36 |
actions => 'My_Actions', |
| 37 |
default_action => 'do_first_arg', |
| 38 |
source => \(<<'END_OF_SOURCE'), |
| 39 |
:start ::= Script |
| 40 |
Script ::= Expression+ separator => <op comma> action => do_script |
| 41 |
Expression ::= |
| 42 |
Number |
| 43 |
| (<op lparen>) Expression (<op rparen>) action => do_parens assoc => group |
| 44 |
|| Expression (<op pow>) Expression action => do_pow assoc => right |
| 45 |
|| Expression (<op times>) Expression action => do_multiply |
| 46 |
| Expression (<op divide>) Expression action => do_divide |
| 47 |
|| Expression (<op add>) Expression action => do_add |
| 48 |
| Expression (<op subtract>) Expression action => do_subtract |
| 49 |
END_OF_SOURCE |
| 50 |
} |
| 51 |
); |
| 52 |
|
| 53 |
# Marpa::R2::Display::End |
| 54 |
|
| 55 |
sub My_Actions::do_parens { shift; return $_[0] } |
| 56 |
sub My_Actions::do_add { shift; return $_[0] + $_[1] } |
| 57 |
sub My_Actions::do_subtract { shift; return $_[0] - $_[1] } |
| 58 |
sub My_Actions::do_multiply { shift; return $_[0] * $_[1] } |
| 59 |
sub My_Actions::do_divide { shift; return $_[0] / $_[1] } |
| 60 |
sub My_Actions::do_pow { shift; return $_[0]**$_[1] } |
| 61 |
sub My_Actions::do_first_arg { shift; return shift; } |
| 62 |
sub My_Actions::do_script { shift; return join q{ }, @_ } |
| 63 |
|
| 64 |
$grammar->precompute(); |
| 65 |
|
| 66 |
my @terminals = ( |
| 67 |
[ Number => qr/\d+/xms, "Number" ], |
| 68 |
[ 'op pow' => qr/[\^]/xms, 'Exponentiation operator' ], |
| 69 |
[ 'op pow' => qr/[*][*]/xms, 'Exponentiation' ], # order matters! |
| 70 |
[ 'op times' => qr/[*]/xms, 'Multiplication operator' ], # order matters! |
| 71 |
[ 'op divide' => qr/[\/]/xms, 'Division operator' ], |
| 72 |
[ 'op add' => qr/[+]/xms, 'Addition operator' ], |
| 73 |
[ 'op subtract' => qr/[-]/xms, 'Subtraction operator' ], |
| 74 |
[ 'op lparen' => qr/[(]/xms, 'Left parenthesis' ], |
| 75 |
[ 'op rparen' => qr/[)]/xms, 'Right parenthesis' ], |
| 76 |
[ 'op comma' => qr/[,]/xms, 'Comma operator' ], |
| 77 |
); |
| 78 |
|
| 79 |
sub my_parser { |
| 80 |
my ( $grammar, $string ) = @_; |
| 81 |
my $recce = Marpa::R2::Recognizer->new( { grammar => $grammar } ); |
| 82 |
my $length = length $string; |
| 83 |
my $last_position = 0; |
| 84 |
pos $string = $last_position; |
| 85 |
TOKEN: while (1) { |
| 86 |
$last_position = pos $string; |
| 87 |
last TOKEN if $last_position >= $length; |
| 88 |
next TOKEN if $string =~ m/\G\s+/gcxms; # skip whitespace |
| 89 |
TOKEN_TYPE: for my $t (@terminals) { |
| 90 |
my ( $token_name, $regex, $long_name ) = @{$t}; |
| 91 |
next TOKEN_TYPE if not $string =~ m/\G($regex)/gcxms; |
| 92 |
next TOKEN if defined $recce->read( $token_name, $1 ); |
| 93 |
die |
| 94 |
qq{Parser rejected token "$long_name" at position $last_position, before "}, |
| 95 |
substr( $string, $last_position, 40 ), q{"}; |
| 96 |
} ## end TOKEN_TYPE: for my $t (@terminals) |
| 97 |
die qq{No token found at position $last_position, before "}, |
| 98 |
substr( $string, pos $string, 40 ), q{"}; |
| 99 |
} ## end TOKEN: while (1) |
| 100 |
my $value_ref = $recce->value; |
| 101 |
return $value_ref ? ${$value_ref} : 'No Parse'; |
| 102 |
} ## end sub my_parser |
| 103 |
|
| 104 |
my $value = my_parser( $grammar, '42*2+7/3, 42*(2+7)/3, 2**7-3, 2**(7-3)' ); |
| 105 |
|
| 106 |
Test::More::like( $value, qr/\A 86[.]3\d+ \s+ 126 \s+ 125 \s+ 16\z/xms, 'Value of Stuifzand parse' ); |
| 107 |
|
| 108 |
# Local Variables: |
| 109 |
# mode: cperl |
| 110 |
# cperl-indent-level: 4 |
| 111 |
# fill-column: 100 |
| 112 |
# End: |
| 113 |
# vim: expandtab shiftwidth=4: |