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 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063
|
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<meta name="GENERATOR" content="Microsoft FrontPage 2.0">
<title>Geyacc Some Simple Examples</title>
</head>
<body bgcolor="#FFFFFF">
<table border="0" width="100%">
<tr>
<td><font size="6"><strong>Some Simple Examples</strong></font></td>
<td align="right"><a href="stages.html"><img
src="../image/previous.gif" alt="Previous" border="0"
width="40" height="40"></a><a href="description.html"><img
src="../image/next.gif" alt="Next" border="0" width="40"
height="40"></a></td>
</tr>
</table>
<hr size="1">
<p>First some simple examples to get the flavor of how one uses <em>geyacc</em>:
a reverse polish notation calculator, an algebraic (infix)
notation calculator and a calculator with memory. Each produces a
usable, though limited, interactive desk-top calculator. These
examples are simple, but <em>geyacc</em> grammars for real
programming languages are written the same way. </p>
<h2>Reverse Polish Notation Calculator</h2>
<p>The first example is that of a simple double-precision <em>reverse
polish notation</em> calculator (a calculator using postfix
operators). This example provides a good starting point, since
operator precedence is not an issue. The second example will
illustrate how operator precedence is handled.</p>
<p>The source code for this calculator can be found in <font
color="#800000"><tt>$GOBO\example\parse\rpcalc</tt></font>. The <em>geyacc</em>
grammar file is named <font color="#800000"><tt>rpcalc.y</tt></font>.
The `<font color="#800000"><tt>.y</tt></font>' extension is a
convention used for <em>geyacc</em> input files. </p>
<h3>Declarations for <tt>rpcalc</tt></h3>
<p>Here are the Eiffel and <em>geyacc</em> declarations for the
reverse polish notation calculator. Comments follow Eiffel style
conventions and have no effect on the description's semantics.</p>
<blockquote>
<pre><font color="#0000FF">%{
</font><font color="#008080"><em><strong>indexing</strong></em><em>
description</em>:<em> "Reverse polish notation calculator"
</em><em><strong>class</strong></em><em> RPCALC
</em><em><strong>inherit</strong></em><em>
YY_PARSER_SKELETON </em>[<em>DOUBLE</em>]<em>
</em><em><strong>creation</strong></em><em>
make
</em></font><font color="#0000FF">
%}
%token </font><font color="#FF0000">NUM</font><font
color="#0000FF">
%% </font><font color="#008080">-- Grammar rules and actions follow.</font></pre>
</blockquote>
<p>The Eiffel declarations subsection specifies the indexing,
class header, inherit and creation clauses of the generated
parser class. This class is called <font color="#008080"><em><tt>RPCALC</tt></em></font>
and inherits its parsing engine from <font color="#008080"><em><tt>YY_PARSER_SKELETON</tt></em></font>.
Since the generic parameter of class <font color="#008080"><em><tt>YY_PARSER_SKELETON</tt></em></font><font
color="#008080" size="2" face="Courier New"><em> </em></font>is <font
color="#008080"><em><tt>DOUBLE</tt></em></font>, the semantic
values handled by the calculator are double precision real
numbers.</p>
<p>The second subsection, <a href="declarations.html"><em>geyacc</em>
declarations</a>, provides information to <em>geyacc</em> about
the token types. Each terminal symbol that is not a
single-character literal must be declared here. (Single-character
literals normally don't need to be declared.) In this example,
all the arithmetic operators are designated by single-character
literals, so the only terminal symbol that needs to be declared
is <font color="#FF0000"><tt>NUM</tt></font>, the token type for
numeric constants.</p>
<h3>Grammar Rules for <tt>rpcalc</tt></h3>
<p>Here are the grammar rules for the reverse polish notation
calculator:</p>
<blockquote>
<pre><font color="#800080">input</font><font color="#0000FF">: </font><font
color="#008080">-- Empty</font><font color="#0000FF">
| </font><font color="#800080">input line</font><font
color="#0000FF">
;
</font><font color="#800080">line</font><font color="#0000FF">: </font><font
color="#FF0000">'\n'</font><font color="#0000FF">
| </font><font color="#800080">exp</font><font
color="#0000FF"> </font><font color="#FF0000">'\n'</font><font
color="#0000FF"> { </font><font color="#008080"><em>print</em> (</font><font
color="#0000FF">$1</font><font color="#008080">); <em>print</em> (<em>'%N'</em>)</font><font
color="#0000FF"> }
;
</font><font color="#800080">exp</font><font color="#0000FF">: </font><font
color="#FF0000">NUM </font><font color="#0000FF">{ $$ </font><font
color="#008080">:=</font><font color="#0000FF"> $1 }
| </font><font color="#800080">exp exp</font><font
color="#0000FF"> </font><font color="#FF0000">'+'</font><font
color="#0000FF"> { $$ </font><font color="#008080">:=</font><font
color="#0000FF"> $1</font><font color="#008080"> +</font><font
color="#0000FF"> $2 }
| </font><font color="#800080">exp exp</font><font
color="#0000FF"> </font><font color="#FF0000">'-'</font><font
color="#0000FF"> { $$ </font><font color="#008080">:=</font><font
color="#0000FF"> $1 </font><font color="#008080">-</font><font
color="#0000FF"> $2 }
| </font><font color="#800080">exp exp </font><font
color="#FF0000">'*'</font><font color="#0000FF"> { $$ </font><font
color="#008080">:=</font><font color="#0000FF"> $1 </font><font
color="#008080">*</font><font color="#0000FF"> $2 }
| </font><font color="#800080">exp exp</font><font
color="#0000FF"> </font><font color="#FF0000">'/'</font><font
color="#0000FF"> { $$ := $1 </font><font color="#008080">/</font><font
color="#0000FF"> $2 }
</font><font color="#008080">-- Unary minus</font><font
color="#0000FF">
| </font><font color="#800080">exp</font><font
color="#0000FF"> </font><font color="#FF0000">'n'</font><font
color="#0000FF"> { $$ </font><font color="#008080">:= -</font><font
color="#0000FF">$1 }
;
%%</font></pre>
</blockquote>
<p>The groupings of the <tt>rpcalc</tt> "language"
defined here are the expression (given the name <font
color="#800080"><tt>exp</tt></font>), the line of input (<font
color="#800080"><tt>line</tt></font>), and the complete input
transcript (<font color="#800080"><tt>input</tt></font>). Each of
these nonterminal symbols has several alternate rules, joined by
the<font color="#0000FF" size="2" face="Courier New"> </font><font
color="#0000FF"><tt>|</tt></font> punctuator which is read as
"or". The following sections explain what these rules
mean.</p>
<p>The semantics of the language is determined by the <a
href="actions.html">actions</a> taken when a grouping is
recognized. The actions are the Eiffel code that appears inside
braces. You must specify these actions in Eiffel, but <em>geyacc</em>
provides the means for passing <a
href="introduction.html#semantic_values">semantic values</a>
between the rules. In each action, the pseudo-variable <font
color="#0000FF"><tt>$$</tt></font> stands for the semantic value
for the grouping that the rule is going to construct. Assigning a
value to <font color="#0000FF"><tt>$$</tt></font> is the main job
of most actions. The semantic values of the components of the
rule are referred to as <font color="#0000FF"><tt>$1</tt></font>,
<font color="#0000FF"><tt>$2</tt></font>, and so on.</p>
<h4>Explanation of <tt>input</tt></h4>
<p>Consider the definition of <font color="#800080"><tt>input</tt></font>:</p>
<blockquote>
<pre><font color="#800080">input</font><font color="#0000FF">: </font><font
color="#008080">-- Empty</font><font color="#0000FF">
| </font><font color="#800080">input line</font><font
color="#0000FF">
;</font></pre>
</blockquote>
<p>This definition reads as follows: "A complete input is
either an empty string, or a complete input followed by an input
line". Notice that "complete input" is defined in
terms of itself. This definition is said to be <a
href="rules.html#recursive"><em>left recursive</em></a> since <font
color="#800080" size="2" face="Courier New">input</font> appears
always as the leftmost symbol in the sequence.</p>
<p>The first alternative is empty because there are no symbols
between the colon and the first <font color="#0000FF"><tt>|</tt></font>;
this means that <font color="#800080"><tt>input</tt></font> can
match an empty string of input (no tokens). We write the rules
this way because it is legitimate to type `Ctrl-d' right after
you start the calculator. It's conventional to put an empty
alternative first and write the comment <font color="#008080"><tt>--
Empty </tt></font>in it.</p>
<p>The second alternate rule (<font color="#800080"><tt>input
line</tt></font>) handles all nontrivial input. It means,
"After reading any number of lines, read one more line if
possible". The left recursion makes this rule into a loop.
Since the first alternative matches empty input, the loop can be
executed zero or more times.</p>
<p>The parser routine <font color="#008080"><em><tt>parse</tt></em></font>
continues to process input until a grammatical error is seen or
the lexical analyzer says there are no more input tokens; we will
arrange for the latter to happen at end of file.</p>
<h4>Explanation of <tt>line</tt></h4>
<p>Now consider the definition of <font color="#800080"><tt>line</tt></font>:</p>
<blockquote>
<pre><font color="#800080">line</font><font color="#0000FF">: </font><font
color="#FF0000">'\n'</font><font color="#0000FF">
| </font><font color="#800080">exp</font><font
color="#0000FF"> </font><font color="#FF0000">'\n'</font><font
color="#0000FF"> { </font><font color="#008080"><em>print</em> (</font><font
color="#0000FF">$1</font><font color="#008080">); <em>print</em> (<em>'%N'</em>)</font><font
color="#0000FF"> }
;</font></pre>
</blockquote>
<p>The first alternative is a token which is a newline character;
this means that <tt>rpcalc </tt>accepts a blank line (and ignores
it, since there is no action). The second alternative is an
expression followed by a newline. This is the alternative that
makes <tt>rpcalc</tt> useful. The semantic value of the <font
color="#800080"><tt>exp</tt></font> grouping is the value of <font
color="#0000FF"><tt>$1</tt></font> because the <font
color="#800080"><tt>exp</tt></font> in question is the first
symbol in the alternative. The action prints this value, which is
the result of the computation the user asked for.</p>
<p>This action is unusual because it does not assign a value to <font
color="#0000FF"><tt>$$</tt></font>. As a consequence, the
semantic value associated with the <font color="#800080"><tt>line</tt></font>
is initialized to its default value. This would be a bug if that
value were ever used, but we don't use it: once <tt>rpcalc</tt>
has printed the value of the user's input line, that value is no
longer needed.</p>
<h4>Explanation of <tt>expr</tt></h4>
<p>The <font color="#800080"><tt>exp</tt></font> grouping has
several rules, one for each kind of expression. The first rule
handles the simplest expressions: those that are just numbers.
The second handles an addition-expression, which looks like two
expressions followed by a plus-sign. The third handles
subtraction, and so on.</p>
<blockquote>
<pre><font color="#800080">exp</font><font color="#0000FF">: </font><font
color="#FF0000">NUM </font><font color="#0000FF">{ $$ </font><font
color="#008080">:=</font><font color="#0000FF"> $1 }
| </font><font color="#800080">exp exp</font><font
color="#0000FF"> </font><font color="#FF0000">'+'</font><font
color="#0000FF"> { $$ </font><font color="#008080">:=</font><font
color="#0000FF"> $1</font><font color="#008080"> +</font><font
color="#0000FF"> $2 }
| </font><font color="#800080">exp exp</font><font
color="#0000FF"> </font><font color="#FF0000">'-'</font><font
color="#0000FF"> { $$ </font><font color="#008080">:=</font><font
color="#0000FF"> $1 </font><font color="#008080">-</font><font
color="#0000FF"> $2 }
</font>...<font color="#0000FF">
;</font></pre>
</blockquote>
<p>We have used <font color="#0000FF"><tt>|</tt></font> to join
all the rules for <font color="#800080"><tt>exp</tt></font>, but
we could equally well have written them separately:</p>
<blockquote>
<pre><font color="#800080">exp</font><font color="#0000FF">: </font><font
color="#FF0000">NUM </font><font color="#0000FF">{ $$ </font><font
color="#008080">:=</font><font color="#0000FF"> $1 } ;
</font><font color="#800080">exp</font><font color="#0000FF">: </font><font
color="#800080">exp exp</font><font color="#0000FF"> </font><font
color="#FF0000">'+'</font><font color="#0000FF"> { $$ </font><font
color="#008080">:=</font><font color="#0000FF"> $1</font><font
color="#008080"> +</font><font color="#0000FF"> $2 } ;
</font><font color="#800080">exp</font><font color="#0000FF">: </font><font
color="#800080">exp exp</font><font color="#0000FF"> </font><font
color="#FF0000">'-'</font><font color="#0000FF"> { $$ </font><font
color="#008080">:=</font><font color="#0000FF"> $1 </font><font
color="#008080">-</font><font color="#0000FF"> $2 } ;</font>
...</pre>
</blockquote>
<p>Most of the rules have <a href="actions.html">actions</a> that
compute the value of the expression in terms of the value of its
parts. For example, in the rule for addition, <font
color="#0000FF"><tt>$1</tt></font> refers to the first component <font
color="#800080"><tt>exp</tt></font> and <font color="#0000FF"><tt>$2</tt></font>
refers to the second one. The third component, <font
color="#FF0000"><tt>'+'</tt></font>, has no meaningful associated
semantic value, but if it had one you could refer to it as <font
color="#0000FF"><tt>$3</tt></font>. When <font color="#008080"><em><tt>parse</tt></em></font>
recognizes a sum expression using this rule, the sum of the two
subexpressions' values is produced as the value of the entire
expression.</p>
<p>The formatting shown here is the recommended convention, but <em>geyacc</em>
does not require it. You can add or change whitespace as much as
you wish. For example, this:</p>
<blockquote>
<pre><font color="#800080">exp</font><font color="#0000FF"> : </font><font
color="#FF0000">NUM </font><font color="#0000FF">{ $$ </font><font
color="#008080">:=</font><font color="#0000FF"> $1 } | </font><font
color="#800080">exp exp</font><font color="#0000FF"> </font><font
color="#FF0000">'+'</font><font color="#0000FF"> {$$</font><font
color="#008080"> :=</font><font color="#0000FF"> $1 </font><font
color="#008080">+</font><font color="#0000FF"> $2 } |</font> ...</pre>
</blockquote>
<p>means the same thing as this:</p>
<blockquote>
<pre><font color="#800080">exp</font><font color="#0000FF">: </font><font
color="#FF0000">NUM </font><font color="#0000FF">{ $$ </font><font
color="#008080">:=</font><font color="#0000FF"> $1 }
| </font><font color="#800080">exp exp</font><font
color="#0000FF"> </font><font color="#FF0000">'+'</font><font
color="#0000FF"> { $$</font><font color="#008080"> :=</font><font
color="#0000FF"> $1 </font><font color="#008080">+</font><font
color="#0000FF"> $2 }
|</font> ...</pre>
</blockquote>
<p>The latter, however, is much more readable.</p>
<h3>The <tt>rpcalc</tt> Lexical Analyzer</h3>
<p>The <a href="parser.html#lexical_analyzer">lexical analyzer</a>'s
job is low-level parsing: converting characters or sequences of
characters into tokens. The <em>geyacc</em> parser gets its
tokens by calling the lexical analyzer.</p>
<p>Only a simple lexical analyzer is needed for the <font
size="2">RPN</font> calculator. This lexical analyzer skips
blanks and tabs, then reads in numbers as <font color="#008080"><em><tt>DOUBLE</tt></em></font>
and returns them as <font color="#FF0000"><tt>NUM</tt></font>
tokens. Any other character that isn't part of a number is a
separate token. Note that the token-code for such a
single-character token is the character itself.</p>
<p>The lexical analyzer routine <font color="#008080"><em><tt>read_token</tt></em></font>
stores into <font color="#008080"><em><tt>last_token</tt></em></font><font
color="#008080" size="2" face="Courier New"><em> </em></font>a
numeric code which represents a token type. The same text used in
<em>geyacc</em> rules to stand for this token type is also an
Eiffel expression for the numeric code for the type. This works
in two ways. If the token type is a character literal, then its
numeric code is the <font size="2">ASCII</font> code for that
character; the same character literal can be used in the lexical
analyzer to express the number. If the token type is an
identifier, that identifier is defined by <em>geyacc</em> as an
integer constant feature with the appropriate number. In this
example, therefore, <font color="#FF0000"><tt>NUM</tt></font>
becomes an integer constant for <font color="#008080"><em><tt>read_token</tt></em></font>
to use.</p>
<p>The semantic value of the token (if it has one) is made
available in <font color="#008080"><em><tt>last_value</tt></em></font>,
which is where the <em>geyacc</em> parser will look for it.</p>
<p>A token type code of zero is returned if the end-of-file is
encountered. (<em>Geyacc</em> recognizes any nonpositive value as
indicating that an error occurred in the lexical analysis.)</p>
<p>Here is the code for the lexical analyzer routine:</p>
<blockquote>
<pre><font color="#008080"><em>read_token </em><em><strong>is</strong></em><em>
</em>-- Lexical analyzer returns a double floating point
-- number on the stack and the token NUM, or the ASCII
-- character read if not a number. Skips all blanks
-- and tabs, returns 0 for EOF.<em>
</em><em><strong>local</strong></em><em>
c</em>:<em> CHARACTER
buffer</em>:<em> STRING
</em><em><strong>do</strong></em><em>
</em>-- Skip white space<em>
</em><em><strong>from
if </strong></em><em>has_pending_character</em><em><strong> then
</strong></em><em>c </em>:=<em><strong> </strong></em><em>pending_character</em><em><strong>
</strong></em><em>has_pending_character</em><em><strong> </strong></em>:=<em><strong> </strong></em><em>False</em><em><strong>
elseif not </strong></em><em>io.end_of_file</em><em><strong> then</strong></em><em>
io</em>.<em>read_character
c </em>:=<em> io</em>.<em>last_character
</em><em><strong>end</strong></em><em>
</em><em><strong>until</strong></em><em>
io.end_of_file </em><em><strong>or else</strong></em><em>
</em>(<em>c </em>/=<em> ' ' </em><em><strong>and</strong></em><em> c </em>/=<em> '%T'</em>)<em>
</em><em><strong>loop</strong></em><em>
io.read_character
c </em>:=<em> io.last_character
</em><em><strong>end</strong></em><em>
</em><em><strong>if</strong></em><em> io.end_of_file </em><em><strong>then</strong></em><em>
</em>-- Return end-of-file<em>
last_token </em>:=<em> 0
</em><em><strong>elseif</strong></em><em> </em>(<em>c </em>>=<em> '0' </em><em><strong>and</strong></em><em> c </em><=<em> '9'</em>)<em> </em><em><strong>then</strong></em><em>
</em>-- Process numbers<em>
last_token </em>:=<em> NUM
</em><em><strong>from</strong></em><em>
</em>!!<em> buffer.make </em>(<em>10</em>)<em>
buffer.append_character </em>(<em>c</em>)<em>
io.read_character
c </em>:= <em>io.last_character
</em><em><strong>until</strong></em><em>
io.end_of_file </em><em><strong>or else</strong></em><em>
</em>(<em>c </em><<em> '0' </em><em><strong>or</strong></em><em> c </em>><em> '9'</em>)<em>
</em><em><strong>loop</strong></em><em>
buffer.append_character </em>(<em>c</em>)<em>
io.read_character
c </em>:=<em> io.last_character
</em><em><strong>end</strong></em><em>
</em><em><strong>if not</strong></em><em> io.end_of_file </em><em><strong>and then</strong></em><em> c = '.' </em><em><strong>then</strong></em><em>
</em><em><strong>from</strong></em><em>
buffer.append_character </em>(<em>'.'</em>)<em>
io.read_character
c </em>:= <em>io.last_character
</em><em><strong>until</strong></em><em>
io.end_of_file </em><em><strong>or else</strong></em><em>
</em>(<em>c </em><<em> '0' </em><em><strong>or</strong></em><em> c </em>><em> '9'</em>)<em>
</em><em><strong>loop</strong></em><em>
buffer.append_character </em>(<em>c</em>)<em>
io.read_character
c </em>:=<em> io.last_character
</em><em><strong>end</strong></em><em>
</em><em><strong>end</strong></em><em>
</em><em><strong>if</strong></em><em> </em><em><strong>not</strong></em><em> io.end_of_file </em><em><strong>then</strong></em><em>
pending_character </em>:=<em> c
has_pending_character </em>:=<em> True
</em><em><strong>end</strong></em><em>
last_value </em>:=<em> buffer.to_double
</em><em><strong>else</strong></em><em>
</em>-- Return single character<em>
last_token </em>:= <em>c.code
</em><em><strong>end</strong></em><em>
</em><em><strong>end</strong></em></font></pre>
</blockquote>
<h3>The Error Reporting Routine</h3>
<p>When <font color="#008080"><em><tt>parse</tt></em></font>
detects a syntax error, it calls the error reporting routine <font
color="#008080"><em><tt>report_error</tt></em></font> to print an
error message (usually but not always "parse error").
It is up to the programmer to redefine <font color="#008080"><em><tt>report_error</tt></em></font>
when needed.</p>
<p>After <font color="#008080"><em><tt>report_error</tt></em></font>
returns, the <em>geyacc</em> parser may recover from the error
and continue parsing if the grammar contains a suitable <a
href="error.html">error rule</a>. Otherwise, <font
color="#008080"><em><tt>parse</tt></em></font> terminates with <font
color="#008080"><em><tt>syntax_error</tt></em></font> being set
to true. There is no error rules in this example, so any invalid
input will cause the calculator program to exit. This is not
clean behavior for a real calculator, but it is adequate in the
first example.</p>
<h2>Infix Notation Calculator: <tt>calc</tt></h2>
<p>We now modify <tt>rpcalc</tt> to handle infix operators
instead of postfix. Infix notation involves the concept of
operator precedence and the need for parentheses nested to
arbitrary depth. Here is the <em>geyacc</em> code for<font
color="#800000" size="2" face="Courier New"> </font><font
color="#800000"><tt>calc.y</tt></font>, an infix desk-top
calculator (the source code for this calculator can be found in <font
color="#800000"><tt>$GOBO\example\parse\calc</tt></font>).</p>
<blockquote>
<pre><font color="#0000FF">%{
</font><font color="#008080"><em><strong>indexing</strong></em><em>
description</em>:<em> "Infix notation calculator"
</em><em><strong>class</strong></em><em> CALC
</em><em><strong>inherit</strong></em><em>
YY_PARSER_SKELETON </em>[<em>DOUBLE</em>]<em>
</em><em><strong>creation</strong></em><em>
make
</em></font><font color="#0000FF">
%}</font>
<font color="#008080">-- geyacc declarations.</font>
<font color="#0000FF">%token </font><font color="#FF0000">NUM</font><font
color="#0000FF">
%left </font><font color="#FF0000">'-' '+'</font><font
color="#0000FF">
%left </font><font color="#FF0000">'*' '/'</font><font
color="#0000FF">
%left </font><font color="#FF0000">NEG</font><font
color="#0000FF"> </font><font color="#008080">-- negation--unary minus</font><font
color="#0000FF">
%right </font><font color="#FF0000">'^'</font><font
color="#0000FF"> </font><font color="#008080">-- exponentiation</font><font
color="#0000FF">
</font><font color="#008080">-- Grammar follows.</font><font
color="#0000FF">
%%
</font><font color="#800080">input</font><font color="#0000FF">: </font><font
color="#008080">-- Empty</font><font color="#0000FF">
| </font><font color="#800080">input line</font><font
color="#0000FF">
;
</font><font color="#800080">line</font><font color="#0000FF">: </font><font
color="#FF0000">'\n'</font><font color="#0000FF">
| </font><font color="#800080">exp</font><font
color="#0000FF"> </font><font color="#FF0000">'\n'</font><font
color="#0000FF"> { </font><font color="#008080"><em>print</em> (</font><font
color="#0000FF">$1</font><font color="#008080">); <em>print</em> (<em>'%N'</em>)</font><font
color="#0000FF"> }
;
</font><font color="#800080">exp</font><font color="#0000FF">: </font><font
color="#FF0000">NUM </font><font color="#0000FF">{ $$ </font><font
color="#008080">:=</font><font color="#0000FF"> $1 }
| </font><font color="#800080">exp </font><font
color="#FF0000">'+'</font><font color="#800080"> exp</font><font
color="#0000FF"> { $$ </font><font color="#008080">:=</font><font
color="#0000FF"> $1 </font><font color="#008080">+</font><font
color="#0000FF"> $3 }
| </font><font color="#800080">exp </font><font
color="#FF0000">'-' </font><font color="#800080">exp</font><font
color="#0000FF"> { $$ </font><font color="#008080">:=</font><font
color="#0000FF"> $1 </font><font color="#008080">-</font><font
color="#0000FF"> $3 }
| </font><font color="#800080">exp</font><font
color="#0000FF"> </font><font color="#FF0000">'*'</font><font
color="#0000FF"> </font><font color="#800080">exp</font><font
color="#0000FF"> { $$ </font><font color="#008080">:=</font><font
color="#0000FF"> $1 </font><font color="#008080">*</font><font
color="#0000FF"> $3 }
| </font><font color="#800080">exp</font><font
color="#0000FF"> </font><font color="#FF0000">'/'</font><font
color="#0000FF"> </font><font color="#800080">exp</font><font
color="#0000FF"> { $$ </font><font color="#008080">:=</font><font
color="#0000FF"> $1 </font><font color="#008080">/</font><font
color="#0000FF"> $3 }
| </font><font color="#FF0000">'-'</font><font
color="#0000FF"> </font><font color="#800080">exp</font><font
color="#0000FF"> %prec </font><font color="#FF0000">NEG</font><font
color="#0000FF"> { $$ </font><font color="#008080">:= -</font><font
color="#0000FF">$2 }
| </font><font color="#FF0000">'(' </font><font
color="#800080">exp</font><font color="#FF0000"> ')'</font><font
color="#0000FF"> { $$ </font><font color="#008080">:=</font><font
color="#0000FF"> $2 }
;
%%
</font><font color="#008080"><em><strong>feature</strong></em>
<em>read_token</em> <em><strong>is</strong></em>
<em><strong>do</strong></em>
...
<em><strong>end</strong></em>
...
<em><strong>end</strong></em> -- class CALC</font></pre>
</blockquote>
<p>The routine <font color="#008080"><em><tt>read_token</tt></em></font>
is the same as before. There are two important new features shown
in this code.</p>
<p>In the first section (<em>geyacc</em> declarations), <font
color="#0000FF"><tt>%left</tt></font> declares token types and
says they are left-associative operators. The declarations <font
color="#0000FF"><tt>%left</tt></font> and <font color="#0000FF"><tt>%right</tt></font>
(right associativity) take the place of <font color="#0000FF"><tt>%token</tt></font>
which is used to declare a token type name without associativity.
(These tokens are single-character literals, which ordinarily
don't need to be declared. We declare them here to specify the
associativity.)</p>
<p><a href="precedence.html">Operator precedence</a> is
determined by the line ordering of the declarations; the higher
the line number of the declaration (lower on the page or screen),
the higher the precedence. Hence, exponentiation has the highest
precedence, unary minus (<font color="#FF0000"><tt>NEG</tt></font>)
is next, followed by <font color="#FF0000"><tt>'*'</tt></font>
and <font color="#FF0000"><tt>'/'</tt></font>, and so on.</p>
<p>The other important new feature is the <a
href="precedence.html#context"><font color="#0000FF"><tt>%prec</tt></font></a>
in the grammar section for the unary minus operator. The <font
color="#0000FF"><tt>%prec</tt></font> simply instructs <em>geyacc</em>
that the rule <font color="#FF0000"><tt>'-'</tt></font><tt> </tt><font
color="#800080"><tt>exp</tt></font> has the same precedence as <font
color="#FF0000" face="Times New Roman"><tt>NEG</tt></font><font
color="#FF0000" size="2" face="Courier New"> </font><font
face="Symbol">-</font> in this case the next-to-highest.</p>
<h2>Simple Error Recovery</h2>
<p>Up to this point, this manual has not addressed the issue of <a
href="error.html"><em>error recovery</em></a> <font
face="Courier New">-</font> how to continue parsing after the
parser detects a syntax error. All we have handled is error
reporting with feature <font color="#008080"><em><tt>report_error</tt></em></font>.
Recall that by default <font color="#008080"><em><tt>parse</tt></em></font>
terminates after calling <font color="#008080"><em><tt>report_error</tt></em></font>.
This means that an erroneous input line causes the calculator
program to exit. Now we show how to rectify this deficiency.</p>
<p>The <em>geyacc</em> language itself includes the reserved word
<font color="#0000FF"><tt>error</tt></font>, which may be
included in the grammar rules. In the example below it has been
added to one of the alternatives for <font color="#800080"><tt>line</tt></font>:</p>
<blockquote>
<pre><font color="#800080">line</font><font color="#0000FF">: </font><font
color="#FF0000">'\n'</font><font color="#0000FF">
| </font><font color="#800080">exp</font><font
color="#0000FF"> </font><font color="#FF0000">'\n'</font><font
color="#0000FF"> { </font><font color="#008080"><em>print</em> (</font><font
color="#0000FF">$1</font><font color="#008080">); <em>print</em> (<em>'%N'</em>)</font><font
color="#0000FF"> }
| error </font><font color="#FF0000">'\n'</font><font
color="#0000FF"> { </font><font color="#008080"><em>recover</em></font><font
color="#0000FF"> }
;</font></pre>
</blockquote>
<p>This addition to the grammar allows for simple error recovery
in the event of a parse error. If an expression that cannot be
evaluated is read, the error will be recognized by the third rule
for <font color="#800080"><tt>line</tt></font>, and parsing will
continue. (The <font color="#008080"><em><tt>report_error</tt></em></font>
routine is still called upon to print its message as well.) The
action executes the routine <font color="#008080"><em><tt>recover</tt></em></font>,
a feature inherited from <font color="#008080"><em><tt>YY_PARSER_SKELETON</tt></em></font>;
its meaning is that error recovery is complete.</p>
<p>This form of error recovery deals with syntax errors. There
are other kinds of errors; for example, division by zero, which
raises an exception that is normally fatal. A real calculator
program must handle this kind exception in a rescue clause and
resume parsing input lines; it would also have to discard the
rest of the current line of input.</p>
<h2>Calculator with Memory: <tt>mcalc</tt></h2>
<p>Now that the basics of <em>geyacc</em> have been discussed, it
is time to move on to a more advanced problem. The above
calculators provided only five functions: <tt>+</tt>, <tt>-</tt>,
<tt>*</tt> , <tt>/</tt> and unary minus. It would be nice to add
memory to the calculator, by allowing you to create named
variables, store values in them, and use them later. Here is a
sample session with the calculator:</p>
<blockquote>
<pre><font color="#808000">% mcalc
pi := 3.141592
3.141592
2 * pi
6.283184
gobo := 2 + 3
5
gobo := gobo * 4
20
gobo
20
%</font></pre>
</blockquote>
<p>The source code for this calculator can be found in <font
color="#800000"><tt>$GOBO\example\parse\mcalc</tt></font>. The <em>geyacc</em>
grammar file is named <font color="#800000"><tt>mcalc.y</tt></font>.
</p>
<h3>Declarations for <tt>mcalc</tt></h3>
<p>Here are the Eiffel and <em>geyacc</em> declarations for the
calculator with memory.</p>
<blockquote>
<pre><font color="#0000FF">%{
</font><font color="#008080"><em><strong>indexing</strong></em><em>
description</em>:<em> "Calculator with memory"
</em><em><strong>class</strong></em><em> MCALC
</em><em><strong>inherit</strong></em><em>
YY_PARSER_SKELETON </em>[<em>ANY</em>]<em>
</em><em><strong>rename</strong></em><em>
make </em><em><strong>as</strong></em><em> make_parser_skeleton
</em><em><strong>end</strong></em><em>
</em><em><strong>creation</strong></em><em>
make
</em></font><font color="#0000FF">
%}</font>
<font color="#008080">-- geyacc declarations.</font>
<font color="#0000FF">%token <</font><font color="#008080"><em>DOUBLE</em></font><font
color="#0000FF">> </font><font color="#FF0000">NUM</font><font
color="#0000FF"> </font><font color="#008080">-- Double precision number</font><font
color="#0000FF">
%token <</font><font color="#008080"><em>STRING</em></font><font
color="#0000FF">> </font><font color="#FF0000">VAR</font><font
color="#008080"> -- Memory name</font><font color="#0000FF">
%type <</font><font color="#008080"><em>DOUBLE</em></font><font
color="#0000FF">> </font><font color="#800080">exp</font><font
color="#0000FF">
%right </font><font color="#FF0000">ASSIGN</font><font
color="#0000FF"> </font><font color="#008080">-- Assignment sign `:=</font><font
color="#0000FF">'
%left </font><font color="#FF0000">'-' '+'</font><font
color="#0000FF">
%left </font><font color="#FF0000">'*' '/'</font><font
color="#0000FF">
%left </font><font color="#FF0000">NEG</font><font
color="#0000FF"> </font><font color="#008080">-- negation--unary minus</font><font
color="#0000FF">
%right </font><font color="#FF0000">'^'</font><font
color="#0000FF"> </font><font color="#008080"> -- exponentiation</font><font
color="#0000FF">
</font><font color="#008080">-- Grammar follows.</font><font
color="#0000FF">
%%</font></pre>
</blockquote>
<p>The above grammar introduces only one new feature of the <em>geyacc</em>
language. This feature allows semantic values to have various
types.</p>
<p>Instead of using <font color="#008080"><em><tt>DOUBLE</tt></em></font>
as generic parameter of the skeleton class, we now use <font
color="#008080"><em><tt>ANY</tt></em></font>, which is a common
ancestor of all possible types. These allowable types are now <font
color="#008080"><em><tt>DOUBLE</tt></em></font> (for <font
color="#800080"><tt>exp</tt></font> and <font color="#FF0000"><tt>NUM</tt></font>)
and <font color="#008080"><em><tt>STRING</tt></em></font> (for
memory names <font color="#FF0000"><tt>VAR</tt></font>).</p>
<p>Since values can now have various types, it is necessary to
associate a type with each grammar symbol whose semantic value is
used. These symbols are <font color="#FF0000"><tt>NUM</tt></font>,
<font color="#FF0000"><tt>VAR</tt></font>, and <font
color="#800080"><tt>exp</tt></font>. Their declarations are
augmented with information about their Eiffel type (placed
between angle brackets).</p>
<p>The <em>geyacc</em> construct <font color="#0000FF"><tt>%type</tt></font>
is used for declaring nonterminal symbols, just as <font
color="#0000FF"><tt>%token</tt></font> is used for declaring
tokens. We have not used <font color="#0000FF"><tt>%type</tt></font>
before because nonterminal symbols are normally declared
implicitly by the rules that define them. But <font
color="#800080"><tt>exp</tt></font> must be declared explicitly
so we can specify its value type. </p>
<h3>Grammar Rules for <tt>mcalc</tt></h3>
<p>Here are the grammar rules for the calculator. Most of them
are copied directly from <tt>calc</tt>; two rules, those which
mention <font color="#FF0000"><tt>VAR</tt></font>, are new.</p>
<blockquote>
<pre><font color="#800080">input</font><font color="#0000FF">: </font><font
color="#008080">-- Empty</font><font color="#0000FF">
| </font><font color="#800080">input line</font><font
color="#0000FF">
;
</font><font color="#800080">line</font><font color="#0000FF">: </font><font
color="#FF0000">'\n'</font><font color="#0000FF">
| </font><font color="#800080">exp</font><font
color="#0000FF"> </font><font color="#FF0000">'\n'</font><font
color="#0000FF"> { </font><font color="#008080"><em>print</em> (</font><font
color="#0000FF">$1</font><font color="#008080">); <em>print</em> (<em>'%N'</em>)</font><font
color="#0000FF"> }
| error </font><font color="#FF0000">'\n'</font><font
color="#0000FF"> { </font><font color="#008080"><em>recover</em></font><font
color="#0000FF"> }
;
</font><font color="#800080">exp</font><font color="#0000FF">: </font><font
color="#FF0000">NUM </font><font color="#0000FF"> { $$ </font><font
color="#008080">:=</font><font color="#0000FF"> $1 }
| </font><font color="#FF0000">VAR</font><font
color="#0000FF"> { $$ </font><font color="#008080">:= <em>memory_value</em> (</font><font
color="#0000FF">$1</font><font color="#008080">)</font><font
color="#0000FF"> }
| </font><font color="#FF0000">VAR</font><font
color="#0000FF"> </font><font color="#FF0000">ASSIGN</font><font
color="#0000FF"> </font><font color="#800080">exp</font><font
color="#0000FF"> { $$ </font><font color="#008080">:= </font><font
color="#0000FF">$3</font><font color="#008080">; <em>set_memory_value</em> (</font><font
color="#0000FF">$$</font><font color="#008080">,</font><font
color="#0000FF"> $1</font><font color="#008080">)</font><font
color="#0000FF"> }
| </font><font color="#800080">exp </font><font
color="#FF0000">'+'</font><font color="#800080"> exp</font><font
color="#0000FF"> { $$ </font><font color="#008080">:=</font><font
color="#0000FF"> $1 </font><font color="#008080">+</font><font
color="#0000FF"> $3 }
| </font><font color="#800080">exp </font><font
color="#FF0000">'-' </font><font color="#800080">exp</font><font
color="#0000FF"> { $$ </font><font color="#008080">:=</font><font
color="#0000FF"> $1 </font><font color="#008080">-</font><font
color="#0000FF"> $3 }
| </font><font color="#800080">exp</font><font
color="#0000FF"> </font><font color="#FF0000">'*'</font><font
color="#0000FF"> </font><font color="#800080">exp</font><font
color="#0000FF"> { $$ </font><font color="#008080">:=</font><font
color="#0000FF"> $1 </font><font color="#008080">*</font><font
color="#0000FF"> $3 }
| </font><font color="#800080">exp</font><font
color="#0000FF"> </font><font color="#FF0000">'/'</font><font
color="#0000FF"> </font><font color="#800080">exp</font><font
color="#0000FF"> { $$ </font><font color="#008080">:=</font><font
color="#0000FF"> $1 </font><font color="#008080">/</font><font
color="#0000FF"> $3 }
| </font><font color="#FF0000">'-'</font><font
color="#0000FF"> </font><font color="#800080">exp</font><font
color="#0000FF"> %prec </font><font color="#FF0000">NEG</font><font
color="#0000FF"> { $$ </font><font color="#008080">:= -</font><font
color="#0000FF">$2 }
| </font><font color="#FF0000">'(' </font><font
color="#800080">exp</font><font color="#FF0000"> ')'</font><font
color="#0000FF"> { $$ </font><font color="#008080">:=</font><font
color="#0000FF"> $2 }
;
%%</font></pre>
</blockquote>
<h3>The <tt>mcalc</tt> Lexical Analyzer</h3>
<p>The lexical analyzer must now recognize memory variables,
numeric values and arithmetic operators. Memory variables are
strings of alphanumeric characters with a leading nondigit
character.</p>
<blockquote>
<pre><font color="#008080"><em>read_token </em><em><strong>is</strong></em><em>
</em>-- Lexical analyzer returns a double floating point
-- number on the stack and the token NUM, a STRING and
-- and the token VAR, a token ASSIGN, or the ASCII
-- character read if not a number. Skips all blanks
-- and tabs, returns 0 for EOF.<em>
</em><em><strong>local</strong></em><em>
c</em>:<em> CHARACTER
buffer</em>:<em> STRING
</em><em><strong>do</strong></em><em>
</em>-- Skip white space<em>
</em><em><strong>from
if </strong></em><em>has_pending_character</em><em><strong> then
</strong></em><em>c </em>:=<em><strong> </strong></em><em>pending_character</em><em><strong>
</strong></em><em>has_pending_character</em><em><strong> </strong></em>:=<em><strong> </strong></em><em>False</em><em><strong>
elseif not </strong></em><em>io.end_of_file</em><em><strong> then</strong></em><em>
io</em>.<em>read_character
c </em>:=<em> io</em>.<em>last_character
</em><em><strong>end</strong></em><em>
</em><em><strong>until</strong></em><em>
io.end_of_file </em><em><strong>or else</strong></em><em>
</em>(<em>c </em>/=<em> ' ' </em><em><strong>and</strong></em><em> c </em>/=<em> '%T'</em>)<em>
</em><em><strong>loop</strong></em><em>
io.read_character
c </em>:=<em> io.last_character
</em><em><strong>end</strong></em><em>
</em><em><strong>if</strong></em><em> io.end_of_file </em><em><strong>then</strong></em><em>
</em>-- Return end-of-file<em>
last_token </em>:=<em> 0
</em><em><strong>else</strong></em><em>
</em><em><strong>inspect</strong></em><em> c
</em><em><strong>when</strong></em><em> '0'..'9' </em><em><strong>then</strong></em><em>
</em><em><strong> </strong></em>-- Process numbers<em>
last_token </em>:=<em> NUM
</em><em><strong>from</strong></em><em>
</em>!!<em> buffer.make </em>(<em>10</em>)<em>
buffer.append_character </em>(<em>c</em>)<em>
io.read_character
c </em>:= <em>io.last_character
</em><em><strong>until</strong></em><em>
io.end_of_file </em><em><strong>or else</strong></em><em>
</em>(<em>c </em><<em> '0' </em><em><strong>or</strong></em><em> c </em>><em> '9'</em>)<em>
</em><em><strong> loop</strong></em><em>
buffer.append_character </em>(<em>c</em>)<em>
io.read_character
c </em>:=<em> io.last_character
</em><em><strong>end</strong></em><em>
</em><em><strong>if not</strong></em><em> io.end_of_file </em><em><strong>and then</strong></em><em> c = '.' </em><em><strong>then</strong></em><em>
</em><em><strong>from</strong></em><em>
buffer.append_character </em>(<em>'.'</em>)<em>
io.read_character
c </em>:= <em>io.last_character
</em><em><strong>until</strong></em><em>
io.end_of_file </em><em><strong>or else</strong></em><em>
</em>(<em>c </em><<em> '0' </em><em><strong>or</strong></em><em> c </em>><em> '9'</em>)<em>
</em><em><strong>loop</strong></em><em>
buffer.append_character </em>(<em>c</em>)<em>
io.read_character
c </em>:=<em> io.last_character
</em><em><strong> end</strong></em><em>
</em><em><strong>end</strong></em><em>
</em><em><strong>if</strong></em><em> </em><em><strong>not</strong></em><em> io.end_of_file </em><em><strong>then</strong></em><em>
pending_character </em>:=<em> c
has_pending_character </em>:=<em> True
</em><em><strong>end</strong></em><em>
last_value </em>:=<em> buffer.to_double
</em><em><strong>when</strong></em><em> 'a'..'z', 'A'..'Z' </em><em><strong>then</strong></em><em>
</em>-- Process variables.<em>
last_token </em>:=<em> VAR
</em><em><strong>from</strong></em><em>
</em>!!<em> buffer.make </em>(<em>10</em>)<em>
buffer.append_character </em>(<em>c</em>)<em>
io.read_character
c </em>:= <em>io.last_character
</em><em><strong>until</strong></em><em>
io.end_of_file </em><em><strong>or else</strong></em><em>
</em><em><strong>not</strong></em> ((<em>'a' </em><=<em> c </em><em><strong>and</strong></em><em> c </em><= <em>'z'</em>)<em> </em><em><strong>or</strong></em>
(<em>'A'</em> <= <em>c</em> <em><strong>and</strong></em> <em>c</em> <= <em>'Z'</em>) <em><strong>or</strong></em>
(<em>'0'</em> <= <em>c</em> <em><strong>and</strong></em> <em>c</em> <= <em>'9'</em>))<em>
</em><em><strong> loop</strong></em><em>
buffer.append_character </em>(<em>c</em>)<em>
io.read_character
c </em>:=<em> io.last_character
</em><em><strong>end</strong></em><em>
</em><em><strong>if</strong></em><em> </em><em><strong>not</strong></em><em> io.end_of_file </em><em><strong>then</strong></em><em>
pending_character </em>:=<em> c
has_pending_character </em>:=<em> True
</em><em><strong>end</strong></em><em>
last_value </em>:=<em> buffer
</em><em><strong>when </strong></em><em>':'</em><em><strong> then
</strong></em><em>io.read_character
c </em>:= <em>io.last_character
</em><em><strong>if not</strong></em><em> io.end_of_file </em><em><strong>then</strong></em><em>
</em><em><strong>if</strong></em><em> c </em>=<em> '=' </em><em><strong>then</strong></em><em>
</em>-- Found ":="<em>
last_token </em>:=<em> ASSIGN
</em><em><strong>else</strong></em><em>
</em> -- Return single character<em>
last_token </em>:= (<em>':'</em>)<em>.code
pending_character </em>:=<em> c
has_pending_character </em>:=<em> True
</em><em><strong>end</strong></em><em>
</em><em><strong>else</strong></em><em>
</em> -- Return single character<em>
last_token </em>:= (<em>':'</em>)<em>.code
</em><em><strong>end
else</strong></em><em>
</em>-- Return single character<em>
last_token </em>:= <em>c.code
</em><em><strong>end</strong></em><em>
</em><em><strong>end</strong></em><em>
</em><em><strong>end</strong></em></font></pre>
</blockquote>
<h3>Memory management of <tt>mcalc</tt></h3>
<p>Following is some remaining source code taking care of the
memory management of <tt>mcalc</tt>.</p>
<blockquote>
<pre><font color="#008080"><em><strong>feature </strong></em>{<em>NONE</em>} -- Initialization<em>
make </em><em><strong>is</strong></em><em>
</em>-- Create a new calculator with memory.<em>
</em><em><strong>do</strong></em><em>
make_parser_skeleton
</em>!!<em> memory_values.make </em>(<em>10</em>)<em>
</em><em><strong>end
feature</strong></em><em> </em>-- Memory management<em>
memory_value </em>(<em>a_name</em>:<em> STRING</em>):<em> DOUBLE </em><em><strong>is</strong></em><em>
</em>-- Value associated with memory <em>a_name</em>;
-- 0.0 if no value has been stored in <em>a_name</em> yet<em>
</em><em><strong>require</strong></em><em>
a_name_not_void</em>:<em> a_name </em>/=<em> Void
</em><em><strong>do</strong></em><em>
</em><em><strong>if</strong></em><em> memory_values.has </em>(<em>a_name</em>)<em> </em><em><strong>then</strong></em><em>
Result </em>:=<em> memory_values.item </em>(<em>a_name</em>)<em>
</em><em><strong>else</strong></em><em>
Result </em>:=<em> 0.0
</em><em><strong>end
end</strong></em><em>
set_memory_value </em>(<em>a_value</em>:<em> DOUBLE</em>;<em> a_name</em>:<em> STRING</em>)<em> </em><em><strong>is</strong></em><em>
</em>-- Store <em>a_value</em> into <em>a_name</em>.<em>
</em><em><strong>require</strong></em><em>
a_name_not_void</em>:<em> a_name </em>/=<em> Void
</em><em><strong>do</strong></em><em>
memory_values.force </em>(<em>a_value</em>,<em> a_name</em>)<em>
</em><em><strong>ensure</strong></em><em>
memory_value_set</em>:<em> memory_value </em>(<em>a_name</em>) =<em> a_value
</em><em><strong>end</strong></em><em>
</em><em><strong>feature</strong></em><em> </em>{<em>NONE</em>} -- Implementation<em>
memory_values</em>:<em> DS_HASH_TABLE </em>[<em>DOUBLE, STRING</em>]<em>
</em>-- Values already stored so far<em>
</em><em><strong>invariant</strong></em><em>
memory_values_not_void</em>:<em> memory_values </em>/= <em>Void
</em>
<em><strong>end</strong></em> -- class MCALC</font></pre>
</blockquote>
<hr size="1">
<table border="0" width="100%">
<tr>
<td><address>
<font size="2"><b>Copyright 1999</b></font><font
size="1"><b>, </b></font><font size="2"><strong>Eric
Bezault</strong></font><strong> </strong><font
size="2"><br>
<strong>mailto:</strong></font><a
href="mailto:ericb@gobosoft.com"><font size="2">ericb@gobosoft.com</font></a><font
size="2"> <br>
<strong>http:</strong></font><a
href="http://www.gobosoft.com"><font size="2">//www.gobosoft.com</font></a><font
size="2"><br>
<strong>Last Updated:</strong> 25 March 1999</font><br>
<!--webbot bot="PurpleText"
preview="
$Date: 1999/06/12 18:56:07 $
$Revision: 1.9 $"
-->
</address>
</td>
<td align="right" valign="top"><a
href="http://www.gobosoft.com"><img
src="../image/home.gif" alt="Home" border="0" width="40"
height="40"></a><a href="index.html"><img
src="../image/toc.gif" alt="Toc" border="0" width="40"
height="40"></a><a href="stages.html"><img
src="../image/previous.gif" alt="Previous" border="0"
width="40" height="40"></a><a href="description.html"><img
src="../image/next.gif" alt="Next" border="0" width="40"
height="40"></a></td>
</tr>
</table>
</body>
</html>
|