1 .\" Copyright (C) Caldera International Inc. 2001-2002. All rights reserved.
3 .\" Redistribution and use in source and binary forms, with or without
4 .\" modification, are permitted provided that the following conditions are
7 .\" Redistributions of source code and documentation must retain the above
8 .\" copyright notice, this list of conditions and the following
11 .\" Redistributions in binary form must reproduce the above copyright
12 .\" notice, this list of conditions and the following disclaimer in the
13 .\" documentation and/or other materials provided with the distribution.
15 .\" All advertising materials mentioning features or use of this software
16 .\" must display the following acknowledgement:
18 .\" This product includes software developed or owned by Caldera
19 .\" International, Inc. Neither the name of Caldera International, Inc.
20 .\" nor the names of other contributors may be used to endorse or promote
21 .\" products derived from this software without specific prior written
24 .\" USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
25 .\" INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
26 .\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
27 .\" WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 .\" DISCLAIMED. IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE
29 .\" FOR ANY DIRECT, INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 .\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 .\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 .\" BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
33 .\" WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
34 .\" OR OTHERWISE) RISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
35 .\" IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 .\" @(#)ssa 8.1 (Berkeley) 6/8/93
41 Appendix A: A Simple Example
43 This example gives the complete Yacc specification for a small desk calculator;
44 the desk calculator has 26 registers, labeled ``a'' through ``z'', and accepts
45 arithmetic expressions made up of the operators +, \-, *, /,
46 % (mod operator), & (bitwise and), | (bitwise or), and assignment.
47 If an expression at the top level is an assignment, the value is not
48 printed; otherwise it is.
49 As in C, an integer that begins with 0 (zero) is assumed to be octal;
50 otherwise, it is assumed to be decimal.
52 As an example of a Yacc specification, the desk calculator
53 does a reasonable job of showing how precedences and ambiguities
54 are used, and demonstrating simple error recovery.
55 The major oversimplifications are that the
56 lexical analysis phase is much simpler than for most applications, and the
57 output is produced immediately, line by line.
58 Note the way that decimal and octal integers are read in by the grammar rules;
59 This job is probably better done by the lexical analyzer.
62 .ta .5i 1i 1.5i 2i 2.5i
80 %left \'*\' \'/\' \'%\'
81 %left UMINUS /* supplies precedence for unary minus */
83 %% /* beginning of rules section */
92 { printf( "%d\en", $1 ); }
97 expr : \'(\' expr \')\'
113 | \'\-\' expr %prec UMINUS
121 { $$ = $1; base = ($1==0) ? 8 : 10; }
123 { $$ = base * $1 + $2; }
126 %% /* start of programs */
128 yylex() { /* lexical analysis routine */
129 /* returns LETTER for a lower case letter, yylval = 0 through 25 */
130 /* return DIGIT for a digit, yylval = 0 through 9 */
131 /* all other characters are returned immediately */
135 while( (c=getchar()) == \' \' ) { /* skip blanks */ }
137 /* c is now nonblank */