]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - share/doc/psd/15.yacc/ssa
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / share / doc / psd / 15.yacc / ssa
1 .\" Copyright (C) Caldera International Inc. 2001-2002.  All rights reserved.
2 .\" 
3 .\" Redistribution and use in source and binary forms, with or without
4 .\" modification, are permitted provided that the following conditions are
5 .\" met:
6 .\" 
7 .\" Redistributions of source code and documentation must retain the above
8 .\" copyright notice, this list of conditions and the following
9 .\" disclaimer.
10 .\" 
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.
14 .\" 
15 .\" All advertising materials mentioning features or use of this software
16 .\" must display the following acknowledgement:
17 .\" 
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
22 .\" permission.
23 .\" 
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.
36 .\"
37 .\"     @(#)ssa 8.1 (Berkeley) 6/8/93
38 .\"
39 .\" $FreeBSD$
40 .SH
41 Appendix A:  A Simple Example
42 .PP
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.
51 .PP
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.
60 .sp
61 .nf
62 .ta .5i 1i 1.5i 2i 2.5i
63
64 %{
65 #  include  <stdio.h>
66 #  include  <ctype.h>
67
68 int  regs[26];
69 int  base;
70
71 %}
72
73 %start  list
74
75 %token  DIGIT  LETTER
76
77 %left  \'|\'
78 %left  \'&\'
79 %left  \'+\'  \'\-\'
80 %left  \'*\'  \'/\'  \'%\'
81 %left  UMINUS      /*  supplies  precedence  for  unary  minus  */
82
83 %%      /*  beginning  of  rules  section  */
84
85 list    :       /*  empty  */
86         |       list  stat  \'\en\'
87         |       list  error  \'\en\'
88                         {       yyerrok;  }
89         ;
90
91 stat    :       expr
92                         {       printf( "%d\en", $1 );  }
93         |       LETTER  \'=\'  expr
94                         {       regs[$1]  =  $3;  }
95         ;
96
97 expr    :       \'(\'  expr  \')\'
98                         {       $$  =  $2;  }
99         |       expr  \'+\'  expr
100                         {       $$  =  $1  +  $3;  }
101         |       expr  \'\-\'  expr
102                         {       $$  =  $1  \-  $3;  }
103         |       expr  \'*\'  expr
104                         {       $$  =  $1  *  $3;  }
105         |       expr  \'/\'  expr
106                         {       $$  =  $1  /  $3;  }
107         |       expr  \'%\'  expr
108                         {       $$  =  $1  %  $3;  }
109         |       expr  \'&\'  expr
110                         {       $$  =  $1  &  $3;  }
111         |       expr  \'|\'  expr
112                         {       $$  =  $1  |  $3;  }
113         |       \'\-\'  expr        %prec  UMINUS
114                         {       $$  =  \-  $2;  }
115         |       LETTER
116                         {       $$  =  regs[$1];  }
117         |       number          
118         ;
119
120 number  :       DIGIT
121                         {       $$ = $1;    base  =  ($1==0)  ?  8  :  10;  }
122         |       number  DIGIT
123                         {       $$  =  base * $1  +  $2;  }
124         ;
125
126 %%      /*  start  of  programs  */
127
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  */
132
133         int  c;
134
135         while(  (c=getchar())  ==  \' \'  )  {  /*  skip  blanks  */  }
136
137         /*  c  is  now  nonblank  */
138
139         if(  islower(  c  )  )  {       
140                 yylval  =  c  \-  \'a\';
141                 return  (  LETTER  );
142                 }
143         if(  isdigit(  c  )  )  {       
144                 yylval  =  c  \-  \'0\';
145                 return(  DIGIT  );
146                 }
147         return(  c  );
148         }
149 .fi
150 .bp