]>
Commit | Line | Data |
---|---|---|
82c035a8 AD |
1 | # Executing Actions. -*- Autotest -*- |
2 | # Copyright 2001 Free Software Foundation, Inc. | |
3 | ||
4 | # This program is free software; you can redistribute it and/or modify | |
5 | # it under the terms of the GNU General Public License as published by | |
6 | # the Free Software Foundation; either version 2, or (at your option) | |
7 | # any later version. | |
8 | ||
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. | |
13 | ||
14 | # You should have received a copy of the GNU General Public License | |
15 | # along with this program; if not, write to the Free Software | |
16 | # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA | |
17 | # 02111-1307, USA. | |
18 | ||
19 | AT_BANNER([[User Actions.]]) | |
20 | ||
21 | ## ------------------ ## | |
22 | ## Mid-rule actions. ## | |
23 | ## ------------------ ## | |
24 | ||
25 | AT_SETUP([Mid-rule actions]) | |
26 | ||
27 | # Bison once forgot the mid-rule actions. It was because the action | |
28 | # was attached to the host rule (the one with the mid-rule action), | |
29 | # instead of being attached to the empty rule dedicated to this | |
30 | # action. | |
31 | ||
32 | AT_DATA([[input.y]], | |
33 | [[%{ | |
d99361e6 AD |
34 | # include <stdio.h> |
35 | # include <stdlib.h> | |
36 | static void yyerror (const char *msg); | |
37 | static int yylex (void); | |
38 | # define YYDEBUG 1 | |
39 | # define YYERROR_VERBOSE 1 | |
82c035a8 AD |
40 | %} |
41 | %% | |
931394cb AD |
42 | exp: { putchar ('0'); } |
43 | '1' { putchar ('1'); } | |
44 | '2' { putchar ('2'); } | |
45 | '3' { putchar ('3'); } | |
46 | '4' { putchar ('4'); } | |
47 | '5' { putchar ('5'); } | |
48 | '6' { putchar ('6'); } | |
49 | '7' { putchar ('7'); } | |
50 | '8' { putchar ('8'); } | |
51 | '9' { putchar ('9'); } | |
52 | { putchar ('\n'); } | |
82c035a8 AD |
53 | ; |
54 | %% | |
55 | static int | |
56 | yylex (void) | |
57 | { | |
58 | static const char *input = "123456789"; | |
59 | return *input++; | |
60 | } | |
61 | ||
62 | static void | |
63 | yyerror (const char *msg) | |
64 | { | |
65 | fprintf (stderr, "%s\n", msg); | |
66 | } | |
67 | ||
68 | int | |
69 | main (void) | |
70 | { | |
71 | return yyparse (); | |
72 | } | |
73 | ]]) | |
74 | ||
75 | AT_CHECK([bison input.y -d -v -o input.c]) | |
76 | AT_CHECK([$CC $CFLAGS $CPPFLAGS input.c -o input], 0, [], [ignore]) | |
30d2f3d5 | 77 | AT_CHECK([./input], 0, |
931394cb | 78 | [[0123456789 |
82c035a8 AD |
79 | ]]) |
80 | ||
81 | AT_CLEANUP | |
75d1fe16 AD |
82 | |
83 | ||
84 | ||
85 | ## ---------------- ## | |
86 | ## Exotic Dollars. ## | |
87 | ## ---------------- ## | |
88 | ||
89 | AT_SETUP([Exotic Dollars]) | |
90 | ||
91 | # Make sure complex $n work. | |
92 | ||
93 | AT_DATA([[input.y]], | |
94 | [[%{ | |
95 | # include <stdio.h> | |
96 | # include <stdlib.h> | |
97 | static void yyerror (const char *msg); | |
98 | static int yylex (void); | |
99 | # define YYDEBUG 1 | |
100 | # define YYERROR_VERBOSE 1 | |
101 | %} | |
102 | ||
103 | %union | |
104 | { | |
105 | int val; | |
106 | }; | |
107 | ||
108 | %type <val> a_1 a_2 a_4 a_5 | |
109 | sum_of_the_five_previous_values | |
110 | ||
111 | %% | |
112 | exp: a_1 a_2 { $<val>$ = 3; } a_4 a_5 sum_of_the_five_previous_values | |
113 | { | |
114 | printf ("%d\n", $6); | |
115 | } | |
116 | ; | |
117 | a_1: { $$ = 1; }; | |
118 | a_2: { $$ = 2; }; | |
119 | a_4: { $$ = 4; }; | |
120 | a_5: { $$ = 5; }; | |
121 | ||
122 | sum_of_the_five_previous_values: | |
123 | { | |
124 | $$ = $<val>0 + $<val>-1 + $<val>-2 + $<val>-3 + $<val>-4; | |
125 | } | |
126 | ; | |
127 | ||
128 | %% | |
129 | static int | |
130 | yylex (void) | |
131 | { | |
132 | return EOF; | |
133 | } | |
134 | ||
135 | static void | |
136 | yyerror (const char *msg) | |
137 | { | |
138 | fprintf (stderr, "%s\n", msg); | |
139 | } | |
140 | ||
141 | int | |
142 | main (void) | |
143 | { | |
144 | return yyparse (); | |
145 | } | |
146 | ]]) | |
147 | ||
148 | AT_CHECK([bison input.y -d -v -o input.c]) | |
149 | AT_CHECK([$CC $CFLAGS $CPPFLAGS input.c -o input], 0, [], [ignore]) | |
150 | AT_CHECK([./input], 0, | |
151 | [[15 | |
152 | ]]) | |
153 | ||
154 | AT_CLEANUP |