7740 1968 P 8 18 F B (Thinkage) S 100 J ( Ltd.) S E
6160 2448 P B (YAY Reference Manual) S E
2880 8448 P 0 12 F 8 9 F (Last) S 50 J ( Revision: November 11, 1995) S 0 12 F
PE
PE
7040 2448 P 8 18 F B (Table) S 100 J ( of Contents) S E
2880 4468 P 0 12 F 8 12 F B (1.) S 67 J ( Introduction) S 0 LL 5398 J ( ) S LE (1) S E
3168 5088 P 0 12 F 8 9 F B (1.1) S 50 J ( A Note to Novices) S 0 LL 4922 J ( ) S LE (1) S E
2880 5748 P 0 12 F 8 12 F B (2.) S 67 J ( How YAY Works) S 0 LL 4933 J ( ) S LE (3) S E
3168 6368 P 0 12 F 8 9 F B (2.1) S 50 J ( "yyparse" and "yylex") S 0 LL 4572 J ( ) S LE (3) S E
3168 6748 P B (2.2) S 50 J ( Grammar Rules) S 0 LL 5122 J ( ) S LE (4) S E
3168 7128 P B (2.3) S 50 J ( Notes on Compiling Source Code Produced by YAY) S 0 LL 2042 J ( ) S LE (4) S E
2880 7788 P 0 12 F 8 12 F B (3) S 67 J ( Input to YAY) S 0 LL 5425 J ( ) S LE (6) S E
3168 8408 P 0 12 F 8 9 F B (3.1) S 50 J ( The Declarations Section) S 0 LL 4312 J ( ) S LE (7) S E
3456 8668 P 0 12 F 8 9 F (3.1.1) S 50 J ( Token Declarations) S 0 LL 4474 J ( ) S LE (7) S
3456 8928 P (3.1.2) S 50 J ( Precedence and Binding) S 0 LL 4064 J ( ) S LE (8) S
3456 9188 P (3.1.3) S 50 J ( Variable/Function Declarations) S 0 LL 3474 J ( ) S LE (10) S
3456 9448 P (3.1.4) S 50 J ( Summary) S 0 LL 5154 J ( ) S LE (11) S
3168 9828 P 0 12 F 8 9 F B (3.2) S 50 J ( Grammar Rules) S 0 LL 5022 J ( ) S LE (11) S E
3456 10088 P 0 12 F 8 9 F (3.2.1) S 50 J ( Recognition Actions) S 0 LL 4334 J ( ) S LE (13) S
3456 10348 P (3.2.2) S 50 J ( Token and Symbol Values) S 0 LL 3824 J ( ) S LE (13) S
3456 10608 P (3.2.3) S 50 J ( Precedence in the Grammar Rules) S 0 LL 3154 J ( ) S LE (15) S
3456 10868 P (3.2.4) S 50 J ( The Start Symbol) S 0 LL 4534 J ( ) S LE (16) S
3456 11128 P (3.2.5) S 50 J ( The End Marker) S 0 LL 4634 J ( ) S LE (17) S
3456 11388 P (3.2.6) S 50 J ( Declarations in yyparse) S 0 LL 4054 J ( ) S LE (17) S
3168 11768 P 0 12 F 8 9 F B (3.3) S 50 J ( The Programs Section) S 0 LL 4452 J ( ) S LE (18) S E
3456 12028 P 0 12 F 8 9 F (3.3.1) S 50 J ( The Lexical Analyzer) S 0 LL 4254 J ( ) S LE (18) S
2880 12688 P 0 12 F 8 12 F B (4.) S 67 J ( Internal Structures) S 0 LL 4534 J ( ) S LE (20) S E
2880 14520 P 0 12 F 57 12 F (DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD) S
2880 14800 P 0 12 F 24 10 F (Table) S 50 J ( of Contents) S 5709 J ( i) S 0 12 F
PE
2448 1968 P 8 9 F B (4.1) S 50 J ( States) S 0 LL 5822 J ( ) S LE (20) S E
2448 2348 P B (4.2) S 50 J ( Diagramming States) S 0 LL 4622 J ( ) S LE (21) S E
2448 2728 P B (4.3) S 50 J ( State Actions) S 0 LL 5212 J ( ) S LE (22) S E
2736 2988 P 0 12 F 8 9 F (4.3.1) S 50 J ( The Accept Action) S 0 LL 4464 J ( ) S LE (23) S
2736 3248 P (4.3.2) S 50 J ( The Shift Action) S 0 LL 4654 J ( ) S LE (23) S
2736 3508 P (4.3.3) S 50 J ( The Reduce Action) S 0 LL 4394 J ( ) S LE (24) S
2736 3768 P (4.3.4) S 50 J ( The Goto Action) S 0 LL 4624 J ( ) S LE (25) S
2736 4028 P (4.3.5) S 50 J ( The Error Action) S 0 LL 4614 J ( ) S LE (26) S
2160 4688 P 0 12 F 8 12 F B (5.) S 67 J ( Error-Handling) S 0 LL 4973 J ( ) S LE (27) S E
2448 5308 P 0 12 F 8 9 F B (5.1) S 50 J ( The "error" Symbol) S 0 LL 4702 J ( ) S LE (27) S E
2448 5688 P B (5.2) S 50 J ( The Error Condition) S 0 LL 4662 J ( ) S LE (27) S E
2448 6068 P B (5.3) S 50 J ( Examples) S 0 LL 5522 J ( ) S LE (28) S E
2448 6448 P B (5.4) S 50 J ( Error Recognition Actions) S 0 LL 4122 J ( ) S LE (30) S E
2448 6828 P B (5.5) S 50 J ( The yyclearin Macro) S 0 LL 4632 J ( ) S LE (30) S E
2448 7208 P B (5.6) S 50 J ( The yyerror Function) S 0 LL 4562 J ( ) S LE (31) S E
2736 7468 P 0 12 F 8 9 F (5.6.1) S 50 J ( Changing yychar in yyerror) S 0 LL 3774 J ( ) S LE (31) S
2448 7848 P 0 12 F 8 9 F B (5.7) S 50 J ( The yyerrflag Variable) S 0 LL 4482 J ( ) S LE (32) S E
2448 8228 P B (5.8) S 50 J ( The yyerrok Macro) S 0 LL 4762 J ( ) S LE (33) S E
2448 8608 P B (5.9) S 50 J ( Other Error Support Routines) S 0 LL 3832 J ( ) S LE (33) S E
2160 9268 P 0 12 F 8 12 F B (6.) S 67 J ( YAY Output) S 0 LL 5319 J ( ) S LE (35) S E
2448 9888 P 0 12 F 8 9 F B (6.1) S 50 J ( Rules Summary) S 0 LL 5002 J ( ) S LE (36) S E
2448 10268 P B (6.2) S 50 J ( State Descriptions) S 0 LL 4782 J ( ) S LE (36) S E
2448 10648 P B (6.3) S 50 J ( Conflict Summary) S 0 LL 4822 J ( ) S LE (39) S E
2448 11028 P B (6.4) S 50 J ( Parser Statistics) S 0 LL 4952 J ( ) S LE (39) S E
2160 11688 P 0 12 F 8 12 F B (7.) S 67 J ( Types) S 0 LL 5974 J ( ) S LE (42) S E
2448 12308 P 0 12 F 8 9 F B (The) S 50 J ( Default Action) S 0 LL 5122 J ( ) S LE (43) S E
2160 12968 P 0 12 F 8 12 F B (8.) S 67 J ( Ambiguities) S 0 LL 5293 J ( ) S LE (45) S E
2160 14520 P 0 12 F 57 12 F (DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD) S
2160 14800 P 0 12 F 24 10 F (ii) S 5653 J ( Table) S 50 J ( of Contents) S 0 12 F
PE
3168 1968 P 8 9 F B (8.1) S 50 J ( Resolving Conflicts by Precedence) S 0 LL 3362 J ( ) S LE (46) S E
3168 2348 P B (8.2) S 50 J ( Disambiguating Rules) S 0 LL 4472 J ( ) S LE (46) S E
3168 2728 P B (8.3) S 50 J ( Conflicts in YAY Output) S 0 LL 4322 J ( ) S LE (48) S E
2880 3388 P 0 12 F 8 12 F B (9.) S 67 J ( Advanced Features) S 0 LL 4455 J ( ) S LE (50) S E
3168 4008 P 0 12 F 8 9 F B (9.1) S 50 J ( LALR\(2\) Grammars) S 0 LL 4712 J ( ) S LE (50) S E
3456 4268 P 0 12 F 8 9 F (9.1.1) S 50 J ( The Lookahead Action) S 0 LL 4124 J ( ) S LE (50) S
3456 4528 P (9.1.2) S 50 J ( The yy2lex Routine) S 0 LL 4384 J ( ) S LE (51) S
3456 4788 P (9.1.3) S 50 J ( Conflicts) S 0 LL 5234 J ( ) S LE (52) S
3168 5168 P 0 12 F 8 9 F B (9.2) S 50 J ( Multiple Actions) S 0 LL 4972 J ( ) S LE (52) S E
3168 5548 P B (9.3) S 50 J ( Selection Preference) S 0 LL 4582 J ( ) S LE (54) S E
3168 5928 P B (9.4) S 50 J ( Negative Numbers in $N Constructs) S 0 LL 3292 J ( ) S LE (58) S E
3168 6308 P B (9.5) S 50 J ( Lists and Handling Null Strings) S 0 LL 3692 J ( ) S LE (59) S E
3168 6688 P B (9.6) S 50 J ( Right vs. Left Recursion) S 0 LL 4292 J ( ) S LE (60) S E
3168 7068 P B (9.7) S 50 J ( YYDEBUG) S 0 LL 5472 J ( ) S LE (62) S E
3168 7448 P B (9.8) S 50 J ( Important Symbols) S 0 LL 4732 J ( ) S LE (63) S E
3168 7828 P B (9.9) S 50 J ( How YYERROR May Be Used) S 0 LL 3872 J ( ) S LE (64) S E
3168 8208 P B (9.10) S 50 J ( The Default Action) S 0 LL 4672 J ( ) S LE (66) S E
3168 8588 P B (9.11) S 50 J ( Invalid Tokens) S 0 LL 5012 J ( ) S LE (67) S E
3168 8968 P B (9.12) S 50 J ( Dynamic Stack Allocation) S 0 LL 4062 J ( ) S LE (67) S E
3168 9348 P B (9.13) S 50 J ( Synchronizing the Lookahead) S 0 LL 3702 J ( ) S LE (68) S E
3168 9728 P B (9.14) S 50 J ( YYSHIFT) S 0 LL 5502 J ( ) S LE (69) S E
2880 10388 P 0 12 F 8 12 F B (An) S 67 J ( Example) S 0 LL 5561 J ( ) S LE (70) S E
2880 11288 P B (YAY) S 67 J ( vs. YACC) S 0 LL 5295 J ( ) S LE (73) S E
2880 12188 P B (The) S 67 J ( YAY Command Line) S 0 LL 4105 J ( ) S LE (74) S E
2880 14520 P 0 12 F 57 12 F (DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD) S
2880 14800 P 0 12 F 24 10 F (Table) S 50 J ( of Contents) S 5597 J ( iii) S 0 12 F
PE
PE
7580 2448 P 8 18 F B (1.) S 100 J ( Introduction) S E
2880 4048 P 0 12 F 8 9 F (YAY) S 50 J ( is a tool for writing compilers and other programs that parse input according to strict) S
2880 4288 P ("grammar" rules. In the terminology of parsing theory, YAY can handle LALR\(2\)) S
2880 4528 P (grammars with disambiguating rules. This is a fairly broad class of grammars -- most of) S
2880 4768 P (the computing languages in use today can be described using LALR\(2\) specifications.) S
2880 5208 P (For a precise definition of this sort of grammar, see any standard text on parsing theory,) S
2880 5448 P (e.g. ) S 0 12 F 8 9 F I (Principles) S 50 J ( of Compiler Design) S E 0 12 F 8 9 F () S 50 J ( by A.V.Aho and J.D.Ullman, Addison-Wesley, 1977, or) S
2880 5688 P 0 12 F 8 9 F I (LR) S 50 J ( Parsing) S E 0 12 F 8 9 F () S 50 J ( by Aho and Johnson, in Computing Surveys.) S
2880 6128 P (The appearance of YAY input is based on the input to YACC \(Yet Another Compiler-) S
2880 6368 P (Compiler\), written by S.C.Johnson of Bell Telephone Laboratories, Murray Hill, N.J. The) S
2880 6608 P (LALR\(1\) version of YAY was written by K.W.Lalonde of the Software Development Group) S
2880 6848 P (of the University of Waterloo. Enhancements to allow YAY to handle LALR\(2\) grammars) S
2880 7088 P (were made by P.J.Fraser, also of SDG.) S
2880 7528 P (The parsing algorithm used by YAY is derived from the article "Methods for Computing) S
2880 7768 P (LALR\(k\) Lookahead" by B.B.Kristensen and O.L.Madsen, ) S 0 12 F 8 9 F I (ACM) S 50 J ( Transactions on) S E
2880 8008 P I (Programming Languages and Systems) S E 0 12 F 8 9 F (,) S 50 J ( Vol.3, No.1, January 1981, pp.60-82. Those) S
2880 8248 P (interested in reading this article should first have a good grasp of parsing theory principles.) S
2880 9068 P 0 12 F 8 12 F B (1.1) S 67 J ( A Note to Novices) S E
2880 9788 P 0 12 F 8 9 F (YAY) S 50 J ( can produce anything from a simple parser for a desk calculator program to a very) S
2880 10028 P (complicated parser for a programming language. Those who are using YAY for complex) S
2880 10268 P (tasks have to know all the idiosyncrasies of the program, including a good deal about the) S
2880 10508 P (internal workings of YAY. On the other hand, the internal workings are mostly irrelevant to) S
2880 10748 P (someone who is making an easy straightforward parser.) S
2880 11188 P (For this reason, novices may want to skip some the sections of the manual which describe) S
2880 11428 P (very technical aspects of YAY. This is particularly true on first reading, when it is more) S
2880 11668 P (important to get an overview of how YAY is used than to try to comprehend every little) S
2880 11908 P (detail. We therefore suggest that novices concentrate on the following sections, and only) S
2880 12148 P (look at other sections if the requirements of your parser make it necessary.) S
2880 12588 P ( -- All of Chapter 2) S
2880 12828 P ( -- All of Chapter 3) S
2880 13068 P ( -- All of Chapter 4) S
2880 14520 P 0 12 F 57 12 F (DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD) S
2880 14800 P 0 12 F 24 10 F (1) S 4338 J ( Thinkage) S 50 J ( YAY Reference Manual) S 0 12 F
PE
2160 1968 P 8 9 F () S 50 J ( -- Sections 6.0, 6.1, 6.2) S
2160 2208 P ( -- all of Chapter 7) S
2160 2448 P ( -- Sections 9.3, 9.5, 9.6) S
2160 2888 P (Novices may be particularly interested in Appendix A which gives an example of YAY) S
2160 3128 P (input for a simple parser.) S
2160 14520 P 0 12 F 57 12 F (DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD) S
2160 14800 P 0 12 F 24 10 F (Thinkage) S 50 J ( YAY Reference Manual) S 4338 J ( 2) S 0 12 F
PE
6880 2448 P 8 18 F B (2.) S 100 J ( How YAY Works) S E
2880 4048 P 0 12 F 8 9 F (The) S 50 J ( input to YAY describes the rules of a grammar. YAY uses these rules to produce the) S
2880 4288 P (source code for a program that will parse the grammar. This source code can then be) S
2880 4528 P (compiled to obtain a program that reads input, parses it according to the grammar, and) S
2880 4768 P (takes action based on the result.) S
2880 5208 P (The source code produced by YAY consists of a number of data tables that represent the) S
2880 5448 P (grammar, plus a C function named ) S 0 12 F 0 9 F ("yyparse") S 0 12 F 8 9 F (.) S 50 J ( By default, all the symbol names used) S
2880 5688 P (by YAY begin with ) S 0 12 F 0 9 F ("yy") S 0 12 F 8 9 F (.) S 50 J ( This is an historical convention, dating back to YAY's) S
2880 5928 P (predecessor YACC. Users can avoid conflicts with YAY names by avoiding symbols that) S
2880 6168 P (start with ) S 0 12 F 0 9 F ("yy") S 0 12 F 8 9 F (.) S
2880 6608 P (Users) S 50 J ( who want to use a different prefix may indicate this with a line of the form) S
3600 7028 P 0 12 F 0 9 F (%prefix) S 108 J ( PREFIX) S
2880 7468 P 0 12 F 8 9 F (at) S 50 J ( the beginning of their YAY input. For example,) S
3600 7888 P 0 12 F 0 9 F (%prefix) S 108 J ( ww) S
2880 8328 P 0 12 F 8 9 F (asks) S 50 J ( for a prefix of ) S 0 12 F 0 9 F ("ww") S 0 12 F 8 9 F () S 50 J ( instead of ) S 0 12 F 0 9 F ("yy") S 0 12 F 8 9 F (.) S 50 J ( The prefix chosen should be one or two) S
2880 8568 P (characters long -- longer prefixes will lead to name conflicts on machines where external) S
2880 8808 P (names are truncated to six characters during the loading process. In addition, at least one) S
2880 9048 P (of the characters in the prefix should be a lower case letter \(because YAY uses an all) S
2880 9288 P (upper case version of the prefix for some special names, and this has to be different from) S
2880 9528 P (the real prefix\).) S
2880 9968 P (Different prefixes are useful when two YAY-produced parsers are to be merged into a) S
2880 10208 P (single program. For the sake of convenience, however, the ) S 0 12 F 0 9 F ("yy") S 0 12 F 8 9 F () S 50 J ( convention will be used) S
2880 10448 P (in this manual.) S
2880 11268 P 0 12 F 8 12 F B (2.1) S 67 J ( "yyparse" and "yylex") S E
2880 11988 P 0 12 F 0 9 F ("yyparse") S 0 12 F 8 9 F () S 50 J ( returns a value of 0 if the input it parses is valid according to the given) S
2880 12228 P (grammar rules. It returns a 1 if the input is invalid and error recovery is impossible.) S
2880 12468 P 0 12 F 0 9 F ("yyparse") S 0 12 F 8 9 F () S 50 J ( does not do its own lexical analysis. In other words, it does not pull the input) S
2880 12708 P (apart into tokens ready for parsing. Instead, it calls a routine called ) S 0 12 F 0 9 F ("yylex") S 0 12 F 8 9 F () S 50 J ( every time it) S
2880 12948 P (wishes to obtain a token from the input.) S
2880 14520 P 0 12 F 57 12 F (DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD) S
2880 14800 P 0 12 F 24 10 F (3) S 4338 J ( Thinkage) S 50 J ( YAY Reference Manual) S 0 12 F
PE
2160 1968 P 0 9 F ("yylex") S 0 12 F 8 9 F () S 50 J ( returns a value indicating the ) S 0 12 F 8 9 F I (type) S E 0 12 F 8 9 F () S 50 J ( of token that has been obtained. If the token) S
2160 2208 P (has an actual ) S 0 12 F 8 9 F I (value) S E 0 12 F 8 9 F (,) S 50 J ( this value \(or some representation of the value, e.g. a pointer to a) S
2160 2448 P (string containing the value\) is returned in an external variable named ) S 0 12 F 0 9 F ("yylval") S 0 12 F 8 9 F (.) S
2160 2888 P (It) S 50 J ( is up to the user to write a ) S 0 12 F 0 9 F ("yylex") S 0 12 F 8 9 F () S 50 J ( routine that breaks the input into tokens and returns) S
2160 3128 P (the tokens one by one to ) S 0 12 F 0 9 F ("yyparse") S 0 12 F 8 9 F (.) S 50 J ( We will say more about the lexical analyzer in) S
2160 3368 P (Section 3.3.) S
2160 4188 P 0 12 F 8 12 F B (2.2) S 67 J ( Grammar Rules) S E
2160 4908 P 0 12 F 8 9 F (The) S 50 J ( grammar rules given to YAY not only describe what inputs are valid according to the) S
2160 5148 P (grammar but also specify what action should be taken when a given input is encountered.) S
2160 5388 P (For example, if the parser recognizes a statement that assigns a value to a variable, the) S
2160 5628 P (parser should either perform the assignment itself or take some action to ensure that the) S
2160 5868 P (assignment will eventually take place.) S
2160 6308 P (As an example, if the parser is part of an interactive desk calculator, it could carry out) S
2160 6548 P (arithmetic calculations as soon as the instructions are recognized. However, if the parser) S
2160 6788 P (is acting as the first pass in a multi-pass compiler, it may simply encode the input in a way) S
2160 7028 P (that will be used in a later code-generation pass.) S
2160 7468 P (In summary, the user must provide a number of things when using YAY to produce a) S
2160 7708 P (parser:) S
2160 8148 P (\(1\) grammar rules indicating what input is and isn't valid;) S
2160 8588 P (\(2\) a lexical analyzer \() S 0 12 F 0 9 F ("yylex") S 0 12 F 8 9 F (\)) S 50 J ( that breaks raw input into tokens for the parsing) S
2880 8828 P (routine ) S 0 12 F 0 9 F ("yyparse") S 0 12 F 8 9 F (;) S
2160 9268 P (\(3\)) S 500 J ( any) S 50 J ( source code or functions that may be needed to perform appropriate actions) S
2880 9508 P (once particular inputs are recognized;) S
2160 9948 P (\(4\) a mainline routine that performs any necessary initializations, calls ) S 0 12 F 0 9 F ("yyparse") S 0 12 F 8 9 F (,) S
2880 10188 P (then) S 50 J ( performs possible clean-up actions. The simplest kind of mainline is just a) S
2880 10428 P (function ) S 0 12 F 0 9 F ("main") S 0 12 F 8 9 F () S 50 J ( that calls ) S 0 12 F 0 9 F ("yyparse") S 0 12 F 8 9 F (,) S 50 J ( then returns.) S
2160 11248 P 0 12 F 8 12 F B (2.3) S 67 J ( Notes on Compiling Source Code Produced by YAY) S E
2160 11968 P 0 12 F 8 9 F (By) S 50 J ( default, C source code produced by YAY contains the line) S
2880 12388 P 0 12 F 0 9 F (#define) S 108 J ( YYSCTAB const) S
2160 14520 P 0 12 F 57 12 F (DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD) S
2160 14800 P 0 12 F 24 10 F (Thinkage) S 50 J ( YAY Reference Manual) S 4338 J ( 4) S 0 12 F
PE
2880 1968 P 8 9 F (It) S 50 J ( then uses the ) S 0 12 F 0 9 F (YYSCTAB) S 0 12 F 8 9 F () S 50 J ( manifest to declare various data objects as ) S 0 12 F 0 9 F B (const) S E 0 12 F 8 9 F (.) S 50 J ( If you will) S
2880 2208 P (be compiling the YAY output with an old C compiler that doesn't recognize the ) S 0 12 F 0 9 F B (const) S E
2880 2448 P 0 12 F 8 9 F (qualifier,) S 50 J ( you should change the definition to read) S
3600 2868 P 0 12 F 0 9 F (#define) S 108 J ( YYSCTAB) S
2880 14520 P 0 12 F 57 12 F (DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD) S
2880 14800 P 0 12 F 24 10 F (5) S 4338 J ( Thinkage) S 50 J ( YAY Reference Manual) S 0 12 F
PE
6900 2448 P 8 18 F B (3) S 100 J ( Input to YAY) S E
2160 4048 P 0 12 F 8 9 F (This) S 50 J ( chapter describes the input to YAY when you are defining an LALR\(1\) grammar.) S
2160 4288 P (Chapter 9 will talk about additional needs for LALR\(2\) grammars.) S
2160 4728 P (The input to YAY is broken into three sections:) S
2160 5168 P ( -- declarations) S
2160 5408 P ( -- grammar rules) S
2160 5648 P ( -- programs) S
2160 6088 P (The contents of each section will be described shortly, but first we will give some overall) S
2160 6328 P (rules for YAY input.) S
2160 6768 P (Sections of YAY input are separated by the symbol ) S 0 12 F 0 9 F (%%) S 0 12 F 8 9 F (.) S 50 J ( The general lay-out of YAY input) S
2160 7008 P (is therefore) S
2880 7428 P 0 12 F 0 9 F (declarations) S
2880 7648 P (%%) S
2880 7868 P (grammar) S 108 J ( rules) S
2880 8088 P (%%) S
2880 8308 P (programs) S
2160 8748 P 0 12 F 8 9 F (The) S 50 J ( declarations section may be omitted if no declarations are necessary. This will mean) S
2160 8988 P (that the input starts with the first ) S 0 12 F 0 9 F (%%) S 0 12 F 8 9 F (.) S 50 J ( The programs section may also be omitted, from the) S
2160 9228 P (second ) S 0 12 F 0 9 F (%%) S 0 12 F 8 9 F () S 50 J ( on. The simplest input for YAY is therefore) S
2880 9648 P 0 12 F 0 9 F (%%) S
2880 9868 P (grammar) S 108 J ( rules) S
2160 10308 P 0 12 F 8 9 F (Blanks,) S 50 J ( tabs, and new-lines are used to separate items in YAY input. These are called) S
2160 10548 P 0 12 F 8 9 F I (white-space) S E 0 12 F 8 9 F () S 50 J ( characters. Any place one white-space character is valid, any number of) S
2160 10788 P (blanks, tabs, and/or new-lines may be used. This means, for example, that the ) S 0 12 F 0 9 F (%%) S 0 12 F 8 9 F () S 50 J ( to) S
2160 11028 P (separate sections does not have to be on a line all by itself. However, giving it a line of its) S
2160 11268 P (own makes the YAY input easier to read.) S
2160 11708 P (Comments may appear anywhere a blank is valid. As in C, comments begin with ) S 0 12 F 0 9 F (/*) S 0 12 F 8 9 F () S 50 J ( and) S
2160 11948 P (end with ) S 0 12 F 0 9 F (*/) S 0 12 F 8 9 F (.) S
2160 12388 P (Identifiers) S 50 J ( used in YAY input may be of arbitrary length, and may consist of all letters) S
2160 12628 P (\(upper and lower case\), all digits, and the characters dot \() S 0 12 F 0 9 F (.) S 0 12 F 8 9 F (\)) S 50 J ( and underscore \() S 0 12 F 0 9 F (_) S 0 12 F 8 9 F (\).) S 50 J ( The first) S
2160 12868 P (character of an identifier may not be a digit. YAY distinguishes between upper and lower) S
2160 13108 P (case letters, so ) S 0 12 F 0 9 F ("this") S 0 12 F 8 9 F () S 50 J ( and ) S 0 12 F 0 9 F ("THIS") S 0 12 F 8 9 F () S 50 J ( and ) S 0 12 F 0 9 F ("This") S 0 12 F 8 9 F () S 50 J ( are all different symbols.) S
2160 14520 P 0 12 F 57 12 F (DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD) S
2160 14800 P 0 12 F 24 10 F (Thinkage) S 50 J ( YAY Reference Manual) S 4338 J ( 6) S 0 12 F
PE
2880 2168 P 8 9 F (Literals) S 50 J ( in YAY input consist of a single ASCII character enclosed in single quotes, e.g.) S
2880 2408 P 0 12 F 0 9 F ('c') S 0 12 F 8 9 F (.) S 50 J ( The standard C escape sequences are recognized:) S
3600 2828 P 0 12 F 0 9 F (\\b) S 108 J ( -- ) S 0 12 F 8 9 F (backspace) S
3600 3048 P 0 12 F 0 9 F (\\n) S 108 J ( -- ) S 0 12 F 8 9 F (new-line) S
3600 3268 P 0 12 F 0 9 F (\\r) S 108 J ( -- ) S 0 12 F 8 9 F (carriage) S 50 J ( return) S
3600 3488 P 0 12 F 0 9 F (\\t) S 108 J ( -- ) S 0 12 F 8 9 F (tab) S
3600 3708 P 0 12 F 0 9 F (\\') S 108 J ( -- ) S 0 12 F 8 9 F (single) S 50 J ( quote) S
3600 3928 P 0 12 F 0 9 F (\\\\) S 108 J ( -- ) S 0 12 F 8 9 F (backslash) S
3600 4148 P 0 12 F 0 9 F (\\xxx) S 108 J ( -- ) S 0 12 F 8 9 F (any) S 50 J ( ASCII character) S
3600 4368 P 0 12 F 0 9 F () S 108 J ( ) S 0 12 F 8 9 F (\() S 0 12 F 0 9 F ("xxx") S 0 12 F 8 9 F () S 50 J ( is octal representation\)) S
2880 4808 P (For technical reasons, the null character ) S 0 12 F 0 9 F ('\\000') S 0 12 F 8 9 F () S 50 J ( should never appear in YAY input.) S
2880 5628 P 0 12 F 8 12 F B (3.1) S 67 J ( The Declarations Section) S E
2880 6348 P 0 12 F 8 9 F (The) S 50 J ( declarations section describes many of the identifiers that will be used in the rest of) S
2880 6588 P (the YAY input. There are two types of declarations:) S
2880 7028 P (\(a\) Token declarations;) S
2880 7468 P (\(b\) Declarations of functions and variables used in the actions that the parser takes) S
3600 7708 P (when a particular input is recognized.) S
2880 8148 P (The declarations section can also specify rules for the precedence and binding of) S
2880 8388 P (operators used in the grammar. For example, the standard order of arithmetic operations) S
2880 8628 P (would normally be set up in the declarations section.) S
2880 9748 P 0 12 F 8 12 F I (3.1.1) S 67 J ( Token Declarations) S E
2880 10468 P 0 12 F 8 9 F (All) S 50 J ( ASCII characters are automatically recognized as tokens. For example, ) S 0 12 F 0 9 F ('a') S 0 12 F 8 9 F () S 50 J ( stands for) S
2880 10708 P (a token that is the literal character ) S 0 12 F 0 9 F ("a") S 0 12 F 8 9 F (.) S
2880 11148 P (Other) S 50 J ( tokens are declared with statements of the form) S
3600 11568 P 0 12 F 0 9 F (%token) S 108 J ( name1 name2 name3 ...) S
2880 12008 P 0 12 F 8 9 F (This) S 50 J ( tells YAY that the given names will refer to tokens. For example,) S
3600 12428 P 0 12 F 0 9 F (%token) S 108 J ( INTNUM) S
2880 14520 P 0 12 F 57 12 F (DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD) S
2880 14800 P 0 12 F 24 10 F (7) S 4338 J ( Thinkage) S 50 J ( YAY Reference Manual) S 0 12 F
PE
2160 1968 P 8 9 F (indicates) S 50 J ( that the identifier ) S 0 12 F 0 9 F (INTNUM) S 0 12 F 8 9 F () S 50 J ( will refer to a particular type of token returned by the) S
2160 2208 P (lexical analyzer ) S 0 12 F 0 9 F ("yylex") S 0 12 F 8 9 F (.) S 50 J ( If ) S 0 12 F 0 9 F (INTNUM) S 0 12 F 8 9 F () S 50 J ( stands for any integer number token, you might) S
2160 2448 P (have the following code in ) S 0 12 F 0 9 F ("yylex") S 0 12 F 8 9 F (.) S
2880 2868 P 0 12 F 0 9 F (c) S 108 J ( = getchar\(\);) S
2880 3088 P (if \( \(c >= '0'\) && \(c <= '9'\) \) {) S
2880 3308 P ( yylval = 0;) S
2880 3528 P ( do {) S
2880 3748 P ( yylval = \(yylval * 10\) + \(c - '0'\);) S
2880 3968 P ( c = getchar\(\);) S
2880 4188 P ( } while \(c >= '0' && c <= '9'\);) S
2880 4408 P ( ungetc\(c, stdin\);) S
2880 4628 P ( return\( INTNUM \);) S
2880 4848 P (}) S
2160 5288 P ("yylex") S 0 12 F 8 9 F () S 50 J ( returns ) S 0 12 F 0 9 F (INTNUM) S 0 12 F 8 9 F () S 50 J ( to indicate that a certain kind of token \(an integer number\) has) S
2160 5528 P (been returned. The actual value of this number is returned in ) S 0 12 F 0 9 F ("yylval") S 0 12 F 8 9 F (.) S 50 J ( The grammar) S
2160 5768 P (rules in the YAY input would dictate where an ) S 0 12 F 0 9 F (INTNUM) S 0 12 F 8 9 F () S 50 J ( token is valid.) S
2160 6208 P (In the C source code produced by YAY, the identifiers named in a ) S 0 12 F 0 9 F (%token) S 0 12 F 8 9 F () S 50 J ( statement will) S
2160 6448 P (appear as constants set up with ) S 0 12 F 0 9 F B (#define) S E 0 12 F 8 9 F (.) S 50 J ( The first named token has a #defined value of) S
2160 6688 P 0 12 F 0 9 F (257) S 0 12 F 8 9 F (,) S 50 J ( the next is #defined as ) S 0 12 F 0 9 F (258) S 0 12 F 8 9 F (,) S 50 J ( and so on. Token values start at ) S 0 12 F 0 9 F (257) S 0 12 F 8 9 F () S 50 J ( so they will not) S
2160 6928 P (conflict with any of the ) S 0 12 F 0 9 F (256) S 0 12 F 8 9 F () S 50 J ( ASCII characters.) S
2160 7368 P (Because token identifiers are set up as manifest constants, they must not conflict with) S
2160 7608 P (reserved words or other identifiers that will be used by the parser. For example,) S
2880 8028 P 0 12 F 0 9 F (%token) S 108 J ( if yyparse ...) S
2160 8468 P 0 12 F 8 9 F (will) S 50 J ( almost certainly lead to errors when you try to compile the source code output of YAY.) S
2160 8708 P (By convention, this manual will always create token names in upper case, and we) S
2160 8948 P (recommend that you follow the same practice.) S
2160 10068 P 0 12 F 8 12 F I (3.1.2) S 67 J ( Precedence and Binding) S E
2160 10788 P 0 12 F 8 9 F (Parsers) S 50 J ( that evaluate expressions usually have to establish the order in which various) S
2160 11028 P (operations are carried out. For example, parsers for arithmetic expressions usually carry) S
2160 11268 P (out multiplications before additions. Two factors affect order of operation: precedence and) S
2160 11508 P (binding.) S
2160 11948 P 0 12 F 8 9 F I (Precedence) S E 0 12 F 8 9 F () S 50 J ( dictates which of two ) S 0 12 F 8 9 F B (different) S E 0 12 F 8 9 F () S 50 J ( operations should be carried out first. For) S
2160 12188 P (example, in) S
2880 12608 P 0 12 F 0 9 F (A) S 108 J ( + B * C) S
2160 14520 P 0 12 F 57 12 F (DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD) S
2160 14800 P 0 12 F 24 10 F (Thinkage) S 50 J ( YAY Reference Manual) S 4338 J ( 8) S 0 12 F
PE
2880 1968 P 8 9 F (the) S 50 J ( standard arithmetic rules of precedence dictate that the multiplication should take) S
2880 2208 P (place before the addition. Operations that should be carried out first are said to have a) S
2880 2448 P 0 12 F 8 9 F I (higher) S E 0 12 F 8 9 F () S 50 J ( precedence than operations that should be performed later.) S
2880 2888 P (Different operators can sometimes have the same precedence. In C, for example,) S
2880 3128 P (addition and subtraction are similar enough to share the same precedence.) S
2880 3568 P 0 12 F 8 9 F I (Binding) S E 0 12 F 8 9 F () S 50 J ( indicates which of two ) S 0 12 F 8 9 F B (similar) S E 0 12 F 8 9 F () S 50 J ( operations should be carried out first. By "similar",) S
2880 3808 P (we mean operations with the same precedence \(e.g. addition and subtraction in C\). For) S
2880 4048 P (example, C chooses to parse) S
3600 4468 P 0 12 F 0 9 F (A) S 108 J ( - B - C) S
2880 4908 P 0 12 F 8 9 F (as) S
3600 5328 P 0 12 F 0 9 F (\(A) S 108 J ( - B\) - C) S
2880 5768 P 0 12 F 8 9 F (while) S 50 J ( a language like APL would use) S
3600 6188 P 0 12 F 0 9 F (A) S 108 J ( - \(B - C\)) S
2880 6628 P 0 12 F 8 9 F (If) S 50 J ( we evaluate the first operation before the second \(as C does\), we say the operation is) S
2880 6868 P 0 12 F 8 9 F I (left-associative) S E 0 12 F 8 9 F (.) S 50 J ( If we evaluate the second operation before the first \(as APL does\), we say) S
2880 7108 P (the operation is ) S 0 12 F 8 9 F I (right-associative.) S E
2880 7548 P 0 12 F 8 9 F (Occasionally,) S 50 J ( a compiler may have operations which are not associative. For example,) S
2880 7788 P (Fortran regards) S
3600 8208 P 0 12 F 0 9 F (A) S 108 J ( .GT. B .GT. C) S
2880 8648 P 0 12 F 8 9 F (as) S 50 J ( invalid. In this case, we say the operation is ) S 0 12 F 8 9 F I (non-associative) S E 0 12 F 8 9 F (.) S 50 J ( The precedence and) S
2880 8888 P (associativity of operator tokens may be declared in the declarations section using the) S
2880 9128 P (keywords) S
3600 9548 P 0 12 F 0 9 F (%left) S
3600 9768 P (%right) S
3600 9988 P (%nonassoc) S
2880 10428 P 0 12 F 8 9 F (For) S 50 J ( example,) S
3600 10848 P 0 12 F 0 9 F (%left) S 108 J ( '+' '-') S
2880 11288 P 0 12 F 8 9 F (indicates) S 50 J ( that the ) S 0 12 F 0 9 F (+) S 0 12 F 8 9 F () S 50 J ( and ) S 0 12 F 0 9 F (-) S 0 12 F 8 9 F () S 50 J ( operations have the same precedence and are left-associative.) S
2880 11728 P (Associativity declarations should be given in order of precedence. Operations with lowest) S
2880 11968 P (precedence are listed first, and those with highest precedence are listed last. Operations) S
2880 12208 P (with equal precedence are listed on the same line. For example,) S
3600 12628 P 0 12 F 0 9 F (%right) S 108 J ( '=') S
3600 12848 P (%left '+' '-') S
3600 13068 P (%left '*' '/' '%') S
2880 14520 P 0 12 F 57 12 F (DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD) S
2880 14800 P 0 12 F 24 10 F (9) S 4338 J ( Thinkage) S 50 J ( YAY Reference Manual) S 0 12 F
PE
2160 2168 P 8 9 F (says) S 50 J ( that ) S 0 12 F 0 9 F (=) S 0 12 F 8 9 F () S 50 J ( has a lower precedence than ) S 0 12 F 0 9 F (+) S 0 12 F 8 9 F () S 50 J ( and ) S 0 12 F 0 9 F (-) S 0 12 F 8 9 F (,) S 50 J ( which in turn have a lower precedence) S
2160 2408 P (than ) S 0 12 F 0 9 F (*) S 0 12 F 8 9 F (,) S 50 J ( ) S 0 12 F 0 9 F (/) S 0 12 F 8 9 F (,) S 50 J ( and ) S 0 12 F 0 9 F (%) S 0 12 F 8 9 F (.) S 50 J ( ) S 0 12 F 0 9 F (=) S 0 12 F 8 9 F () S 50 J ( is also right-associative, so that) S
2880 2828 P 0 12 F 0 9 F (A) S 108 J ( = B = C) S
2160 3268 P 0 12 F 8 9 F (is) S 50 J ( parsed as) S
2880 3688 P 0 12 F 0 9 F (A) S 108 J ( = \(B = C\)) S
2160 4128 P 0 12 F 8 9 F (Because) S 50 J ( of the way YAY specifies precedence and associativity, operators with equal) S
2160 4368 P (precedence will always have the same associativity. For example, if A and B have equal) S
2160 4608 P (precedence, their precedence must have been set with one of) S
2880 5028 P 0 12 F 0 9 F (%left) S 108 J ( A B) S
2880 5248 P (%right A B) S
2880 5468 P (%nonassoc A B) S
2160 5908 P 0 12 F 8 9 F (which) S 50 J ( means A and B must have the same associativity.) S
2160 6348 P (The names supplied with ) S 0 12 F 0 9 F (%right) S 0 12 F 8 9 F (,) S 50 J ( ) S 0 12 F 0 9 F (%left) S 0 12 F 8 9 F (,) S 50 J ( and ) S 0 12 F 0 9 F (%nonassoc) S 0 12 F 8 9 F () S 50 J ( may be literals or YAY) S
2160 6588 P (identifiers. If they are identifiers, they are regarded as token names. YAY generates a) S
2160 6828 P 0 12 F 0 9 F (%token) S 0 12 F 8 9 F () S 50 J ( directive for such names if they have not already been declared. For example, in) S
2880 7248 P 0 12 F 0 9 F (%left) S 108 J ( '+' '-') S
2880 7468 P (%left '*' '/') S
2880 7688 P (%left UMINUS) S
2160 8128 P (UMINUS) S 0 12 F 8 9 F () S 50 J ( is taken to be a token identifier. There is no need to define ) S 0 12 F 0 9 F (UMINUS) S 0 12 F 8 9 F () S 50 J ( as a token) S
2160 8368 P (identifier -- a ) S 0 12 F 0 9 F (%token) S 0 12 F 8 9 F () S 50 J ( directive will be generated automatically if necessary. It is perfectly) S
2160 8608 P (valid to have an explicit) S
2880 9028 P 0 12 F 0 9 F (%token) S 108 J ( UMINUS) S
2160 9468 P 0 12 F 8 9 F (if) S 50 J ( you want. However, it must precede the ) S 0 12 F 0 9 F (%left) S 0 12 F 8 9 F () S 50 J ( declaration.) S
2160 9908 P (For a more technical discussion of how precedence and binding rules affect a parser, see) S
2160 10148 P (Chapter 8.) S
2160 11268 P 0 12 F 8 12 F I (3.1.3) S 67 J ( Variable/Function Declarations) S E
2160 11988 P 0 12 F 8 9 F (The) S 50 J ( declarations section may contain standard C declarations for variables and/or) S
2160 12228 P (functions used in the actions specified in the grammar rules section. All such declarations) S
2160 12468 P (should be included in a block that begins with ) S 0 12 F 0 9 F (%{) S 0 12 F 8 9 F () S 50 J ( and ends with ) S 0 12 F 0 9 F (%}) S 0 12 F 8 9 F (.) S 50 J ( For example,) S
2160 14520 P 0 12 F 57 12 F (DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD) S
2160 14800 P 0 12 F 24 10 F (Thinkage) S 50 J ( YAY Reference Manual) S 4238 J ( 10) S 0 12 F
PE
3600 1948 P 0 9 F (%{) S
3600 2168 P () S 108 J ( int i, j, k;) S
3600 2388 P ( static float x = 1.0;) S
3600 2608 P (%}) S
2880 3048 P 0 12 F 8 9 F (gives) S 50 J ( a few variable declarations. These declarations are essentially transferred "as is" to) S
2880 3288 P (the ) S 0 12 F 8 9 F I (beginning) S E 0 12 F 8 9 F () S 50 J ( of the source code that is produced by YAY. This means that they will be) S
2880 3528 P 0 12 F 8 9 F I (external) S E 0 12 F 8 9 F () S 50 J ( to ) S 0 12 F 0 9 F ("yyparse") S 0 12 F 8 9 F () S 50 J ( and therefore ) S 0 12 F 0 9 F B (extern) S E 0 12 F 8 9 F () S 50 J ( definitions.) S
2880 4648 P 0 12 F 8 12 F I (3.1.4) S 67 J ( Summary) S E
2880 5368 P 0 12 F 8 9 F (The) S 50 J ( source code produced by YAY contains the following.) S
2880 5808 P ( -- code from the declarations section) S
2880 6048 P ( -- code given in the programs section) S
2880 6288 P ( -- parsing tables produced by YAY to represent the grammar) S
2880 6528 P ( -- the ) S 0 12 F 0 9 F ("yyparse") S 0 12 F 8 9 F () S 50 J ( routine) S
2880 7348 P 0 12 F 8 12 F B (3.2) S 67 J ( Grammar Rules) S E
2880 8068 P 0 12 F 8 9 F (A) S 50 J ( YAY grammar rule has the general form) S
3600 8488 P 0 12 F 0 9 F (identifier) S 108 J ( : definition ;) S
2880 8928 P 0 12 F 8 9 F (A) S 50 J ( colon separates the definition from the identifier being defined. A semicolon marks the) S
2880 9168 P (end of the definition.) S
2880 9608 P (The identifiers defined in the grammar rule section are known as ) S 0 12 F 8 9 F I (non-terminal) S 50 J ( symbols) S E 0 12 F 8 9 F (.) S
2880 9848 P ("Non-terminal") S 50 J ( suggests that these symbols aren't the "end of the line". Instead, they are) S
2880 10088 P (made up of smaller things: tokens or other non-terminal symbols.) S
2880 10528 P (Here is a simple example of the definition of a non-terminal symbol.) S
3600 10948 P 0 12 F 0 9 F (paren_expr) S 108 J ( : '\(' expr '\)' ;) S
2880 11388 P 0 12 F 8 9 F (This) S 50 J ( says that a ) S 0 12 F 0 9 F ("paren_expr") S 0 12 F 8 9 F () S 50 J ( consists of a left parenthesis, followed by an ) S 0 12 F 0 9 F ("expr") S 0 12 F 8 9 F (,) S
2880 11628 P (followed) S 50 J ( by a right parenthesis. The ) S 0 12 F 0 9 F ("expr") S 0 12 F 8 9 F () S 50 J ( is either a token or a non-terminal symbol) S
2880 11868 P (defined in another grammar rule. This grammar rule could be interpreted to say that a) S
2880 12108 P (parenthesized expression consists of a normal expression inside parentheses.) S
2880 12548 P (A non-terminal symbol may have more than one definition. For example, we might define) S
2880 12788 P 0 12 F 0 9 F ("if") S 0 12 F 8 9 F () S 50 J ( statements with) S
2880 14520 P 0 12 F 57 12 F (DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD) S
2880 14800 P 0 12 F 24 10 F (11) S 4238 J ( Thinkage) S 50 J ( YAY Reference Manual) S 0 12 F
PE
2880 1948 P 0 9 F (if_stat) S 108 J ( : IF '\(' expr '\)' stat ;) S
2880 2168 P (if_stat : IF '\(' expr '\)' stat ELSE stat ;) S
2160 2608 P 0 12 F 8 9 F (This) S 50 J ( definition assumes that ) S 0 12 F 0 9 F (IF) S 0 12 F 8 9 F () S 50 J ( and ) S 0 12 F 0 9 F (ELSE) S 0 12 F 8 9 F () S 50 J ( are tokens recognized by the lexical analyzer) S
2160 2848 P (\(which means that this parser's ) S 0 12 F 0 9 F ("yylex") S 0 12 F 8 9 F () S 50 J ( can recognize keywords\). The definition also) S
2160 3088 P (assumes that ) S 0 12 F 0 9 F ("expr") S 0 12 F 8 9 F () S 50 J ( and ) S 0 12 F 0 9 F ("stat") S 0 12 F 8 9 F () S 50 J ( are non-terminal symbols defined elsewhere.) S
2160 3528 P (When a single symbol has more than one meaning, YAY lets you join the various) S
2160 3768 P (possibilities into a single definition. Different meanings are separated by or-bars \() S 0 12 F 0 9 F (|) S 0 12 F 8 9 F (\).) S
2160 4008 P (Thus) S 50 J ( you could write) S
2880 4428 P 0 12 F 0 9 F (if_stat) S 108 J ( : IF '\(' expr '\)' stat) S
2880 4648 P ( | IF '\(' expr '\)' stat ELSE stat ;) S
2160 5088 P 0 12 F 8 9 F (This) S 50 J ( technique is highly recommended, since it makes YAY input more readable.) S
2160 5528 P (Definitions in a grammar may be recursive. For example,) S
2880 5948 P 0 12 F 0 9 F (list) S 108 J ( : item) S
2880 6168 P ( | list ',' item;) S
2160 6608 P 0 12 F 8 9 F (defines) S 50 J ( ) S 0 12 F 0 9 F ("list") S 0 12 F 8 9 F () S 50 J ( to be one or more items separated by commas.) S
2880 7028 P 0 12 F 0 9 F (intexp) S 108 J ( : '\(' intexp '\)') S
2880 7248 P ( | intexp '+' intexp) S
2880 7468 P ( | intexp '-' intexp) S
2880 7688 P ( | intexp '*' intexp) S
2880 7908 P ( | intexp '/' intexp) S
2880 8128 P ( | INTNUM ;) S
2160 8568 P 0 12 F 8 9 F (says) S 50 J ( that an integer expression can be another integer expression in parentheses, the) S
2160 8808 P (sum of integer expressions, the difference of integer expressions, the product of integer) S
2160 9048 P (expressions, the quotient of integer expressions, or an integer number standing on its own) S
2160 9288 P (\(where ) S 0 12 F 0 9 F (INTNUM) S 0 12 F 8 9 F () S 50 J ( is a token recognized by the lexical analyzer\).) S
2160 9728 P (In recursive symbol definitions, it is often useful to have the empty string as one of the) S
2160 9968 P (possible definitions. For example,) S
2880 10388 P 0 12 F 0 9 F (program) S 108 J ( :) S
2880 10608 P ( /* the empty string */) S
2880 10828 P ( | statement ';' program ;) S
2160 11268 P 0 12 F 8 9 F (defines) S 50 J ( a program as zero or more statements separated by semicolons.) S
2160 11708 P (Our definition of ) S 0 12 F 0 9 F ("list") S 0 12 F 8 9 F () S 50 J ( above was an example of ) S 0 12 F 8 9 F I (left) S 50 J ( recursion) S E 0 12 F 8 9 F () S 50 J ( because ) S 0 12 F 0 9 F ("list") S 0 12 F 8 9 F () S 50 J ( was on) S
2160 11948 P (the left in the recursive definition. The definition of ) S 0 12 F 0 9 F ("program") S 0 12 F 8 9 F () S 50 J ( was an example of ) S 0 12 F 8 9 F I (right) S E
2160 12188 P I (recursion) S E 0 12 F 8 9 F (.) S 50 J ( In Section 9.6, we discuss the pros and cons of the two types of recursion.) S
2160 14520 P 0 12 F 57 12 F (DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD) S
2160 14800 P 0 12 F 24 10 F (Thinkage) S 50 J ( YAY Reference Manual) S 4238 J ( 12) S 0 12 F
PE
2880 2308 P 8 12 F I (3.2.1) S 67 J ( Recognition Actions) S E
2880 3028 P 0 12 F 8 9 F (In) S 50 J ( addition to defining what a non-terminal symbol is, a grammar rule usually describes) S
2880 3268 P (what to do if the non-terminal symbol is encountered in parser input. We call this a) S
2880 3508 P 0 12 F 8 9 F I (recognition) S 50 J ( action.) S E
2880 3948 P 0 12 F 8 9 F (Recognition) S 50 J ( actions are specified as part of the grammar rule. They are enclosed in brace) S
2880 4188 P (brackets in the definition, as in) S
3600 4608 P 0 12 F 0 9 F (break_stat) S 108 J ( : BREAK ';') S
3600 4828 P ( { breakfn\(\); };) S
2880 5268 P 0 12 F 8 9 F (In) S 50 J ( this definition, ) S 0 12 F 0 9 F ("break_stat") S 0 12 F 8 9 F () S 50 J ( is a non-terminal symbol made up of the token known as) S
2880 5508 P 0 12 F 0 9 F (BREAK) S 0 12 F 8 9 F (,) S 50 J ( followed by a semicolon. If this symbol is recognized, the parser will invoke a) S
2880 5748 P (function named ) S 0 12 F 0 9 F ("breakfn") S 0 12 F 8 9 F (.) S 50 J ( Presumably this is a user-defined function that handles a) S
2880 5988 P 0 12 F 0 9 F ("break;") S 0 12 F 8 9 F () S 50 J ( statement.) S
2880 6428 P (Note that a semicolon is needed to mark the end of the definition even though the) S
2880 6668 P (recognition action ends in a brace bracket. Programmers who are used to the way C) S
2880 6908 P (works should bear this in mind.) S
2880 7348 P (For compatibility with some versions of YACC, YAY lets you put an equals sign \() S 0 12 F 0 9 F (=) S 0 12 F 8 9 F (\)) S 50 J ( before) S
2880 7588 P (the opening brace that begins a recognition action, as in) S
3600 8008 P 0 12 F 0 9 F (break_stat) S 108 J ( : BREAK ';') S
3600 8228 P ( = { breakfn\(\); };) S
2880 8668 P 0 12 F 8 9 F (When) S 50 J ( a symbol has more than one definition, a different recognition action may be) S
2880 8908 P (associated with each definition. We will show some examples of this in a moment.) S
2880 10028 P 0 12 F 8 12 F I (3.2.2) S 67 J ( Token and Symbol Values) S E
2880 10748 P 0 12 F 8 9 F (One) S 50 J ( of the most common recognition actions is to return a value. For example, if an input) S
2880 10988 P (is recognized as an expression to be evaluated, the parser may want to return the) S
2880 11228 P (resulting value of the expression. To return a value, the recognition action merely assigns) S
2880 11468 P (the value to a special variable named ) S 0 12 F 0 9 F ($$) S 0 12 F 8 9 F (.) S 50 J ( For example,) S
3600 11888 P 0 12 F 0 9 F (hexdigit) S 108 J ( : '0' { $$ = 0; }) S
3600 12108 P ( | '1' { $$ = 1; }) S
3600 12328 P ( ...) S
3600 12548 P ( | 'A' { $$ = 10; }) S
3600 12768 P ( | 'B' { $$ = 11; }) S
3600 12988 P ( | 'C' { $$ = 12; }) S
3600 13208 P ( | 'D' { $$ = 13; }) S
2880 14520 P 0 12 F 57 12 F (DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD) S
2880 14800 P 0 12 F 24 10 F (13) S 4238 J ( Thinkage) S 50 J ( YAY Reference Manual) S 0 12 F
PE
2880 1948 P 0 9 F () S 108 J ( | 'E' { $$ = 14; }) S
2880 2168 P ( | 'F' { $$ = 15; } ;) S
2160 2608 P 0 12 F 8 9 F (could) S 50 J ( be a way to convert hexadecimal digits into numeric values. In this case, ) S 0 12 F 0 9 F ("yylex") S
2160 2848 P 0 12 F 8 9 F (just) S 50 J ( returns the digits it finds and ) S 0 12 F 0 9 F ("yyparse") S 0 12 F 8 9 F () S 50 J ( performs the actual conversion.) S
2160 3288 P (Another common recognition action is to return a value based on one or more of the items) S
2160 3528 P (that make up the non-terminal symbol. Inside the recognition action, ) S 0 12 F 0 9 F ($1) S 0 12 F 8 9 F () S 50 J ( stands for the) S
2160 3768 P (value of the first item in the symbol, ) S 0 12 F 0 9 F ($2) S 0 12 F 8 9 F () S 50 J ( stands for the value of the second item, and so on.) S
2160 4008 P (If the item is a token, its value is the ) S 0 12 F 0 9 F ("yylval") S 0 12 F 8 9 F () S 50 J ( value returned by ) S 0 12 F 0 9 F ("yylex") S 0 12 F 8 9 F () S 50 J ( when the) S
2160 4248 P (token was read. If the item is non-terminal symbol, its value is the ) S 0 12 F 0 9 F ($$) S 0 12 F 8 9 F () S 50 J ( value set by the) S
2160 4488 P (recognition action associated with the symbol. Thus we might write) S
2880 4908 P 0 12 F 0 9 F (intexp) S 108 J ( : '\(' intexp '\)') S
2880 5128 P ( {) S
2880 5348 P ( $$ = $2;) S
2880 5568 P ( }) S
2880 5788 P ( /* value of parenthesized expression) S
2880 6008 P ( is expression inside parentheses */) S
2880 6228 P ( | intexp '+' intexp) S
2880 6448 P ( {) S
2880 6668 P ( $$ = $1 + $3 ;) S
2880 6888 P ( }) S
2880 7108 P ( /* value of addition is sum of two) S
2880 7328 P ( expressions */) S
2880 7548 P ( | intexp '-' intexp) S
2880 7768 P ( {) S
2880 7988 P ( $$ = $1 - $3 ;) S
2880 8208 P ( }) S
2880 8428 P ( /* value of subtraction is difference) S
2880 8648 P ( of two expressions */) S
2880 8868 P ( | /* and so on */) S
2880 9088 P ( ;) S
2160 9528 P 0 12 F 8 9 F (Note) S 50 J ( that this particular definition shows that each part of a multiple definition may have a) S
2160 9768 P (different recognition action.) S
2160 10208 P (In the source code for ) S 0 12 F 0 9 F ("yyparse") S 0 12 F 8 9 F (,) S 50 J ( this set of actions will be turned into a large ) S 0 12 F 0 9 F B (switch) S E
2160 10448 P 0 12 F 8 9 F (statement.) S 50 J ( The cases of the ) S 0 12 F 0 9 F B (switch) S E 0 12 F 8 9 F () S 50 J ( will be the various possible recognition actions.) S
2160 10888 P (If no recognition action is specified for a definition, the default is) S
2880 11308 P 0 12 F 0 9 F ({) S 108 J ( $$ = $1 ; }) S
2160 11748 P 0 12 F 8 9 F (This) S 50 J ( action just returns the value of the first item \(if the item has a value\).) S
2160 14520 P 0 12 F 57 12 F (DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD) S
2160 14800 P 0 12 F 24 10 F (Thinkage) S 50 J ( YAY Reference Manual) S 4238 J ( 14) S 0 12 F
PE
2880 2308 P 8 12 F I (3.2.3) S 67 J ( Precedence in the Grammar Rules) S E
2880 3028 P 0 12 F 8 9 F (In) S 50 J ( our discussion of the declarations section, we showed how precedence could be) S
2880 3268 P (assigned to ) S 0 12 F 8 9 F I (operators) S E 0 12 F 8 9 F (.) S 50 J ( Precedence can also be assigned to ) S 0 12 F 8 9 F I (grammar) S 50 J ( rules,) S E 0 12 F 8 9 F () S 50 J ( and this is) S
2880 3508 P (done in the grammar input section.) S
2880 3948 P (One way to give a grammar rule a precedence uses the ) S 0 12 F 0 9 F (%prec) S 0 12 F 8 9 F () S 50 J ( construct.) S
3600 4368 P 0 12 F 0 9 F (%prec) S 108 J ( TOKEN) S
2880 4808 P 0 12 F 8 9 F (in) S 50 J ( a grammar rule indicates that the rule should have the same precedence as the) S
2880 5048 P (specified token.) S
2880 5488 P (As a simple example, consider the case of the unary minus operator. Suppose our) S
2880 5728 P (declaration section contains) S
3600 6148 P 0 12 F 0 9 F (%left) S 108 J ( '+' '-') S
3600 6368 P (%left '*' '/') S
3600 6588 P (%left UMINUS) S
2880 7028 P 0 12 F 8 9 F (In) S 50 J ( the grammar rules section, we can write) S
3600 7448 P 0 12 F 0 9 F (exp) S 108 J ( : exp '+' exp) S
3600 7668 P ( | exp '-' exp) S
3600 7888 P ( | exp '*' exp) S
3600 8108 P ( | exp '/' exp) S
3600 8328 P ( | '-' exp %prec UMINUS) S
3600 8548 P ( | /* and so on */) S
3600 8768 P ( ;) S
2880 9208 P 0 12 F 8 9 F (We) S 50 J ( could not directly set up a precedence for the unary minus, since we had already set) S
2880 9448 P (up a precedence for the ) S 0 12 F 0 9 F (-) S 0 12 F 8 9 F () S 50 J ( token. Instead, we created a token named UMINUS and gave it) S
2880 9688 P (the precedence we wanted to assign the unary minus. When we wrote out the grammar) S
2880 9928 P (rule for the unary minus, we added) S
3600 10348 P 0 12 F 0 9 F (%prec) S 108 J ( UMINUS) S
2880 10788 P 0 12 F 8 9 F (to) S 50 J ( show that this rule should have the precedence of ) S 0 12 F 0 9 F (UMINUS) S 0 12 F 8 9 F (.) S
2880 11228 P (As) S 50 J ( another example, we might set up precedence rules for the right shift and left shift) S
2880 11468 P (operations of C with) S
3600 11888 P 0 12 F 0 9 F (%left) S 108 J ( RS LS) S
3600 12108 P ( ...) S
3600 12328 P (exp :) S
3600 12548 P ( | exp '<' '<' exp %prec LS) S
3600 12768 P ( | exp '>' '>' exp %prec RS) S
3600 12988 P ( ...) S
2880 14520 P 0 12 F 57 12 F (DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD) S
2880 14800 P 0 12 F 24 10 F (15) S 4238 J ( Thinkage) S 50 J ( YAY Reference Manual) S 0 12 F
PE
2160 1968 P 8 9 F (In) S 50 J ( this way we give the shift operations the proper precedence and avoid confusing them) S
2160 2208 P (with the comparison operations ) S 0 12 F 0 9 F (>) S 0 12 F 8 9 F () S 50 J ( and ) S 0 12 F 0 9 F (<) S 0 12 F 8 9 F (.) S 50 J ( Of course, another way to resolve this problem) S
2160 2448 P (is to make the lexical analyzer clever enough to recognize ) S 0 12 F 0 9 F (>>) S 0 12 F 8 9 F () S 50 J ( and ) S 0 12 F 0 9 F (<<) S 0 12 F 8 9 F () S 50 J ( and to return the RS) S
2160 2688 P (or LS tokens directly.) S
2160 3128 P (Although symbols like ) S 0 12 F 0 9 F (UMINUS) S 0 12 F 8 9 F (,) S 50 J ( ) S 0 12 F 0 9 F (LS) S 0 12 F 8 9 F (,) S 50 J ( and ) S 0 12 F 0 9 F (RS) S 0 12 F 8 9 F () S 50 J ( are treated as tokens, they do not have to) S
2160 3368 P (correspond to actual input. They may just be placeholders for operator tokens that have) S
2160 3608 P (two different meanings.) S
2160 4048 P (We should note that the use of ) S 0 12 F 0 9 F (%prec) S 0 12 F 8 9 F () S 50 J ( is relatively rare in YAY. People don't usually think) S
2160 4288 P (of ) S 0 12 F 0 9 F (%prec) S 0 12 F 8 9 F () S 50 J ( in their first draft of a grammar. ) S 0 12 F 0 9 F (%prec) S 0 12 F 8 9 F () S 50 J ( is only added in later drafts, when it is) S
2160 4528 P (needed to resolve conflicts that appear when the rules are run through YAY.) S
2160 4968 P (If a grammar rule is not assigned a precedence using ) S 0 12 F 0 9 F (%prec) S 0 12 F 8 9 F (,) S 50 J ( the precedence of the rule is) S
2160 5208 P (taken from the last ) S 0 12 F 8 9 F I (token) S E 0 12 F 8 9 F () S 50 J ( in the rule. For example, if the rule is) S
2880 5628 P 0 12 F 0 9 F (expr) S 108 J ( : expr '+' expr) S
2160 6068 P 0 12 F 8 9 F (the) S 50 J ( last token in the rule is ) S 0 12 F 0 9 F (+) S 0 12 F 8 9 F () S 50 J ( \(since ) S 0 12 F 0 9 F ("expr") S 0 12 F 8 9 F () S 50 J ( is a non-terminal symbol, not a token\). Thus) S
2160 6308 P (the precedence of the rule is the same as the precedence of ) S 0 12 F 0 9 F (+) S 0 12 F 8 9 F (.) S
2160 6748 P (If) S 50 J ( the last token in a rule has no assigned precedence, the rule will not have a precedence.) S
2160 6988 P (This can result in some surprises if you aren't careful. For example, if you define) S
2880 7408 P 0 12 F 0 9 F (expr) S 108 J ( : expr '+' expr ';') S
2160 7848 P 0 12 F 8 9 F (the) S 50 J ( last token in the rule is ) S 0 12 F 0 9 F (;) S 0 12 F 8 9 F () S 50 J ( so the rule probably won't have a precedence even if ) S 0 12 F 0 9 F (+) S 0 12 F 8 9 F () S 50 J ( does.) S
2160 8288 P (A rule that contains no tokens cannot have a precedence.) S
2160 9408 P 0 12 F 8 12 F I (3.2.4) S 67 J ( The Start Symbol) S E
2160 10128 P 0 12 F 8 9 F (The) S 50 J ( first non-terminal symbol defined in the rules section is called the ) S 0 12 F 8 9 F I (start) S 50 J ( symbol) S E 0 12 F 8 9 F (.) S 50 J ( This) S
2160 10368 P (symbol is taken to be the largest, most general structure described by the grammar rules.) S
2160 10608 P (For example, if you are generating the parser for a compiler, the start symbol should) S
2160 10848 P (describe what a complete program looks like in the language to be parsed.) S
2160 11288 P (If you do not want the first grammar rule to be taken as the start symbol, you can use the) S
2160 11528 P (directive) S
2880 11948 P 0 12 F 0 9 F (%start) S 108 J ( NAME) S
2160 12388 P 0 12 F 8 9 F (in) S 50 J ( your rules section. This indicates that the non-terminal symbol named ) S 0 12 F 0 9 F (NAME) S 0 12 F 8 9 F () S 50 J ( is the start) S
2160 12628 P (symbol. ) S 0 12 F 0 9 F (NAME) S 0 12 F 8 9 F () S 50 J ( must be defined somewhere in the rules section.) S
2160 14520 P 0 12 F 57 12 F (DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD) S
2160 14800 P 0 12 F 24 10 F (Thinkage) S 50 J ( YAY Reference Manual) S 4238 J ( 16) S 0 12 F
PE
2880 1968 P 8 9 F (The) S 50 J ( start symbol must be all-encompassing: every other rule in the grammar must be) S
2880 2208 P (related to the start symbol. In a sense, the grammar forms a "tree": the root is the start) S
2880 2448 P (symbol, the first set of branches are the symbols that make up the start symbol, the next) S
2880 2688 P (set of branches are the symbols that make up the first set, and so on. Any symbol that is) S
2880 2928 P ("outside" this tree is reported as a ) S 0 12 F 8 9 F I (useless) S 50 J ( variable) S E 0 12 F 8 9 F () S 50 J ( in YAY output. Useless variables are) S
2880 3168 P (ignored by the parser -- the parser is looking for a complete start symbol, and nothing) S
2880 3408 P (else.) S
2880 4528 P 0 12 F 8 12 F I (3.2.5) S 67 J ( The End Marker) S E
2880 5248 P 0 12 F 8 9 F (The) S 50 J ( end of parser input is marked by a special token called the ) S 0 12 F 8 9 F I (end) S 50 J ( marker) S E 0 12 F 8 9 F (.) S 50 J ( This token is) S
2880 5488 P (often written as ) S 0 12 F 0 9 F ($end) S 0 12 F 8 9 F (;) S 50 J ( the value of the token is zero.) S
2880 5928 P (It is the job of the lexical analyzer "yylex" to return a zero to indicate ) S 0 12 F 0 9 F ($end) S 0 12 F 8 9 F () S 50 J ( when the end of) S
2880 6168 P (input is reached \(e.g. at end of file, or at a keyword that indicates end of input\).) S
2880 6608 P 0 12 F 0 9 F ("yyparse") S 0 12 F 8 9 F () S 50 J ( terminates when it has parsed a start symbol followed by the end marker.) S
2880 7728 P 0 12 F 8 12 F I (3.2.6) S 67 J ( Declarations in yyparse) S E
2880 8448 P 0 12 F 8 9 F (You) S 50 J ( can specify C declarations that will be local to ) S 0 12 F 0 9 F ("yyparse") S 0 12 F 8 9 F () S 50 J ( in much the same way) S
2880 8688 P (that you specify external declarations in the Declarations Section. Enclose the) S
2880 8928 P (declarations in ) S 0 12 F 0 9 F (%{) S 0 12 F 8 9 F () S 50 J ( and ) S 0 12 F 0 9 F (%}) S 0 12 F 8 9 F () S 50 J ( symbols, as in) S
3600 9348 P 0 12 F 0 9 F (%{) S
3600 9568 P () S 108 J ( /* External declaratios */) S
3600 9788 P (%}) S
3600 10008 P (%%) S
3600 10228 P (/* Rules Section starts here */) S
3600 10448 P ({%) S
3600 10668 P ( /* Declarations here will be) S
3600 10888 P ( local to "yyparse" */) S
3600 11108 P (%}) S
3600 11328 P (/* Rules */) S
3600 11548 P (%%) S
3600 11768 P (/* Program section */) S
2880 12208 P 0 12 F 8 9 F (You) S 50 J ( may also put declarations at the start of recognition action code.) S
2880 14520 P 0 12 F 57 12 F (DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD) S
2880 14800 P 0 12 F 24 10 F (17) S 4238 J ( Thinkage) S 50 J ( YAY Reference Manual) S 0 12 F
PE
2160 2308 P 8 12 F B (3.3) S 67 J ( The Programs Section) S E
2160 3028 P 0 12 F 8 9 F (The) S 50 J ( programs section of YAY input may contain functions that should be linked in with the) S
2160 3268 P 0 12 F 0 9 F ("yyparse") S 0 12 F 8 9 F () S 50 J ( routine. YAY itself does nothing with these functions -- it simply adds the) S
2160 3508 P (source code on the end of the source code produced from the grammar rules. In this way,) S
2160 3748 P (the functions can be compiled at the same time that the YAY-produced code is compiled.) S
2160 4188 P (Of course, these additional functions could be compiled separately and linked with the) S
2160 4428 P (YAY-produced code later on \(once everything is in object code format\). Separate) S
2160 4668 P (compilation of modules is strongly recommended for large parsers. However, functions) S
2160 4908 P (that are compiled separately need a special mechanism if they want to use any manifests) S
2160 5148 P (that are defined in the YAY-produced code, and it is sometimes simpler to make the) S
2160 5388 P (program part of the YAY input.) S
2160 5828 P (For example, consider the case of ) S 0 12 F 0 9 F ("yylex") S 0 12 F 8 9 F (.) S 50 J ( Every time ) S 0 12 F 0 9 F ("yylex") S 0 12 F 8 9 F () S 50 J ( obtains a token from) S
2160 6068 P (the input, it returns to ) S 0 12 F 0 9 F ("yyparse") S 0 12 F 8 9 F () S 50 J ( with a value that indicates the type of token found.) S
2160 6308 P (Obviously then, ) S 0 12 F 0 9 F ("yylex") S 0 12 F 8 9 F () S 50 J ( and ) S 0 12 F 0 9 F ("yyparse") S 0 12 F 8 9 F () S 50 J ( must agree on which return values indicate) S
2160 6548 P (which kind of tokens. Since ) S 0 12 F 0 9 F ("yyparse") S 0 12 F 8 9 F () S 50 J ( already refers to tokens using manifest) S
2160 6788 P (constants \(created in the declarations section with the ) S 0 12 F 0 9 F (%token) S 0 12 F 8 9 F () S 50 J ( directive\), it makes sense) S
2160 7028 P (for ) S 0 12 F 0 9 F ("yylex") S 0 12 F 8 9 F () S 50 J ( to use the same manifest constants. The lexical analyzer can do this very) S
2160 7268 P (easily if it is compiled along with ) S 0 12 F 0 9 F ("yyparse") S 0 12 F 8 9 F (.) S
2160 7708 P (Size) S 50 J ( might be the determining factor. With very simple parsers, it's easier to put ) S 0 12 F 0 9 F ("yylex") S
2160 7948 P 0 12 F 8 9 F (in) S 50 J ( the Programs Section. With larger parsers, the advantages of separate compilation are) S
2160 8188 P (well worth the extra effort.) S
2160 8628 P (If you are going to compile ) S 0 12 F 0 9 F ("yylex") S 0 12 F 8 9 F () S 50 J ( or other routines separately from ) S 0 12 F 0 9 F ("yyparse") S 0 12 F 8 9 F (,) S 50 J ( use) S
2160 8868 P (the) S
2880 9288 P 0 12 F 0 9 F (Header=file) S
2160 9728 P 0 12 F 8 9 F (option) S 50 J ( on the YAY command line. YAY will write out the manifest definitions to the file of) S
2160 9968 P (your choice. This file can then be #included to obtain these definitions for ) S 0 12 F 0 9 F ("yylex") S 0 12 F 8 9 F () S 50 J ( or) S
2160 10208 P (any other routine that needs them. The manifests are already included in the generated) S
2160 10448 P (parser code, so you only need them for separately compiled modules.) S
2160 11568 P 0 12 F 8 12 F I (3.3.1) S 67 J ( The Lexical Analyzer) S E
2160 12288 P 0 12 F 8 9 F (The) S 50 J ( lexical analyzer ) S 0 12 F 0 9 F ("yylex") S 0 12 F 8 9 F () S 50 J ( reads input and breaks it into tokens. It is ) S 0 12 F 0 9 F ("yylex") S 0 12 F 8 9 F () S 50 J ( that) S
2160 12528 P (determines what constitutes a token. For example, some lexical analyzers may return) S
2160 12768 P (numbers one digit at a time while others collect numbers in their entirety before passing) S
2160 13008 P (them to the parser.) S
2160 14520 P 0 12 F 57 12 F (DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD) S
2160 14800 P 0 12 F 24 10 F (Thinkage) S 50 J ( YAY Reference Manual) S 4238 J ( 18) S 0 12 F
PE
2880 1968 P 8 9 F (Similarly,) S 50 J ( some lexical analyzers may recognize keywords such as ) S 0 12 F 0 9 F (IF) S 0 12 F 8 9 F () S 50 J ( or ) S 0 12 F 0 9 F (WHILE) S 0 12 F 8 9 F () S 50 J ( and will) S
2880 2208 P (tell the parser that an ) S 0 12 F 0 9 F (IF) S 0 12 F 8 9 F () S 50 J ( token or ) S 0 12 F 0 9 F (WHILE) S 0 12 F 8 9 F () S 50 J ( token has been found. Others may not be) S
2880 2448 P (designed to recognize keywords, so it is up to the parser itself to distinguish between) S
2880 2688 P (keywords and other things like variable names.) S
2880 3128 P (As noted before, each token named in the declarations section of the YAY input is set up) S
2880 3368 P (as a manifest constant. The value of the first token named is ) S 0 12 F 0 9 F (257) S 0 12 F 8 9 F (,) S 50 J ( the value of the next is) S
2880 3608 P 0 12 F 0 9 F (258) S 0 12 F 8 9 F (,) S 50 J ( and so on. You can also set your own values for tokens by placing a positive integer) S
2880 3848 P (after the first appearance of any token in the declarations section. For example,) S
3600 4268 P 0 12 F 0 9 F (%token) S 108 J ( AA 56) S
2880 4708 P 0 12 F 8 9 F (assigns) S 50 J ( a value of ) S 0 12 F 0 9 F (56) S 0 12 F 8 9 F () S 50 J ( to the manifest \(token\) symbol ) S 0 12 F 0 9 F (AA) S 0 12 F 8 9 F (.) S 50 J ( This mechanism is very seldom) S
2880 4948 P (needed, and we recommend that users avoid it whenever possible.) S
2880 5388 P (There is little else to say about requirements for ) S 0 12 F 0 9 F ("yylex") S 0 12 F 8 9 F (.) S 50 J ( If it wishes to return the value) S
2880 5628 P (of a token as well as an indication of its type, the value is assigned to the external variable) S
2880 5868 P 0 12 F 0 9 F ("yylval") S 0 12 F 8 9 F (.) S 50 J ( By default, ) S 0 12 F 0 9 F ("yylval") S 0 12 F 8 9 F () S 50 J ( is defined as an ) S 0 12 F 0 9 F B (int) S E 0 12 F 8 9 F () S 50 J ( value but it can also be used to) S
2880 6108 P (hold other types of values. For more information, see the description of ) S 0 12 F 0 9 F (%union) S 0 12 F 8 9 F () S 50 J ( in) S
2880 6348 P (Chapter 7.) S
2880 14520 P 0 12 F 57 12 F (DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD) S
2880 14800 P 0 12 F 24 10 F (19) S 4238 J ( Thinkage) S 50 J ( YAY Reference Manual) S 0 12 F
PE
5760 2448 P 8 18 F B (4.) S 100 J ( Internal Structures) S E
2160 4048 P 0 12 F 8 9 F (In) S 50 J ( order to use YAY effectively, it is helpful to understand some of the internal workings of) S
2160 4288 P (the parser that YAY produces. In this chapter, we will look at some of these structures.) S
2160 4728 P (To give us a point of reference, suppose we have produced a parser with grammar) S
2880 5148 P 0 12 F 0 9 F (%token) S 108 J ( NUM) S
2880 5368 P (%left '+' '-') S
2880 5588 P (%left '*' '/') S
2880 5808 P (%%) S
2880 6028 P (expr : NUM) S
2880 6248 P ( | expr '+' expr) S
2880 6468 P ( | expr '-' expr) S
2880 6688 P ( | expr '*' expr) S
2880 6908 P ( | expr '/' expr) S
2880 7128 P ( | '\(' expr '\)') S
2880 7348 P ( ;) S
2160 8168 P 0 12 F 8 12 F B (4.1) S 67 J ( States) S E
2160 8888 P 0 12 F 8 9 F (As) S 50 J ( the parser reads in token after token, it switches between various ) S 0 12 F 8 9 F I (states) S E 0 12 F 8 9 F (.) S 50 J ( You can) S
2160 9128 P (think of a state as point where the parser says, "I have read ) S 0 12 F 8 9 F I (this) S E 0 12 F 8 9 F () S 50 J ( particular sequence of) S
2160 9368 P (input tokens and now I am looking for one of ) S 0 12 F 8 9 F I (these) S E 0 12 F 8 9 F () S 50 J ( tokens.") S
2160 9808 P (For example, a parser for the C language might be in a state where it has finished reading) S
2160 10048 P (a complete statement and is ready for the start of a new statement. It therefore expects) S
2160 10288 P (some token that can legitimately start a statement \(e.g. a keyword like ) S 0 12 F 0 9 F (IF) S 0 12 F 8 9 F () S 50 J ( or ) S 0 12 F 0 9 F (WHILE) S 0 12 F 8 9 F (,) S 50 J ( or the) S
2160 10528 P (name of a variable for an assignment\).) S
2160 10968 P (In this state, it reads a token. Say it finds the token corresponding to the keyword ) S 0 12 F 0 9 F (IF) S 0 12 F 8 9 F (.) S 50 J ( It) S
2160 11208 P (then switches to a new state where it says "I have seen an ) S 0 12 F 0 9 F (IF) S 0 12 F 8 9 F () S 50 J ( and now I want to see the ) S 0 12 F 0 9 F (\() S
2160 11448 P 0 12 F 8 9 F (that) S 50 J ( begins the ) S 0 12 F 0 9 F (IF) S 0 12 F 8 9 F () S 50 J ( condition." When it finds the ) S 0 12 F 0 9 F (\() S 0 12 F 8 9 F (,) S 50 J ( it switches again to a state that says "I) S
2160 11688 P (have found ) S 0 12 F 0 9 F (IF\() S 0 12 F 8 9 F () S 50 J ( and now I want the start of a condition expression.") S
2160 12128 P (States break the parsing process into simple steps. At each step, the parser knows what) S
2160 12368 P (it has seen and what it is looking for next.) S
2160 14520 P 0 12 F 57 12 F (DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD) S
2160 14800 P 0 12 F 24 10 F (Thinkage) S 50 J ( YAY Reference Manual) S 4238 J ( 20) S 0 12 F
PE
2880 1968 P 8 9 F (YAY) S 50 J ( assigns numbers to every possible state the parser can enter. The 0th state is) S
2880 2208 P (always the one that describes the parser's condition before it has read any input. Other) S
2880 2448 P (states are numbered arbitrarily.) S
2880 2888 P (Sometimes a particular input can only be the start of one construct. For example, the ) S 0 12 F 0 9 F (FOR) S
2880 3128 P 0 12 F 8 9 F (keyword) S 50 J ( in C can only be the start of a ) S 0 12 F 0 9 F (FOR) S 0 12 F 8 9 F () S 50 J ( statement, and the ) S 0 12 F 0 9 F (FOR) S 0 12 F 8 9 F () S 50 J ( statement only has) S
2880 3368 P (one form.) S
2880 3808 P (On the other hand, a grammar may have several non-terminal symbols that start the same) S
2880 4048 P (way. In our sample grammar, all of) S
3600 4468 P 0 12 F 0 9 F (expr) S 108 J ( '+' expr) S
3600 4688 P (expr '-' expr) S
3600 4908 P (expr '*' expr) S
3600 5128 P (expr '/' expr) S
2880 5568 P 0 12 F 8 9 F (start) S 50 J ( with an ) S 0 12 F 0 9 F ("expr") S 0 12 F 8 9 F (.) S 50 J ( If the parser finds that the input begins with an ) S 0 12 F 0 9 F ("expr") S 0 12 F 8 9 F (,) S 50 J ( the parser) S
2880 5808 P (has no idea which rule matches the input until it has read the operator following the first) S
2880 6048 P 0 12 F 0 9 F ("expr") S 0 12 F 8 9 F (.) S
2880 6488 P (The) S 50 J ( parser chooses which state it will enter next by looking at the next input token. This) S
2880 6728 P (token is called the ) S 0 12 F 8 9 F I (lookahead) S 50 J ( symbol) S E 0 12 F 8 9 F () S 50 J ( for that state.) S
2880 7548 P 0 12 F 8 12 F B (4.2) S 67 J ( Diagramming States) S E
2880 8268 P 0 12 F 8 9 F (YAY) S 50 J ( uses simple diagrams to describe the various states of the parser. These diagrams) S
2880 8508 P (show what the parser has seen and what it is looking for next. The diagrams are given in) S
2880 8748 P (the Parser Description report prouced by YAY \(see Chapter 6\).) S
2880 9188 P (For example, consider the state where the parser has just read a complete ) S 0 12 F 0 9 F ("expr") S 0 12 F 8 9 F () S 50 J ( at the) S
2880 9428 P (beginning of a larger expression. It is now in a state where it expects to see one of the) S
2880 9668 P (operators ) S 0 12 F 0 9 F (+) S 0 12 F 8 9 F (,) S 50 J ( ) S 0 12 F 0 9 F (-) S 0 12 F 8 9 F (,) S 50 J ( ) S 0 12 F 0 9 F (*) S 0 12 F 8 9 F (,) S 50 J ( or ) S 0 12 F 0 9 F (/) S 0 12 F 8 9 F (,) S 50 J ( or perhaps the ) S 0 12 F 0 9 F ($end) S 0 12 F 8 9 F () S 50 J ( marker indicating the end of input. YAY) S
2880 9908 P (diagrams this state as) S
3600 10328 P 0 12 F 0 9 F ($accept:) S 108 J ( expr.$end) S
3600 10548 P (expr: expr.'+' expr) S
3600 10768 P (expr: expr.'-' expr) S
3600 10988 P (expr: expr.'*' expr) S
3600 11208 P (expr: expr.'/' expr) S
2880 11648 P 0 12 F 8 9 F (This) S 50 J ( lists the possible grammar constructs that the parser may be working on. \(The first) S
2880 11888 P (line ) S 0 12 F 0 9 F ($accept) S 0 12 F 8 9 F () S 50 J ( stands for the Start symbol.\) The ) S 0 12 F 0 9 F (".") S 0 12 F 8 9 F () S 50 J ( indicates how much the parser has) S
2880 12128 P (read so far.) S
2880 12568 P (If the lookahead symbol is ) S 0 12 F 0 9 F (*) S 0 12 F 8 9 F (,) S 50 J ( the parser will switch to a state diagrammed by) S
3600 12988 P 0 12 F 0 9 F (expr:) S 108 J ( expr '*'.expr) S
2880 14520 P 0 12 F 57 12 F (DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD) S
2880 14800 P 0 12 F 24 10 F (21) S 4238 J ( Thinkage) S 50 J ( YAY Reference Manual) S 0 12 F
PE
2160 1968 P 8 9 F (In) S 50 J ( this state, the parser knows that the next thing to come will be another ) S 0 12 F 0 9 F ("expr") S 0 12 F 8 9 F (.) S 50 J ( This) S
2160 2208 P (means that the only valid tokens that can be read next are ) S 0 12 F 0 9 F (\() S 0 12 F 8 9 F () S 50 J ( or ) S 0 12 F 0 9 F (NUM) S 0 12 F 8 9 F (,) S 50 J ( since those are the) S
2160 2448 P (only things that start a valid ) S 0 12 F 0 9 F ("expr") S 0 12 F 8 9 F (.) S
2160 3268 P 0 12 F 8 12 F B (4.3) S 67 J ( State Actions) S E
2160 3988 P 0 12 F 8 9 F (There) S 50 J ( are several possible actions that the parser can take in a state:) S
2160 4428 P (\(a\) ) S 0 12 F 8 9 F I (accept) S E 0 12 F 8 9 F () S 50 J ( the input;) S
2160 4868 P (\(b\) ) S 0 12 F 8 9 F I (shift) S E 0 12 F 8 9 F () S 50 J ( to a new state;) S
2160 5308 P (\(c\)) S 510 J ( ) S 0 12 F 8 9 F I (reduce) S E 0 12 F 8 9 F () S 50 J ( one or more input tokens to a single non-terminal symbol, according to a) S
2880 5548 P (grammar rule;) S
2160 5988 P (\(d\) ) S 0 12 F 8 9 F I (goto) S E 0 12 F 8 9 F () S 50 J ( a new state;) S
2160 6428 P (\(e\) raise an ) S 0 12 F 8 9 F I (error) S E 0 12 F 8 9 F () S 50 J ( condition.) S
2160 6868 P (In order to decide which action to take, the parser checks the lookahead symbol \(except in) S
2160 7108 P (states where the parser can only take one possible action so the lookahead symbol is) S
2160 7348 P (irrelevant\).) S
2160 7788 P (This means that a typical state has a series of possible actions based upon the possible) S
2160 8028 P (values of the lookahead symbol. In YAY output, you might see) S
2880 8448 P 0 12 F 0 9 F ('+') S 108 J ( shift 8) S
2880 8668 P ('-' shift 7) S
2880 8888 P ('*' shift 6) S
2880 9108 P ('/' shift 5) S
2880 9328 P ('\)' shift 9) S
2880 9548 P ( . error) S
2160 9988 P 0 12 F 8 9 F (This) S 50 J ( says that if the parser is in this state and the lookahead symbol is ) S 0 12 F 0 9 F (+) S 0 12 F 8 9 F (,) S 50 J ( the parser will) S
2160 10228 P (shift to State 8. If the lookahead symbol is ) S 0 12 F 0 9 F (-) S 0 12 F 8 9 F (,) S 50 J ( the parser will shift to State 7, and so on.) S
2160 10668 P (The ) S 0 12 F 0 9 F (".") S 0 12 F 8 9 F () S 50 J ( in the final line stands for any other token not mentioned in the preceding list. If) S
2160 10908 P (the parser finds any unexpected tokens in this particular state, it will take the ) S 0 12 F 0 9 F (error) S
2160 11148 P 0 12 F 8 9 F (action.) S
2160 11588 P (The) S 50 J ( sections that follow explain precisely what each state action means and what the) S
2160 11828 P (parser does to handle these actions.) S
2160 14520 P 0 12 F 57 12 F (DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD) S
2160 14800 P 0 12 F 24 10 F (Thinkage) S 50 J ( YAY Reference Manual) S 4238 J ( 22) S 0 12 F
PE
2880 2308 P 8 12 F I (4.3.1) S 67 J ( The Accept Action) S E
2880 3028 P 0 12 F 8 9 F (The) S 50 J ( Accept action only happens when the parser is in a state that indicates it has seen a) S
2880 3268 P (complete input and the lookahead symbol is the end marker ) S 0 12 F 0 9 F ($end) S 0 12 F 8 9 F (.) S 50 J ( When the parser) S
2880 3508 P (takes the Accept action, ) S 0 12 F 0 9 F ("yyparse") S 0 12 F 8 9 F () S 50 J ( terminates and returns a zero to indicate that the) S
2880 3748 P (input was correct.) S
2880 4868 P 0 12 F 8 12 F I (4.3.2) S 67 J ( The Shift Action) S E
2880 5588 P 0 12 F 8 9 F (The) S 50 J ( Shift action happens when the parser is part way through a grammar construct and a) S
2880 5828 P (new token is read in. As an example, State 4 in our sample parser is diagrammed with) S
3600 6248 P 0 12 F 0 9 F (expr:) S 108 J ( expr.'+' expr) S
3600 6468 P (expr: expr.'-' expr) S
3600 6688 P (expr: expr.'*' expr) S
3600 6908 P (expr: expr.'/' expr) S
3600 7128 P (expr: '\(' expr.'\)') S
3600 7548 P ('+' shift 8) S
3600 7768 P ('-' shift 7) S
3600 7988 P ('*' shift 6) S
3600 8208 P ('/' shift 5) S
3600 8428 P ('\)' shift 9) S
3600 8648 P ( . error) S
2880 9088 P 0 12 F 8 9 F (This) S 50 J ( shows that the parser will shift to various other states depending on the value of the) S
2880 9328 P (lookahead symbol. For example, if the lookahead symbol is ) S 0 12 F 0 9 F (*) S 0 12 F 8 9 F () S 50 J ( the parser shifts to State 6) S
2880 9568 P (which has the diagram) S
3600 10028 P 0 12 F 0 9 F (expr:) S 108 J ( expr '*'.expr) S
3600 10448 P (NUM shift 2) S
3600 10668 P ('\(' shift 1) S
3600 10888 P ( . error) S
3600 11308 P (expr goto 11) S
2880 11748 P 0 12 F 8 9 F (In) S 50 J ( this new state, the parser has further shifts it can make, depending on the next) S
2880 11988 P (lookahead symbol.) S
2880 12428 P (When the parser shifts to a new state, it saves the previous state on a stack called the) S
2880 12668 P 0 12 F 8 9 F I (state) S 50 J ( stack) S E 0 12 F 8 9 F (.) S 50 J ( The stack provides a history of the states that the parser has passed through) S
2880 12908 P (while it was reading input. It is also a control mechanism as will be seen later in this) S
2880 13148 P (chapter.) S
2880 14520 P 0 12 F 57 12 F (DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD) S
2880 14800 P 0 12 F 24 10 F (23) S 4238 J ( Thinkage) S 50 J ( YAY Reference Manual) S 0 12 F
PE
2160 2168 P 8 9 F (Paralleling) S 50 J ( the state stack is a ) S 0 12 F 8 9 F I (value) S E 0 12 F 8 9 F () S 50 J ( stack that records the values of tokens and non-) S
2160 2408 P (terminal symbols encountered while parsing. The value of a token is the ) S 0 12 F 0 9 F ("yylval") S 0 12 F 8 9 F () S 50 J ( value) S
2160 2648 P (returned by ) S 0 12 F 0 9 F ("yylex") S 0 12 F 8 9 F () S 50 J ( at the time the token was read. The value of a non-terminal symbol) S
2160 2888 P (is the ) S 0 12 F 0 9 F ($$) S 0 12 F 8 9 F () S 50 J ( value set by the recognition action associated with that symbol's definition. If the) S
2160 3128 P (definition didn't have an associate recognition action, the value of the symbol is the value) S
2160 3368 P (of the first item in the symbol's definition.) S
2160 3808 P (At the same time that the Shift action pushes the current state onto the state stack, it also) S
2160 4048 P (pushes the ) S 0 12 F 0 9 F ("yylval") S 0 12 F 8 9 F () S 50 J ( value of the lookahead symbol \(token\) onto the value stack.) S
2160 5168 P 0 12 F 8 12 F I (4.3.3) S 67 J ( The Reduce Action) S E
2160 5888 P 0 12 F 8 9 F (The) S 50 J ( Reduce action takes place in states where the parser has recognized all the items) S
2160 6128 P (that make up a non-terminal symbol. For example, the diagram of State 9 in our sample) S
2160 6368 P (grammar is) S
2880 6788 P 0 12 F 0 9 F (expr:) S 108 J ( '\(' expr '\)'.) S
2880 7208 P ( . reduce \(6\)) S
2160 7648 P 0 12 F 8 9 F (At) S 50 J ( this point, the parser has seen all three components that make up the non-terminal) S
2160 7888 P (symbol ) S 0 12 F 0 9 F ("expr") S 0 12 F 8 9 F (:) S
2880 8308 P 0 12 F 0 9 F ('\(') S
2880 8528 P (expr) S
2880 8748 P ('\)') S
2160 9188 P 0 12 F 8 9 F (As) S 50 J ( the line) S
2880 9608 P 0 12 F 0 9 F () S 108 J ( . reduce \(6\)) S
2160 10048 P 0 12 F 8 9 F (shows,) S 50 J ( it doesn't matter what the lookahead symbol is at this point. The non-terminal) S
2160 10288 P (symbol has been recognized, and the parser is ready for a Reduce action. \(Note that the) S
2160 10528 P (\(6\) in the above line just means that the parser has recognized the non-terminal symbol) S
2160 10768 P (defined in rule \(6\) of the grammar. We will say more about this in a later chapter.\)) S
2160 11208 P (The Reduce action performs a number of operations. First, it pops states off the state) S
2160 11448 P (stack. If the recognized non-terminal symbol had N components, a reduction pops N-1) S
2160 11688 P (states off the stack. In other words, the parser goes back to the state it was in when it first) S
2160 11928 P (began to gather the recognized construct.) S
2160 12368 P (Next, the Reduce action pops values off the value stack. If the definition that is being) S
2160 12608 P (reduced consisted of N items, the Reduce action conceptually pops N values off the stack.) S
2160 12848 P (The topmost value on the stack is assigned to ) S 0 12 F 0 9 F ($N) S 0 12 F 8 9 F (,) S 50 J ( the next to ) S 0 12 F 0 9 F ($N-1) S 0 12 F 8 9 F (,) S 50 J ( and so on down to) S
2160 13088 P 0 12 F 0 9 F ($1) S 0 12 F 8 9 F (.) S
2160 14520 P 0 12 F 57 12 F (DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD) S
2160 14800 P 0 12 F 24 10 F (Thinkage) S 50 J ( YAY Reference Manual) S 4238 J ( 24) S 0 12 F
PE
2880 2168 P 8 9 F (Once) S 50 J ( the Reduce action has gathered all the ) S 0 12 F 0 9 F ($X) S 0 12 F 8 9 F () S 50 J ( values, the parser invokes the recognition) S
2880 2408 P (action that was associated with the grammar rule being reduced. This recognition will use) S
2880 2648 P (the ) S 0 12 F 0 9 F ($X) S 0 12 F 8 9 F () S 50 J ( values to come up with a ) S 0 12 F 0 9 F ($$) S 0 12 F 8 9 F () S 50 J ( value for the non-terminal symbol. This value is) S
2880 2888 P (pushed onto the value stack, thereby replacing the N values that were previously on the) S
2880 3128 P (stack.) S
2880 3568 P (If the non-terminal symbol had no recognition action, or if the recognition action did not set) S
2880 3808 P 0 12 F 0 9 F ($$) S 0 12 F 8 9 F (,) S 50 J ( the parser puts the value of ) S 0 12 F 0 9 F ($1) S 0 12 F 8 9 F () S 50 J ( back on the stack. \(In reality, the value is never) S
2880 4048 P (popped off.\)) S
2880 4488 P (Lastly, the Reduce action sets things up so that the lookahead symbol seems to be the) S
2880 4728 P (non-terminal symbol that was just recognized. For example, it may say that the lookahead) S
2880 4968 P (symbol is now an ) S 0 12 F 0 9 F ("expr") S 0 12 F 8 9 F () S 50 J ( instead of a token.) S
2880 6088 P 0 12 F 8 12 F I (4.3.4) S 67 J ( The Goto Action) S E
2880 6808 P 0 12 F 8 9 F (The) S 50 J ( Goto action is a continuation of the Reduce process. Goto is almost the same as) S
2880 7048 P (Shift -- the only difference is that the Goto action takes place when the lookahead symbol) S
2880 7288 P (is a non-terminal symbol while a Shift takes place when the lookahead symbol is a token.) S
2880 7728 P (For example, State 6 in our sample grammar reads) S
3600 8148 P 0 12 F 0 9 F (expr:) S 108 J ( expr '*'.expr) S
3600 8568 P (NUM shift 2) S
3600 8788 P ('\(' shift 1) S
3600 9008 P ( . error) S
3600 9428 P (expr goto 12) S
2880 9868 P 0 12 F 8 9 F (The) S 50 J ( first time the parser enters this state, the lookahead symbol will be a token and the) S
2880 10108 P (parser will Shift on the token into some state where it will begin to gather an ) S 0 12 F 0 9 F ("expr") S 0 12 F 8 9 F (.) S
2880 10348 P (When) S 50 J ( it has a complete ) S 0 12 F 0 9 F ("expr") S 0 12 F 8 9 F (,) S 50 J ( it will perform a Reduce action that will return to this) S
2880 10588 P (state and set the lookahead symbol to ) S 0 12 F 0 9 F ("expr") S 0 12 F 8 9 F (.) S 50 J ( Now when the parser has to decide what) S
2880 10828 P (to do next, it sees that it has an ) S 0 12 F 0 9 F ("expr") S 0 12 F 8 9 F () S 50 J ( for the lookahead symbol and therefore takes the) S
2880 11068 P (Goto action and moves to State 12.) S
2880 11508 P (The Shift action pushes the current state onto the state stack. The Goto does not have to) S
2880 11748 P (do this -- the state was on the stack already. Similarly, Shift pushes a value onto the value) S
2880 11988 P (stack, but Goto does not, since the value corresponding to the non-terminal symbol was) S
2880 12228 P (already put on the value stack by the Reduce action.) S
2880 12668 P (When the parser reaches the new state, the lookahead symbol will be restored to) S
2880 12908 P (whatever it was at the time of the Reduce action.) S
2880 14520 P 0 12 F 57 12 F (DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD) S
2880 14800 P 0 12 F 24 10 F (25) S 4238 J ( Thinkage) S 50 J ( YAY Reference Manual) S 0 12 F
PE
2160 1968 P 8 9 F (Essentially) S 50 J ( then, a Goto is like a Shift except that it takes place when you come ) S 0 12 F 8 9 F I (back) S E 0 12 F 8 9 F () S 50 J ( to a) S
2160 2208 P (state with the Reduce action. Also, a Shift is based on the value of a single input token) S
2160 2448 P (while a Goto is based on a non-terminal symbol.) S
2160 3568 P 0 12 F 8 12 F I (4.3.5) S 67 J ( The Error Action) S E
2160 4288 P 0 12 F 8 9 F (The) S 50 J ( parser takes the Error action when it encounters any input token which cannot validly) S
2160 4528 P (appear in a particular input location. When this happens, the parser raises the ) S 0 12 F 0 9 F ("error") S
2160 4768 P 0 12 F 8 9 F (condition.) S 50 J ( Since error-handling can be quite complicated, we will devote the whole of the) S
2160 5008 P (next chapter to the subject.) S
2160 14520 P 0 12 F 57 12 F (DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD) S
2160 14800 P 0 12 F 24 10 F (Thinkage) S 50 J ( YAY Reference Manual) S 4238 J ( 26) S 0 12 F
PE
7140 2448 P 8 18 F B (5.) S 100 J ( Error-Handling) S E
2880 4048 P 0 12 F 8 9 F (If) S 50 J ( a piece of input is invalid, the parser can do nothing with it. Except in extreme cases,) S
2880 4288 P (however, it is inappropriate for the parser to stop all processing as soon as an error is) S
2880 4528 P (found. Instead, the parser should skip over the invalid input and resume parsing as soon) S
2880 4768 P (after the error as possible. In this way, the parser can find many syntax errors in one pass) S
2880 5008 P (through the input, saving time and trouble for the user.) S
2880 5448 P (YAY therefore tries to generate a parser that can "restart" as soon as possible after an) S
2880 5688 P (error occurs. YAY does this by letting you specify points at which the parser can pick up) S
2880 5928 P (after errors. You may also dictate what special processing should take place if an error is) S
2880 6168 P (encountered at one of these points.) S
2880 6988 P 0 12 F 8 12 F B (5.1) S 67 J ( The "error" Symbol) S E
2880 7708 P 0 12 F 8 9 F (YAY's) S 50 J ( error handling facilities use the keyword ) S 0 12 F 0 9 F B (error) S E 0 12 F 8 9 F () S 50 J ( to signify an erroneous input. As a) S
2880 7948 P (result, ) S 0 12 F 0 9 F B (error) S E 0 12 F 8 9 F () S 50 J ( may not be used as the name of a user-defined token or non-terminal) S
2880 8188 P (symbol.) S
2880 8628 P (You should put ") S 0 12 F 0 9 F B (error) S E 0 12 F 8 9 F (") S 50 J ( in your grammar rules where error recovery might take place.) S
2880 8868 P (For example, you might write) S
3600 9288 P 0 12 F 0 9 F (statement:) S 108 J ( error) S
3600 9508 P ( | /* other definitions of a statement */;) S
2880 9948 P 0 12 F 8 9 F (This) S 50 J ( tells YAY that errors may occur in statements, and that after an error, the parser is) S
2880 10188 P (free to restart parsing at the end of a complete statement.) S
2880 11008 P 0 12 F 8 12 F B (5.2) S 67 J ( The Error Condition) S E
2880 11728 P 0 12 F 8 9 F (As) S 50 J ( noted in the last chapter, YAY will take the Error action if it finds an input that is not) S
2880 11968 P (valid in a particular location. The Error action has the following steps.) S
2880 12408 P (\(a\) See if the current state has a Shift action associated with the ) S 0 12 F 0 9 F B (error) S E 0 12 F 8 9 F () S 50 J ( symbol. If it) S
3600 12648 P (does, shift on this action.) S
2880 14520 P 0 12 F 57 12 F (DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD) S
2880 14800 P 0 12 F 24 10 F (27) S 4238 J ( Thinkage) S 50 J ( YAY Reference Manual) S 0 12 F
PE
2160 1968 P 8 9 F (\(b\)) S 500 J ( If) S 50 J ( the current state has no such action, pop the state off the stack and check the) S
2880 2208 P (next state. Also pop off the top value on the value stack, so that the state stack) S
2880 2448 P (and value stack stay in synch.) S
2160 2888 P (\(c\)) S 510 J ( Repeat) S 50 J ( \(b\) until the parser finds a state that can shift on the ) S 0 12 F 0 9 F B (error) S E 0 12 F 8 9 F () S 50 J ( symbol.) S
2160 3328 P (\(d\) Take the Shift action associated with the ) S 0 12 F 0 9 F B (error) S E 0 12 F 8 9 F () S 50 J ( symbol. Note that this pushes) S
2880 3568 P (the current state on the stack, i.e. the state that is capable of handling errors. No) S
2880 3808 P (new value is pushed onto the value stack -- the parser keeps whatever value was) S
2880 4048 P (already associated with the state that can handle errors.) S
2160 4488 P (When the parser shifts out of the state that can handle errors, the lookahead symbol will) S
2160 4728 P (be whatever token caused the error condition in the first place. The parser will then try to) S
2160 4968 P (proceed with normal processing.) S
2160 5408 P (Of course, it is quite possible that the original lookahead symbol is invalid in the new) S
2160 5648 P (context. If the lookahead symbol causes an error again, it is discarded and the error) S
2160 5888 P (condition stays in effect. The parser will continue to read new tokens and discard them) S
2160 6128 P (until it finds a token that can validly follow the ) S 0 12 F 0 9 F B (error) S E 0 12 F 8 9 F (.) S 50 J ( The parser will then take whatever) S
2160 6368 P (action is associated with the valid token.) S
2160 6808 P (In a typical grammar, the state that has been handling errors will eventually be popped off) S
2160 7048 P (the stack in a Reduce operation.) S
2160 7488 P (Notice that the parser always ) S 0 12 F 8 9 F I (Shifts) S E 0 12 F 8 9 F () S 50 J ( on the ) S 0 12 F 0 9 F B (error) S E 0 12 F 8 9 F () S 50 J ( token. It will never Reduce on ) S 0 12 F 0 9 F B (error) S E 0 12 F 8 9 F (,) S
2160 7728 P (even) S 50 J ( if the grammar has a state where ) S 0 12 F 0 9 F B (error) S E 0 12 F 8 9 F () S 50 J ( is associated with a Reduce action.) S
2160 8168 P (In some situations, an error condition will be raised and the parser will pop all the way to) S
2160 8408 P (the bottom of the state stack without finding a state that can handle the ) S 0 12 F 0 9 F B (error) S E 0 12 F 8 9 F () S 50 J ( symbol.) S
2160 8648 P (For example, the grammar may have no provisions for error recovery. In this case,) S
2160 8888 P 0 12 F 0 9 F ("yyparse") S 0 12 F 8 9 F () S 50 J ( simply terminates and returns a 1 to its caller.) S
2160 9708 P 0 12 F 8 12 F B (5.3) S 67 J ( Examples) S E
2160 10428 P 0 12 F 8 9 F (As) S 50 J ( a simple example, consider a parser for a simple desk calculator. All statements end in) S
2160 10668 P (a semicolon. Thus we might see the rule) S
2880 11088 P 0 12 F 0 9 F (statement) S 108 J ( : var '=' expr ';') S
2880 11308 P ( | expr ';') S
2880 11528 P ( | error ';') S
2880 11748 P ( ;) S
2160 12188 P 0 12 F 8 9 F (When) S 50 J ( an error occurs in input, the parser will pop back through the state stack until it) S
2160 12428 P (comes to a state where the ) S 0 12 F 0 9 F B (error) S E 0 12 F 8 9 F () S 50 J ( symbol is recognized. For example, the state might be) S
2160 12668 P (diagrammed as) S
2160 14520 P 0 12 F 57 12 F (DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD) S
2160 14800 P 0 12 F 24 10 F (Thinkage) S 50 J ( YAY Reference Manual) S 4238 J ( 28) S 0 12 F
PE
3600 1948 P 0 9 F ($accept:) S 108 J ( .statement $end) S
3600 2368 P (error shift 2) S
3600 2588 P (NUM shift 4) S
3600 2808 P (. error) S
3600 3228 P (var goto 7) S
3600 3448 P (expr goto 3) S
3600 3668 P (statement goto 5) S
2880 4108 P 0 12 F 8 9 F (If) S 50 J ( an error occurs anywhere in an input statement, the parser will pop back to this state,) S
2880 4348 P (then Shift to State 2. State 2 will look like) S
3600 4768 P 0 12 F 0 9 F (statement:) S 108 J ( error.';') S
2880 4968 P 0 12 F 8 7 F (.) S
3600 5188 P 0 12 F 0 9 F (';') S 108 J ( shift 6) S
3600 5408 P ( . error) S
2880 5848 P 0 12 F 8 9 F (In) S 50 J ( other words, the next token must be a semicolon. If it isn't, another error occurs. The) S
2880 6088 P (parser pops back to the previous state and takes the ) S 0 12 F 0 9 F B (error) S E 0 12 F 8 9 F () S 50 J ( shift again. Input will be) S
2880 6328 P (discarded token by token until a semicolon is found. When the semicolon is found, the) S
2880 6568 P (parser will be able to shift from State 2 to State 6 which is) S
3600 6988 P 0 12 F 0 9 F (statement:) S 108 J ( error ';'.) S
3600 7408 P ( . reduce \(3\)) S
2880 7848 P 0 12 F 8 9 F (The) S 50 J ( erroneous line is reduced to a ) S 0 12 F 0 9 F ("statement") S 0 12 F 8 9 F () S 50 J ( non-terminal symbol.) S
2880 8288 P (Now this example is simple, but it has its drawbacks. It will get you into trouble if the) S
2880 8528 P (grammar has any concept of block structure or parenthesization. Why? Once an error) S
2880 8768 P (occurs, the rule) S
3600 9188 P 0 12 F 0 9 F (statement) S 108 J ( : error ';') S
2880 9628 P 0 12 F 8 9 F (effectively) S 50 J ( tells the parser to discard absolutely everything until it finds a ) S 0 12 F 0 9 F (;) S 0 12 F 8 9 F () S 50 J ( character. If) S
2880 9868 P (you have a parser for C, for example, it would skip over important characters like ) S 0 12 F 0 9 F (\)) S 0 12 F 8 9 F () S 50 J ( or ) S 0 12 F 0 9 F (}) S
2880 10108 P 0 12 F 8 9 F (until) S 50 J ( it found a semicolon. Your parentheses and braces would be out of balance for the) S
2880 10348 P (rest of the input and the whole parsing process would be a waste of time. The same) S
2880 10588 P (principle applies to any rule that shows the ) S 0 12 F 0 9 F B (error) S E 0 12 F 8 9 F () S 50 J ( token followed by some other non-null) S
2880 10828 P (symbol: it can lead to hopeless confusion in a lot of grammars.) S
2880 11268 P (It is safer to write the rule in a form like) S
3600 11688 P 0 12 F 0 9 F (statement) S 108 J ( : error) S
3600 11908 P ( | ';') S
3600 12128 P ( | /* other stuff */) S
2880 12568 P 0 12 F 8 9 F (In) S 50 J ( this case, the ) S 0 12 F 0 9 F B (error) S E 0 12 F 8 9 F () S 50 J ( token only matches material until the parser finds something else) S
2880 12808 P (it recognizes \(e.g. the semicolon\). Once this happens, the ) S 0 12 F 0 9 F B (error) S E 0 12 F 8 9 F () S 50 J ( state will be reduced to) S
2880 13048 P (a ) S 0 12 F 0 9 F ("statement") S 0 12 F 8 9 F () S 50 J ( symbol and popped off the stack. Parsing can then proceed as usual.) S
2880 14520 P 0 12 F 57 12 F (DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD) S
2880 14800 P 0 12 F 24 10 F (29) S 4238 J ( Thinkage) S 50 J ( YAY Reference Manual) S 0 12 F
PE
2160 2308 P 8 12 F B (5.4) S 67 J ( Error Recognition Actions) S E
2160 3028 P 0 12 F 8 9 F (The) S 50 J ( easiest way to generate an error message is to associate a recognition action with the) S
2160 3268 P (grammar rule that recognizes the error. You can do something simple like) S
2880 3688 P 0 12 F 0 9 F (statement:) S 108 J ( error) S
2880 3908 P ( {) S
2880 4128 P ( printf\("You made an error!\\n"\);) S
2880 4348 P ( }) S
2160 4788 P 0 12 F 8 9 F (or) S 50 J ( you can be fancier, as in) S
2880 5208 P 0 12 F 0 9 F (line:) S 108 J ( error '\\n' prompt line) S
2880 5428 P ( { $$ = $4; };) S
2880 5648 P (prompt: /* null token */) S
2880 5868 P ( { printf\("Please re-enter line.\\n"\); };) S
2160 6308 P 0 12 F 8 9 F (If) S 50 J ( an error occurs, the parser skips until it finds a new-line character. After the new-line, it) S
2160 6548 P (will always find a null token matching ) S 0 12 F 0 9 F ("prompt") S 0 12 F 8 9 F (,) S 50 J ( and the recognition action for ) S 0 12 F 0 9 F ("prompt") S
2160 6788 P 0 12 F 8 9 F (will) S 50 J ( print out the message) S
2880 7208 P 0 12 F 0 9 F (Please) S 108 J ( re-enter line) S
2160 7648 P 0 12 F 8 9 F (The) S 50 J ( final symbol in the rule is another ) S 0 12 F 0 9 F ("line") S 0 12 F 8 9 F (,) S 50 J ( and the action after the ) S 0 12 F 0 9 F B (error) S E 0 12 F 8 9 F () S 50 J ( rule shows) S
2160 7888 P (that the result of the rule \() S 0 12 F 0 9 F ($$) S 0 12 F 8 9 F (\)) S 50 J ( should be the material associated with the second input line.) S
2160 8328 P (All this means that if the user makes a mistake entering an input line, the parser prints out) S
2160 8568 P (an error message and accepts a second input line in place of the first. This allows for an) S
2160 8808 P (interactive user to correct an input line that was incorrectly typed the first time.) S
2160 9248 P (Of course, this set-up will only work if the user doesn't make an error the second time the) S
2160 9488 P (line is typed too. If the next token he or she types is also invalid, the parser will discard the) S
2160 9728 P (token and decide that it's still gobbling up the original error.) S
2160 10548 P 0 12 F 8 12 F B (5.5) S 67 J ( The yyclearin Macro) S E
2160 11268 P 0 12 F 8 9 F (After) S 50 J ( an Error action, the parser will restore the lookahead symbol to the value it had at the) S
2160 11508 P (time the error was detected. However, this is sometimes undesirable.) S
2160 11948 P (For example, your grammar may have a recognition action associated with the ) S 0 12 F 0 9 F B (error) S E
2160 12188 P 0 12 F 8 9 F (symbol) S 50 J ( and this may read through the next lot of input until it finds the next sure-to-be-) S
2160 12428 P (valid data. If this happens, you certainly don't want the parser to pick up the old) S
2160 12668 P (lookahead symbol again once error recovery is finished.) S
2160 13108 P (If you want the parser to throw away the old lookahead symbol after an error, put) S
2160 14520 P 0 12 F 57 12 F (DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD) S
2160 14800 P 0 12 F 24 10 F (Thinkage) S 50 J ( YAY Reference Manual) S 4238 J ( 30) S 0 12 F
PE
3600 2148 P 0 9 F (yyclearin) S 108 J ( ;) S
2880 2588 P 0 12 F 8 9 F (in) S 50 J ( the recognition action associated with the ) S 0 12 F 0 9 F B (error) S E 0 12 F 8 9 F () S 50 J ( symbol. ) S 0 12 F 0 9 F ("yyclearin") S 0 12 F 8 9 F () S 50 J ( is a macro) S
2880 2828 P (that expands into code that discards the lookahead symbol.) S
2880 3648 P 0 12 F 8 12 F B (5.6) S 67 J ( The yyerror Function) S E
2880 4368 P 0 12 F 8 9 F (The) S 50 J ( first thing the parser does when it performs the Error action is to call a function named) S
2880 4608 P 0 12 F 0 9 F ("yyerror") S 0 12 F 8 9 F (.) S 50 J ( This happens ) S 0 12 F 8 9 F I (before) S E 0 12 F 8 9 F () S 50 J ( the parser begins going down the state stack in search) S
2880 4848 P (of a state that can handle the ) S 0 12 F 0 9 F B (error) S E 0 12 F 8 9 F () S 50 J ( symbol. ) S 0 12 F 0 9 F ("yyerror") S 0 12 F 8 9 F () S 50 J ( must be supplied by the user --) S
2880 5088 P (note that its name is in lower case.) S
2880 5528 P (The simplest ) S 0 12 F 0 9 F ("yyerror") S 0 12 F 8 9 F () S 50 J ( functions either abort the parsing job or just return so that the) S
2880 5768 P (parser can perform its standard error handling.) S
2880 6208 P (The ) S 0 12 F 0 9 F ("yyerror") S 0 12 F 8 9 F () S 50 J ( function is passed one argument: a character string describing the type) S
2880 6448 P (of error that just took place. This string is almost always) S
3600 6868 P 0 12 F 0 9 F ("Syntax) S 108 J ( error") S
2880 7308 P 0 12 F 8 9 F (The) S 50 J ( only other argument strings that might be passed are) S
3600 7728 P 0 12 F 0 9 F ("Not) S 108 J ( enough space for parser stacks") S
3600 7948 P ("Parser stack overflow") S
2880 8388 P 0 12 F 8 9 F (which) S 50 J ( are used when the parser runs out of memory for the state stack.) S
2880 8828 P (Once ) S 0 12 F 0 9 F ("yyerror") S 0 12 F 8 9 F () S 50 J ( returns to ) S 0 12 F 0 9 F ("yyparse") S 0 12 F 8 9 F (,) S 50 J ( the parser will proceed popping down the stack) S
2880 9068 P (in search of a state that can handle errors.) S
2880 9508 P (If another error is encountered soon after the first, ) S 0 12 F 0 9 F ("yyerror") S 0 12 F 8 9 F () S 50 J ( will ) S 0 12 F 8 9 F I (not) S E 0 12 F 8 9 F () S 50 J ( be called again.) S
2880 9748 P (The parser considers itself to be in a "potential error" situation until it finds three correct) S
2880 9988 P (tokens in a row. This avoids the torrents of error messages that often occur as the parser) S
2880 10228 P (wades through input in search of some recognizable sequence.) S
2880 10668 P (Once the parser has found three correct tokens in a row, it leaves the "potential error") S
2880 10908 P (situation. If a new error is found later on, ) S 0 12 F 0 9 F ("yyerror") S 0 12 F 8 9 F () S 50 J ( will be called again.) S
2880 12028 P 0 12 F 8 12 F I (5.6.1) S 67 J ( Changing yychar in yyerror) S E
2880 12748 P 0 12 F 8 9 F (The) S 50 J ( ) S 0 12 F 0 9 F ("yyerror") S 0 12 F 8 9 F () S 50 J ( function may change the value of ) S 0 12 F 0 9 F ("yychar") S 0 12 F 8 9 F () S 50 J ( \(the variable that indicates) S
2880 12988 P (the type of token just read by ) S 0 12 F 0 9 F ("yylex") S 0 12 F 8 9 F (\).) S 50 J ( If ) S 0 12 F 0 9 F ("yyerror") S 0 12 F 8 9 F () S 50 J ( does this, ) S 0 12 F 0 9 F ("yyparse") S
2880 13228 P 0 12 F 8 9 F (immediately) S 50 J ( gets rid of the error condition and tries to make a shift or reduce using the) S
2880 14520 P 0 12 F 57 12 F (DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD) S
2880 14800 P 0 12 F 24 10 F (31) S 4238 J ( Thinkage) S 50 J ( YAY Reference Manual) S 0 12 F
PE
2160 1968 P 8 9 F (new) S 50 J ( ) S 0 12 F 0 9 F ("yychar") S 0 12 F 8 9 F () S 50 J ( token. If there is a valid action that can be taken, ) S 0 12 F 0 9 F ("yyparse") S 0 12 F 8 9 F () S 50 J ( will) S
2160 2208 P (proceed as if the error never happened.) S
2160 2648 P (This behavior can help you deal with at least two kinds of unusual situations:) S
2160 3088 P (\(a\) When ) S 0 12 F 0 9 F ("yyerror") S 0 12 F 8 9 F () S 50 J ( is attempting to correct a problem by supplying the correct) S
2880 3328 P (input. For example, it may be possible to determine what input should have been) S
2880 3568 P (received; the classic example is a compiler which can figure out that a particular) S
2880 3808 P (statement should have ended in a semicolon. In this case, ) S 0 12 F 0 9 F ("yyerror") S 0 12 F 8 9 F () S 50 J ( can) S
2880 4048 P (supply the missing semicolon by assigning an appropriate value to ) S 0 12 F 0 9 F ("yychar") S 0 12 F 8 9 F (.) S 50 J ( If) S
2880 4288 P 0 12 F 0 9 F ("yyerror") S 0 12 F 8 9 F () S 50 J ( guessed correctly, action can proceed as if the problem didn't occur.) S
2160 4728 P (\(b\) When a particular token can be interpreted in different ways in different contexts.) S
2880 4968 P (For example, suppose that ) S 0 12 F 0 9 F (X) S 0 12 F 8 9 F () S 50 J ( can be a keyword in some contexts and a variable) S
2880 5208 P (name in others. You can write ) S 0 12 F 0 9 F ("yylex") S 0 12 F 8 9 F () S 50 J ( so that it always interprets ) S 0 12 F 0 9 F (X) S 0 12 F 8 9 F () S 50 J ( as a) S
2880 5448 P (keyword. If ) S 0 12 F 0 9 F ("yyerror") S 0 12 F 8 9 F () S 50 J ( is invoked because that keyword is invalid in a particular) S
2880 5688 P (context, ) S 0 12 F 0 9 F ("yyerror") S 0 12 F 8 9 F () S 50 J ( can change ) S 0 12 F 0 9 F ("yychar") S 0 12 F 8 9 F () S 50 J ( and see if the input is valid when ) S 0 12 F 0 9 F (X) S
2880 5928 P 0 12 F 8 9 F (is) S 50 J ( interpreted as a variable name.) S
2160 6368 P (If ) S 0 12 F 0 9 F ("yyerror") S 0 12 F 8 9 F () S 50 J ( changes ) S 0 12 F 0 9 F ("yychar") S 0 12 F 8 9 F (,) S 50 J ( ) S 0 12 F 0 9 F ("yyerror") S 0 12 F 8 9 F () S 50 J ( may have to inform ) S 0 12 F 0 9 F ("yylex") S 0 12 F 8 9 F () S 50 J ( of the) S
2160 6608 P (situation so that ) S 0 12 F 0 9 F ("yylex") S 0 12 F 8 9 F () S 50 J ( can return the proper input token the next time it is called.) S
2160 7428 P 0 12 F 8 12 F B (5.7) S 67 J ( The yyerrflag Variable) S E
2160 8148 P 0 12 F 0 9 F ("yyerrflag") S 0 12 F 8 9 F () S 50 J ( is an external integer variable whose value must be ) S 0 12 F 0 9 F (0) S 0 12 F 8 9 F (,) S 50 J ( ) S 0 12 F 0 9 F (1) S 0 12 F 8 9 F (,) S 50 J ( ) S 0 12 F 0 9 F (2) S 0 12 F 8 9 F (,) S 50 J ( or ) S 0 12 F 0 9 F (3) S 0 12 F 8 9 F (.) S
2160 8388 P 0 12 F 0 9 F (YYABORT) S 0 12 F 8 9 F () S 50 J ( \(described in a later section\) is called if ) S 0 12 F 0 9 F ("yyerrflag") S 0 12 F 8 9 F () S 50 J ( does not have one of) S
2160 8628 P (these values.) S
2160 9068 P (As we mentioned earlier, ) S 0 12 F 0 9 F ("yyparse") S 0 12 F 8 9 F () S 50 J ( doesn't leave its error state until three consecutive) S
2160 9308 P (valid tokens have been read. ) S 0 12 F 0 9 F ("yyerrflag") S 0 12 F 8 9 F () S 50 J ( is used in this process.) S
2160 9748 P (If ) S 0 12 F 0 9 F ("yyerrflag") S 0 12 F 8 9 F () S 50 J ( is zero, ) S 0 12 F 0 9 F ("yyerror") S 0 12 F 8 9 F () S 50 J ( will be invoked the next time an error is) S
2160 9988 P (encountered. As soon as ) S 0 12 F 0 9 F ("yyerror") S 0 12 F 8 9 F () S 50 J ( is invoked, it sets ) S 0 12 F 0 9 F ("yyerrflag") S 0 12 F 8 9 F () S 50 J ( to ) S 0 12 F 0 9 F (3) S 0 12 F 8 9 F (.) S 50 J ( Each time a) S
2160 10228 P 0 12 F 0 9 F ("yyparse") S 0 12 F 8 9 F () S 50 J ( shifts on a valid token, ) S 0 12 F 0 9 F ("yyerrflag") S 0 12 F 8 9 F () S 50 J ( is decremented, until it gets to zero. At) S
2160 10468 P (this point, ) S 0 12 F 0 9 F ("yyparse") S 0 12 F 8 9 F () S 50 J ( leaves its error state; if a new error occurs, ) S 0 12 F 0 9 F ("yyerror") S 0 12 F 8 9 F () S 50 J ( will be) S
2160 10708 P (called again.) S
2160 11148 P (There are two ways in which actions can use ) S 0 12 F 0 9 F ("yyerrflag") S 0 12 F 8 9 F (.) S 50 J ( First, they can check the) S
2160 11388 P (variable to see if there has been a recent error. For example, if an action encounters a) S
2160 11628 P (semantic error, it can check ) S 0 12 F 0 9 F ("yyerrflag") S 0 12 F 8 9 F () S 50 J ( to see if it is non-zero \(meaning that there has) S
2160 11868 P (been a recent syntax error\). If ) S 0 12 F 0 9 F ("yyerrflag") S 0 12 F 8 9 F () S 50 J ( is non-zero, the semantic error is almost) S
2160 12108 P (certainly a result of the previous syntax error. Using this information, the action may) S
2160 12348 P (choose to modify or suppress the semantic error message.) S
2160 12788 P (Second, actions can set ) S 0 12 F 0 9 F ("yyerrflag") S 0 12 F 8 9 F () S 50 J ( to a value if they want to prevent calls to) S
2160 13028 P 0 12 F 0 9 F ("yyerror") S 0 12 F 8 9 F (.) S 50 J ( As long as ) S 0 12 F 0 9 F ("yyerrflag") S 0 12 F 8 9 F () S 50 J ( is non-zero, ) S 0 12 F 0 9 F ("yyerror") S 0 12 F 8 9 F () S 50 J ( will not be called. This) S
2160 14520 P 0 12 F 57 12 F (DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD) S
2160 14800 P 0 12 F 24 10 F (Thinkage) S 50 J ( YAY Reference Manual) S 4238 J ( 32) S 0 12 F
PE
2880 1968 P 8 9 F (may) S 50 J ( be useful if an action is smart enough to realize that it is still recovering from a) S
2880 2208 P (previous error and ) S 0 12 F 0 9 F ("yyerror") S 0 12 F 8 9 F () S 50 J ( should not be called again for a while.) S
2880 3028 P 0 12 F 8 12 F B (5.8) S 67 J ( The yyerrok Macro) S E
2880 3748 P 0 12 F 8 9 F (In) S 50 J ( some situations, you may want ) S 0 12 F 0 9 F ("yyerror") S 0 12 F 8 9 F () S 50 J ( to be called even if the parser hasn't seen) S
2880 3988 P (three correct tokens since the last error.) S
2880 4428 P (For example, suppose you have a parser for a line by line desk calculator. A line of input) S
2880 4668 P (contains errors, so ) S 0 12 F 0 9 F ("yyerror") S 0 12 F 8 9 F () S 50 J ( is called. ) S 0 12 F 0 9 F ("yyerror") S 0 12 F 8 9 F () S 50 J ( prints an error message to the) S
2880 4908 P (user, throws away the rest of the line, and prompts for new input. If the next line contains) S
2880 5148 P (an error in the first three tokens, the parser will normally start discarding input ) S 0 12 F 8 9 F I (without) S E
2880 5388 P 0 12 F 8 9 F (calling) S 50 J ( ) S 0 12 F 0 9 F ("yyerror") S 0 12 F 8 9 F () S 50 J ( again. This means that ) S 0 12 F 0 9 F ("yyerror") S 0 12 F 8 9 F () S 50 J ( doesn't print an error message) S
2880 5628 P (for the user, even though the input line is wrong.) S
2880 6068 P (To avoid this problem, you can explicitly tell the parser to leave its "potential error" state,) S
2880 6308 P (even if it hasn't yet seen three correct tokens. Simply say) S
3600 6728 P 0 12 F 0 9 F (yyerrok) S 108 J ( ;) S
2880 7168 P 0 12 F 8 9 F (as) S 50 J ( part of the error recognition action. For example, you might have the rule) S
3600 7588 P 0 12 F 0 9 F (expr) S 108 J ( : error) S
3600 7808 P ( {) S
3600 8028 P ( yyerrok;) S
3600 8248 P ( printf\("Please re-enter line.\\n"\);) S
3600 8468 P ( yyclearin;) S
3600 8688 P ( }) S
2880 9128 P ("yyerrok") S 0 12 F 8 9 F () S 50 J ( is a macro that is expanded into code that takes the parser out of its) S
2880 9368 P ("potential error" state and lets it start fresh. More precisely, ) S 0 12 F 0 9 F ("yyerrok") S 0 12 F 8 9 F () S 50 J ( sets) S
2880 9608 P 0 12 F 0 9 F ("yyerrflag") S 0 12 F 8 9 F () S 50 J ( to zero.) S
2880 10428 P 0 12 F 8 12 F B (5.9) S 67 J ( Other Error Support Routines) S E
2880 11148 P 0 12 F 0 9 F (YYABORT) S 0 12 F 8 9 F () S 50 J ( halts ) S 0 12 F 0 9 F ("yyparse") S 0 12 F 8 9 F () S 50 J ( in midstream and immediately returns a ) S 0 12 F 0 9 F (1) S 0 12 F 8 9 F (.) S 50 J ( To the function) S
2880 11388 P (that called ) S 0 12 F 0 9 F ("yyparse") S 0 12 F 8 9 F (,) S 50 J ( this means that ) S 0 12 F 0 9 F ("yyparse") S 0 12 F 8 9 F () S 50 J ( failed for some reason.) S
2880 11828 P 0 12 F 0 9 F (YYACCEPT) S 0 12 F 8 9 F () S 50 J ( halts the parser in midstream and returns a ) S 0 12 F 0 9 F (0) S 0 12 F 8 9 F (.) S 50 J ( To the function that called) S
2880 12068 P 0 12 F 0 9 F ("yyparse") S 0 12 F 8 9 F (,) S 50 J ( this means that ) S 0 12 F 0 9 F ("yyparse") S 0 12 F 8 9 F () S 50 J ( terminated successfully, even if the entire input) S
2880 12308 P (has not yet been scanned.) S
2880 12748 P 0 12 F 0 9 F (YYERROR) S 0 12 F 8 9 F () S 50 J ( \(note that this is upper case\) is a macro that "fakes" an error. When ) S 0 12 F 0 9 F (YYERROR) S 0 12 F 8 9 F () S 50 J ( is) S
2880 12988 P (encountered in the code, the parser will react as if it just saw an error and will go about) S
2880 14520 P 0 12 F 57 12 F (DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD) S
2880 14800 P 0 12 F 24 10 F (33) S 4238 J ( Thinkage) S 50 J ( YAY Reference Manual) S 0 12 F
PE
2160 1968 P 8 9 F (recovering) S 50 J ( from the error. In Chapter 9, we will give an example of how ) S 0 12 F 0 9 F (YYERROR) S 0 12 F 8 9 F () S 50 J ( can be) S
2160 2208 P (useful.) S
2160 14520 P 0 12 F 57 12 F (DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD) S
2160 14800 P 0 12 F 24 10 F (Thinkage) S 50 J ( YAY Reference Manual) S 4238 J ( 34) S 0 12 F
PE
7660 2448 P 8 18 F B (6.) S 100 J ( YAY Output) S E
2880 4048 P 0 12 F 8 9 F (YAY) S 50 J ( can produce several output files. Options on the YAY command line dictate which) S
2880 4288 P (files are actually generated.) S
2880 4728 P (The most important output file is the one containing source code that can be compiled into) S
2880 4968 P (the actual parser. The name of this file is specified with the ) S 0 12 F 0 9 F (Parser=file) S 0 12 F 8 9 F () S 50 J ( command line) S
2880 5208 P (option.) S
2880 5648 P (Another possible output file contains manifest definitions. The name of this file is specified) S
2880 5888 P (with ) S 0 12 F 0 9 F (Header=file) S 0 12 F 8 9 F () S 50 J ( on the command line. This file is a distillation of the declarations) S
2880 6128 P (section of the YAY input. For example, all the ) S 0 12 F 0 9 F (%token) S 0 12 F 8 9 F () S 50 J ( directives are restated in terms of) S
2880 6368 P (manifest definitions.) S
3600 6788 P 0 12 F 0 9 F (%token) S 108 J ( IF) S
2880 7228 P 0 12 F 8 9 F (would) S 50 J ( appear as) S
3600 7648 P 0 12 F 0 9 F (#define) S 108 J ( IF 257) S
2880 8088 P 0 12 F 8 9 F (in) S 50 J ( the C version of the manifests \(assuming that ) S 0 12 F 0 9 F (IF) S 0 12 F 8 9 F () S 50 J ( was the first token in the declarations) S
2880 8328 P (section\). By including this file with) S
3600 8748 P 0 12 F 0 9 F (#include) S 108 J ( "filename") S
2880 9188 P 0 12 F 8 9 F (separately) S 50 J ( compiled modules can make use of all the pertinent definitions in the YAY) S
2880 9428 P (input.) S
2880 9868 P (The third output file that YAY can produce is called the Parser Description. The name of) S
2880 10108 P (the file is specified with ) S 0 12 F 0 9 F (Description=file) S 0 12 F 8 9 F () S 50 J ( on the command line. The Parser) S
2880 10348 P (Description is split into three sections:) S
2880 10788 P (\(a\) a summary of the grammar rules;) S
2880 11228 P (\(b\) a list of state descriptions;) S
2880 11668 P (\(c\)) S 510 J ( a) S 50 J ( list of statistics for the parser generated by YAY.) S
2880 12108 P (In the sections that follow, we will show what the Parser Description looks like for the) S
2880 12348 P (following grammar:) S
2880 14520 P 0 12 F 57 12 F (DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD) S
2880 14800 P 0 12 F 24 10 F (35) S 4238 J ( Thinkage) S 50 J ( YAY Reference Manual) S 0 12 F
PE
2880 1948 P 0 9 F (%token) S 108 J ( IF ELSE A) S
2880 2168 P (%%) S
2880 2388 P (stmt : IF stmt ELSE stmt) S
2880 2608 P ( | IF stmt) S
2880 2828 P ( | A) S
2880 3048 P ( ;) S
2160 3868 P 0 12 F 8 12 F B (6.1) S 67 J ( Rules Summary) S E
2160 4588 P 0 12 F 8 9 F (The) S 50 J ( rules summary section of the Parser Description begins with the command line used) S
2160 4828 P (to invoke YAY. This is intended to serve as a "heading" for the output material. We'll use) S
2160 5068 P (the command line) S
2880 5488 P 0 12 F 0 9 F (yay) S 108 J ( infile.y description=out +verbose) S
2160 5928 P 0 12 F 8 9 F (YAY) S 50 J ( input is in the file ) S 0 12 F 0 9 F (infile.y) S 0 12 F 8 9 F () S 50 J ( and output is written to the file ) S 0 12 F 0 9 F (out) S 0 12 F 8 9 F (.) S 50 J ( We have specified) S
2160 6168 P (the ) S 0 12 F 0 9 F (+Verbose) S 0 12 F 8 9 F () S 50 J ( option because this provides the largest amount of output. See Appendix) S
2160 6408 P (C for more information on the YAY command line.) S
2160 6848 P (Next comes a summary of the grammar rules. In our example, we will have) S
2880 7268 P 0 12 F 0 9 F (Rules:) S
2880 7488 P () S 108 J ( \(0\) $accept: stmt $end) S
2880 7708 P ( \(1\) stmt: IF stmt ELSE stmt) S
2880 7928 P ( \(2\) stmt: IF stmt) S
2880 8148 P ( \(3\) stmt: A) S
2160 8588 P 0 12 F 8 9 F (The) S 50 J ( 0th rule will always be the definition for a symbol named ) S 0 12 F 0 9 F ($accept) S 0 12 F 8 9 F (.) S 50 J ( This describes) S
2160 8828 P (what a complete input looks like: the Start symbol followed by the end marker. Other rules) S
2160 9068 P (are those given in the grammar.) S
2160 9508 P (YAY puts a formfeed character on the line after the last grammar rule so that the next part) S
2160 9748 P (of the Parser Description starts on a new page.) S
2160 10568 P 0 12 F 8 12 F B (6.2) S 67 J ( State Descriptions) S E
2160 11288 P 0 12 F 8 9 F (The) S 50 J ( Parser Description output contains complete descriptions of every possible state. For) S
2160 11528 P (example, here is the description of one state from our sample grammar.) S
2880 11948 P 0 12 F 0 9 F (State) S 108 J ( 2) S
2880 12168 P ( stmt : IF.stmt ELSE stmt) S
2880 12388 P ( stmt : IF.stmt) S
2160 14520 P 0 12 F 57 12 F (DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD) S
2160 14800 P 0 12 F 24 10 F (Thinkage) S 50 J ( YAY Reference Manual) S 4238 J ( 36) S 0 12 F
PE
3600 1948 P 0 9 F () S 108 J ( IF shift 2) S
3600 2168 P ( A shift 1) S
3600 2388 P ( . error) S
3600 2808 P ( stmt goto 4) S
2880 3248 P 0 12 F 8 9 F (By) S 50 J ( now, this sort of diagram should be familiar to you. The numbers after the word) S
2880 3488 P 0 12 F 0 9 F ("shift") S 0 12 F 8 9 F () S 50 J ( indicate the state to which the parser will Shift if the lookahead symbol happens) S
2880 3728 P (to be ) S 0 12 F 0 9 F (IF) S 0 12 F 8 9 F () S 50 J ( or ) S 0 12 F 0 9 F (A) S 0 12 F 8 9 F (.) S 50 J ( If the lookahead symbol is anything else, the parser raises the error) S
2880 3968 P (condition and starts error recovery.) S
2880 4408 P (If the parser pops back to state 2 by means of a Reduce Action, the lookahead symbol will) S
2880 4648 P (now be ) S 0 12 F 0 9 F ("stmt") S 0 12 F 8 9 F () S 50 J ( and the parser will Goto state 4.) S
2880 5088 P (As another example of a state, here is State 1.) S
3600 5508 P 0 12 F 0 9 F (State) S 108 J ( 1) S
3600 5728 P ( stmt : A. \(3\)) S
3600 6148 P ( . reduce \(3\)) S
2880 6588 P 0 12 F 8 9 F (This) S 50 J ( is the state that is entered when an ) S 0 12 F 0 9 F (A) S 0 12 F 8 9 F () S 50 J ( token has been found. The \(3\) on the end of) S
2880 6828 P (the first line is a ) S 0 12 F 8 9 F I (rule) S 50 J ( number) S E 0 12 F 8 9 F (.) S 50 J ( It indicates that this particular line sums up the whole of the) S
2880 7068 P (third grammar rule that was specified in the YAY input. The line) S
3600 7488 P 0 12 F 0 9 F () S 108 J ( . reduce \(3\)) S
2880 7928 P 0 12 F 8 9 F (indicates) S 50 J ( that no matter what token comes next, we can reduce this particular input using) S
2880 8168 P (grammar rule \(3\) and say that we have successfully collected a valid ) S 0 12 F 0 9 F ("stmt") S 0 12 F 8 9 F (.) S 50 J ( The parser) S
2880 8408 P (will perform a reduction by popping the top state off the stack and setting the lookahead) S
2880 8648 P (symbol to ) S 0 12 F 0 9 F ("stmt") S 0 12 F 8 9 F (.) S
2880 9088 P (It) S 50 J ( is important to distinguish between) S
3600 9508 P 0 12 F 0 9 F () S 108 J ( A shift 1) S
2880 9948 P 0 12 F 8 9 F (in) S 50 J ( State 2 and) S
3600 10368 P 0 12 F 0 9 F () S 108 J ( . reduce \(3\)) S
2880 10808 P 0 12 F 8 9 F (in) S 50 J ( State 1. In the Shift instruction, the number that follows is the number of a ) S 0 12 F 8 9 F I (state) S E 0 12 F 8 9 F (.) S 50 J ( In the) S
2880 11048 P (Reduce instruction, the number that follows is the number of a ) S 0 12 F 8 9 F I (grammar) S 50 J ( rule) S E 0 12 F 8 9 F () S 50 J ( \(using the) S
2880 11288 P (numbers given to the grammar rules in the first part of the Parser Description\). The) S
2880 11528 P (Parser Description always encloses rule numbers in parentheses, and leaves state) S
2880 11768 P (numbers as they are.) S
2880 14520 P 0 12 F 57 12 F (DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD) S
2880 14800 P 0 12 F 24 10 F (37) S 4238 J ( Thinkage) S 50 J ( YAY Reference Manual) S 0 12 F
PE
2160 1968 P 8 9 F (The) S 50 J ( complete state descriptions for the grammar are given below.) S
2880 2388 P 0 12 F 0 9 F (State) S 108 J ( 0) S
2880 2608 P ( $accept: .stmt $end) S
2880 3028 P ( IF shift 2) S
2880 3248 P ( A shift 1) S
2880 3468 P ( . error) S
2880 3888 P ( stmt goto 3) S
2880 4308 P (State 1) S
2880 4528 P ( \(3\) stmt: A.) S
2880 4948 P ( . reduce \(3\)) S
2880 5368 P (State 2) S
2880 5588 P ( stmt: IF.stmt ELSE stmt) S
2880 5808 P ( stmt: IF.stmt) S
2880 6228 P ( IF shift 2) S
2880 6448 P ( A shift 1) S
2880 6668 P ( . error) S
2880 7088 P ( stmt goto 4) S
2880 7508 P (State 3) S
2880 7728 P ( $accept: stmt.$end) S
2880 8148 P ( $end accept) S
2880 8368 P ( . error) S
2880 8788 P (State 4) S
2880 9008 P ( stmt: IF stmt.ELSE stmt) S
2880 9228 P ( \(2\) stmt: IF stmt. [ $end ELSE ]) S
2880 9648 P ( Shift/reduce conflict \(5,2\) on ELSE) S
2880 9868 P ( ELSE shift 5) S
2880 10088 P ( . reduce \(2\)) S
2880 10508 P (State 5) S
2880 10728 P ( stmt: IF stmt ELSE.stmt) S
2880 11148 P ( IF shift 2) S
2880 11368 P ( A shift 1) S
2880 11588 P ( . error) S
2880 12008 P ( stmt goto 6) S
2880 12428 P (State 6) S
2880 12648 P ( \(1\) stmt: IF stmt ELSE stmt.) S
2880 13068 P ( . reduce \(1\)) S
2160 14520 P 0 12 F 57 12 F (DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD) S
2160 14800 P 0 12 F 24 10 F (Thinkage) S 50 J ( YAY Reference Manual) S 4238 J ( 38) S 0 12 F
PE
2880 2168 P 8 9 F (The) S 50 J ( parser always begins in State 0, i.e. in a state where no input has been read yet. An) S
2880 2408 P (acceptable input is a ) S 0 12 F 0 9 F ("stmt") S 0 12 F 8 9 F () S 50 J ( followed by the end marker. When a ) S 0 12 F 0 9 F ("stmt") S 0 12 F 8 9 F () S 50 J ( has been) S
2880 2648 P (collected, the parser goes to State 3. In State 3, the required end marker ) S 0 12 F 0 9 F ($end) S 0 12 F 8 9 F () S 50 J ( indicates) S
2880 2888 P (that the input should be accepted. If anything else is found, it is excess input and means) S
2880 3128 P (an error.) S
2880 3568 P (In State 4, the rule labelled \(2\) has) S
3600 3988 P 0 12 F 0 9 F ([) S 108 J ( $end ELSE ]) S
2880 4428 P 0 12 F 8 9 F (on) S 50 J ( the end. This just means that the parser expects to see one of these two tokens next.) S
2880 5248 P 0 12 F 8 12 F B (6.3) S 67 J ( Conflict Summary) S E
2880 5968 P 0 12 F 8 9 F (As) S 50 J ( noted in the sample output, there is a shift/reduce conflict in State 4. If an ) S 0 12 F 0 9 F (ELSE) S 0 12 F 8 9 F () S 50 J ( is) S
2880 6208 P (encountered while the parser is in this state, the parser doesn't know whether to shift \(into) S
2880 6448 P (State 5\) or to reduce using rule \(2\). Thus YAY prints the message) S
3600 6868 P 0 12 F 0 9 F (Shift/reduce) S 108 J ( conflict \(5,2\) on ELSE) S
2880 7308 P 0 12 F 8 9 F (After) S 50 J ( the state descriptions, YAY prints a summary of all such conflicts. With our sample) S
2880 7548 P (grammar, the output contains) S
3600 7968 P 0 12 F 0 9 F (Conflicts:) S
3600 8188 P () S 108 J ( State Token Action) S
3600 8408 P ( 4 ELSE shift 5) S
3600 8628 P ( 4 ELSE reduce \(2\)) S
2880 9068 P 0 12 F 8 9 F (Each) S 50 J ( line in this summary gives the number of the state in which the conflict occurs, the) S
2880 9308 P (token\(s\) that cause the conflict, and the conflicting actions.) S
2880 10128 P 0 12 F 8 12 F B (6.4) S 67 J ( Parser Statistics) S E
2880 10848 P 0 12 F 8 9 F (The) S 50 J ( last section of the Parser Description is a set of statistics summarizing YAY's work.) S
2880 11088 P (Here are the stats we got when we ran our sample grammar through YAY.) S
2880 11508 P 0 12 F 0 10 F (4) S 120 J ( rules, 5 tokens, 2 variables, 7 states) S
2880 11728 P (Memory: max = 8K) S
2880 11948 P (States: 3 wasted, 4 resets) S
2880 12168 P (Items: 18, 0 kernel, \(2,0\) per state, maxival=16 \(1 w/s\)) S
2880 12388 P (Lalr: 1 calls, 2 recurs, \(0 trans, 12 epred\)) S
2880 12608 P (Actions: 0 entries, gotos: 0 entries) S
2880 12828 P (Exceptions: 1 states, 4 entries) S
2880 13048 P (Simple state elim: 0%, Error default elim: 33%) S
2880 14520 P 0 12 F 57 12 F (DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD) S
2880 14800 P 0 12 F 24 10 F (39) S 4238 J ( Thinkage) S 50 J ( YAY Reference Manual) S 0 12 F
PE
2160 1948 P 0 10 F (Optimizer:) S 120 J ( in 0, out 0) S
2160 2168 P (Size of tables: 24 bytes) S
2160 2388 P (2 seconds, final mem = 4) S
2160 2608 P (1 shift/reduce conflict) S
2160 3048 P 0 12 F 8 9 F (Some) S 50 J ( of these values are machine independent \(e.g. the number of rules\), others are) S
2160 3288 P (machine dependent \(e.g. the amount of memory used\), and some can be different every) S
2160 3528 P (time you run the job \(e.g. time elapsed while YAY was running\).) S
2160 3968 P (The statistic lines are described below. Many of these will be of no interest to the normal) S
2160 4208 P (user; YAY only generates them for the use of those maintaining the YAY software. A) S
2160 4448 P (number of the statistics refer to shift/reduce or reduce/reduce conflicts. We will discuss) S
2160 4688 P (these in a later chapter.) S
2160 5128 P 0 12 F 0 9 F (4) S 108 J ( rules, 5 tokens, 2 variables, 7 states) S
2880 5368 P 0 12 F 8 9 F (The) S 50 J ( four rules are the grammar rules given in the first part of the Parser) S
2880 5608 P (Description. The five tokens are ) S 0 12 F 0 9 F (A) S 0 12 F 8 9 F (,) S 50 J ( ) S 0 12 F 0 9 F (IF) S 0 12 F 8 9 F (,) S 50 J ( ) S 0 12 F 0 9 F (ELSE) S 0 12 F 8 9 F (,) S 50 J ( ) S 0 12 F 0 9 F ($end) S 0 12 F 8 9 F (,) S 50 J ( and ) S 0 12 F 0 9 F (error) S 0 12 F 8 9 F () S 50 J ( \(which is always) S
2880 5848 P (defined, even if we don't use it in this grammar\). The two variables are the non-) S
2880 6088 P (terminal symbols, ) S 0 12 F 0 9 F ("stmt") S 0 12 F 8 9 F () S 50 J ( and the special ) S 0 12 F 0 9 F ($accept) S 0 12 F 8 9 F (.) S 50 J ( The seven states are) S
2880 6328 P (states 0 to 6.) S
2160 6768 P 0 12 F 0 9 F (Memory:) S 108 J ( max = 8K) S
2880 7008 P 0 12 F 8 9 F (This) S 50 J ( gives the maximum amount of dynamic memory that YAY required while) S
2880 7248 P (producing the parser. This line may also have a "success rate" that tells how) S
2880 7488 P (often YAY succeeded in having enough memory to handle a situation and how) S
2880 7728 P (often it had to ask for more memory.) S
2160 8168 P 0 12 F 0 9 F (States:) S 108 J ( 3 wasted, 4 resets) S
2880 8408 P 0 12 F 8 9 F (The) S 50 J ( algorithm that constructs states from the grammar rules makes a guess at) S
2880 8648 P (the number of states it will need, very early on in the YAY process. If this guess) S
2880 8888 P (is too high, the excess states are said to be "wasted".) S
2880 9368 P (When creating states from the various grammar rules, it is sometimes found that) S
2880 9608 P (a state from one rule duplicates the state from another \(for example, there were) S
2880 9848 P (two rules that started with ) S 0 12 F 0 9 F (IF) S 0 12 F 8 9 F () S 50 J ( in our example above\). In the final parsing tables,) S
2880 10088 P (such duplicate states are merged together into a single state. The number of) S
2880 10328 P ("resets" is the number of duplicate states formed, then merged.) S
2160 10768 P 0 12 F 0 9 F (Items:) S 108 J ( 18, 0 kernel, \(2,0\) per state, maxival=16 \(1 w/s\)) S
2880 11008 P 0 12 F 8 9 F (A) S 50 J ( state is made of items, and the kernel items are an important subset of these:) S
2880 11248 P (the size of the resulting parsing tables and the running time for YAY are) S
2880 11488 P (proportional to the number of items and kernel items. The rest of the statistics in) S
2880 11728 P (this line are not of interest to normal users.) S
2160 12168 P 0 12 F 0 9 F (Lalr:) S 108 J ( 1 call, 2 recurs, \(0 trans, 12 epred\)) S
2880 12408 P 0 12 F 8 9 F (This) S 50 J ( gives the number of calls and recursive calls to the conflict resolution) S
2880 12648 P (routine. The parenthesized figures are related to the same process. In some) S
2880 12888 P (ways, this is a measure of the complexity of the grammar being parsed. This line) S
2160 14520 P 0 12 F 57 12 F (DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD) S
2160 14800 P 0 12 F 24 10 F (Thinkage) S 50 J ( YAY Reference Manual) S 4238 J ( 40) S 0 12 F
PE
3600 1968 P 8 9 F (will) S 50 J ( not appear if there are no reduce/reduce or shift/reduce conflicts in your) S
3600 2208 P (grammar.) S
2880 2648 P 0 12 F 0 9 F (Actions:) S 108 J ( 0 entries, gotos: 0 entries) S
3600 2888 P 0 12 F 8 9 F (This) S 50 J ( gives the number of entries in the tables ) S 0 12 F 0 9 F ("yyact") S 0 12 F 8 9 F () S 50 J ( and ) S 0 12 F 0 9 F ("yygo") S 0 12 F 8 9 F (.) S 50 J ( ) S 0 12 F 0 9 F ("yyact") S
3600 3128 P 0 12 F 8 9 F (keeps) S 50 J ( track of the possible Shifts that a program may make and ) S 0 12 F 0 9 F ("yygo") S 0 12 F 8 9 F () S 50 J ( keeps) S
3600 3368 P (track of the "gotos" that take place at the end of states.) S
2880 3808 P 0 12 F 0 9 F (Exceptions:) S 108 J ( 1 states, 4 entries) S
3600 4048 P 0 12 F 8 9 F (This) S 50 J ( gives the number of entries in the table ) S 0 12 F 0 9 F ("yydef") S 0 12 F 8 9 F (,) S 50 J ( yet another table used in) S
3600 4288 P (YAY. ) S 0 12 F 0 9 F ("yydef") S 0 12 F 8 9 F () S 50 J ( keeps track of the possible Reduce, Accept, and Error actions) S
3600 4528 P (that a program may make.) S
2880 4968 P 0 12 F 0 9 F (Simple) S 108 J ( state elim: 0%, Error default elim: 33%) S
3600 5208 P 0 12 F 8 9 F (The) S 50 J ( percentage figures indicate how much table space could be saved through) S
3600 5448 P (various optimization processes. The better written your grammar, the greater the) S
3600 5688 P (percentage of space that can be saved. Therefore, high percentages here are an) S
3600 5928 P (indication of a well-written grammar.) S
2880 6368 P 0 12 F 0 9 F (Optimizer:) S 108 J ( in 0, out 0) S
3600 6608 P 0 12 F 8 9 F (More) S 50 J ( optimization statistics: not of interest to normal YAY users.) S
2880 7048 P 0 12 F 0 9 F (Size) S 108 J ( of tables: 24 bytes) S
3600 7288 P 0 12 F 8 9 F (The) S 50 J ( size of the tables generated to represent the parsing rules. This size is given) S
3600 7528 P (in bytes on the host machine, so it will be inaccurate if a cross-compiler is being) S
3600 7768 P (used on the eventual source code output. The size does not include stack space) S
3600 8008 P (used by ) S 0 12 F 0 9 F ("yyparse") S 0 12 F 8 9 F () S 50 J ( or debug tables obtained by defining ) S 0 12 F 0 9 F (YYDEBUG) S 0 12 F 8 9 F (.) S
2880 8448 P 0 12 F 0 9 F (2) S 108 J ( seconds, final mem = 4) S
3600 8688 P 0 12 F 8 9 F (Total) S 50 J ( real time that YAY used to produce the parser, and the final dynamic) S
3600 8928 P (memory of the parser \(in K bytes\).) S
2880 9368 P 0 12 F 0 9 F (1) S 108 J ( shift/reduce conflict) S
3600 9608 P 0 12 F 8 9 F (Number) S 50 J ( of conflicts in the grammar.) S
2880 14520 P 0 12 F 57 12 F (DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD) S
2880 14800 P 0 12 F 24 10 F (41) S 4238 J ( Thinkage) S 50 J ( YAY Reference Manual) S 0 12 F
PE
7920 2448 P 8 18 F B (7.) S 100 J ( Types) S E
2160 4048 P 0 12 F 8 9 F (Earlier) S 50 J ( we mentioned that ) S 0 12 F 0 9 F ("yylval") S 0 12 F 8 9 F () S 50 J ( is ) S 0 12 F 0 9 F B (int) S E 0 12 F 8 9 F () S 50 J ( by default, as are ) S 0 12 F 0 9 F ($$) S 0 12 F 8 9 F (,) S 50 J ( ) S 0 12 F 0 9 F ($1) S 0 12 F 8 9 F (,) S 50 J ( ) S 0 12 F 0 9 F ($2) S 0 12 F 8 9 F (,) S 50 J ( etc. If you want) S
2160 4288 P (these to have different types, you may redeclare them in the declarations section of the) S
2160 4528 P (YAY input. This is done with a statement of the form) S
2880 4948 P 0 12 F 0 9 F (%union) S 108 J ( {) S
2880 5168 P ( /* possible types for "yylval" and) S
2880 5388 P ( * $$, $1, $2, etc. */) S
2880 5608 P (}) S
2160 6048 P 0 12 F 8 9 F (For) S 50 J ( example, suppose "yylval" can be either integer or floating point. You might write) S
2880 6468 P 0 12 F 0 9 F (%union) S 108 J ( {) S
2880 6688 P ( int intval;) S
2880 6908 P ( float realval;) S
2880 7128 P (}) S
2160 7568 P 0 12 F 8 9 F (in) S 50 J ( the declarations section of the YAY input. YAY converts the ) S 0 12 F 0 9 F (%union) S 0 12 F 8 9 F () S 50 J ( statement into the) S
2160 7808 P (following C source.) S
2880 8228 P 0 12 F 0 9 F (typedef) S 108 J ( union {) S
2880 8448 P ( int intval;) S
2880 8668 P ( float realval;) S
2880 8888 P (} YYSTYPE;) S
2160 9328 P ("yylval") S 0 12 F 8 9 F () S 50 J ( is always declared to have type ) S 0 12 F 0 9 F (YYSTYPE) S 0 12 F 8 9 F (.) S 50 J ( If no ) S 0 12 F 0 9 F (%union) S 0 12 F 8 9 F () S 50 J ( statement is given in) S
2160 9568 P (the YAY input, it will use) S
2880 9988 P 0 12 F 0 9 F (#define) S 108 J ( YYSTYPE int) S
2160 10428 P 0 12 F 8 9 F (Once) S 50 J ( ) S 0 12 F 0 9 F (YYSTYPE) S 0 12 F 8 9 F () S 50 J ( has been defined as a union, you may specify a particular interpretation of) S
2160 10668 P (the union by including a statement of the form) S
2880 11088 P 0 12 F 0 9 F (%type) S 108 J ( <interpretation> SYMBOL) S
2160 11528 P 0 12 F 8 9 F (in) S 50 J ( the declarations section of the YAY input. The "interpretation" enclosed in the angle) S
2160 11768 P (brackets is the name of the interpretation of the union variable that you want to use. The) S
2160 12008 P 0 12 F 0 9 F (SYMBOL) S 0 12 F 8 9 F () S 50 J ( name is the name of a non-terminal symbol defined in the grammar rules. For) S
2160 12248 P (example, you might write) S
2880 12668 P 0 12 F 0 9 F (%type) S 108 J ( <intval> intexp) S
2880 12888 P (%type <realval> realexp) S
2160 14520 P 0 12 F 57 12 F (DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD) S
2160 14800 P 0 12 F 24 10 F (Thinkage) S 50 J ( YAY Reference Manual) S 4238 J ( 42) S 0 12 F
PE
2880 1968 P 8 9 F (to) S 50 J ( indicate that an integer expression has an integer value and a real expression has a) S
2880 2208 P (floating point value.) S
2880 2648 P (Tokens may also be declared to have types. This is done in the ) S 0 12 F 0 9 F (%token) S 0 12 F 8 9 F () S 50 J ( statement in the) S
2880 2888 P (declaration section, and again shows the union interpretation in angle brackets, e.g.) S
3600 3308 P 0 12 F 0 9 F (%token) S 108 J ( <realval> floatnum) S
2880 3748 P 0 12 F 8 9 F (If) S 50 J ( you use types in your YAY input, YAY will enforce compatibility of types in all) S
2880 3988 P (expressions. For example, if you write) S
3600 4408 P 0 12 F 0 9 F ($$) S 108 J ( = $2) S
2880 4848 P 0 12 F 8 9 F (in) S 50 J ( an action, YAY will demand that the two corresponding tokens have the same type;) S
2880 5088 P (otherwise, the assignment will be marked as invalid. The reason for this is that YAY must) S
2880 5328 P (always know what interpretation of the union is being used in order to generate correct) S
2880 5568 P (code.) S
2880 6388 P 0 12 F 8 12 F B (The) S 67 J ( Default Action) S E
2880 7108 P 0 12 F 8 9 F (The) S 50 J ( default action associated with any rule can be written as) S
3600 7528 P 0 12 F 0 9 F ($$) S 108 J ( = $1) S
2880 7968 P 0 12 F 8 9 F (which) S 50 J ( means that the value of associated with ) S 0 12 F 0 9 F ($1) S 0 12 F 8 9 F () S 50 J ( on the value stack is assigned to ) S 0 12 F 0 9 F ($$) S 0 12 F 8 9 F () S 50 J ( on) S
2880 8208 P (the value stack when the rule is reduced. If, for example, ) S 0 12 F 0 9 F ($1) S 0 12 F 8 9 F () S 50 J ( is an integer, then ) S 0 12 F 0 9 F ($$) S 0 12 F 8 9 F () S 50 J ( will be) S
2880 8448 P (the same integer after the reduction occurs.) S
2880 8888 P (On the other hand, suppose that the recognition action associated with a rule explicitly) S
2880 9128 P (states) S
3600 9548 P 0 12 F 0 9 F ($$) S 108 J ( = $1) S
2880 9988 P 0 12 F 8 9 F (This) S 50 J ( explicit assignment may not have the same effect as the implicit assignment. For) S
2880 10228 P (example, suppose that you define) S
3600 10648 P 0 12 F 0 9 F (%union) S 108 J ( {) S
3600 10868 P ( float floatval;) S
3600 11088 P ( int intval;) S
3600 11308 P (}) S
2880 11748 P 0 12 F 8 9 F (Also) S 50 J ( suppose that the type associated with ) S 0 12 F 0 9 F ($$) S 0 12 F 8 9 F () S 50 J ( is ) S 0 12 F 0 9 F ("floatval") S 0 12 F 8 9 F () S 50 J ( and the type associated) S
2880 11988 P (with ) S 0 12 F 0 9 F ($1) S 0 12 F 8 9 F () S 50 J ( is ) S 0 12 F 0 9 F ("intval") S 0 12 F 8 9 F (.) S 50 J ( Then the explicit statement) S
3600 12408 P 0 12 F 0 9 F ($$) S 108 J ( = $1) S
2880 12848 P 0 12 F 8 9 F (performs) S 50 J ( an integer to floating point conversion when the value of ) S 0 12 F 0 9 F ($1) S 0 12 F 8 9 F () S 50 J ( is assigned to ) S 0 12 F 0 9 F ($$) S 0 12 F 8 9 F (,) S
2880 13088 P (whereas) S 50 J ( the implicit statement did an integer to integer assignment and did ) S 0 12 F 8 9 F I (not) S E 0 12 F 8 9 F () S 50 J ( perform) S
2880 14520 P 0 12 F 57 12 F (DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD) S
2880 14800 P 0 12 F 24 10 F (43) S 4238 J ( Thinkage) S 50 J ( YAY Reference Manual) S 0 12 F
PE
2160 1968 P 8 9 F (this) S 50 J ( conversion. You must therefore be careful and think about the effects of implicit vs.) S
2160 2208 P (explicit assignments.) S
2160 14520 P 0 12 F 57 12 F (DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD) S
2160 14800 P 0 12 F 24 10 F (Thinkage) S 50 J ( YAY Reference Manual) S 4238 J ( 44) S 0 12 F
PE
7620 2448 P 8 18 F B (8.) S 100 J ( Ambiguities) S E
2880 4048 P 0 12 F 8 9 F (Suppose) S 50 J ( we have a grammar with the rule) S
3600 4468 P 0 12 F 0 9 F (expr) S 108 J ( : expr '-' expr ;) S
2880 4908 P 0 12 F 8 9 F (and) S 50 J ( the parser is reading an expression of the form) S
3600 5328 P 0 12 F 0 9 F (expr) S 108 J ( - expr - expr) S
2880 5768 P 0 12 F 8 9 F (The) S 50 J ( parser reads this token by token, of course, so after three tokens it will have) S
3600 6188 P 0 12 F 0 9 F (expr) S 108 J ( - expr) S
2880 6628 P 0 12 F 8 9 F (The) S 50 J ( parser recognizes this form. In fact, the parser could reduce this right away into a) S
2880 6868 P (single ) S 0 12 F 0 9 F ("expr") S 0 12 F 8 9 F () S 50 J ( according to the given grammar rule.) S
2880 7308 P (However, the parser has a problem. At this point, the parser doesn't know what comes) S
2880 7548 P (next, and perhaps the entire line will be something like) S
3600 7968 P 0 12 F 0 9 F (expr) S 108 J ( - expr * expr) S
2880 8408 P 0 12 F 8 9 F (If) S 50 J ( it is, the precedence rules say that the multiplication should be performed before the) S
2880 8648 P (subtraction, so handling the subtraction first is incorrect. The parser must therefore read) S
2880 8888 P (another token to see if it is really all right to deal with the subtraction now, or if the correct) S
2880 9128 P (action is to skip the subtraction for the moment and deal with whatever follows the second) S
2880 9368 P 0 12 F 0 9 F ("expr") S 0 12 F 8 9 F (.) S
2880 9808 P (In) S 50 J ( terms of parser states, this problem boils down to a choice between ) S 0 12 F 8 9 F I (reducing) S E 0 12 F 8 9 F () S 50 J ( the) S
2880 10048 P (expression) S
3600 10468 P 0 12 F 0 9 F (expr) S 108 J ( - expr) S
2880 10908 P 0 12 F 8 9 F (or) S 50 J ( ) S 0 12 F 8 9 F I (shifting) S E 0 12 F 8 9 F () S 50 J ( and acquiring more input before making a reduction. This is known as a) S
2880 11148 P 0 12 F 8 9 F I (shift/reduce) S 50 J ( conflict.) S E 0 12 F 8 9 F () S 50 J ( Sometimes a parser must also choose between two possible) S
2880 11388 P (reductions. This kind of situation is called a ) S 0 12 F 8 9 F I (reduce/reduce) S 50 J ( conflict.) S E
2880 11828 P 0 12 F 8 9 F (In) S 50 J ( case you are curious, there is no such thing as a shift/shift conflict. To see why this is) S
2880 12068 P (impossible, suppose that we have the following definitions.) S
2880 14520 P 0 12 F 57 12 F (DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD) S
2880 14800 P 0 12 F 24 10 F (45) S 4238 J ( Thinkage) S 50 J ( YAY Reference Manual) S 0 12 F
PE
2880 1948 P 0 9 F (thing) S 108 J ( : a b) S
2880 2168 P ( | a c) S
2880 2388 P ( ;) S
2880 2608 P (b : T rest_of_b;) S
2880 2828 P (c : T rest_of_c;) S
2160 3268 P 0 12 F 8 9 F (If) S 50 J ( the parser is in the state where it has seen ) S 0 12 F 0 9 F ("a") S 0 12 F 8 9 F (,) S 50 J ( you would have the diagram) S
2880 3688 P 0 12 F 0 9 F (thing) S 108 J ( : a.b) S
2880 3908 P (thing : a.c) S
2160 4348 P 0 12 F 8 9 F (You) S 50 J ( might think that if the lookahead symbol was the token ) S 0 12 F 0 9 F (T) S 0 12 F 8 9 F (,) S 50 J ( the parser would be) S
2160 4588 P (confused, since ) S 0 12 F 0 9 F (T) S 0 12 F 8 9 F () S 50 J ( is the first token of both ) S 0 12 F 0 9 F ("b") S 0 12 F 8 9 F () S 50 J ( and ) S 0 12 F 0 9 F ("c") S 0 12 F 8 9 F (.) S 50 J ( However, there is no confusion) S
2160 4828 P (at all. The parser just Shifts to a state that we could diagram as) S
2880 5248 P 0 12 F 0 9 F (b) S 108 J ( : T.rest_of_b) S
2880 5468 P (c : T.rest_of_c) S
2160 6288 P 0 12 F 8 12 F B (8.1) S 67 J ( Resolving Conflicts by Precedence) S E
2160 7008 P 0 12 F 8 9 F (The) S 50 J ( precedence directives \() S 0 12 F 0 9 F (%left) S 0 12 F 8 9 F (,) S 50 J ( ) S 0 12 F 0 9 F (%right) S 0 12 F 8 9 F (,) S 50 J ( and ) S 0 12 F 0 9 F (%nonassoc) S 0 12 F 8 9 F (\)) S 50 J ( let YAY-produced parsers) S
2160 7248 P (resolve shift/reduce conflicts in an obvious way:) S
2160 7688 P (\(a\) The precedence of a Shift operation is defined to be the precedence of the token) S
2880 7928 P (on which the Shift takes place.) S
2160 8368 P (\(b\) The precedence of a Reduce operation is defined to be the precedence of the) S
2880 8608 P (grammar rule that the Reduce operation uses.) S
2160 9048 P (If you have a shift/reduce conflict, and the Shift and Reduce operations both have a) S
2160 9288 P (precedence, the parser chooses the operation with the higher precedence.) S
2160 10108 P 0 12 F 8 12 F B (8.2) S 67 J ( Disambiguating Rules) S E
2160 10828 P 0 12 F 8 9 F (Precedence) S 50 J ( cannot resolve conflicts if one or both conflicting operations have no) S
2160 11068 P (precedence. For example, consider the following.) S
2880 11488 P 0 12 F 0 9 F (statmt:) S 108 J ( IF '\(' cond '\)' statmt) S
2880 11708 P ( | IF '\(' cond '\)' statmt ELSE statmt ;) S
2160 12148 P 0 12 F 8 9 F (Given) S 50 J ( this rule, how should the parser interpret the input) S
2880 12568 P 0 12 F 0 9 F (IF) S 108 J ( \( cond1 \) IF \( cond2 \) statmt1 ELSE statmt2) S
2160 13008 P 0 12 F 8 9 F (There) S 50 J ( are two equally valid interpretations of this input:) S
2160 14520 P 0 12 F 57 12 F (DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD) S
2160 14800 P 0 12 F 24 10 F (Thinkage) S 50 J ( YAY Reference Manual) S 4238 J ( 46) S 0 12 F
PE
3600 1948 P 0 9 F (IF) S 108 J ( \( cond1 \) {) S
3600 2168 P ( IF \( cond2 \) statmt1) S
3600 2388 P ( ELSE statmt2) S
3600 2608 P (}) S
2880 3048 P 0 12 F 8 9 F (and) S
3600 3468 P 0 12 F 0 9 F (IF) S 108 J ( \( cond1 \) {) S
3600 3688 P ( IF \( cond2 \) statmt1) S
3600 3908 P (}) S
3600 4128 P (ELSE statmt2) S
2880 4568 P 0 12 F 8 9 F (In) S 50 J ( a typical grammar, the ) S 0 12 F 0 9 F (IF) S 0 12 F 8 9 F () S 50 J ( and ) S 0 12 F 0 9 F (IF-ELSE) S 0 12 F 8 9 F () S 50 J ( statements would not have a precedence, so) S
2880 4808 P (precedence could not resolve the conflict. Thus consider what happens at the point when) S
2880 5048 P (the parser has read) S
3600 5468 P 0 12 F 0 9 F (IF) S 108 J ( \( cond1 \) IF \( cond2 \) statmt1) S
2880 5908 P 0 12 F 8 9 F (and) S 50 J ( has just picked up ) S 0 12 F 0 9 F (ELSE) S 0 12 F 8 9 F () S 50 J ( as the lookahead symbol.) S
2880 6348 P (\(1\) It can immediately Reduce the) S
3600 6768 P 0 12 F 0 9 F (IF) S 108 J ( \( cond2 \) statmt1) S
2880 7208 P 0 12 F 8 9 F () S 720 J ( using) S 50 J ( the first definition of ) S 0 12 F 0 9 F ("statmt") S 0 12 F 8 9 F () S 50 J ( and obtain) S
3600 7628 P 0 12 F 0 9 F (IF) S 108 J ( \( cond1 \) statmt ELSE ...) S
2880 8068 P 0 12 F 8 9 F () S 720 J ( thereby) S 50 J ( associating the ) S 0 12 F 0 9 F (ELSE) S 0 12 F 8 9 F () S 50 J ( with the first ) S 0 12 F 0 9 F (IF) S 0 12 F 8 9 F (.) S
2880 8508 P (\(2\)) S 500 J ( It) S 50 J ( can Shift, which means ignoring the first part \(the ) S 0 12 F 0 9 F (IF) S 0 12 F 8 9 F () S 50 J ( with ) S 0 12 F 0 9 F ("cond1") S 0 12 F 8 9 F (\)) S 50 J ( and going) S
3600 8748 P (on to handle the second part, thereby associating the ) S 0 12 F 0 9 F (ELSE) S 0 12 F 8 9 F () S 50 J ( with the second ) S 0 12 F 0 9 F (IF) S 0 12 F 8 9 F (.) S
2880 9188 P (In) S 50 J ( this case, most programming languages choose to associate the ) S 0 12 F 0 9 F (ELSE) S 0 12 F 8 9 F () S 50 J ( with the second) S
2880 9428 P 0 12 F 0 9 F (IF) S 0 12 F 8 9 F (,) S 50 J ( i.e. they want the parser to Shift instead of Reduce. Because of this \(and other similar) S
2880 9668 P (situations\), YAY-produced parsers are designed to use the following rule to resolve) S
2880 9908 P (shift/reduce conflicts.) S
2880 10308 P 0 12 F 8 10 F B (Disambiguating) S 56 J ( Rule 1:) S E
3600 10548 P 0 12 F 8 9 F (If) S 50 J ( there is a shift/reduce conflict in situations where no precedence rules have) S
3600 10788 P (been created to resolve the conflict, the default action is to Shift. The conflict will) S
3600 11028 P (also be reported in the YAY output so you can check that Shifting is actually what) S
3600 11268 P (you want. If it isn't what you want, the grammar rules will have to be rewritten.) S
2880 11708 P (This is known as a disambiguating rule because it helps YAY-produced parsers resolve) S
2880 11948 P (ambiguities. The rule is only used in situations where precedence rules cannot resolve) S
2880 12188 P (the conflict. If both the Shift operation and the Reduce operation have an assigned) S
2880 12428 P (precedence, the parser can compare precedences and decide which operation to perform) S
2880 12668 P (first. Even if the precedences are equal, the precedences must have originated from) S
2880 12908 P (either ) S 0 12 F 0 9 F (%left) S 0 12 F 8 9 F (,) S 50 J ( ) S 0 12 F 0 9 F (%right) S 0 12 F 8 9 F (,) S 50 J ( or ) S 0 12 F 0 9 F (%nonassoc) S 0 12 F 8 9 F (,) S 50 J ( so the parser knows how to handle the situation.) S
2880 14520 P 0 12 F 57 12 F (DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD) S
2880 14800 P 0 12 F 24 10 F (47) S 4238 J ( Thinkage) S 50 J ( YAY Reference Manual) S 0 12 F
PE
2160 1968 P 8 9 F (The) S 50 J ( only time the disambiguating rule is needed is when one or both of the Shift or) S
2160 2208 P (Reduce operations does not have an assigned precedence.) S
2160 2648 P (In a similar vein, YAY-produced parsers use the following rule to resolve reduce/reduce) S
2160 2888 P (conflicts.) S
2160 3288 P 0 12 F 8 10 F B (Disambiguating) S 56 J ( Rule 2:) S E
2880 3528 P 0 12 F 8 9 F (If) S 50 J ( there is a reduce/reduce conflict, the parser will always Reduce by the rule that) S
2880 3768 P (was given ) S 0 12 F 8 9 F I (first) S E 0 12 F 8 9 F () S 50 J ( in the rules section of the YAY input. Again, the conflict will be) S
2880 4008 P (reported in the YAY output so that users may ensure that the choice of Reduction) S
2880 4248 P (is correct.) S
2160 4688 P (Note that precedence is ) S 0 12 F 8 9 F I (not) S E 0 12 F 8 9 F () S 50 J ( consulted in reduce/reduce conflicts. YAY always Reduces) S
2160 4928 P (by the earliest grammar rule, regardless of precedence.) S
2160 5368 P (Finally, YAY-produced parsers use a third disambiguating rule to prevent excessive errors.) S
2160 5768 P 0 12 F 8 10 F B (Disambiguating) S 56 J ( Rule 3:) S E
2880 6008 P 0 12 F 8 9 F (If) S 50 J ( the parser is asked to choose between shifting on ) S 0 12 F 0 9 F B (error) S E 0 12 F 8 9 F () S 50 J ( or on any other item,) S
2880 6248 P (it will choose the non-error item.) S
2160 6688 P (The disambiguating rules are simple to state, but they can have complex repercussions if) S
2160 6928 P (the grammar is non-trivial. If the grammar is sufficiently complicated, these simple rules) S
2160 7168 P (for resolving conflicts may not be capable of handling all the necessary intricacies in the) S
2160 7408 P (way you want. You should pay close attention to all conflicts noted in the parsing table) S
2160 7648 P (report produced by YAY and should ensure that the default actions taken by the parser) S
2160 7888 P (are the desired ones.) S
2160 8328 P 0 12 F 8 9 F B (Note:) S E 0 12 F 8 9 F () S 50 J ( the conflict between the rules) S
2880 8748 P 0 12 F 0 9 F (statmt:) S 108 J ( IF '\(' cond '\)' statmt) S
2880 8968 P ( | IF '\(' cond '\)' ELSE statmt ;) S
2160 9408 P 0 12 F 8 9 F (can) S 50 J ( be resolved by adding) S
2880 9828 P 0 12 F 0 9 F (%left) S 108 J ( ELSE) S
2160 10268 P 0 12 F 8 9 F (to) S 50 J ( the declarations section of the YAY input. This puts a precedence on the ) S 0 12 F 0 9 F (ELSE) S 0 12 F 8 9 F () S 50 J ( action) S
2160 10508 P (and says that it is left-associative. This is another example of using a precedence to) S
2160 10748 P (resolve an ambiguity.) S
2160 11568 P 0 12 F 8 12 F B (8.3) S 67 J ( Conflicts in YAY Output) S E
2160 12288 P 0 12 F 8 9 F (If) S 50 J ( your grammar has shift/reduce or reduce/reduce conflicts, you will also see a table of) S
2160 12528 P (conflicts in the statistics section of the Parser Description. For example, if we change the) S
2160 12768 P (rules section of our sample grammar to) S
2160 14520 P 0 12 F 57 12 F (DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD) S
2160 14800 P 0 12 F 24 10 F (Thinkage) S 50 J ( YAY Reference Manual) S 4238 J ( 48) S 0 12 F
PE
3600 1948 P 0 9 F (stmt) S 108 J ( : IF stmt ELSE stmt) S
3600 2168 P ( | IF stmt) S
3600 2388 P ( | stmt stmt) S
3600 2608 P ( | A ;) S
2880 3048 P 0 12 F 8 9 F (we) S 50 J ( get the following conflict report.) S
3600 3468 P 0 12 F 0 9 F (Conflicts:) S
3600 3688 P () S 108 J ( State Token Action) S
3600 3908 P ( 5 IF shift 2) S
3600 4128 P ( 5 IF reduce \(3\)) S
3600 4348 P ( 5 A shift 1) S
3600 4568 P ( 5 A reduce \(3\)) S
2880 5008 P 0 12 F 8 9 F (This) S 50 J ( shows that State 5 has two shift/reduce conflicts. If the parser is in State 5 and) S
2880 5248 P (encounters an IF token, it can shift to state 2 or reduce using rule 3. If the parser) S
2880 5488 P (encounters an A token, it can shift to state 1 or reduce using rule 3. This is summarized in) S
2880 5728 P (the final statistics with the line) S
3600 6148 P 0 12 F 0 9 F (2) S 108 J ( shift/reduce conflicts) S
2880 6588 P 0 12 F 8 9 F (Reading) S 50 J ( the conflict report shows you what action the parser takes in case of a conflict --) S
2880 6828 P (the parser always takes the ) S 0 12 F 8 9 F I (first) S E 0 12 F 8 9 F () S 50 J ( action shown in the report. This action will be chosen in) S
2880 7068 P (accordance with the two disambiguating rules.) S
2880 14520 P 0 12 F 57 12 F (DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD) S
2880 14800 P 0 12 F 24 10 F (49) S 4238 J ( Thinkage) S 50 J ( YAY Reference Manual) S 0 12 F
PE
5640 2448 P 8 18 F B (9.) S 100 J ( Advanced Features) S E
2160 4048 P 0 12 F 8 9 F (In) S 50 J ( this chapter, we describe more specialized features of YAY.) S
2160 4868 P 0 12 F 8 12 F B (9.1) S 67 J ( LALR\(2\) Grammars) S E
2160 5588 P 0 12 F 8 9 F (An) S 50 J ( LALR\(2\) grammar has one more level of "lookahead" than an LALR\(1\) grammar.) S
2160 5828 P (When trying to decide how to parse a given input, an LALR\(1\) parser sometimes looks at) S
2160 6068 P (the next input token to see if that makes a difference. In the same situation, an LALR\(2\)) S
2160 6308 P (parser can look at the next ) S 0 12 F 8 9 F I (two) S E 0 12 F 8 9 F () S 50 J ( tokens.) S
2160 6748 P (LALR\(2\) grammars are described in the same way as LALR\(1\) grammars. To indicate that) S
2160 6988 P (you want an LALR\(2\) parser, you must put the option ) S 0 12 F 0 9 F (+LR2) S 0 12 F 8 9 F () S 50 J ( on the command line that) S
2160 7228 P (invokes YAY.) S
2160 7668 P (An LALR\(2\) parser can perform a ) S 0 12 F 8 9 F I (lookahead) S E 0 12 F 8 9 F () S 50 J ( action as well as Shift, Reduce, Goto,) S
2160 7908 P (Accept, and Error. For example, in a state diagram you might see) S
2880 8328 P 0 12 F 0 9 F (A) S 108 J ( shift 1) S
2880 8548 P (B reduce \(2\)) S
2880 8768 P (C lookahead) S
2160 9208 P 0 12 F 8 9 F (This) S 50 J ( means that if token ) S 0 12 F 0 9 F (A) S 0 12 F 8 9 F () S 50 J ( is encountered in this state, the parser will shift to State 1; if) S
2160 9448 P (token ) S 0 12 F 0 9 F (B) S 0 12 F 8 9 F () S 50 J ( is encountered, it will reduce using Rule \(2\); and if token ) S 0 12 F 0 9 F (C) S 0 12 F 8 9 F () S 50 J ( is encountered, it will) S
2160 9688 P (lookahead.) S
2160 10808 P 0 12 F 8 12 F I (9.1.1) S 67 J ( The Lookahead Action) S E
2160 11528 P 0 12 F 8 9 F (A) S 50 J ( state that has a Lookahead action has a list of possible states the parser can enter next.) S
2160 11768 P (These states conflict with each other -- the Lookahead action isn't needed if there is no) S
2160 12008 P (conflict. The parser attempts to resolve the conflict by reading one additional token. If the) S
2160 12248 P (grammar is set up properly, this token will be valid in only one of the possible contexts, so) S
2160 12488 P (the parser can choose that rule instead of the other possibilities.) S
2160 12928 P (Thus the Lookahead action tests all the possibilities in the list. It begins by making a) S
2160 13168 P (private copy of the state stack. \(Actually, it just sets things up so that it looks like it has a) S
2160 14520 P 0 12 F 57 12 F (DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD) S
2160 14800 P 0 12 F 24 10 F (Thinkage) S 50 J ( YAY Reference Manual) S 4238 J ( 50) S 0 12 F
PE
2880 1968 P 8 9 F (separate) S 50 J ( copy of the state stack -- it doesn't really make a copy.\) Next, it calls on a) S
2880 2208 P (routine named ) S 0 12 F 0 9 F ("yy2lex") S 0 12 F 8 9 F () S 50 J ( \(described later\) to obtain a second lookahead token.) S
2880 2648 P (The Lookahead action then begins going through the possible rules on its list. Some of) S
2880 2888 P (these rules may be Shifts, while others are Reduces -- the reductions are what make it) S
2880 3128 P (necessary to simulate a copy of the state stack, because you don't want to play with the) S
2880 3368 P (true stack.) S
2880 3808 P (The Lookahead action takes the action associated with each possibility in the list, entering) S
2880 4048 P (new states through Shifts or Reduces. It then sees if the second lookahead token causes) S
2880 4288 P (an error in this new state. If an error occurs, the parser pops back to the original) S
2880 4528 P (Lookahead state and checks the next possibility on the list.) S
2880 4968 P (When it finally finds a rule where the second lookahead symbol doesn't cause an error, it) S
2880 5208 P (pops back to the original Lookahead state one last time. It gets rid of the stack set-up that) S
2880 5448 P (the Lookahead action was using and returns to the old state stack. It then takes the action) S
2880 5688 P (that it has discovered in the list of possibilities.) S
2880 6128 P (Note that the Lookahead action takes the first possible rule where the second lookahead) S
2880 6368 P (symbol is valid. It is, of course, possible that there are other valid possibilities in the list.) S
2880 6608 P (The order of the list of possibilities is based on the standard disambiguating rules: a shift) S
2880 6848 P (comes first, followed by reductions in the order in which the corresponding grammar rules) S
2880 7088 P (were given.) S
2880 8208 P 0 12 F 8 12 F I (9.1.2) S 67 J ( The yy2lex Routine) S E
2880 8928 P 0 12 F 8 9 F (In) S 50 J ( order to perform the lookahead, ) S 0 12 F 0 9 F ("yyparse") S 0 12 F 8 9 F () S 50 J ( calls a routine called ) S 0 12 F 0 9 F ("yy2lex") S 0 12 F 8 9 F (.) S 50 J ( This is a) S
2880 9168 P (user-written lexical analyzer, just as ) S 0 12 F 0 9 F ("yylex") S 0 12 F 8 9 F () S 50 J ( is. It returns the same kind of token value) S
2880 9408 P (that ) S 0 12 F 0 9 F ("yylex") S 0 12 F 8 9 F () S 50 J ( does. However, ) S 0 12 F 0 9 F ("yy2lex") S 0 12 F 8 9 F () S 50 J ( should not set the ) S 0 12 F 0 9 F ("yylval") S 0 12 F 8 9 F () S 50 J ( value.) S
2880 9848 P (For some applications, ) S 0 12 F 0 9 F ("yy2lex") S 0 12 F 8 9 F () S 50 J ( could even be the same routine as ) S 0 12 F 0 9 F ("yylex") S 0 12 F 8 9 F (.) S 50 J ( There) S
2880 10088 P (are good reasons why the two should be separate, however. Suppose, for the sake of) S
2880 10328 P (argument, that ) S 0 12 F 0 9 F ("yyparse") S 0 12 F 8 9 F () S 50 J ( called ) S 0 12 F 0 9 F ("yylex") S 0 12 F 8 9 F () S 50 J ( for all input. ) S 0 12 F 0 9 F ("yyparse") S 0 12 F 8 9 F () S 50 J ( might call ) S 0 12 F 0 9 F ("yylex") S
2880 10568 P 0 12 F 8 9 F (for) S 50 J ( the next token, then immediately call ) S 0 12 F 0 9 F ("yylex") S 0 12 F 8 9 F () S 50 J ( again for a lookahead. This second call) S
2880 10808 P (could overwrite important values like ) S 0 12 F 0 9 F ("yylval") S 0 12 F 8 9 F () S 50 J ( before they had been used, thereby) S
2880 11048 P (confusing things greatly. For such reasons, ) S 0 12 F 0 9 F ("yy2lex") S 0 12 F 8 9 F () S 50 J ( and ) S 0 12 F 0 9 F ("yylex") S 0 12 F 8 9 F () S 50 J ( sometimes have to) S
2880 11288 P (be different routines.) S
2880 11728 P (In practice, parsers rarely call ) S 0 12 F 0 9 F ("yy2lex") S 0 12 F 8 9 F () S 50 J ( -- the routine is only needed in special) S
2880 11968 P (circumstances. This means that ) S 0 12 F 0 9 F ("yy2lex") S 0 12 F 8 9 F () S 50 J ( usually can be much simpler than ) S 0 12 F 0 9 F ("yylex") S 0 12 F 8 9 F (:) S
2880 12208 P 0 12 F 0 9 F ("yylex") S 0 12 F 8 9 F () S 50 J ( must be able to handle all possible input tokens, while ) S 0 12 F 0 9 F ("yy2lex") S 0 12 F 8 9 F () S 50 J ( is only called) S
2880 12448 P (in a few special cases. To find out the tokens that ) S 0 12 F 0 9 F ("yy2lex") S 0 12 F 8 9 F () S 50 J ( must be able to handle, run) S
2880 12688 P (your grammar through YAY and get a list of the states where the parser performs a) S
2880 12928 P (Lookahead instead of a Shift or Reduce. These instances are the only ones where the) S
2880 14520 P 0 12 F 57 12 F (DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD) S
2880 14800 P 0 12 F 24 10 F (51) S 4238 J ( Thinkage) S 50 J ( YAY Reference Manual) S 0 12 F
PE
2160 1968 P 8 9 F (parser) S 50 J ( will call ) S 0 12 F 0 9 F ("yy2lex") S 0 12 F 8 9 F (,) S 50 J ( so the associated tokens are the only ones that the routine has) S
2160 2208 P (to handle.) S
2160 2648 P (The values obtained by ) S 0 12 F 0 9 F ("yy2lex") S 0 12 F 8 9 F () S 50 J ( are thrown away once the parser has used them in its) S
2160 2888 P (lookahead. What this means is that the parser doesn't remember what ) S 0 12 F 0 9 F ("yy2lex") S
2160 3128 P 0 12 F 8 9 F (returned,) S 50 J ( so it will call on ) S 0 12 F 0 9 F ("yylex") S 0 12 F 8 9 F () S 50 J ( to re-read the token for normal parsing. This means) S
2160 3368 P (one of two things:) S
2160 3808 P (\(a\) ) S 0 12 F 0 9 F ("yy2lex") S 0 12 F 8 9 F () S 50 J ( can duplicate the processing that ) S 0 12 F 0 9 F ("yylex") S 0 12 F 8 9 F () S 50 J ( does, then leave the) S
2880 4048 P (pertinent information around for ) S 0 12 F 0 9 F ("yylex") S 0 12 F 8 9 F () S 50 J ( to obtain. This means that ) S 0 12 F 0 9 F ("yylex") S
2880 4288 P 0 12 F 8 9 F (must) S 50 J ( have a way of knowing when ) S 0 12 F 0 9 F ("yy2lex") S 0 12 F 8 9 F () S 50 J ( has already read the next token) S
2880 4528 P (\(e.g. ) S 0 12 F 0 9 F ("yy2lex") S 0 12 F 8 9 F () S 50 J ( can set a flag\).) S
2160 4968 P (\(b\) ) S 0 12 F 0 9 F ("yy2lex") S 0 12 F 8 9 F () S 50 J ( can cheat and only do minimal processing. For example, ) S 0 12 F 0 9 F ("yy2lex") S
2880 5208 P 0 12 F 8 9 F (may) S 50 J ( only have to look at the first character of the next token to figure out what is) S
2880 5448 P (coming next. ) S 0 12 F 0 9 F ("yy2lex") S 0 12 F 8 9 F () S 50 J ( can then ) S 0 12 F 0 9 F ("ungetc") S 0 12 F 8 9 F () S 50 J ( that character and return an) S
2880 5688 P (appropriate token value. When ) S 0 12 F 0 9 F ("yylex") S 0 12 F 8 9 F () S 50 J ( is called, it reads the full token in the) S
2880 5928 P (usual manner.) S
2160 6368 P (The second approach is sufficient for most grammars. It simplifies both ) S 0 12 F 0 9 F ("yylex") S 0 12 F 8 9 F () S 50 J ( and) S
2160 6608 P 0 12 F 0 9 F ("yy2lex") S 0 12 F 8 9 F (.) S
2160 7728 P 0 12 F 8 12 F I (9.1.3) S 67 J ( Conflicts) S E
2160 8448 P 0 12 F 8 9 F (With) S 50 J ( an additional level of lookahead, there is the potential for a staggering number of) S
2160 8688 P (conflicts. For example, you may lookahead to a new state to resolve one conflict, only to) S
2160 8928 P (find that the new state also has a lookahead to resolve a conflict.) S
2160 9368 P (If an LALR\(2\) parser is performing a lookahead and enters a state where the secondary) S
2160 9608 P (lookahead token invokes another Lookahead action, the parser ruthlessly resolves the) S
2160 9848 P (second Lookahead action by choosing the first possibility on the second Lookahead list.) S
2160 10088 P (This is not very elegant, so you should avoid this double lookahead situation.) S
2160 10528 P (Conflicts of this nature are reported in the usual conflict table at the end of the State) S
2160 10768 P (Description output.) S
2160 11588 P 0 12 F 8 12 F B (9.2) S 67 J ( Multiple Actions) S E
2160 12308 P 0 12 F 8 9 F (A) S 50 J ( rule can have more than one action. For example, you might have) S
2880 12728 P 0 12 F 0 9 F (a) S 108 J ( : A1 {action1} A2 {action2} A3 {action3};) S
2160 14520 P 0 12 F 57 12 F (DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD) S
2160 14800 P 0 12 F 24 10 F (Thinkage) S 50 J ( YAY Reference Manual) S 4238 J ( 52) S 0 12 F
PE
2880 1968 P 8 9 F (The) S 50 J ( non-terminal symbol ) S 0 12 F 0 9 F ("a") S 0 12 F 8 9 F () S 50 J ( consists of symbols ) S 0 12 F 0 9 F (A1) S 0 12 F 8 9 F (,) S 50 J ( ) S 0 12 F 0 9 F (A2) S 0 12 F 8 9 F (,) S 50 J ( and ) S 0 12 F 0 9 F (A3) S 0 12 F 8 9 F (.) S 50 J ( When ) S 0 12 F 0 9 F (A1) S 0 12 F 8 9 F () S 50 J ( is) S
2880 2208 P (recognized, ) S 0 12 F 0 9 F ("action1") S 0 12 F 8 9 F () S 50 J ( is invoked; when ) S 0 12 F 0 9 F (A2) S 0 12 F 8 9 F () S 50 J ( is recognized, ) S 0 12 F 0 9 F ("action2") S 0 12 F 8 9 F () S 50 J ( is invoked; and) S
2880 2448 P (when ) S 0 12 F 0 9 F (A3) S 0 12 F 8 9 F () S 50 J ( is recognized \(and therefore the entire symbol ) S 0 12 F 0 9 F ("a") S 0 12 F 8 9 F (\),) S 50 J ( ) S 0 12 F 0 9 F ("action3") S 0 12 F 8 9 F () S 50 J ( is invoked. In) S
2880 2688 P (this case,) S
3600 3108 P 0 12 F 0 9 F ($1) S 108 J ( -- ) S 0 12 F 8 9 F (is) S 50 J ( the value of) S 0 12 F 0 9 F () S 108 J ( A1) S
3600 3328 P ($2 -- ) S 0 12 F 8 9 F (is) S 50 J ( the value of) S 0 12 F 0 9 F () S 108 J ( $$) S 0 12 F 8 9 F () S 50 J ( in) S 0 12 F 0 9 F () S 108 J ( action1) S
3600 3548 P ($3 -- ) S 0 12 F 8 9 F (is) S 50 J ( the value of) S 0 12 F 0 9 F () S 108 J ( A2) S
3600 3768 P ($4 -- ) S 0 12 F 8 9 F (is) S 50 J ( the value of) S 0 12 F 0 9 F () S 108 J ( $$) S 0 12 F 8 9 F () S 50 J ( in) S 0 12 F 0 9 F () S 108 J ( action2) S
3600 3988 P ($5 -- ) S 0 12 F 8 9 F (is) S 50 J ( the value of) S 0 12 F 0 9 F () S 108 J ( A3) S
2880 4428 P 0 12 F 8 9 F (If) S 50 J ( types are involved, multiple actions become more complicated. If ) S 0 12 F 0 9 F ("action1") S 0 12 F 8 9 F () S 50 J ( mentions) S
2880 4668 P 0 12 F 0 9 F ($$) S 0 12 F 8 9 F (,) S 50 J ( there is no way for YAY to guess what type ) S 0 12 F 0 9 F ($$) S 0 12 F 8 9 F () S 50 J ( has, since it is not really associated) S
2880 4908 P (with a token or non-terminal symbol. You must therefore state it explicitly by specifying) S
2880 5148 P (the appropriate type name in angle brackets between the two ) S 0 12 F 0 9 F ($) S 0 12 F 8 9 F () S 50 J ( signs. If we had) S
3600 5568 P 0 12 F 0 9 F (%union) S 108 J ( {) S
3600 5788 P ( int intval;) S
3600 6008 P ( float realval;) S
3600 6228 P (}) S
2880 6668 P 0 12 F 8 9 F (you) S 50 J ( might say) S
3600 7088 P 0 12 F 0 9 F ($<realval>$) S
2880 7528 P 0 12 F 8 9 F (in) S 50 J ( place of ) S 0 12 F 0 9 F ($$) S 0 12 F 8 9 F () S 50 J ( in the action, to show that the result had type ) S 0 12 F 0 9 F B (float) S E 0 12 F 8 9 F (.) S 50 J ( In the same way, if) S
2880 7768 P 0 12 F 0 9 F ("action2") S 0 12 F 8 9 F () S 50 J ( refers to ) S 0 12 F 0 9 F ($2) S 0 12 F 8 9 F () S 50 J ( \(the result of ) S 0 12 F 0 9 F (action1) S 0 12 F 8 9 F (\),) S 50 J ( you might say) S
3600 8188 P 0 12 F 0 9 F ($<realval>2) S
2880 8628 P 0 12 F 8 9 F (To) S 50 J ( deal with multiple actions, YAY changes the form of the given grammar rule and) S
2880 8868 P (creates grammar rules for dummy symbols. The dummy symbols have names made up of) S
2880 9108 P (a ) S 0 12 F 0 9 F ($) S 0 12 F 8 9 F () S 50 J ( followed by the rule number. For example,) S
3600 9528 P 0 12 F 0 9 F (a) S 108 J ( : A1 {action1} A2 {action2} A3 {action3};) S
2880 9968 P 0 12 F 8 9 F (might) S 50 J ( be changed to the rules) S
3600 10388 P 0 12 F 0 9 F ($21) S 108 J ( : /* null */ {action1} ;) S
3600 10608 P ($22 : /* null */ {action2} ;) S
3600 10828 P (a : A1 $21 A2 $22 A3 {action3};) S
2880 11268 P 0 12 F 8 9 F (These) S 50 J ( rules will be shown in the Rules Summary of the Parser Description report.) S
2880 11708 P (Note that this technique can introduce conflicts. For example, suppose you have the) S
2880 11948 P (definitions) S
3600 12368 P 0 12 F 0 9 F (a) S 108 J ( : A1 {action1} A2 X;) S
3600 12588 P (b : A1 A2 Y;) S
2880 13028 P 0 12 F 8 9 F (These) S 50 J ( are changed to) S
2880 14520 P 0 12 F 57 12 F (DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD) S
2880 14800 P 0 12 F 24 10 F (53) S 4238 J ( Thinkage) S 50 J ( YAY Reference Manual) S 0 12 F
PE
2880 1948 P 0 9 F ($50) S 108 J ( : /* null */ {action1};) S
2880 2168 P (a : A1 $50 A2 X;) S
2880 2388 P (b : A1 A2 Y;) S
2160 2828 P 0 12 F 8 9 F (The) S 50 J ( definitions of ) S 0 12 F 0 9 F ("a") S 0 12 F 8 9 F () S 50 J ( and ) S 0 12 F 0 9 F ("b") S 0 12 F 8 9 F () S 50 J ( give a shift/reduce conflict because the parser can't tell) S
2160 3068 P (whether ) S 0 12 F 0 9 F (A1) S 0 12 F 8 9 F () S 50 J ( followed by ) S 0 12 F 0 9 F (A2) S 0 12 F 8 9 F () S 50 J ( has the null string for ) S 0 12 F 0 9 F ($50) S 0 12 F 8 9 F () S 50 J ( in the middle. It has to decide) S
2160 3308 P (whether to Reduce ) S 0 12 F 0 9 F ($50) S 0 12 F 8 9 F () S 50 J ( or to Shift to a state diagrammed by) S
2880 3728 P 0 12 F 0 9 F (b) S 108 J ( : A1 A2.Y) S
2160 4168 P 0 12 F 8 9 F (This) S 50 J ( particular conflict could be resolved by using an LALR\(2\) parser instead of LALR\(1\).) S
2160 4408 P (However, there are more complicated examples in which this is not possible.) S
2160 4848 P (There is another situation to watch for. Consider the grammar) S
2880 5268 P 0 12 F 0 9 F (a) S 108 J ( : A1 c A2 X ;) S
2880 5488 P (b : A1 c A2 Y ;) S
2880 5708 P (c : /* nothing */ {action} ;) S
2160 6148 P 0 12 F 8 9 F (You) S 50 J ( might think that this is equivalent to) S
2880 6568 P 0 12 F 0 9 F (a) S 108 J ( : A1 {action} A2 X;) S
2880 6788 P (b : A1 {action} A2 Y;) S
2160 7228 P 0 12 F 8 9 F (but) S 50 J ( it is not. The first will not produce a conflict, but the second one will. The second is) S
2160 7468 P (converted into) S
2880 7888 P 0 12 F 0 9 F (a) S 108 J ( : A1 $50 A2 X;) S
2880 8108 P (b : A1 $51 A2 Y;) S
2880 8328 P ($50 : {action} ;) S
2880 8548 P ($51 : {action} ;) S
2160 8988 P 0 12 F 8 9 F (even) S 50 J ( when the action is the same for both ) S 0 12 F 0 9 F ("a") S 0 12 F 8 9 F () S 50 J ( and ) S 0 12 F 0 9 F ("b") S 0 12 F 8 9 F (.) S 50 J ( If the parser finds an ) S 0 12 F 0 9 F (A1) S 0 12 F 8 9 F () S 50 J ( followed) S
2160 9228 P (by a null string followed by ) S 0 12 F 0 9 F (A2) S 0 12 F 8 9 F (,) S 50 J ( it doesn't know if it should interpret the null string as ) S 0 12 F 0 9 F ($50) S 0 12 F 8 9 F () S 50 J ( or) S
2160 9468 P 0 12 F 0 9 F ($51) S 0 12 F 8 9 F (;) S 50 J ( therefore, it gets a conflict. Obviously, it is better to use the first format, where the) S
2160 9708 P (identical actions are associated with a separate rule.) S
2160 10528 P 0 12 F 8 12 F B (9.3) S 67 J ( Selection Preference) S E
2160 11248 P 0 12 F 8 9 F (A) S 50 J ( ) S 0 12 F 8 9 F I (selection) S 50 J ( preference) S E 0 12 F 8 9 F () S 50 J ( can be added to a grammar rule to help resolve conflicts. The) S
2160 11488 P (following input shows a simple example of how a selection preference can resolve) S
2160 11728 P (conflicts between two rules.) S
2880 12148 P 0 12 F 0 9 F (a1) S 108 J ( : a b ['+' '-']) S
2880 12368 P ( { /* Action 1 */ } ;) S
2880 12588 P (a2 : a b) S
2880 12808 P ( { /* Action 2 */ } ;) S
2160 14520 P 0 12 F 57 12 F (DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD) S
2160 14800 P 0 12 F 24 10 F (Thinkage) S 50 J ( YAY Reference Manual) S 4238 J ( 54) S 0 12 F
PE
2880 1968 P 8 9 F (The) S 50 J ( selection preference is indicated by zero or more ) S 0 12 F 8 9 F I (tokens) S E 0 12 F 8 9 F () S 50 J ( inside square brackets. If) S
2880 2208 P (the token that follows the ) S 0 12 F 0 9 F ("b") S 0 12 F 8 9 F () S 50 J ( is one of the tokens inside the square brackets, the parser) S
2880 2448 P (will use the grammar rule for ) S 0 12 F 0 9 F ("a1") S 0 12 F 8 9 F (.) S 50 J ( If the token that follows the ) S 0 12 F 0 9 F ("b") S 0 12 F 8 9 F () S 50 J ( is not one of the) S
2880 2688 P (given tokens, the parser will use the rule for ) S 0 12 F 0 9 F ("a2") S 0 12 F 8 9 F (.) S 50 J ( In this way, the conflict between the) S
2880 2928 P (two rules is resolved -- the preference tells which one to select, depending on the value of) S
2880 3168 P (the lookahead token.) S
2880 3608 P (Notice that a selection preference states that a rule ) S 0 12 F 8 9 F I (should) S E 0 12 F 8 9 F () S 50 J ( be used when the next token is) S
2880 3848 P (one of the ones listed in the brackets and should ) S 0 12 F 8 9 F I (not) S E 0 12 F 8 9 F () S 50 J ( be used if it is not in the brackets.) S
2880 4288 P (The lookahead token is merely used to determine which rule to select. It is ) S 0 12 F 8 9 F I (not) S E 0 12 F 8 9 F () S 50 J ( part of the) S
2880 4528 P (rule itself. For example, suppose you have) S
3600 4948 P 0 12 F 0 9 F (a1) S 108 J ( : a b ['+' '-'] ;) S
3600 5168 P (a2 : a b ;) S
3600 5388 P (xx : a1 op expr ;) S
2880 5828 P 0 12 F 8 9 F (and) S 50 J ( suppose you have an ) S 0 12 F 0 9 F ("a") S 0 12 F 8 9 F (,) S 50 J ( a ) S 0 12 F 0 9 F ("b") S 0 12 F 8 9 F (,) S 50 J ( and ) S 0 12 F 0 9 F (+) S 0 12 F 8 9 F () S 50 J ( as the lookahead token. The ) S 0 12 F 0 9 F (+) S 0 12 F 8 9 F () S 50 J ( indicates) S
2880 6068 P (that the ) S 0 12 F 0 9 F ("a") S 0 12 F 8 9 F () S 50 J ( and ) S 0 12 F 0 9 F ("b") S 0 12 F 8 9 F () S 50 J ( should be reduced to ) S 0 12 F 0 9 F ("a1") S 0 12 F 8 9 F (.) S 50 J ( The parser does this and finds that the) S
2880 6308 P 0 12 F 0 9 F ("a1") S 0 12 F 8 9 F () S 50 J ( is part of the ) S 0 12 F 0 9 F ("xx") S 0 12 F 8 9 F () S 50 J ( rule. The ) S 0 12 F 0 9 F (+) S 0 12 F 8 9 F () S 50 J ( lookahead token will be associated with the ) S 0 12 F 0 9 F ("op") S
2880 6548 P 0 12 F 8 9 F (symbol) S 50 J ( in the ) S 0 12 F 0 9 F ("xx") S 0 12 F 8 9 F () S 50 J ( rule. In other words, a selection preference does not "use up" an input) S
2880 6788 P (token; it just looks at the token value to help resolve a conflict.) S
2880 7228 P (The ) S 0 12 F 0 9 F ($end) S 0 12 F 8 9 F () S 50 J ( symbol may be used inside square brackets to indicate end-of-file in the input) S
2880 7468 P (being parsed. For example,) S
3600 7888 P 0 12 F 0 9 F (statement) S 108 J ( : line ['\\n' $end]) S
2880 8328 P 0 12 F 8 9 F (shows) S 50 J ( that this rule is preferred if a ) S 0 12 F 0 9 F ("line") S 0 12 F 8 9 F () S 50 J ( is followed by a new-line character or the end) S
2880 8568 P (of the file.) S
2880 9008 P (The square brackets of a selection preference may contain no tokens, as in) S
3600 9428 P 0 12 F 0 9 F (x) S 108 J ( : y z [];) S
2880 9868 P 0 12 F 8 9 F (This) S 50 J ( says that the parser should never use this rule unless it cannot be avoided.) S
2880 10308 P (When there are many selection preferences in the same state, the parser must compare) S
2880 10548 P (them to check for conflicts. To do this, YAY chooses the most likely rule \(based on) S
2880 10788 P (preference\) and compares this to the other possible rules. In order to understand what we) S
2880 11028 P (mean by "most likely", an example will help. Consider the grammar) S
3600 11448 P 0 12 F 0 9 F (a) S 108 J ( : b [ 'x' ]) S
3600 11668 P ( | b [ 'y' ]) S
3600 11888 P ( | b [ ]) S
3600 12108 P ( ;) S
2880 12548 P 0 12 F 8 9 F (YAY) S 50 J ( must consider what happens when a ) S 0 12 F 0 9 F ("b") S 0 12 F 8 9 F () S 50 J ( symbol has been encountered. If the ) S 0 12 F 0 9 F ("b") S
2880 12788 P 0 12 F 8 9 F (is) S 50 J ( followed by an ) S 0 12 F 0 9 F ('x') S 0 12 F 8 9 F (,) S 50 J ( the most likely rule is) S
3600 13208 P 0 12 F 0 9 F (a) S 108 J ( : b [ 'x' ]) S
2880 14520 P 0 12 F 57 12 F (DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD) S
2880 14800 P 0 12 F 24 10 F (55) S 4238 J ( Thinkage) S 50 J ( YAY Reference Manual) S 0 12 F
PE
2160 2168 P 8 9 F (If) S 50 J ( the ) S 0 12 F 0 9 F ("b") S 0 12 F 8 9 F () S 50 J ( is followed by a ) S 0 12 F 0 9 F ('y') S 0 12 F 8 9 F (,) S 50 J ( the most likely rule is) S
2880 2588 P 0 12 F 0 9 F (a) S 108 J ( : b [ 'y' ]) S
2160 3028 P 0 12 F 8 9 F (If) S 50 J ( the ) S 0 12 F 0 9 F ("b") S 0 12 F 8 9 F () S 50 J ( is followed by any other token, the most likely rule is) S
2880 3448 P 0 12 F 0 9 F (a) S 108 J ( : b [ ]) S
2160 3888 P 0 12 F 8 9 F (Once) S 50 J ( the most likely rule has been chosen, it will be compared to the other rules in the set) S
2160 4128 P (to make sure that there are no conflicts. \(In a previous release, YAY would compare) S
2160 4368 P (every pair of rules, without choosing a most likely one. In this case, the rules) S
2880 4788 P 0 12 F 0 9 F (a) S 108 J ( : b [ 'x' ]) S
2880 5008 P (a : b [ 'y' ]) S
2160 5448 P 0 12 F 8 9 F (would) S 50 J ( conflict with each other, because there was no way to choose between the two if) S
2160 5688 P (the next token was not ) S 0 12 F 0 9 F ('x') S 0 12 F 8 9 F () S 50 J ( or ) S 0 12 F 0 9 F ('y') S 0 12 F 8 9 F (.) S 50 J ( In this release, YAY will compare the most likely rule) S
2160 5928 P (to all the others, but will not compare all the possible pairs.\)) S
2160 6368 P (Selection preferences can also be stated using the construct) S
2880 6788 P 0 12 F 0 9 F ([^) S 108 J ( T1 T2 T3 ...]) S
2160 7228 P 0 12 F 8 9 F (where) S 50 J ( the first character is a caret \() S 0 12 F 0 9 F (^) S 0 12 F 8 9 F (\)) S 50 J ( and ) S 0 12 F 0 9 F (T1) S 0 12 F 8 9 F (,) S 50 J ( ) S 0 12 F 0 9 F (T2) S 0 12 F 8 9 F (,) S 50 J ( etc. are tokens. When this is put on the) S
2160 7468 P (end of a rule, it indicates that the rule should be used if the lookahead token is ) S 0 12 F 8 9 F I (not) S E 0 12 F 8 9 F () S 50 J ( one of) S
2160 7708 P (the listed tokens. For example,) S
2880 8128 P 0 12 F 0 9 F (a1) S 108 J ( : a b) S
2880 8348 P ( { /* Action 1 */ } ;) S
2880 8568 P (a2 : a b [^ '+' '-']) S
2880 8788 P ( { /* Action 2 */ } ;) S
2160 9228 P 0 12 F 8 9 F (says) S 50 J ( that rule ) S 0 12 F 0 9 F ("a2") S 0 12 F 8 9 F () S 50 J ( should be used if the token after the ) S 0 12 F 0 9 F ("b") S 0 12 F 8 9 F () S 50 J ( is ) S 0 12 F 8 9 F I (not) S E 0 12 F 8 9 F () S 50 J ( ) S 0 12 F 0 9 F (+) S 0 12 F 8 9 F () S 50 J ( or ) S 0 12 F 0 9 F (-) S 0 12 F 8 9 F (.) S 50 J ( If the token is) S
2160 9468 P 0 12 F 0 9 F (+) S 0 12 F 8 9 F () S 50 J ( or ) S 0 12 F 0 9 F (-) S 0 12 F 8 9 F (,) S 50 J ( ) S 0 12 F 0 9 F ("a2") S 0 12 F 8 9 F () S 50 J ( should not be used \(so ) S 0 12 F 0 9 F ("a1") S 0 12 F 8 9 F () S 50 J ( will be\).) S
2160 9908 P (The construct ) S 0 12 F 0 9 F ([^]) S 0 12 F 8 9 F () S 50 J ( is the reverse of ) S 0 12 F 0 9 F ([]) S 0 12 F 8 9 F (.) S 50 J ( It is used to indicate a rule that should ) S 0 12 F 8 9 F I (always) S E 0 12 F 8 9 F () S 50 J ( be) S
2160 10148 P (taken unless there is another rule that must be taken because of a selection preference.) S
2160 10588 P (Selection preference constructs can be put in the middle of rules as well as on the end.) S
2160 10828 P (For example, we could write) S
2880 11248 P 0 12 F 0 9 F (expr) S 108 J ( : expr ['+' '-'] op expr) S
2880 11468 P ( { /* Action 1 */ }) S
2880 11688 P ( | expr op expr) S
2880 11908 P ( { /* Action 2 */ } ;) S
2160 12348 P 0 12 F 8 9 F (This) S 50 J ( states that if the first ) S 0 12 F 0 9 F ("expr") S 0 12 F 8 9 F () S 50 J ( is followed by ) S 0 12 F 0 9 F (+) S 0 12 F 8 9 F () S 50 J ( or ) S 0 12 F 0 9 F (-) S 0 12 F 8 9 F () S 50 J ( you want to use the first rule;) S
2160 12588 P (otherwise, you want to use the second. Note that the preference does not use up the ) S 0 12 F 0 9 F (+) S 0 12 F 8 9 F () S 50 J ( or) S
2160 12828 P 0 12 F 0 9 F (-) S 0 12 F 8 9 F () S 50 J ( token: you still need a symbol \() S 0 12 F 0 9 F ("op") S 0 12 F 8 9 F (\)) S 50 J ( to represent such tokens.) S
2160 14520 P 0 12 F 57 12 F (DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD) S
2160 14800 P 0 12 F 24 10 F (Thinkage) S 50 J ( YAY Reference Manual) S 4238 J ( 56) S 0 12 F
PE
2880 1968 P 8 9 F (Selection) S 50 J ( preferences that appear in the middle of a rule are implemented in the same) S
2880 2208 P (way as multiple actions, using dummy rules. The above example would result in) S
2880 2448 P (something like) S
3600 2868 P 0 12 F 0 9 F ($23) S 108 J ( : ['+' '-'] ;) S
3600 3088 P (expr : expr $23 op expr) S
3600 3308 P ( { /* Action 1 */ }) S
3600 3528 P ( | expr op expr) S
3600 3748 P ( { /* Action 2 */ } ;) S
2880 4188 P 0 12 F 8 9 F (\(where) S 50 J ( the ) S 0 12 F 0 9 F (23) S 0 12 F 8 9 F () S 50 J ( in ) S 0 12 F 0 9 F ($23) S 0 12 F 8 9 F () S 50 J ( is just a number we chose at random\). The dummy rule that is) S
2880 4428 P (created is a null string with the selection preference on the end. The first token for ) S 0 12 F 0 9 F ("op") S
2880 4668 P 0 12 F 8 9 F (will) S 50 J ( be the ) S 0 12 F 0 9 F (+) S 0 12 F 8 9 F () S 50 J ( or ) S 0 12 F 0 9 F (-) S 0 12 F 8 9 F () S 50 J ( that was the lookahead token in rule ) S 0 12 F 0 9 F ($23) S 0 12 F 8 9 F (.) S
2880 5108 P (If) S 50 J ( a selection preference in the middle of a rule is immediately followed by an action, only) S
2880 5348 P (one dummy rule is created to handle both the action and the preference.) S
2880 5788 P (In most cases, a selection preference counts as a ) S 0 12 F 0 9 F ($N) S 0 12 F 8 9 F () S 50 J ( symbol, but it has no associated) S
2880 6028 P (value. For example, in) S
3600 6448 P 0 12 F 0 9 F (expr) S 108 J ( : expr ['+' '-'] op expr) S
2880 6888 P 0 12 F 8 9 F (we) S 50 J ( have) S
3600 7308 P 0 12 F 0 9 F ($1) S 108 J ( -- ) S 0 12 F 8 9 F (first) S 0 12 F 0 9 F () S 108 J ( "expr") S
3600 7528 P ($2 -- ) S 0 12 F 8 9 F (no) S 50 J ( value) S
3600 7748 P 0 12 F 0 9 F ($3) S 108 J ( -- "op") S
3600 7968 P ($4 -- ) S 0 12 F 8 9 F (second) S 0 12 F 0 9 F () S 108 J ( "expr") S
2880 8408 P 0 12 F 8 9 F (If) S 50 J ( the preference is followed by an action, the preference and the action count as a single) S
2880 8648 P 0 12 F 0 9 F ($N) S 0 12 F 8 9 F () S 50 J ( symbol whose value is equal to the ) S 0 12 F 0 9 F ($$) S 0 12 F 8 9 F () S 50 J ( value of the action. For example, in) S
3600 9068 P 0 12 F 0 9 F (expr) S 108 J ( : expr ['+' '-'] {action} op expr) S
2880 9508 P 0 12 F 8 9 F (we) S 50 J ( have) S
3600 9928 P 0 12 F 0 9 F ($1) S 108 J ( -- ) S 0 12 F 8 9 F (first) S 0 12 F 0 9 F () S 108 J ( "expr") S
3600 10148 P ($2 -- $$) S 0 12 F 8 9 F () S 50 J ( of) S 0 12 F 0 9 F () S 108 J ( action) S
3600 10368 P ($3 -- op) S
3600 10588 P ($4 -- ) S 0 12 F 8 9 F (second) S 0 12 F 0 9 F () S 108 J ( "expr") S
2880 11028 P 0 12 F 8 9 F (The) S 50 J ( ) S 0 12 F 0 9 F B (%prec) S E 0 12 F 8 9 F () S 50 J ( construct is incompatible with rules that contain selection preferences,) S
2880 11268 P (because the preference is all that is needed to resolve conflicts. For this reason, YAY) S
2880 11508 P (issues an error message if a rule contains both a preference and the ) S 0 12 F 0 9 F B (%prec) S E 0 12 F 8 9 F () S 50 J ( construct.) S
2880 11948 P (Selection preferences can be used to resolve most conflicts. Indeed, there may be cases) S
2880 12188 P (where the most practical course of action is to write a number of conflicting rules that) S
2880 12428 P (contain selection preferences to resolve the conflicts, as in) S
2880 14520 P 0 12 F 57 12 F (DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD) S
2880 14800 P 0 12 F 24 10 F (57) S 4238 J ( Thinkage) S 50 J ( YAY Reference Manual) S 0 12 F
PE
2880 1948 P 0 9 F (expr) S 108 J ( : expr ['+' '-'] op expr) S
2880 2168 P ( | expr ['*' '/' '%'] op expr) S
2880 2388 P ( | expr ['&' '|'] op expr) S
2880 2608 P ( ...) S
2160 3048 P 0 12 F 0 9 F B (Note:) S E 0 12 F 8 9 F () S 50 J ( selection preferences of the form) S
2880 3468 P 0 12 F 0 9 F ([error]) S
2880 3688 P ([^) S 108 J ( error]) S
2160 4128 P 0 12 F 8 9 F (are) S 50 J ( not useful. Selection preferences are implemented through \(dummy\) Reduce actions,) S
2160 4368 P (but the parser's error-handling routines always look for Shift actions and ignore) S
2160 4608 P (Reductions.) S
2160 5428 P 0 12 F 8 12 F B (9.4) S 67 J ( Negative Numbers in $N Constructs) S E
2160 6148 P 0 12 F 8 9 F (YAY) S 50 J ( lets you use constructs like ) S 0 12 F 0 9 F ($0) S 0 12 F 8 9 F (,) S 50 J ( ) S 0 12 F 0 9 F ($-1) S 0 12 F 8 9 F (,) S 50 J ( ) S 0 12 F 0 9 F ($-2) S 0 12 F 8 9 F (,) S 50 J ( and so on in recognition actions. These) S
2160 6388 P (were once important, but the techniques for specifying multiple actions have made them) S
2160 6628 P (obsolete. YAY only supports the constructs for compatibility with older grammars.) S
2160 7068 P (To understand what these constructs mean, it is important to think in terms of the state) S
2160 7308 P (stack. Each ) S 0 12 F 0 9 F ($N) S 0 12 F 8 9 F () S 50 J ( construct is associated with a state on the stack; the value of ) S 0 12 F 0 9 F ($N) S 0 12 F 8 9 F () S 50 J ( is the) S
2160 7548 P (value of the token or non-terminal symbol associated with the state at the time of a) S
2160 7788 P (Reduce operation. \(Recall that recognition actions are executed when the appropriate) S
2160 8028 P (Reduce action takes place.\)) S
2160 8468 P 0 12 F 0 9 F ($1) S 0 12 F 8 9 F () S 50 J ( is the value associated with the state that found the first component of the grammar) S
2160 8708 P (rule; ) S 0 12 F 0 9 F ($2) S 0 12 F 8 9 F () S 50 J ( is the value associated with the second state, and so on.) S
2160 9148 P 0 12 F 0 9 F ($0) S 0 12 F 8 9 F () S 50 J ( is the value associated with the state that was on top of the stack before the first) S
2160 9388 P (component of the grammar rule was found.) S
2160 9828 P 0 12 F 0 9 F ($-1) S 0 12 F 8 9 F () S 50 J ( is the value associated with the state before that, and so on. All of these states are) S
2160 10068 P (still on the stack, and their value can be obtained in this way.) S
2160 10508 P (As an artificial example, suppose that a grammar has the rules) S
2880 10928 P 0 12 F 0 9 F (stmt) S 108 J ( : IF condition stmt) S
2880 11148 P ( | WHILE condition stmt) S
2880 11368 P (condition : /* something */) S
2880 11588 P ( { /* action */ }) S
2160 12028 P 0 12 F 8 9 F (The) S 50 J ( action associated with the condition can use the ) S 0 12 F 0 9 F ($-1) S 0 12 F 8 9 F () S 50 J ( construct to find out if the) S
2160 12268 P (preceding token was ) S 0 12 F 0 9 F (IF) S 0 12 F 8 9 F () S 50 J ( or ) S 0 12 F 0 9 F (WHILE) S 0 12 F 8 9 F (.) S 50 J ( \(Of course, this assumes that the only items that can) S
2160 12508 P (precede a condition are the ) S 0 12 F 0 9 F (IF) S 0 12 F 8 9 F () S 50 J ( and ) S 0 12 F 0 9 F (WHILE) S 0 12 F 8 9 F () S 50 J ( tokens.\) There are occasionally times when) S
2160 12748 P (this sort of information is needed.) S
2160 14520 P 0 12 F 57 12 F (DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD) S
2160 14800 P 0 12 F 24 10 F (Thinkage) S 50 J ( YAY Reference Manual) S 4238 J ( 58) S 0 12 F
PE
2880 2308 P 8 12 F B (9.5) S 67 J ( Lists and Handling Null Strings) S E
2880 3028 P 0 12 F 8 9 F (Grammars) S 50 J ( often define lists of items. There are two common ways to do this:) S
3600 3448 P 0 12 F 0 9 F (list) S 108 J ( : item) S
3600 3668 P ( | list item ;) S
2880 4108 P 0 12 F 8 9 F (or) S
3600 4528 P 0 12 F 0 9 F (list) S 108 J ( : /* null */) S
3600 4748 P ( | list item ;) S
2880 5188 P 0 12 F 8 9 F (The) S 50 J ( first definition means that every "list" has at least one item. The second allows zero-) S
2880 5428 P (length lists.) S
2880 5868 P (Using the second definition is sometimes necessary or convenient, but it can lead to) S
2880 6108 P (difficulties. To understand why, consider a grammar with) S
3600 6528 P 0 12 F 0 9 F (list1) S 108 J ( : /* null */) S
3600 6748 P ( | list1 item1 ;) S
3600 6968 P (list2 : /* null */) S
3600 7188 P ( | list2 item2 ;) S
3600 7408 P (list : list1) S
3600 7628 P ( | list2 ;) S
2880 8068 P 0 12 F 8 9 F (When) S 50 J ( the parser is in a position to look for a ) S 0 12 F 0 9 F ("list") S 0 12 F 8 9 F (,) S 50 J ( it automatically finds a null string,) S
2880 8308 P (then gets a conflict because it can't decide if the null string is an instance of ) S 0 12 F 0 9 F ("list1") S 0 12 F 8 9 F () S 50 J ( or) S
2880 8548 P 0 12 F 0 9 F ("list2") S 0 12 F 8 9 F (.) S 50 J ( This problem is less likely to happen if you define) S
3600 8968 P 0 12 F 0 9 F (list1) S 108 J ( : item1) S
3600 9188 P ( | list1 item1 ;) S
3600 9408 P (list2 : item2) S
3600 9628 P ( | list2 item2 ;) S
3600 9848 P (list : /* null */) S
3600 10068 P ( | list1) S
3600 10288 P ( | list2) S
3600 10508 P ( ;) S
2880 10948 P 0 12 F 8 9 F (The) S 50 J ( parser can determine if it has a ) S 0 12 F 0 9 F ("list1") S 0 12 F 8 9 F () S 50 J ( or a ) S 0 12 F 0 9 F ("list2") S 0 12 F 8 9 F () S 50 J ( by seeing if the list starts with) S
2880 11188 P 0 12 F 0 9 F ("item1") S 0 12 F 8 9 F () S 50 J ( or ) S 0 12 F 0 9 F ("item2") S 0 12 F 8 9 F (.) S
2880 11628 P (A) S 50 J ( YAY-produced parser avoids infinite recursions that might result from matching the) S
2880 11868 P (same null string over and over again. If the parser matches a null string in a particular) S
2880 12108 P (state, goes through a few more states and shifts once more into the state where the null) S
2880 12348 P (string was matched, it will not match the null string again. Without this behaviour, you get) S
2880 12588 P (infinite recursions on null strings. However, the behaviour occasionally gets in the way if) S
2880 12828 P (you ) S 0 12 F 8 9 F I (want) S E 0 12 F 8 9 F () S 50 J ( to match more than one null string in a row. For example, consider how you) S
2880 13068 P (might write the grammar rules for types that may be used in a C cast operation, as in) S
2880 14520 P 0 12 F 57 12 F (DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD) S
2880 14800 P 0 12 F 24 10 F (59) S 4238 J ( Thinkage) S 50 J ( YAY Reference Manual) S 0 12 F
PE
2880 2148 P 0 9 F (char_ptr) S 108 J ( = \(char *\) float_ptr;) S
2160 2588 P 0 12 F 8 9 F (The) S 50 J ( rules for the parenthesized cast expression might be written as) S
2880 3008 P 0 12 F 0 9 F (cast) S 108 J ( : '\(' basic_type opt_abstract '\)' ;) S
2880 3228 P (opt_abstract : /* null */) S
2880 3448 P ( | abstract;) S
2880 3668 P (abstract : '\(' abstract '\)') S
2880 3888 P ( | opt_abstract '[' ']') S
2880 4108 P ( | opt_abstract '\(' '\)') S
2880 4328 P ( | '*' opt_abstract) S
2880 4548 P ( ;) S
2160 4988 P 0 12 F 8 9 F (Consider) S 50 J ( what happens with a cast like) S
2880 5408 P 0 12 F 0 9 F (\(int) S 108 J ( *[]\)) S
2160 5848 P 0 12 F 8 9 F (This) S 50 J ( will be interpreted as ) S 0 12 F 0 9 F (*) S 0 12 F 8 9 F () S 50 J ( followed by a null ) S 0 12 F 0 9 F ("opt_abstract") S 0 12 F 8 9 F () S 50 J ( followed by a null) S
2160 6088 P 0 12 F 0 9 F ("opt_abstract") S 0 12 F 8 9 F () S 50 J ( followed by square brackets. However, the parser will ) S 0 12 F 8 9 F I (not) S E 0 12 F 8 9 F () S 50 J ( accept two) S
2160 6328 P (null ) S 0 12 F 0 9 F ("opt_abstracts") S 0 12 F 8 9 F () S 50 J ( in a row, and will take some other course of action.) S
2160 6768 P (To correct this problem, you must rewrite the grammar rules. Instead of using the) S
2160 7008 P 0 12 F 0 9 F ("opt_abstract") S 0 12 F 8 9 F () S 50 J ( rules, have rules with and without an ) S 0 12 F 0 9 F ("abstract") S 0 12 F 8 9 F (.) S
2880 7428 P 0 12 F 0 9 F (cast) S 108 J ( : '\(' basic_type abstract '\)' ;) S
2880 7648 P (abstract : /* null */) S
2880 7868 P ( | abstract '[' ']') S
2880 8088 P ( | '[' ']') S
2880 8308 P ( | abstract '\(' '\)') S
2880 8528 P ( | '\(' '\)') S
2880 8748 P ( | '*' abstract) S
2880 8968 P ( | '*') S
2880 9188 P ( ;) S
2160 10008 P 0 12 F 8 12 F B (9.6) S 67 J ( Right vs. Left Recursion) S E
2160 10728 P 0 12 F 8 9 F (Chapter) S 50 J ( 3 mentioned left and right recursion. For example, if a program consists of a) S
2160 10968 P (number of statements separated by semicolons, we might define it with right recursion as) S
2880 11388 P 0 12 F 0 9 F (program) S 108 J ( : statement) S
2880 11608 P ( | statement ';' program ;) S
2160 12048 P 0 12 F 8 9 F (or) S 50 J ( with left recursion as) S
2880 12468 P 0 12 F 0 9 F (program) S 108 J ( : statement) S
2880 12688 P ( | program ';' statement ;) S
2160 14520 P 0 12 F 57 12 F (DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD) S
2160 14800 P 0 12 F 24 10 F (Thinkage) S 50 J ( YAY Reference Manual) S 4238 J ( 60) S 0 12 F
PE
2880 1968 P 8 9 F (If) S 50 J ( you think about the way that the state stack works, you will see that the second way is) S
2880 2208 P (much to be preferred. Consider, for example, the way something like) S
3600 2628 P 0 12 F 0 9 F (S1) S 108 J ( ; S2 ; S3 ; S4) S
2880 3068 P 0 12 F 8 9 F (would) S 50 J ( be handled \(where all the ) S 0 12 F 0 9 F (Sn) S 0 12 F 8 9 F ('s) S 50 J ( are statements\).) S
2880 3508 P (With right recursion, the parser would gather ) S 0 12 F 0 9 F ("S1;") S 0 12 F 8 9 F () S 50 J ( and then go looking for a program.) S
2880 3748 P (To gather this program, it would gather ) S 0 12 F 0 9 F (S2) S 0 12 F 8 9 F (.) S 50 J ( It would then look at the lookahead symbol ) S 0 12 F 0 9 F (;) S
2880 3988 P 0 12 F 8 9 F (and) S 50 J ( see that this program had the form) S
3600 4408 P 0 12 F 0 9 F (statement) S 108 J ( ';' program) S
2880 4848 P 0 12 F 8 9 F (The) S 50 J ( parser would then go ahead and gather the program after the semicolon. But after) S
2880 5088 P 0 12 F 0 9 F (S3) S 0 12 F 8 9 F (,) S 50 J ( it would find another semicolon, so would begin gathering yet another program. If you) S
2880 5328 P (work the process through, you will find that the state stack grows to seven entries \(one for) S
2880 5568 P (each ) S 0 12 F 0 9 F (Sn) S 0 12 F 8 9 F () S 50 J ( and one for each ) S 0 12 F 0 9 F (;) S 0 12 F 8 9 F (\)) S 50 J ( before the first Reduce takes place.) S
2880 6008 P (On the other hand, if you have the left recursion) S
3600 6428 P 0 12 F 0 9 F (program) S 108 J ( : program ';' statement) S
2880 6868 P 0 12 F 8 9 F (and) S 50 J ( the same input, the parser will perform a Reduce as soon as it sees) S
3600 7288 P 0 12 F 0 9 F (S1) S 108 J ( ; S2) S
2880 7728 P 0 12 F 8 9 F (This) S 50 J ( is Reduced to a single state corresponding to the non-terminal symbol ) S 0 12 F 0 9 F ("program") S 0 12 F 8 9 F (.) S
2880 7968 P (The) S 50 J ( parser reads ) S 0 12 F 0 9 F (";S3") S 0 12 F 8 9 F () S 50 J ( and Reduces) S
3600 8388 P 0 12 F 0 9 F (program) S 108 J ( ; S3) S
2880 8828 P 0 12 F 8 9 F (to) S 50 J ( ) S 0 12 F 0 9 F ("program") S 0 12 F 8 9 F () S 50 J ( again. The process repeats for the last statement.) S
2880 9268 P (If you follow this through, the state stack never grows longer than three states, as) S
2880 9508 P (compared to the seven that are required for the right recursive rule. With right recursion,) S
2880 9748 P (no reduction takes place until the entire list of elements has been read; with left recursion,) S
2880 9988 P (a reduction takes place as each new list element is encountered. Left recursion can) S
2880 10228 P (therefore save a lot of stack space.) S
2880 10668 P (The choice of left or right recursion can also affect the order in which recognition actions) S
2880 10908 P (are performed. Suppose ) S 0 12 F 0 9 F (T) S 0 12 F 8 9 F () S 50 J ( is a token. If we define) S
3600 11328 P 0 12 F 0 9 F (x) S 108 J ( : /* null */) S
3600 11548 P ( | y ',' x {a1} ;) S
3600 11768 P (y : T {a2} ;) S
2880 12208 P 0 12 F 8 9 F (then) S 50 J ( the input) S
3600 12628 P 0 12 F 0 9 F (T) S 108 J ( , T , T) S
2880 13068 P 0 12 F 8 9 F (would) S 50 J ( execute recognition actions in the order) S
2880 14520 P 0 12 F 57 12 F (DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD) S
2880 14800 P 0 12 F 24 10 F (61) S 4238 J ( Thinkage) S 50 J ( YAY Reference Manual) S 0 12 F
PE
2880 2148 P 0 9 F ({a2}) S 108 J ( {a2} {a2} {a1} {a1} {a1}) S
2160 2588 P 0 12 F 8 9 F (The) S 50 J ( ) S 0 12 F 0 9 F ({a2}) S 0 12 F 8 9 F () S 50 J ( actions are performed each time a ) S 0 12 F 0 9 F (T) S 0 12 F 8 9 F () S 50 J ( is Reduced to ) S 0 12 F 0 9 F ("y") S 0 12 F 8 9 F (.) S 50 J ( The ) S 0 12 F 0 9 F ({a1}) S 0 12 F 8 9 F () S 50 J ( actions) S
2160 2828 P (don't happen until the entire list has been read, because right recursion reads the entire) S
2160 3068 P (list before any Reduce actions take place.) S
2160 3508 P (On the other hand, if you define) S
2880 3928 P 0 12 F 0 9 F (x) S 108 J ( : /* null */) S
2880 4148 P ( | x ',' y {a1} ;) S
2880 4368 P (y : T {a2};) S
2160 4808 P 0 12 F 8 9 F (the) S 50 J ( recognition actions for the same input will take place in the order) S
2880 5228 P 0 12 F 0 9 F ({a2}) S 108 J ( {a1} {a2} {a1} {a2} {a1}) S
2160 5668 P 0 12 F 8 9 F (With) S 50 J ( left recursion, Reduce actions take place every time a new element is read in for the) S
2160 5908 P (list.) S
2160 6348 P (This means that if you want the action order) S
2880 6768 P 0 12 F 0 9 F ({a2}) S 108 J ( {a2} {a2} {a1} {a1} {a1}) S
2160 7208 P 0 12 F 8 9 F (you) S 50 J ( must use right recursion even though it takes more stack space.) S
2160 8028 P 0 12 F 8 12 F B (9.7) S 67 J ( YYDEBUG) S E
2160 8748 P 0 12 F 8 9 F (If) S 50 J ( you ) S 0 12 F 0 9 F B (#define) S E 0 12 F 8 9 F () S 50 J ( a symbol named ) S 0 12 F 0 9 F (YYDEBUG) S 0 12 F 8 9 F () S 50 J ( in the declarations section and set the variable) S
2160 8988 P 0 12 F 0 9 F ("yydebug") S 0 12 F 8 9 F () S 50 J ( to a non-zero value, your parser will print a good deal of debugging) S
2160 9228 P (information as it parses input. In this description, we will describe the output you may see.) S
2160 9668 P (Every time ) S 0 12 F 0 9 F ("yylex") S 0 12 F 8 9 F () S 50 J ( obtains a token, the parser prints) S
2880 10088 P 0 12 F 0 9 F (read) S 108 J ( T \(VALUE\)) S
2160 10528 P (T) S 0 12 F 8 9 F () S 50 J ( is the name of the token and ) S 0 12 F 0 9 F (VALUE) S 0 12 F 8 9 F () S 50 J ( is the numeric value. Thus if ) S 0 12 F 0 9 F ("yylex") S 0 12 F 8 9 F () S 50 J ( has read an) S
2160 10768 P 0 12 F 0 9 F (IF) S 0 12 F 8 9 F () S 50 J ( token, you might see) S
2880 11188 P 0 12 F 0 9 F (read) S 108 J ( IF \(257\)) S
2160 11628 P 0 12 F 8 9 F (Every) S 50 J ( time the parser enters a state, it will print out) S
2880 12048 P 0 12 F 0 9 F (state) S 108 J ( N \(X\), char \(C\)) S
2160 12488 P 0 12 F 8 9 F (where) S 50 J ( ) S 0 12 F 0 9 F (N) S 0 12 F 8 9 F () S 50 J ( is the state number as given in the State Description report, and ) S 0 12 F 0 9 F (X) S 0 12 F 8 9 F () S 50 J ( and ) S 0 12 F 0 9 F (C) S 0 12 F 8 9 F () S 50 J ( are) S
2160 12728 P (other integers. ) S 0 12 F 0 9 F (X) S 0 12 F 8 9 F () S 50 J ( is another number for the state -- YAY actually renumbers the states and) S
2160 12968 P (grammar rules after it generates the State Description report in order to improve the) S
2160 13208 P (parser's efficiency, and ) S 0 12 F 0 9 F (X) S 0 12 F 8 9 F () S 50 J ( gives the state number after renumbering. ) S 0 12 F 0 9 F (C) S 0 12 F 8 9 F () S 50 J ( is the token type of) S
2160 14520 P 0 12 F 57 12 F (DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD) S
2160 14800 P 0 12 F 24 10 F (Thinkage) S 50 J ( YAY Reference Manual) S 4238 J ( 62) S 0 12 F
PE
2880 1968 P 8 9 F (the) S 50 J ( lookahead symbol if the symbol is a token. If the symbol is not a token, or if there is) S
2880 2208 P (no lookahead symbol at the moment, ) S 0 12 F 0 9 F (C) S 0 12 F 8 9 F () S 50 J ( is ) S 0 12 F 0 9 F (-1) S 0 12 F 8 9 F (.) S 50 J ( As an example,) S
3600 2628 P 0 12 F 0 9 F (state) S 108 J ( 6 \(22\), char \(-1\)) S
2880 3068 P 0 12 F 8 9 F (indicates) S 50 J ( that the parser has entered State 6 on the State Description Report \(State 22) S
2880 3308 P (after renumbering\) and that the current lookahead symbol is not a token.) S
2880 3748 P (Every time the parser performs a Shift action, it prints) S
3600 4168 P 0 12 F 0 9 F (shift) S 108 J ( N \(X\)) S
2880 4608 P 0 12 F 8 9 F (where) S 50 J ( ) S 0 12 F 0 9 F (N) S 0 12 F 8 9 F () S 50 J ( is the number of the state to which the parser is shifting and ) S 0 12 F 0 9 F (X) S 0 12 F 8 9 F () S 50 J ( is the number of) S
2880 4848 P (the same state after renumbering.) S
2880 5288 P (Every time the parser performs a Reduce action, it prints) S
3600 5708 P 0 12 F 0 9 F (reduce) S 108 J ( N \(X\), pops M \(Y\)) S
2880 6148 P 0 12 F 8 9 F (This) S 50 J ( says that the parser has Reduced by grammar rule ) S 0 12 F 0 9 F (N) S 0 12 F 8 9 F () S 50 J ( \(renumbered to ) S 0 12 F 0 9 F (X) S 0 12 F 8 9 F (\).) S 50 J ( After the) S
2880 6388 P (reduction, the state on top of the state stack was State ) S 0 12 F 0 9 F (M) S 0 12 F 8 9 F () S 50 J ( \(renumbered to ) S 0 12 F 0 9 F (Y) S 0 12 F 8 9 F (\).) S
2880 7208 P 0 12 F 8 12 F B (9.8) S 67 J ( Important Symbols) S E
2880 7928 P 0 12 F 8 9 F (Debugging) S 50 J ( a parser produced by YAY can be a very difficult task, since the code is only) S
2880 8168 P (partly produced by user input. The remaining code is standard software produced by YAY) S
2880 8408 P (itself. The situation is aggravated by another problem: the state and rule numbers shown) S
2880 8648 P (in the State Description report are not the same as the numbers used when the parser) S
2880 8888 P (actually runs. In the interests of optimization, the parser sorts the states into a more) S
2880 9128 P (convenient order. Thus the ) S 0 12 F 8 9 F I (internal) S E 0 12 F 8 9 F () S 50 J ( state number used by the program is usually not the) S
2880 9368 P (same as the ) S 0 12 F 8 9 F I (external) S E 0 12 F 8 9 F () S 50 J ( state number known to the user.) S
2880 9808 P (To help those who are examining parser code using a symbolic debugger, here are a few) S
2880 10048 P (of the important variables that the parser uses.) S
2880 10488 P 0 12 F 0 9 F (yyval) S
3600 10728 P 0 12 F 8 9 F (holds) S 50 J ( the value ) S 0 12 F 0 9 F ($$) S 0 12 F 8 9 F () S 50 J ( at the time of a reduction. This has the type ) S 0 12 F 0 9 F (YYSTYPE) S 0 12 F 8 9 F (.) S
2880 11168 P 0 12 F 0 9 F (yychar) S
3600 11408 P 0 12 F 8 9 F (holds) S 50 J ( the most recent token value returned by ) S 0 12 F 0 9 F ("yylex") S 0 12 F 8 9 F (.) S
2880 11848 P 0 12 F 0 9 F (yystate) S
3600 12088 P 0 12 F 8 9 F (is) S 50 J ( the ) S 0 12 F 8 9 F I (internal) S E 0 12 F 8 9 F () S 50 J ( number of the current state.) S
2880 12528 P 0 12 F 0 9 F (yyps) S
3600 12768 P 0 12 F 8 9 F (points) S 50 J ( to the current top of the state stack. Thus ) S 0 12 F 0 9 F (yyps[0]) S 0 12 F 8 9 F () S 50 J ( is the internal number) S
3600 13008 P (of the current state, ) S 0 12 F 0 9 F (yyps[-1]) S 0 12 F 8 9 F () S 50 J ( is the internal number of the previous state, and) S
3600 13248 P (so on.) S
2880 14520 P 0 12 F 57 12 F (DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD) S
2880 14800 P 0 12 F 24 10 F (63) S 4238 J ( Thinkage) S 50 J ( YAY Reference Manual) S 0 12 F
PE
2160 2168 P 0 9 F (yypv) S
2880 2408 P 0 12 F 8 9 F (points) S 50 J ( to the top of the current value stack. The entries in this stack have the) S
2880 2648 P (type ) S 0 12 F 0 9 F (YYSTYPE) S 0 12 F 8 9 F (.) S 50 J ( When a Reduce operation executes a recognition action, this) S
2880 2888 P (pointer is moved down the stack to the point where) S
2880 3308 P 0 12 F 0 9 F (yypv[1]) S 108 J ( = $1) S
2880 3528 P (yypv[2] = $2) S
2880 3748 P ( etc.) S
2160 4188 P (yyi) S
2880 4428 P 0 12 F 8 9 F (is) S 50 J ( the internal number of the rule being reduced by a Reduce action.) S
2160 4868 P 0 12 F 0 9 F (yyrmap) S
2880 5108 P 0 12 F 8 9 F (is) S 50 J ( an array present only when ) S 0 12 F 0 9 F (YYDEBUG) S 0 12 F 8 9 F () S 50 J ( is defined. It is used to convert internal) S
2880 5348 P (rule numbers to external ones. For example,) S
2880 5768 P 0 12 F 0 9 F (yyrmap[yyi]) S
2880 6208 P 0 12 F 8 9 F (is) S 50 J ( the external number of the rule being reduced by a Reduce action.) S
2160 6648 P 0 12 F 0 9 F (yysmap) S
2880 6888 P 0 12 F 8 9 F (is) S 50 J ( an array present only when ) S 0 12 F 0 9 F (YYDEBUG) S 0 12 F 8 9 F () S 50 J ( is defined. It is used to convert internal) S
2880 7128 P (state numbers to external ones. For example,) S
2880 7548 P 0 12 F 0 9 F (yysmap[yystate]) S
2880 7988 P 0 12 F 8 9 F (is) S 50 J ( the external number of the current state.) S
2160 8808 P 0 12 F 8 12 F B (9.9) S 67 J ( How YYERROR May Be Used) S E
2160 9528 P 0 12 F 8 9 F (The) S 50 J ( ) S 0 12 F 0 9 F (YYERROR) S 0 12 F 8 9 F () S 50 J ( macro creates an artificial error condition. To show how this can be useful,) S
2160 9768 P (suppose we have a line-by-line desk calculator that allows parenthesization of expressions) S
2160 10008 P (and suppose we have a variable named ) S 0 12 F 0 9 F ("depth") S 0 12 F 8 9 F () S 50 J ( that keeps track of how deeply) S
2160 10248 P (parentheses are nested. Every time the parser finds an opening parenthesis, it adds 1 to) S
2160 10488 P 0 12 F 0 9 F ("depth") S 0 12 F 8 9 F (.) S 50 J ( Every time it finds a closing parenthesis, it subtracts 1.) S
2160 10928 P (Consider how the following definitions will work.) S
2880 11348 P 0 12 F 0 9 F (expr) S 108 J ( : lp expr '\)') S
2880 11568 P ( {depth--;}) S
2880 11788 P ( | lp error) S
2880 12008 P ( {depth--;}) S
2880 12228 P ( ;) S
2880 12448 P (lp : '\(' {depth++;};) S
2160 12888 P 0 12 F 8 9 F (If) S 50 J ( no error occurs, the ) S 0 12 F 0 9 F ("depth") S 0 12 F 8 9 F () S 50 J ( variable is incremented and decremented correctly. If an) S
2160 13128 P (error does occur, however, what happens? Your ) S 0 12 F 0 9 F ("yyerror") S 0 12 F 8 9 F () S 50 J ( routine is called on to) S
2160 14520 P 0 12 F 57 12 F (DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD) S
2160 14800 P 0 12 F 24 10 F (Thinkage) S 50 J ( YAY Reference Manual) S 4238 J ( 64) S 0 12 F
PE
2880 1968 P 8 9 F (recover) S 50 J ( from the error in the middle of an expression. Often, it is more reasonable to) S
2880 2208 P (postpone this recovery until you reach a point where you have a whole expression.) S
2880 2448 P (Therefore, you might use the following alternate definition.) S
3600 2868 P 0 12 F 0 9 F (expr) S 108 J ( : lp error) S
3600 3088 P ( {depth--; YYERROR;}) S
3600 3308 P ( ;) S
3600 3528 P (line : error '\\n' prompt line) S
3600 3748 P ( { $$ = $4; } ;) S
3600 3968 P (prompt : /* null token */) S
3600 4188 P ( {printf\("Please re-enter line.\\n"\);};) S
2880 4628 P 0 12 F 8 9 F (Now,) S 50 J ( what happens when the grammar is asked to parse a line like) S
3600 5048 P 0 12 F 0 9 F (1) S 108 J ( + \(\( a +) S
2880 5488 P 0 12 F 8 9 F (When) S 50 J ( the end of the line is encountered, the parser recognizes an error has occurred.) S
2880 5728 P (Going up the stack, the first state ready to handle the error is) S
3600 6148 P 0 12 F 0 9 F (expr) S 108 J ( : lp error ;) S
2880 6588 P 0 12 F 8 9 F (At) S 50 J ( this point, the parser will Reduce the input) S
3600 7008 P 0 12 F 0 9 F (\() S 108 J ( a +) S
2880 7448 P 0 12 F 8 9 F (into) S 50 J ( an ) S 0 12 F 0 9 F ("expr") S 0 12 F 8 9 F (.) S 50 J ( The Reduction executes the recognition action: it decrements ) S 0 12 F 0 9 F ("depth") S 0 12 F 8 9 F (,) S
2880 7688 P (then) S 50 J ( signals that an error has taken place. The Error action begins popping the stack) S
2880 7928 P (again. It will find the previous opening parenthesis, recognize another) S
3600 8348 P 0 12 F 0 9 F (lp) S 108 J ( error) S
2880 8788 P 0 12 F 8 9 F (construct) S 50 J ( and perform another reduction. The parenthesis count is again decremented,) S
2880 9028 P (and another error condition generated.) S
2880 9468 P (This time, the grammar rule that deals with the error is the definition of ) S 0 12 F 0 9 F ("line") S 0 12 F 8 9 F (.) S 50 J ( An error) S
2880 9708 P (message is issued and a new line is requested. In this way, the parser has worked its way) S
2880 9948 P (back to error-handling code that can deal with the situation. Along the way, the parser) S
2880 10188 P (correctly decremented the ) S 0 12 F 0 9 F ("depth") S 0 12 F 8 9 F () S 50 J ( variable to account for the missing parentheses.) S
2880 10628 P (This method of dealing with errors decrements ) S 0 12 F 0 9 F ("depth") S 0 12 F 8 9 F () S 50 J ( for every unbalanced opening) S
2880 10868 P (parenthesis on the line. This corrects the ) S 0 12 F 0 9 F ("depth") S 0 12 F 8 9 F () S 50 J ( count properly. Our first definition) S
2880 11108 P (\(without the ) S 0 12 F 0 9 F (YYERROR) S 0 12 F 8 9 F () S 50 J ( call\) would only have decremented ) S 0 12 F 0 9 F ("depth") S 0 12 F 8 9 F () S 50 J ( once.) S
2880 11548 P (This example is somewhat contrived, of course -- you could always just set ) S 0 12 F 0 9 F ("depth") S 0 12 F 8 9 F () S 50 J ( to) S
2880 11788 P (zero whenever you started a new line of input. The usefulness of the technique is more) S
2880 12028 P (apparent in situations where you obtain memory with ) S 0 12 F 0 9 F ("malloc") S 0 12 F 8 9 F () S 50 J ( whenever you get an) S
2880 12268 P (opening delimiter and free the memory with ) S 0 12 F 0 9 F ("free") S 0 12 F 8 9 F () S 50 J ( whenever you get a closing delimiter.) S
2880 12508 P (In this case, it is obvious that you need to do precisely as many ) S 0 12 F 0 9 F ("free") S 0 12 F 8 9 F () S 50 J ( operations as) S
2880 12748 P 0 12 F 0 9 F ("malloc") S 0 12 F 8 9 F () S 50 J ( operations, so you must raise the error condition for each unbalanced opening) S
2880 12988 P (delimiter.) S
2880 14520 P 0 12 F 57 12 F (DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD) S
2880 14800 P 0 12 F 24 10 F (65) S 4238 J ( Thinkage) S 50 J ( YAY Reference Manual) S 0 12 F
PE
2160 1968 P 8 9 F (You) S 50 J ( might think that the symbol ) S 0 12 F 0 9 F ("lp") S 0 12 F 8 9 F () S 50 J ( is unnecessary, and you could just define) S
2880 2388 P 0 12 F 0 9 F (expr) S 108 J ( : '\(' {depth++;} expr '\)' {depth--;}) S
2880 2608 P ( | '\(' error {depth--;} ;) S
2160 3048 P 0 12 F 8 9 F (However,) S 50 J ( this would not work in general. There is no guarantee that the action) S
2880 3468 P 0 12 F 0 9 F ({depth++;}) S
2160 3908 P 0 12 F 8 9 F (would) S 50 J ( be executed in all cases, particularly if the token after the ) S 0 12 F 0 9 F ('\(') S 0 12 F 8 9 F () S 50 J ( was one that could) S
2160 4148 P (not start an expression.) S
2160 4588 P (As an interesting example of another way to use ) S 0 12 F 0 9 F (YYERROR) S 0 12 F 8 9 F (,) S 50 J ( consider the following \(taken) S
2160 4828 P (from a parser for the Pascal programming language\).) S
2160 5248 P 0 12 F 0 10 F (label_list) S 120 J ( : label_list ',' label) S
2160 5468 P ( | label) S
2160 5688 P ( | error) S
2160 5908 P ( | error [LABEL CONST VAR PROC FUNC BEGIN]) S
2160 6128 P ( {YYERROR; /* more code */};) S
2160 6568 P 0 12 F 8 9 F (This) S 50 J ( deals with errors in two different ways:) S
2160 7008 P (\(a\) If an error is followed by one of the tokens ) S 0 12 F 0 9 F (LABEL) S 0 12 F 8 9 F (,) S 50 J ( ) S 0 12 F 0 9 F (CONST) S 0 12 F 8 9 F (,) S 50 J ( etc. \(representing the) S
2880 7248 P (beginning of new declaration sections in Pascal\), the input is reduced to a) S
2880 7488 P (complete ) S 0 12 F 0 9 F ("label_list") S 0 12 F 8 9 F () S 50 J ( input is Reduced to a complete ) S 0 12 F 0 9 F ("label_list") S 0 12 F 8 9 F () S 50 J ( and) S
2880 7728 P (an appropriate action is taken. This action uses ) S 0 12 F 0 9 F (YYERROR) S 0 12 F 8 9 F () S 50 J ( to raise the error) S
2880 7968 P (condition, but only ) S 0 12 F 8 9 F I (after) S E 0 12 F 8 9 F () S 50 J ( the reduction has taken place.) S
2160 8408 P (\(b\) The other rule is used when the parser finds an error which is not followed by one) S
2880 8648 P (of the listed tokens. This corresponds to an error in the middle of a label list and) S
2880 8888 P (requires a different sort of handling. In this case, error-handling is allowed to) S
2880 9128 P (take place immediately, without reduction, because there may be more) S
2880 9368 P 0 12 F 0 9 F ("label_list") S 0 12 F 8 9 F () S 50 J ( to come.) S
2160 9808 P (This kind of approach can be used to distinguish different kinds of errors that may take) S
2160 10048 P (place in a particular situation.) S
2160 10868 P 0 12 F 8 12 F B (9.10) S 67 J ( The Default Action) S E
2160 11588 P 0 12 F 8 9 F (The) S 50 J ( default action is the one that is taken when the parser finds a token which has no) S
2160 11828 P (specified effect in the current state. Understanding how default actions work will help you) S
2160 12068 P (understand what is going on when a YAY-produced parser encounters an error.) S
2160 12508 P (In a state diagram, the default action is marked with a ) S 0 12 F 0 9 F (".") S 0 12 F 8 9 F (.) S 50 J ( The default will always be a) S
2160 12748 P (Reduce or Error action, chosen according to the following rules.) S
2160 14520 P 0 12 F 57 12 F (DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD) S
2160 14800 P 0 12 F 24 10 F (Thinkage) S 50 J ( YAY Reference Manual) S 4238 J ( 66) S 0 12 F
PE
2880 1968 P 8 9 F (\(a\)) S 500 J ( If) S 50 J ( the state has no Shift actions and only one Reduce, the default will be the) S
3600 2208 P (Reduce action.) S
2880 2648 P (\(b\) Apart from \(a\), an empty rule will never have Reduce as a default.) S
2880 3088 P (\(c\)) S 510 J ( If) S 50 J ( a state has more than one Reduce action, the parser examines the "popularity") S
3600 3328 P (of each Reduce. For example, if Reduction A is used with any of three different) S
3600 3568 P (input tokens and Reduction B is used with only one input token, Reduction A is) S
3600 3808 P (three times as "popular" as B. If one Reduce action is more than twice as) S
3600 4048 P (popular as its closest contender \(i.e. if it is taken on more than twice as many) S
3600 4288 P (input tokens\), and if that Reduce action is associated with a rule that contains) S
3600 4528 P (reductions on at least ) S 0 12 F 8 9 F I (five) S E 0 12 F 8 9 F () S 50 J ( tokens, the popular Reduce action is made the default.) S
2880 4968 P (\(d\) In all other cases, the default action will be an Error action. For example, Error is) S
3600 5208 P (chosen when a rule has more than one Reduce action, and there is no Reduce) S
3600 5448 P (that is more than twice as popular as all the other contenders.) S
2880 5888 P (Note: YAY's predecessor YACC always chooses the most popular Reduce action as) S
2880 6128 P (default \(if there is one\). It does not use the same requirements as \(c\) above. As a result) S
2880 6368 P (of this difference YAY's parser tables are about 20% larger than YACC's, but a YAY-) S
2880 6608 P (generated parser usually detects errors much earlier than a parser generated by YACC.) S
2880 7048 P (Because of the way default actions are treated, a YAY-produced parser will sometimes) S
2880 7288 P (begin reducing when it encounters an error. Several reductions may take place before the) S
2880 7528 P (error state is finally triggered. Thus your grammar may need some way to determine what) S
2880 7768 P (actions have taken place between the time the erroneous token was read in and the time) S
2880 8008 P (that the error was actually triggered.) S
2880 8828 P 0 12 F 8 12 F B (9.11) S 67 J ( Invalid Tokens) S E
2880 9548 P 0 12 F 8 9 F (It) S 50 J ( is invalid to say either) S
3600 9968 P 0 12 F 0 9 F (%token) S 108 J ( X 0) S
3600 10188 P ( or) S
3600 10408 P (%token X 256) S
2880 10848 P 0 12 F 8 9 F (The) S 50 J ( value 0 is reserved for the end marker and 256 is reserved for ) S 0 12 F 0 9 F ("error") S 0 12 F 8 9 F (.) S
2880 11668 P 0 12 F 8 12 F B (9.12) S 67 J ( Dynamic Stack Allocation) S E
2880 12388 P 0 12 F 8 9 F (The) S 50 J ( manifest ) S 0 12 F 0 9 F (YYSSIZE) S 0 12 F 8 9 F () S 50 J ( is used to determine the size of the state and value stacks used by) S
2880 12628 P 0 12 F 0 9 F ("yyparse") S 0 12 F 8 9 F (.) S 50 J ( ) S 0 12 F 0 9 F (YYSSIZE) S 0 12 F 8 9 F () S 50 J ( gives the maximum number of elements that these stacks will be) S
2880 12868 P (expected to hold; the size of each value element is dictated by the ) S 0 12 F 0 9 F (YYSTYPE) S 0 12 F 8 9 F () S 50 J ( type and the) S
2880 13108 P (size of each state element is determined by YAY. The default value of ) S 0 12 F 0 9 F (YYSSIZE) S 0 12 F 8 9 F () S 50 J ( is 150.) S
2880 14520 P 0 12 F 57 12 F (DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD) S
2880 14800 P 0 12 F 24 10 F (67) S 4238 J ( Thinkage) S 50 J ( YAY Reference Manual) S 0 12 F
PE
2160 1968 P 8 9 F (By) S 50 J ( increasing the value of ) S 0 12 F 0 9 F (YYSSIZE) S 0 12 F 8 9 F (,) S 50 J ( you can allow for grammars with a larger number of) S
2160 2208 P (pending states.) S
2160 2648 P (If you ) S 0 12 F 0 9 F B (#define) S E 0 12 F 8 9 F () S 50 J ( ) S 0 12 F 0 9 F (YYALLOC) S 0 12 F 8 9 F () S 50 J ( in the declarations section, the state and value stacks used by) S
2160 2888 P 0 12 F 0 9 F ("yyparse") S 0 12 F 8 9 F () S 50 J ( will be allocated dynamically via ) S 0 12 F 0 9 F ("malloc") S 0 12 F 8 9 F () S 50 J ( and freed before ) S 0 12 F 0 9 F ("yyparse") S
2160 3128 P 0 12 F 8 9 F (returns.) S 50 J ( What this effectively means is that ) S 0 12 F 0 9 F ("yyparse") S 0 12 F 8 9 F () S 50 J ( makes itself re-entrant by saving) S
2160 3368 P (a number of externals when it begins execution and restoring them upon completion. The) S
2160 3608 P (externals involved are) S
2880 4028 P 0 12 F 0 9 F (yylval) S 108 J ( yyval yypvt) S
2880 4248 P (yynerrs yychar yyerrflag) S
2160 4688 P 0 12 F 8 9 F (If) S 50 J ( you ) S 0 12 F 0 9 F ("longjmp") S 0 12 F 8 9 F () S 50 J ( out of ) S 0 12 F 0 9 F ("yyparse") S 0 12 F 8 9 F () S 50 J ( \(due to an action\), the externals are ) S 0 12 F 8 9 F I (not) S E 0 12 F 8 9 F () S 50 J ( restored,) S
2160 4928 P (and ) S 0 12 F 0 9 F ("yyparse") S 0 12 F 8 9 F () S 50 J ( will not be re-entrant.) S
2160 5368 P (If you use ) S 0 12 F 0 9 F (YYALLOC) S 0 12 F 8 9 F (,) S 50 J ( ) S 0 12 F 0 9 F ("yyerror") S 0 12 F 8 9 F () S 50 J ( will be called if ) S 0 12 F 0 9 F ("malloc") S 0 12 F 8 9 F () S 50 J ( fails to allocate space for the) S
2160 5608 P (state and value stacks.) S
2160 6048 P (If you ) S 0 12 F 0 9 F B (#define) S E 0 12 F 8 9 F () S 50 J ( ) S 0 12 F 0 9 F (YYALLOC) S 0 12 F 8 9 F () S 50 J ( with a value greater than 10, the parser allocates the state and) S
2160 6288 P (value stacks dynamically, beginning with a size of ) S 0 12 F 0 9 F (YYSSIZE) S 0 12 F 8 9 F (.) S 50 J ( If this is not big enough to) S
2160 6528 P (hold the two stacks, ) S 0 12 F 0 9 F ("yyparse") S 0 12 F 8 9 F () S 50 J ( attempts to use ) S 0 12 F 0 9 F ("realloc") S 0 12 F 8 9 F () S 50 J ( to grow the stacks by the) S
2160 6768 P (amount given by ) S 0 12 F 0 9 F (YYALLOC) S 0 12 F 8 9 F (.) S 50 J ( For example, if ) S 0 12 F 0 9 F (YYALLOC) S 0 12 F 8 9 F () S 50 J ( is 20, ) S 0 12 F 0 9 F ("yyparse") S 0 12 F 8 9 F () S 50 J ( tries to grow the) S
2160 7008 P (stacks to a size that will allow 20 additional elements \(whenever ) S 0 12 F 0 9 F ("yyparse") S 0 12 F 8 9 F () S 50 J ( needs more) S
2160 7248 P (space for the stacks\). ) S 0 12 F 0 9 F ("yyerror") S 0 12 F 8 9 F () S 50 J ( is called if the call to ) S 0 12 F 0 9 F ("realloc") S 0 12 F 8 9 F () S 50 J ( fails to allocate) S
2160 7488 P (additional space.) S
2160 7928 P (If you set up your parser in such a way that it may grow the stacks, you must be careful) S
2160 8168 P (not to take a pointer into a stack in one action and use that pointer inside a different) S
2160 8408 P (action. The reason is that the stack may be grown using ) S 0 12 F 0 9 F ("realloc") S 0 12 F 8 9 F () S 50 J ( in between the two) S
2160 8648 P (actions. Since ) S 0 12 F 0 9 F ("realloc") S 0 12 F 8 9 F () S 50 J ( may actually move the entire stack, the pointer will no longer) S
2160 8888 P (be valid. Thus you should not create pointers with expressions like) S
2880 9308 P 0 12 F 0 9 F (&\($1\)) S
2160 9748 P 0 12 F 8 9 F (and) S 50 J ( expect those pointers to be valid in other actions. Within the code of a single action,) S
2160 9988 P (however, such pointers ) S 0 12 F 0 9 F B (will) S E 0 12 F 8 9 F () S 50 J ( remain valid.) S
2160 10428 P (If you ) S 0 12 F 0 9 F B (#define) S E 0 12 F 8 9 F () S 50 J ( ) S 0 12 F 0 9 F (YYSTATIC) S 0 12 F 8 9 F (,) S 50 J ( both the state and value stacks will be static. Otherwise, the) S
2160 10668 P (state stack will be "auto" \(allocated on the program stack\) and the value stack will be) S
2160 10908 P (static. Defining ) S 0 12 F 0 9 F (YYALLOC) S 0 12 F 8 9 F () S 50 J ( saves both stack space and static space; defining ) S 0 12 F 0 9 F (YYSTATIC) S
2160 11148 P 0 12 F 8 9 F (saves) S 50 J ( stack space.) S
2160 11968 P 0 12 F 8 12 F B (9.13) S 67 J ( Synchronizing the Lookahead) S E
2160 12688 P 0 12 F 8 9 F (If) S 50 J ( you ) S 0 12 F 0 9 F B (#define) S E 0 12 F 8 9 F () S 50 J ( ) S 0 12 F 0 9 F (YYSYNC) S 0 12 F 8 9 F (,) S 50 J ( the parser will always have a lookahead token when it performs) S
2160 12928 P (a shift or reduce action. If the symbol is not defined, the parser will only obtain a) S
2160 13168 P (lookahead token if the value of the token is needed.) S
2160 14520 P 0 12 F 57 12 F (DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD) S
2160 14800 P 0 12 F 24 10 F (Thinkage) S 50 J ( YAY Reference Manual) S 4238 J ( 68) S 0 12 F
PE
2880 2168 P 8 9 F (You) S 50 J ( would define ) S 0 12 F 0 9 F (YYSYNC) S 0 12 F 8 9 F () S 50 J ( in situations where you wanted to keep track of the line number) S
2880 2408 P (on which various situations occurred \(e.g. errors\). If ) S 0 12 F 0 9 F ("yylex") S 0 12 F 8 9 F () S 50 J ( ) S 0 12 F 8 9 F I (always) S E 0 12 F 8 9 F () S 50 J ( does a lookahead,) S
2880 2648 P (you know that the line number of the token you are working with is the line number of the) S
2880 2888 P (second last token read. If ) S 0 12 F 0 9 F ("yylex") S 0 12 F 8 9 F () S 50 J ( sometimes does not do a lookahead, you don't know) S
2880 3128 P (if the current line number in ) S 0 12 F 0 9 F ("yylex") S 0 12 F 8 9 F () S 50 J ( is the line number for the current token or for a) S
2880 3368 P (lookahead token.) S
2880 3808 P (You would avoid defining ) S 0 12 F 0 9 F (YYSYNC) S 0 12 F 8 9 F () S 50 J ( in situations where some actions do their own reading.) S
2880 4048 P (For example, suppose that ) S 0 12 F 0 9 F (/*) S 0 12 F 8 9 F () S 50 J ( is a token that indicates the beginning of a comment. You) S
2880 4288 P (could create an action for that token which reads all input up to a closing ) S 0 12 F 0 9 F (*/) S 0 12 F 8 9 F (.) S 50 J ( With this) S
2880 4528 P (kind of action, you would not want ) S 0 12 F 0 9 F ("yylex") S 0 12 F 8 9 F () S 50 J ( to read a lookahead token, since that token) S
2880 4768 P (would be the first token inside the comment, not the first token after the comment.) S
2880 5588 P 0 12 F 8 12 F B (9.14) S 67 J ( YYSHIFT) S E
2880 6308 P 0 12 F 8 9 F (The) S 50 J ( generated parser program invokes a macro named ) S 0 12 F 0 9 F (YYSHIFT) S 0 12 F 8 9 F () S 50 J ( whenever the parser) S
2880 6548 P (performs a shift action in response to a token. The macro is invoked without arguments.) S
2880 6788 P (By default, ) S 0 12 F 0 9 F (YYSHIFT) S 0 12 F 8 9 F () S 50 J ( is defined to do nothing; however, users can redefine ) S 0 12 F 0 9 F (YYSHIFT) S 0 12 F 8 9 F () S 50 J ( if) S
2880 7028 P (desired, in the token definition part of the YAY input.) S
2880 7468 P (As an example of how you might use ) S 0 12 F 0 9 F (YYSHIFT) S 0 12 F 8 9 F (,) S 50 J ( consider a situation where input is) S
2880 7708 P (freeform and may be split over many lines of text, including the insertion of blank lines in) S
2880 7948 P (the input. You may want to keep a count of the number of lines you've read as you parse) S
2880 8188 P (the input so that you can provide error messages like "Line 12: Syntax error".) S
2880 8628 P (When the parser finishes reading a line, it often has to read ahead to the next token before) S
2880 8868 P (it can decide whether to reduce what it already has or keep on reading additional input.) S
2880 9108 P (Since there may be blank lines in the input, this means that the parser may actually read) S
2880 9348 P (ahead several lines before finding the next token. When it sees the next token, the parser) S
2880 9588 P (may decide to reduce what it has seen already before shifting in response to the token.) S
2880 10028 P (While the reduction is happening, you want your line count to reflect the last line of input.) S
2880 10268 P (You do not want the line count to reflect the line where the parser found the new token,) S
2880 10508 P (because the parser isn't really using that token yet; it's working with older input. You only) S
2880 10748 P (want to update the line count when the parser is ready to shift on the new token.) S
2880 11188 P (That's where ) S 0 12 F 0 9 F (YYSHIFT) S 0 12 F 8 9 F () S 50 J ( comes in. You can define the ) S 0 12 F 0 9 F (YYSHIFT) S 0 12 F 8 9 F () S 50 J ( macro to update the line) S
2880 11428 P (count at the point when the token is actually used, not when it is first read.) S
2880 11868 P 0 12 F 0 9 F (YYSHIFT) S 0 12 F 8 9 F () S 50 J ( is ) S 0 12 F 8 9 F I (not) S E 0 12 F 8 9 F () S 50 J ( invoked if a token is discarded \(because of error handling or some other) S
2880 12108 P (reason\). It is only invoked when the parser shifts on a token.) S
2880 14520 P 0 12 F 57 12 F (DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD) S
2880 14800 P 0 12 F 24 10 F (69) S 4238 J ( Thinkage) S 50 J ( YAY Reference Manual) S 0 12 F
PE
7520 1968 P 8 18 F (Appendix) S 100 J ( A) S
7300 2448 P 0 12 F 8 18 F B (An) S 100 J ( Example) S E
2160 4048 P 0 12 F 8 9 F (This) S 50 J ( appendix gives a simple example of YAY input. We have omitted the recognition) S
2160 4288 P (actions of the grammar rules in order to keep the example as simple as possible.) S
2160 4728 P (The parser implements a simple string manipulation language. It has two types of objects:) S
2160 4968 P (strings and variables. Strings can have a maximum of 100 characters and are enclosed in) S
2160 5208 P (double quotes. Variable names are a maximum of eight characters long.) S
2160 5648 P (There are three operations:) S
2160 6088 P (\(a\) ) S 0 12 F 0 9 F (=) S 0 12 F 8 9 F () S 50 J ( assigns a string to a variable;) S
2160 6528 P (\(b\) ) S 0 12 F 0 9 F (JOIN) S 0 12 F 8 9 F () S 50 J ( or ) S 0 12 F 0 9 F (+) S 0 12 F 8 9 F () S 50 J ( concatenates strings or the contents of variables;) S
2160 6968 P (\(c\)) S 510 J ( ) S 0 12 F 0 9 F (PRINT) S 0 12 F 8 9 F () S 50 J ( outputs strings or the contents of variables.) S
2160 7408 P (Every statement is a single input line and blanks are used to separate tokens on the line.) S
2160 7648 P (Multiple ) S 0 12 F 0 9 F (=) S 0 12 F 8 9 F () S 50 J ( assignments are permitted as in) S
2880 8068 P 0 12 F 0 9 F (A) S 108 J ( = B = "hello") S
2160 8508 P 0 12 F 8 9 F (The) S 50 J ( keyword ) S 0 12 F 0 9 F (PRINT) S 0 12 F 8 9 F () S 50 J ( can only appear once on the line.) S
2160 8948 P (If a line does not contain an assignment, the value of the expression on the line is merely) S
2160 9188 P (printed out. This means that it is valid to have a line that only contains a string or a) S
2160 9428 P (variable name. The end of the input file marks the end of input.) S
2160 9868 P (Here are the declarations section of the YAY input and the ) S 0 12 F 0 9 F ("yylex") S 0 12 F 8 9 F () S 50 J ( routine \(both written) S
2160 10108 P (in C\).) S
2160 10528 P 0 12 F 0 10 F (%{) S 120 J ( /* declarations */) S
2160 10748 P (#include <stdio.h>) S
2160 10968 P (#define MAXSTRING 100) S
2160 11188 P (char str[MAXSTRING];) S
2160 11408 P (#define MAXVAR 8) S
2160 11628 P (char var[MAXVAR];) S
2160 11848 P (%}) S
2160 12268 P (%token VARIABLE STRING) S
2160 12488 P (%nonassoc PRINT) S
2160 12708 P (%right '=') S
2160 12928 P (%left JOIN) S
2160 14520 P 0 12 F 57 12 F (DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD) S
2160 14800 P 0 12 F 24 10 F (Thinkage) S 50 J ( YAY Reference Manual) S 4238 J ( 70) S 0 12 F
PE
2880 1948 P 0 10 F (%union) S 120 J ( {) S
2880 2168 P ( char *cstr;) S
2880 2388 P (}) S
2880 2608 P (%%) S
2880 3028 P (program : stat) S
2880 3248 P ( | program stat) S
2880 3468 P ( ;) S
2880 3688 P (stat : printexpr '\\n') S
2880 3908 P ( | expr '\\n') S
2880 4128 P ( ;) S
2880 4348 P (printexpr : PRINT expr ;) S
2880 4568 P (expr : VARIABLE '=' expr) S
2880 4788 P ( | JOIN expr expr) S
2880 5008 P ( | STRING) S
2880 5228 P ( | VARIABLE) S
2880 5448 P ( ;) S
2880 5668 P (%%) S
2880 6088 P (int yylex\(\)) S
2880 6308 P ({) S
2880 6528 P ( char c, *stringet\(\), *wordget\(\);) S
2880 6748 P ( extern YYSTYPE yylval;) S
2880 6968 P ( while \( \(c=getchar\(\)\) == ' ' \)) S
2880 7188 P ( /* skip blanks */;) S
2880 7408 P ( switch\(c\) {) S
2880 7628 P ( case '\\n': /* end of line */) S
2880 7848 P ( return\('\\n'\);) S
2880 8068 P ( case EOF: /* end of file */) S
2880 8288 P ( return\(0\);) S
2880 8508 P ( case '+': /* same as JOIN */) S
2880 8728 P ( return\(JOIN\);) S
2880 8948 P ( case '"': /* start of string */) S
2880 9168 P ( yylval.cstr = stringet\(\);) S
2880 9388 P ( return\(STRING\);) S
2880 9608 P ( default: /* keyword or variable */) S
2880 9828 P ( yylval.cstr = wordget\(c\);) S
2880 10048 P ( if \( !strcmp\("JOIN",yylval.cstr\) \)) S
2880 10268 P ( return\(JOIN\);) S
2880 10488 P ( else if \( !strcmp\("PRINT",yylval.cstr\) \)) S
2880 10708 P ( return\(PRINT\);) S
2880 10928 P ( else return\(VARIABLE\);) S
2880 11148 P ( }) S
2880 11368 P (}) S
2880 11788 P (char *stringet\(\)) S
2880 12008 P ({) S
2880 12228 P ( extern char str[];) S
2880 12448 P ( int i;) S
2880 14520 P 0 12 F 57 12 F (DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD) S
2880 14800 P 0 12 F 24 10 F (71) S 4238 J ( Thinkage) S 50 J ( YAY Reference Manual) S 0 12 F
PE
2160 1948 P 0 10 F () S 120 J ( for \( i=0; i < MAXSTRING; i++ \) {) S
2160 2168 P ( str[i] = getchar\(\);) S
2160 2388 P ( if \( \(str[i]=='"'\) || \(str[i]=='\\n'\) \)) S
2160 2608 P ( break;) S
2160 2828 P ( }) S
2160 3048 P ( str[i] = '\\0'; /* mark end of string */) S
2160 3268 P ( return\(str\);) S
2160 3488 P (}) S
2160 3908 P (char *wordget\(char c\)) S
2160 4128 P ({) S
2160 4348 P ( extern char var[];) S
2160 4568 P ( int i;) S
2160 4788 P ( var[0] = c; /* first letter obtained already */) S
2160 5008 P ( for \( i=1; i < MAXVAR; i++ \) {) S
2160 5228 P ( var[i] = getchar\(\);) S
2160 5448 P ( if \( \(var[i]==' '\) || \(var[i]=='\\n'\) \)) S
2160 5668 P ( break;) S
2160 5888 P ( }) S
2160 6108 P ( var[i] = '\\0';) S
2160 6328 P ( return\(var\);) S
2160 6548 P (}) S
2160 6968 P (void yyerror\(char *s\)) S
2160 7188 P ({) S
2160 7408 P ( fprintf\(stderr,s\);) S
2160 7628 P (}) S
2160 8068 P 0 12 F 8 9 F (Note) S 50 J ( that the lexical analyzer always flags the keywords ) S 0 12 F 0 9 F (JOIN) S 0 12 F 8 9 F () S 50 J ( and ) S 0 12 F 0 9 F (PRINT) S 0 12 F 8 9 F () S 50 J ( as operations.) S
2160 8308 P (This means that you cannot use ) S 0 12 F 0 9 F (JOIN) S 0 12 F 8 9 F () S 50 J ( or ) S 0 12 F 0 9 F (PRINT) S 0 12 F 8 9 F () S 50 J ( as variable names. In fact, this is a) S
2160 8548 P (general limitation of LALR\(1\) parsers. It is very difficult to have names that are keywords) S
2160 8788 P (in some contexts and variable names in others, without a great deal of fiddling. Therefore,) S
2160 9028 P (we recommend that you resign yourself to having ) S 0 12 F 8 9 F I (reserved) S E 0 12 F 8 9 F () S 50 J ( keywords, i.e. keywords) S
2160 9268 P (forbidden for use as variable names.) S
2160 9708 P (The above example omitted recognition actions in the Rules Section to make things easier) S
2160 9948 P (to read.) S
2160 14520 P 0 12 F 57 12 F (DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD) S
2160 14800 P 0 12 F 24 10 F (Thinkage) S 50 J ( YAY Reference Manual) S 4238 J ( 72) S 0 12 F
PE
8240 1968 P 8 18 F (Appendix) S 100 J ( B) S
7620 2448 P 0 12 F 8 18 F B (YAY) S 100 J ( vs. YACC) S E
2880 4048 P 0 12 F 8 9 F (YAY) S 50 J ( differs from its predecessor YACC in a number of respects. These are summarized) S
2880 4288 P (below.) S
2880 4728 P (\(a\) YACC does not support the following.) S
3600 5168 P (-- all the LALR\(2\) features described in 9.1) S
3600 5408 P (-- selection preferences, as described in 9.3) S
3600 5648 P (-- the use of ) S 0 12 F 0 9 F (YYSTATIC) S 0 12 F 8 9 F (,) S 50 J ( ) S 0 12 F 0 9 F (YYSYNC) S 0 12 F 8 9 F (,) S 50 J ( or ) S 0 12 F 0 9 F (YYALLOC) S 0 12 F 8 9 F () S 50 J ( \(9.12\)) S
2880 6088 P (\(b\) If a state has several Reduce action, YACC always chooses the most popular of) S
3600 6328 P (these as the default action. YAY's rules for choosing a default are given in 9.10.) S
3600 6568 P (In general, YAY's approach will detect errors more quickly \(sooner after they) S
3600 6808 P (actually occur\).) S
2880 7248 P (\(c\)) S 510 J ( The) S 50 J ( two programs handle ) S 0 12 F 0 9 F (YYERROR) S 0 12 F 8 9 F () S 50 J ( differently. YAY always reduces and pops) S
3600 7488 P (the current state off the stack before generating the artificial error condition.) S
3600 7728 P (YACC doesn't do this. With YACC, you cannot use ) S 0 12 F 0 9 F (YYERROR) S 0 12 F 8 9 F () S 50 J ( inside a rule with) S
3600 7968 P ("error" in it; YAY, however, will resolve the current rule and then trigger error) S
3600 8208 P (handling.) S
2880 8648 P (\(d\) Output from the two programs differs slightly.) S
2880 9088 P (As a rule of thumb, YAY accepts any YACC grammar and creates a parser that behaves) S
2880 9328 P (the same for correct input. However, the YAY version usually detects errors earlier than) S
2880 9568 P (the YACC parser, and has more Error actions in the state tables.) S
2880 14520 P 0 12 F 57 12 F (DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD) S
2880 14800 P 0 12 F 24 10 F (73) S 4238 J ( Thinkage) S 50 J ( YAY Reference Manual) S 0 12 F
PE
7500 1968 P 8 18 F (Appendix) S 100 J ( C) S
5120 2448 P 0 12 F 8 18 F B (The) S 100 J ( YAY Command Line) S E
2160 4008 P 0 12 F 8 10 F B (Syntax:) S E
2880 4228 P 0 12 F 0 9 F (yay) S 108 J ( sourcefile Parser=outfile [option]*) S
2880 4648 P (\(+|-\)LR2 \(-\) \(+|-\)Verbose \(-\)) S
2880 4868 P (\(+|-\)Warnings \(+\) Description=file) S
2880 5088 P (Header=file INSTallation=file) S
2880 5308 P (Language=C|C++ \(C\) Parser=file) S
2160 5708 P 0 12 F 8 10 F B (Examples:) S E
2880 5928 P 0 12 F 0 9 F (yay) S 108 J ( cgram.y parse=myparse.c) S
2880 6148 P (c myparse.c) S
2880 6568 P (yay ccgram.y lang=c++ pars=myparse.cpp) S
2160 6968 P 0 12 F 8 10 F B (Options:) S E
2160 7208 P 0 12 F 0 9 F (sourcefile) S
2880 7448 P 0 12 F 8 9 F (is) S 50 J ( a file containing YAY input.) S
2160 7888 P 0 12 F 0 9 F (Language=C) S
2880 8128 P 0 12 F 8 9 F (produces) S 50 J ( parsing tables in the C programming language. This is the default.) S
2160 8568 P 0 12 F 0 9 F (Language=C++) S
2880 8808 P 0 12 F 8 9 F (produces) S 50 J ( parsing tables in the C++ programming language. Note that YAY only) S
2880 9048 P (produces the tables; the routines that use the tables to parse input are) S
2880 9288 P (predefined.) S
2160 9728 P 0 12 F 0 9 F (+LR2) S
2880 9968 P 0 12 F 8 9 F (says) S 50 J ( that the YAY input describes an LALR\(2\) grammar. Without ) S 0 12 F 0 9 F (+LR2) S 0 12 F 8 9 F (,) S 50 J ( YAY) S
2880 10208 P (assumes that the grammar is LALR\(1\). The manual describes modifications that) S
2880 10448 P (need to be made for LALR\(2\) grammars.) S
2160 10888 P 0 12 F 0 9 F (Description=file) S
2880 11128 P 0 12 F 8 9 F (translates) S 50 J ( the parsing tables into a format that humans can read, and writes this) S
2880 11368 P (output into the given file.) S
2160 11808 P 0 12 F 0 9 F (Header=file) S
2880 12048 P 0 12 F 8 9 F (writes) S 50 J ( token definitions and other information necessary for separate compilation,) S
2880 12288 P (to the named file.) S
2160 14520 P 0 12 F 57 12 F (DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD) S
2160 14800 P 0 12 F 24 10 F (Thinkage) S 50 J ( YAY Reference Manual) S 4238 J ( 74) S 0 12 F
PE
2880 1968 P 0 9 F (INSTallation=file) S
3600 2208 P 0 12 F 8 9 F (tells) S 50 J ( YAY where to find the installation file. The installation file tells where various) S
3600 2448 P (software components have been installed. For more information, see the section) S
3600 2688 P (on ) S 0 12 F 8 9 F I (Installation) S 50 J ( Files) S E 0 12 F 8 9 F () S 50 J ( below.) S
3600 3128 P (If you do not specify an ) S 0 12 F 0 9 F (INSTallation=) S 0 12 F 8 9 F () S 50 J ( option on the command line, YAY) S
3600 3368 P (checks for an environment variable named ) S 0 12 F 0 9 F (YAY_INST) S 0 12 F 8 9 F () S 50 J ( and uses its value as the) S
3600 3608 P (name of the installation file. If this environment variable does not exist, YAY uses) S
3600 3848 P (the default installation file.) S
2880 4288 P 0 12 F 0 9 F (Parser=file) S
3600 4528 P 0 12 F 8 9 F (writes) S 50 J ( the resulting source code for the parser into the named file. If this option is) S
3600 4768 P (omitted, YAY just checks the syntax of your input.) S
2880 5208 P 0 12 F 0 9 F (+Verbose) S
3600 5448 P 0 12 F 8 9 F (produces) S 50 J ( verbose output -- everything that can be flagged is flagged.) S
2880 5888 P 0 12 F 0 9 F (-Warnings) S
3600 6128 P 0 12 F 8 9 F (suppresses) S 50 J ( a number of warning messages that YAY normally issues.) S
2880 6528 P 0 12 F 8 10 F B (Description:) S E
2880 6768 P 0 12 F 8 9 F (YAY) S 50 J ( converts your context-free grammar into a C or C++ program that is written to the file) S
2880 7008 P (specified by the ) S 0 12 F 0 9 F (Parser=) S 0 12 F 8 9 F () S 50 J ( option.) S
2880 7448 P (If you use the ) S 0 12 F 0 9 F (Description=) S 0 12 F 8 9 F () S 50 J ( option, YAY writes a full description of the grammar to the) S
2880 7688 P (specified file. YAY only displays a brief message on the standard output, summarizing) S
2880 7928 P (conflicts \(and other information if you specify ) S 0 12 F 0 9 F (+Verbose) S 0 12 F 8 9 F (\).) S 50 J ( On the other hand, if you do) S
2880 8168 P (not use the ) S 0 12 F 0 9 F (Description=) S 0 12 F 8 9 F () S 50 J ( option, YAY writes more information to standard output,) S
2880 8408 P (including descriptions of the states where conflicts occur. In this case, YAY actually) S
2880 8648 P (provides additional information to help you identify the source of the conflicts; if you ask for) S
2880 8888 P (a description file, YAY outputs less information when reporting the conflicts because it) S
2880 9128 P (assumes you can track down additional information by looking at the description file. For) S
2880 9368 P (this reason, you can sometimes get a quicker idea of what has gone wrong if you do not) S
2880 9608 P (ask for a description file.) S
2880 10008 P 0 12 F 8 10 F B (C++) S 56 J ( Parsers) S E
2880 10248 P 0 12 F 8 9 F (In) S 50 J ( general, you only need to use ) S 0 12 F 0 9 F (Language=C++) S 0 12 F 8 9 F () S 50 J ( if you intend ) S 0 12 F 0 9 F (YYSTYPE) S 0 12 F 8 9 F () S 50 J ( to contain a C++) S
2880 10488 P (object with constructors. If you intend to compile the parser with C++ but the ) S 0 12 F 0 9 F (%union) S
2880 10728 P 0 12 F 8 9 F (statement) S 50 J ( does not have any elements that need constructors, it's best to use) S
2880 10968 P 0 12 F 0 9 F (Language=C) S 0 12 F 8 9 F () S 50 J ( to get more efficient C code.) S
2880 11408 P (If ) S 0 12 F 0 9 F (YYSTYPE) S 0 12 F 8 9 F () S 50 J ( does contain elements that need constructors, you need to define an) S
2880 11648 P (appropriate constructor-like function for the ) S 0 12 F 0 9 F (YYSTYPE) S 0 12 F 8 9 F () S 50 J ( type. This function should have the) S
2880 11888 P (prototype) S
3600 12308 P 0 12 F 0 9 F (void) S 108 J ( name\(YYSTYPE *p\)) S
2880 12748 P 0 12 F 8 9 F (where) S 50 J ( ) S 0 12 F 0 9 F ("name") S 0 12 F 8 9 F () S 50 J ( can be any valid name. In the declarations section of the grammar, you) S
2880 12988 P (must then add the statement) S
2880 14520 P 0 12 F 57 12 F (DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD) S
2880 14800 P 0 12 F 24 10 F (75) S 4238 J ( Thinkage) S 50 J ( YAY Reference Manual) S 0 12 F
PE
2880 1948 P 0 9 F (#define) S 108 J ( YYSTYPE_INIT name) S
2160 2388 P 0 12 F 8 9 F (where) S 50 J ( ) S 0 12 F 0 9 F ("name") S 0 12 F 8 9 F () S 50 J ( is the name of the constructor-like function.) S
2160 2828 P (With ) S 0 12 F 0 9 F (Language=C++) S 0 12 F 8 9 F (,) S 50 J ( the ) S 0 12 F 0 9 F (%union) S 0 12 F 8 9 F () S 50 J ( statement generates a structure type rather than a) S
2160 3068 P (union, since C++ does not allow objects with constructors to belong to unions.) S
2160 3508 P (In many cases, the same grammar may be processed with either ) S 0 12 F 0 9 F (Language=C) S 0 12 F 8 9 F () S 50 J ( or) S
2160 3748 P 0 12 F 0 9 F (Language=C++) S 0 12 F 8 9 F (.) S
2160 4148 P 0 12 F 8 10 F B (Installation) S 56 J ( Files:) S E
2160 4388 P 0 12 F 8 9 F (An) S 50 J ( installation file specifies the pathnames for software and data files used by YAY.) S
2160 4628 P (Installation files are text files made up of comment lines and option lines.) S
2160 5088 P (Comment lines:) S
2880 5328 P (Any line whose first non-blank character is ) S 0 12 F 0 9 F (#) S 0 12 F 8 9 F () S 50 J ( will be taken as a comment. Blank) S
2880 5568 P (lines are also considered comments.) S
2160 6028 P (Option lines:) S
2880 6268 P (Option lines have the format) S
2880 6688 P 0 12 F 0 9 F (Keyword=pathname) S
2880 7128 P 0 12 F 8 9 F (In) S 50 J ( this documentation, keywords are written with some letters in upper case and) S
2880 7368 P (some in lower case. You may abbreviate keywords by omitting any or all of the) S
2880 7608 P (letters shown in lower case. The remaining letters may be entered in either) S
2880 7848 P (upper or lower case; the documentation simply uses upper case to show which) S
2880 8088 P (characters may not be omitted.) S
2160 8528 P (In this version of YAY, there is only one valid option line:) S
2880 8948 P 0 12 F 0 9 F (Library=pathname) S
2160 9388 P 0 12 F 8 9 F (The) S 50 J ( pathname should be the directory containing the YAY parser template files \(e.g.) S
2160 9628 P 0 12 F 0 9 F (yyparse.c) S 0 12 F 8 9 F (\).) S
2160 10028 P 0 12 F 8 10 F B (Notes:) S E
2160 10268 P 0 12 F 8 9 F (If) S 50 J ( you define ) S 0 12 F 0 9 F (YYALLOC) S 0 12 F 8 9 F (,) S 50 J ( the parser allocates its state and value stacks dynamically via) S
2160 10508 P (malloc and free. This shrinks your parser and helps prevent stack overflows.) S
2160 10948 P (Copyright 1995, Thinkage Ltd.) S
2160 14520 P 0 12 F 57 12 F (DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD) S
2160 14800 P 0 12 F 24 10 F (Thinkage) S 50 J ( YAY Reference Manual) S 4238 J ( 76) S 0 12 F