| 1 | #!/usr/local/bin/perl -w | ||
| 2 | # #################################################################################### # | ||
| 3 | # Program testmarpagrammar # | ||
| 4 | # # | ||
| 5 | # Author Axel Zuber # | ||
| 6 | # Created 29.05.2022 # | ||
| 7 | # # | ||
| 8 | # Description read a Marpa grammar from a HERE document, # | ||
| 9 | # read an input file from a HERE document, # | ||
| 10 | # apply the grammar to the input file and dump the parse tree # | ||
| 11 | # # | ||
| 12 | # ------------------------------------------------------------------------------------ # | ||
| 13 | # # | ||
| 14 | # Input Marpa::R2 grammar # | ||
| 15 | # input file # | ||
| 16 | # # | ||
| 17 | # ------------------------------------------------------------------------------------ # | ||
| 18 | # # | ||
| 19 | # Output Parse tree # | ||
| 20 | # # | ||
| 21 | # ------------------------------------------------------------------------------------ # | ||
| 22 | # # | ||
| 23 | # Exit code # | ||
| 24 | # 0 : Successful # | ||
| 25 | # 4 : Warnings # | ||
| 26 | # 8 : Errors # | ||
| 27 | # # | ||
| 28 | # ------------------------------------------------------------------------------------ # | ||
| 29 | # # | ||
| 30 | # History # | ||
| 31 | # Ver Date Name Description # | ||
| 32 | # 0.9 29.05.2022 A. Zuber Initial version # | ||
| 33 | # # | ||
| 34 | # #################################################################################### # | ||
| 35 | |||
| 36 | package Main; | ||
| 37 | use strict; | ||
| 38 | use warnings FATAL => 'all'; | ||
| 39 | |||
| 40 | use strict; | ||
| 41 | |||
| 42 | use Marpa::R2; | ||
| 43 | use Data::Dumper; | ||
| 44 | |||
| 45 | my $dsl = <<'END_OF_DSL'; | ||
| 46 | lexeme default = latm => 1 | ||
| 47 | |||
| 48 | :start ::= eval | ||
| 49 | |||
| 50 | # --- | ||
| 51 | # Discard rule from redirect options : | ||
| 52 | :discard ~ <discarded redirects> | ||
| 53 | <discarded redirects> ~ WS | ||
| 54 | # --- | ||
| 55 | eval ::= additionExp | ||
| 56 | additionExp ::= multiplyExp additionExp_002 action => do_add | ||
| 57 | additionExp_001 ::= '+' multiplyExp | ||
| 58 | | '-' multiplyExp | ||
| 59 | additionExp_002 ::= additionExp_001* | ||
| 60 | |||
| 61 | multiplyExp ::= atomExp multiplyExp_002 action => do_mult | ||
| 62 | multiplyExp_001 ::= '*' atomExp | ||
| 63 | | '/' atomExp | ||
| 64 | multiplyExp_002 ::= multiplyExp_001* | ||
| 65 | |||
| 66 | atomExp ::= Number | ||
| 67 | | '(' additionExp ')' | ||
| 68 | Number ~ UNSIGNED_INTEGER opt_Number_006 | ||
| 69 | SINGLE_DIGIT ~ [0-9] | ||
| 70 | UNSIGNED_INTEGER ~ SINGLE_DIGIT+ | ||
| 71 | Number_005 ~ '.' UNSIGNED_INTEGER | ||
| 72 | opt_Number_006 ~ | ||
| 73 | opt_Number_006 ~ Number_005 | ||
| 74 | |||
| 75 | WS ~ [ \t\r\n]+ | ||
| 76 | END_OF_DSL | ||
| 77 | |||
| 78 | my $grammar = Marpa::R2::Scanless::G->new({ | ||
| 79 | source => \$dsl, | ||
| 80 | action_object => 'Actions', | ||
| 81 | default_action => 'default_action' | ||
| 82 | }); | ||
| 83 | my $parser = Marpa::R2::Scanless::R->new({ grammar => $grammar }); | ||
| 84 | |||
| 85 | my $input = <<'__INPUT__'; | ||
| 86 | 2*((5+2)*3) | ||
| 87 | __INPUT__ | ||
| 88 | |||
| 89 | $Actions::generateparsetree = 1; | ||
| 90 | $Actions::traceprocessing = 1; | ||
| 91 | |||
| 92 | $parser->read(\$input); | ||
| 93 | |||
| 94 | my $value_ref = $parser->value(); | ||
| 95 | |||
| 96 | print Dumper($$value_ref); | ||
| 97 | |||
| 98 | exit 0; | ||
| 99 | |||
| 100 | # ------------------------------------------------------------------------------------ # | ||
| 101 | # Example grammar actions # | ||
| 102 | # ------------------------------------------------------------------------------------ # | ||
| 103 | |||
| 104 | package Actions; | ||
| 105 | use strict; | ||
| 106 | |||
| 107 | sub new | ||
| 108 | { | ||
| 109 | my ($class) = @_; | ||
| 110 | return bless {}, $class; | ||
| 111 | } | ||
| 112 | |||
| 113 | sub trace | ||
| 114 | { | ||
| 115 | my ($self, $function, $direction, $data) = @_; | ||
| 116 | |||
| 117 | return if !$Actions::traceprocessing; | ||
| 118 | |||
| 119 | printf "%1s %-8s", $direction, $function; | ||
| 120 | print Dumper($data); | ||
| 121 | printf "\n"; | ||
| 122 | } | ||
| 123 | |||
| 124 | sub do_add | ||
| 125 | { | ||
| 126 | my ($self, @items) = @_; | ||
| 127 | |||
| 128 | return \@items if $Actions::generateparsetree; | ||
| 129 | |||
| 130 | $self->trace('do_add', '<', \@items); | ||
| 131 | |||
| 132 | my $result = $items[0]; | ||
| 133 | shift @items; | ||
| 134 | |||
| 135 | if (!scalar @{$items[0]}) | ||
| 136 | { | ||
| 137 | $self->trace('do_add', '>', $result); | ||
| 138 | return $result; | ||
| 139 | } | ||
| 140 | |||
| 141 | for my $item (@items) | ||
| 142 | { | ||
| 143 | my $op = $item->[0]; | ||
| 144 | my $opnd = $item->[1]; | ||
| 145 | $opnd = $opnd->[1] if ref $opnd eq "ARRAY" && $opnd->[0] eq "("; | ||
| 146 | SWITCH: { | ||
| 147 | ($op eq "+") && do { $result += $opnd; last SWITCH; }; | ||
| 148 | ($op eq "-") && do { $result -= $opnd; last SWITCH; }; | ||
| 149 | } | ||
| 150 | } | ||
| 151 | |||
| 152 | $self->trace('do_add', '>', $result); | ||
| 153 | |||
| 154 | return $result; | ||
| 155 | } | ||
| 156 | |||
| 157 | sub do_mult | ||
| 158 | { | ||
| 159 | my ($self, @items) = @_; | ||
| 160 | |||
| 161 | return \@items if $Actions::generateparsetree; | ||
| 162 | |||
| 163 | $self->trace('do_mult', '<', \@items); | ||
| 164 | |||
| 165 | my $result = $items[0]; | ||
| 166 | $result = $result->[1] if ref $result eq "ARRAY" && $result->[0] eq "("; | ||
| 167 | shift @items; | ||
| 168 | if (!scalar @{$items[0]}) | ||
| 169 | { | ||
| 170 | $self->trace('do_mult', '>', $result); | ||
| 171 | return $result; | ||
| 172 | } | ||
| 173 | |||
| 174 | for my $item (@items) | ||
| 175 | { | ||
| 176 | my $op = $item->[0]; | ||
| 177 | my $opnd = $item->[1]; | ||
| 178 | $opnd = $opnd->[1] if ref $opnd eq "ARRAY" && $opnd->[0] eq "("; | ||
| 179 | SWITCH: { | ||
| 180 | ($op eq "*") && do { $result *= $opnd; last SWITCH; }; | ||
| 181 | ($op eq "/") && do { $result /= $opnd; last SWITCH; }; | ||
| 182 | } | ||
| 183 | } | ||
| 184 | |||
| 185 | $self->trace('do_mult', '>', $result); | ||
| 186 | |||
| 187 | return $result; | ||
| 188 | } | ||
| 189 | |||
| 190 | sub default_action | ||
| 191 | { | ||
| 192 | my ($self, @items ) = @_; | ||
| 193 | |||
| 194 | my $result = \@items; | ||
| 195 | |||
| 196 | if (scalar @items == 1) | ||
| 197 | { | ||
| 198 | $result = $items[0]; | ||
| 199 | } | ||
| 200 | else | ||
| 201 | { | ||
| 202 | my $count = 0; | ||
| 203 | my $state = 0; | ||
| 204 | |||
| 205 | for my $item (@items) | ||
| 206 | { | ||
| 207 | if (ref $item ne "ARRAY" || (ref $item eq "ARRAY" && scalar @$item > 0)) | ||
| 208 | { | ||
| 209 | $state = 2 if !$state; | ||
| 210 | ++$count; | ||
| 211 | } | ||
| 212 | $state = 1 if !$state; | ||
| 213 | } | ||
| 214 | |||
| 215 | $result = $items[0] if $state == 2 && $count == 1; | ||
| 216 | } | ||
| 217 | |||
| 218 | return $result; | ||
| 219 | } | ||
| 220 | 1; |