1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
|
package Parser::MGC::Examples::EvaluateExpression 0.23;
=head1 NAME
C<Parser::MGC::Examples::EvaluateExpression> - an example parser to evaluate simple numerical expressions
=head1 DESCRIPTION
This evaluator-parser takes simple mathematical expressions involving the four
basic arithmetic operators (+, -, *, /) applied to integers, and returns the
numerical result. It handles operator precedence, with * and / having a higher
level than + and -, and copes with parentheses.
Operator precedence is implemented by using two different parsing functions to
handle the two different precedence levels.
=cut
=head2 Boilerplate
=for highlighter language=perl
We start off by declaring a package and subclassing L<Parser::MGC>.
package ExprParser;
use base qw( Parser::MGC );
use strict;
use warnings;
=head2 parse
The topmost parsing function, C<parse>, handles the outermost level of
operator precedence, the + and - operators. It first parses a single term from
the input by callling C<parse_term> to obtain its value.
It then uses the C<any_of> structure-forming method to look for either a + or -
operator which would indicate another term will follow it. If it finds either
of these, it parses the next term from after the operator by another call to
C<parse_term> and then adds or subtracts the value of it from the running
total.
The C<any_of> call itself is used as the conditional expression of a C<while>
loop, to ensure it gets called multiple times. Whenever another term has been
parsed, the body function returns a true value, to indicate that the while
loop should be invoked again. Only when there are no more + or - operators,
indicating no more terms, does the body return false, causing the while loop
to stop.
This continues until there are no more + or - operators, when the overall
total value is returned to the caller.
=cut
=pod
sub parse
{
my $self = shift;
my $val = $self->parse_term
1 while $self->any_of(
sub { $self->expect( "+" ); $val += $self->parse_term; 1 },
sub { $self->expect( "-" ); $val -= $self->parse_term; 1 },
sub { 0 },
);
return $val;
}
=cut
=pod
This function recognises input matching the following EBNF grammar:
=for highlighter
EXPR = TERM { ( '+' | '-' ) TERM };
=cut
=head2 parse_term
Called by C<parse>, the next function is C<parse_term> which has a similar
structure. This function implements the next level of operator precedence, of
the * and / operators. In a similar fashion to the previous function, this one
parses a single factor from the input by calling C<parse_factor>, and then
looks for * or / operators, multiplying or dividing the value by the next
factor it expects to find after those. This continues until there are no more
* or / operators, when the overall product is returned.
=cut
=pod
=for highlighter language=perl
sub parse_term
{
my $self = shift;
my $val = $self->parse_factor;
1 while $self->any_of(
sub { $self->expect( "*" ); $val *= $self->parse_factor; 1 },
sub { $self->expect( "/" ); $val /= $self->parse_factor; 1 },
sub { 0 },
);
return $val;
}
=cut
=pod
This function recognises input matching the following EBNF grammar:
TERM = FACTOR { ( '*' | '/' ) FACTOR };
=cut
=head2 parse_factor
Finally, the innermost C<parse_factor> function is called by C<parse_term> to
parse out the actual numerical values. This is also the point at which the
grammar can recurse, recognising a parenthesized expression. It uses an
C<any_of> with two alternative function bodies, to cover these two cases.
The first case, to handle a parenthesized sub-expression, consists of a call
to C<scope_of>. This call would expect to find a C<(> symbol to indicate the
parenthesized expression. If it finds one, it will recurse back to the
toplevel C<parse> method to obtain its value, then expects the final C<)>
symbol. The value of this factor is then the value of the sub-expression
contained within the parentheses.
If the first case fails, because it does not find that leading C<(> symbol,
the second case is attempted instead. This handles an actual integer constant.
This case is simply a call to the C<token_int> method of the underlying class,
which recognises various string forms of integer constants, returning their
numerical value.
=cut
=pod
=for highlighter language=perl
sub parse_factor
{
my $self = shift;
$self->any_of(
sub { $self->scope_of( "(", sub { $self->parse }, ")" ) },
sub { $self->token_int },
);
}
=cut
=pod
This function recognises input matching the following EBNF grammar:
=for highlighter
FACTOR = '(' EXPR ')'
| integer
=cut
=head1 EXAMPLES OF OPERATION
=head2 A single integer
The simplest form of operation of this parser is when it is given a single
integer value as its input; for example C<"15">.
INPUT: 15
POSITION: ^
The outermost call to C<parse> will call C<parse_term>, which in turn calls
C<parse_factor>.
INPUT: 15
POSITION ^
CALLS: parse
=> parse_term
=> parse_factor
The C<any_of> inside C<parse_factor> will first attempt to find a
parenthesized sub-expression by using C<scope_of>, but this will fail because
it does not start with an open parenthesis symbol. The C<any_of> will then
attempt the second case, calling C<token_int> which will succeed at obtaining
an integer value from the input stream, consuming it by advancing the stream
position. The value of 15 is then returned by C<parse_factor> back to
C<parse_term> where it is stored in the C<$val> lexical.
INPUT: 15
POSITION: ^
CALLS: parse
=> parse_term -- $val = 15
At this point, the C<any_of> inside C<parse_term> will attempt to find a * or
/ operator, but both will fail because there is none, causing the final
alternative function to be invoked, which stops the C<while> loop executing.
The value of 15 is then returned to the outer caller, C<parse>. A similar
process happens there, where it fails to find a + or - operator, and thus the
final value of 15 is returned as the result of the entire parsing operation.
INPUT: 15
OUTPUT: 15
=head2 A simple sum of two integers
Next lets consider a case that actually requires some real parsing, such as an
expression requesting the sum of two values; C<"6 + 9">.
INPUT: 6 + 9
POSITION: ^
This parsing operation starts the same as the previous; with C<parse> calling
C<parse_term> which in turn calls C<parse_factor>.
INPUT: 6 + 9
POSITION: ^
CALLS: parse
=> parse_term
=> parse_factor
As before, the C<any_of> inside C<parse_factor> first attempts and fails to
find a parenthesized sub-expression and so tries C<token_int> instead. As
before this obtains an integer value from the stream and advances the
position. This value is again returned to C<parse_term>. As before, the
C<any_of> attempts but fails to find a * or / operator so the value gets
returned to C<parse> to be stored in C<$val>.
INPUT: 6 + 9
POSITION: ^
CALLS: parse -- $val = 6
This time, the C<any_of> in the outer C<parse> method attempts to find a +
operator and succeeds, because there is one at the next position in the
stream. This causes the first case to continue, making another call to
C<parse_term>.
INPUT: 6 + 9
POSITION: ^
CALLS: parse -- $val = 6
=> parse_term
This call to C<parse_term> proceeds much like the first, eventually returning
the value 9 by consuming it from the input stream. This value is added to
C<$val> by the code inside the C<any_of> call.
INPUT: 6 + 9
POSITION: ^
CALLS: parse -- $val = 15
C<parse> then calls C<any_of> a second time, which attempts to find another
operator. This time there is none, so it returns false, which stops the
C<while> loop and the value is returned as the final result of the operation.
INPUT: 6 + 9
OUTPUT: 15
=head2 Operator precedence
The two kinds of operators (+ and - vs * and /) are split across two different
method calls to allow them to implement precedence; to say that some of the
operators bind more tightly than others. Those operators that are implemented
in more inwardly-nested functions bind tighter than the ones implemented
further out.
To see this in operation consider an expression that mixes the two kinds of
operators, such as C<"15 - 2 * 3">
INPUT: 15 - 2 * 3
POSITION: ^
The parsing operation starts by calling down from C<parse> all the way to
C<token_int> which extracts the first integer, 15, from the stream and returns
it all the way up to C<parse> as before:
INPUT: 15 - 2 * 3
POSITION: ^
CALLS: parse -- $val = 15
As before, the C<parse> function looks for a * or - operator by its C<any_of>
test, and finds this time the - operator, which then causes it to call
C<parse_term> to parse its value:
INPUT: 15 - 2 * 3
POSITION: ^
CALLS: parse -- $val = 15
=> parse_term
Again, C<parse_term> starts by calling C<parse_factor> which extracts the next
integer from the stream and returns it. C<parse_factor> temporarily stores
that in its own C<$val> lexical (which remember, is a lexical variable local
to that call, so is distinct from the one in C<parse>).
INPUT: 15 - 2 * 3
POSITION: ^
CALLS: parse -- $val = 15
=> parse_term -- $val = 2
This time, when C<parse_term> attempts its own C<any_of> test to look for a *
or / operator, it manages to find one. By a process similar to the way that
the outer C<parse> method forms a sum of terms, C<parse_term> forms a product
of factors by calling down to C<parse_factor> and accumulating the result.
Here it will call C<parse_factor> again, which returns the value 3. This gets
multiplied into C<$var>.
INPUT: 15 - 2 * 3
POSITION: ^
CALLS: parse -- $val = 15
=> parse_term -- $val = 6
C<parse_term> will try again to look for a * or / operator, but this time
fails to find one, and so returns its final result, 6, back to C<parse>, which
then subtracts it from its own C<$val>.
INPUT: 15 - 2 * 3
POSITION: ^
CALLS: parse -- $val = 9
The outer C<parse> call similarly fails to find any more + or - operators and
so returns the final result of the parsing operation.
INPUT: 15 - 2 * 3
OUTPUT: 9
By implementing the * and / operators separately in a different piece of logic
inside the one that implements the + and - operators, we have ensured that
they operate more greedily. That is, that they bind tighter, consuming their
values first, before the outer + and - operators. This is the way that
operator precedence is implemented.
=head2 Parentheses
This grammar, like many others, provides a way for expressions to override the
usual operator precedence by supplying a sub-expression in parentheses. The
expression inside those parentheses is parsed in the usual way, and then its
result stands in place of the entire parenthesized part, overriding whatever
rules might have governed the order between those operators inside it and
those outside.
In this parser we implement this as a recursive call, where one possibility
of the innermost part (the C<parse_factor> function or the C<FACTOR> EBNF
rule) is to recurse back to the outermost thing, inside parentheses. This
example examines what happens to the input string C<"(15 - 2) * 3">.
INPUT: (15 - 2) * 3
POSITION: ^
As with all the other examples the parsing operation starts by C<parse>
calling C<parse_term> which calls C<parse_factor>. This time, the first case
within the C<any_of> in C<parse_factor> does successfully manage to find an
open parenthesis, so consumes it. It then stores the close parenthesis pattern
as the end-of-scope marker, and makes a recursive call back to the parse
method again.
INPUT: (15 - 2) * 3
POSITION: ^
CALLS: parse
=> parse_term
=> parse_factor
=> parse EOS = ")"
The operation of the inner call to C<parse> proceeds much like the first few
examples, calling down through C<parse_term> to C<parse_factor> to obtain
the 15.
INPUT: (15 - 2) * 3
POSITION: ^
CALLS: parse
=> parse_term
=> parse_factor
=> parse -- $val = 15 EOS = ")"
Similar to previous examples, this then finds the - operator, and parses
another term to subtract from it.
INPUT: (15 - 2) * 3
POSITION: ^
CALLS: parse
=> parse_term
=> parse_factor
=> parse -- $val = 13 EOS = ")"
At this point, the C<any_of> test in the inner call to C<parse> tries again to
look for a + or - operator, and this time fails because it believes it is at
the end of the input. It isn't really at the end of the string, of course, but
it believes it to be at the end because of the "end-of-scope" pattern that the
call to C<scope_of> established. This pretends that the input has finished
whenever the next part of the input matches the end-of-scope pattern.
Because this inner call to C<parse> now believes it has got to the end of its
input, it returns its final answer back to the caller, which in this case was
the C<scope_of> call that C<parse_factor> made. As the C<scope_of> call
returns, it consumes the input matching the end-of-scope pattern. This return
value is then stored by C<parse_term>.
INPUT: (15 - 2) * 3
POSITION: ^
CALLS: parse
=> parse_term -- $val = 13
At this point, C<parse_term> proceeds as before, finding and extracting the *
operator and calling C<parse_factor> a second time, multiplying them together
and returning that to the outer C<parse> call.
INPUT: (15 - 2) * 3
POSITION: ^
CALLS: parse -- $val = 39
At this point C<parse> fails to extract any more operators because it is at
the (real) end of input, so returns the final answer.
INPUT: (15 - 2) * 3
OUTPUT: 39
=head1 AUTHOR
Paul Evans <leonerd@leonerd.org.uk>
=cut
|