| 1 |
#!/usr/bin/env 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 |
# This example searches for recursively nested braces -- |
| 18 |
# curly, square and round -- in a "salad" of other things. |
| 19 |
# It's to show general BNF search -- sort of a grep or an ack, |
| 20 |
# but for general BNF, instead of regexes. The term |
| 21 |
# "salad" I picked up from Michael Roberts, to suggest |
| 22 |
# that the targets occur in a sort of "lexeme salad". |
| 23 |
# In the literature, this is called a supersequence |
| 24 |
# search. |
| 25 |
|
| 26 |
use 5.010001; |
| 27 |
use strict; |
| 28 |
use warnings; |
| 29 |
use Marpa::R2 2.097_002; |
| 30 |
use Data::Dumper; |
| 31 |
use Test::More tests => 3; |
| 32 |
use Getopt::Long (); |
| 33 |
|
| 34 |
my $verbose; |
| 35 |
die if not Getopt::Long::GetOptions( verbose => \$verbose ); |
| 36 |
|
| 37 |
my $grammar = << '=== GRAMMAR ==='; |
| 38 |
:default ::= action => [ name, value ] |
| 39 |
lexeme default = action => [ name, value ] latm => 1 # to add token names to ast |
| 40 |
|
| 41 |
<prefixed target> ::= prefix target |
| 42 |
prefix ::= <prefix lexeme>* |
| 43 |
|
| 44 |
target ::= balanced |
| 45 |
event target = completed target |
| 46 |
|
| 47 |
balanced ::= |
| 48 |
lparen contents rparen |
| 49 |
| lcurly contents rcurly |
| 50 |
| lsquare contents rsquare |
| 51 |
|
| 52 |
contents ::= <content item>* |
| 53 |
<content item> ::= balanced | filler |
| 54 |
|
| 55 |
<prefix lexeme> ~ <deep filler> |
| 56 |
filler ~ <deep filler> |
| 57 |
# x5b is left square bracket |
| 58 |
# x5d is right square bracket |
| 59 |
<deep filler> ~ [^(){}\x5B\x5D]+ |
| 60 |
|
| 61 |
<prefix lexeme> ~ <deep lparen> |
| 62 |
lparen ~ <deep lparen> |
| 63 |
<deep lparen> ~ '(' |
| 64 |
|
| 65 |
<prefix lexeme> ~ <deep rparen> |
| 66 |
rparen ~ <deep rparen> |
| 67 |
<deep rparen> ~ ')' |
| 68 |
|
| 69 |
<prefix lexeme> ~ <deep lcurly> |
| 70 |
lcurly ~ <deep lcurly> |
| 71 |
<deep lcurly> ~ '{' |
| 72 |
|
| 73 |
<prefix lexeme> ~ <deep rcurly> |
| 74 |
rcurly ~ <deep rcurly> |
| 75 |
<deep rcurly> ~ '}' |
| 76 |
|
| 77 |
<prefix lexeme> ~ <deep lsquare> |
| 78 |
lsquare ~ <deep lsquare> |
| 79 |
<deep lsquare> ~ '[' |
| 80 |
|
| 81 |
<prefix lexeme> ~ <deep rsquare> |
| 82 |
rsquare ~ <deep rsquare> |
| 83 |
<deep rsquare> ~ ']' |
| 84 |
|
| 85 |
=== GRAMMAR === |
| 86 |
|
| 87 |
my $g = Marpa::R2::Scanless::G->new( { source => \($grammar) } ); |
| 88 |
|
| 89 |
my @tests = ( |
| 90 |
[ 'z}ab)({[]})))(([]))zz', ( join "\n", '({[]})', '(([]))', '' ) ], |
| 91 |
[ '9\090]{[][][9]89]8[][]90]{[]\{}{}09[]}[', |
| 92 |
join "\n", '[]', '[]', '[9]', '[]', '[]', '{[]\{}{}09[]}', '' |
| 93 |
], |
| 94 |
[ '([]([])([]([]', join "\n", '[]', '([])', '[]', '[]', '' ], |
| 95 |
); |
| 96 |
|
| 97 |
for my $test (@tests) { |
| 98 |
my ( $string, $expected_result ) = @{$test}; |
| 99 |
my $actual_result = test( $g, $string ); |
| 100 |
diag("Input: $string") if $verbose; |
| 101 |
Test::More::is( $actual_result, $expected_result, |
| 102 |
qq{Result of "$string"} ); |
| 103 |
} ## end for my $test (@tests) |
| 104 |
|
| 105 |
sub test { |
| 106 |
my ($g, $string) = @_; |
| 107 |
my @found = (); |
| 108 |
diag( "Input: $string" ) if $verbose; |
| 109 |
my $input_length = length $string; |
| 110 |
my $target_start = 0; |
| 111 |
|
| 112 |
# my $recce_debug_args = { trace_terminals => 1, trace_values => 1 }; |
| 113 |
state $recce_debug_args = {}; |
| 114 |
|
| 115 |
# One pass through this loop for every target found, |
| 116 |
# until we reach end of string without finding a target |
| 117 |
|
| 118 |
TARGET: while ( $target_start < $input_length ) { |
| 119 |
|
| 120 |
# First we find the "shortest span" -- the one which ends earliest. |
| 121 |
# This tells us where the prefix should end. |
| 122 |
# No prefix should go beyond the first location of the shortest span. |
| 123 |
|
| 124 |
# Marpa::R2::Display |
| 125 |
# name: SLIF exhaustion recognizer setting synopsis |
| 126 |
|
| 127 |
my @shortest_span = (); |
| 128 |
my $recce = Marpa::R2::Scanless::R->new( |
| 129 |
{ grammar => $g, |
| 130 |
exhaustion => 'event', |
| 131 |
}, |
| 132 |
$recce_debug_args |
| 133 |
); |
| 134 |
my $pos = $recce->read( \$string, $target_start ); |
| 135 |
|
| 136 |
EVENT: |
| 137 |
for my $event ( @{ $recce->events() } ) { |
| 138 |
my ($name) = @{$event}; |
| 139 |
if ( $name eq 'target' ) { |
| 140 |
@shortest_span = $recce->last_completed_span('target'); |
| 141 |
diag( |
| 142 |
"Preliminary target at $pos: ", |
| 143 |
$recce->literal(@shortest_span) |
| 144 |
) if $verbose; |
| 145 |
next EVENT; |
| 146 |
} ## end if ( $name eq 'target' ) |
| 147 |
# Not all exhaustion has an exhaustion event, |
| 148 |
# so we look for exhaustion explicitly below. |
| 149 |
next EVENT if $name eq q('exhausted); |
| 150 |
die join q{ }, "Spurious event at position $pos: '$name'"; |
| 151 |
} ## end EVENT: for my $event ( @{ $recce->events() } ) |
| 152 |
|
| 153 |
# Marpa::R2::Display::End |
| 154 |
|
| 155 |
last TARGET if not scalar @shortest_span; |
| 156 |
|
| 157 |
# We now have found the longest allowed prefix. |
| 158 |
# Our "longest match" will begin at the end of this prefix, |
| 159 |
# or before it. |
| 160 |
|
| 161 |
# We just run until exhausted, the look for the last |
| 162 |
# completed <target>. This will be our longest match. |
| 163 |
|
| 164 |
diag( join q{ }, @shortest_span ) if $verbose; |
| 165 |
my $prefix_end = $shortest_span[0]; |
| 166 |
$recce = Marpa::R2::Scanless::R->new( |
| 167 |
{ grammar => $g, |
| 168 |
exhaustion => 'event', |
| 169 |
rejection => 'event', |
| 170 |
}, |
| 171 |
$recce_debug_args |
| 172 |
); |
| 173 |
$recce->activate( 'target', 0 ); |
| 174 |
$recce->read( \$string, $target_start, $prefix_end - $target_start ); |
| 175 |
|
| 176 |
# Marpa::R2::Display |
| 177 |
# name: SLIF recognizer lexeme_priority_set() synopsis |
| 178 |
|
| 179 |
$recce->lexeme_priority_set( 'prefix lexeme', -1 ); |
| 180 |
|
| 181 |
# Marpa::R2::Display::End |
| 182 |
|
| 183 |
$pos = $recce->resume($prefix_end); |
| 184 |
|
| 185 |
# Marpa::R2::Display |
| 186 |
# name: SLIF recognizer last_completed_span() synopsis |
| 187 |
|
| 188 |
my @longest_span = $recce->last_completed_span('target'); |
| 189 |
diag( "Actual target at $pos: ", $recce->literal(@longest_span) ) if $verbose; |
| 190 |
|
| 191 |
# Marpa::R2::Display::End |
| 192 |
|
| 193 |
last TARGET if not scalar @longest_span; |
| 194 |
push @found, $recce->literal(@longest_span); |
| 195 |
diag( "Found target at $pos: ", $recce->literal(@longest_span) ) if $verbose; |
| 196 |
|
| 197 |
# Move the search location forward, |
| 198 |
# in preparation for looking for the next target |
| 199 |
|
| 200 |
$target_start = $longest_span[0] + $longest_span[1]; |
| 201 |
|
| 202 |
} ## end TARGET: while ( $target_start < $input_length ) |
| 203 |
return join "\n", @found, q{}; |
| 204 |
} ## end sub test |
| 205 |
|
| 206 |
# vim: expandtab shiftwidth=4: |