04b073aea38b8013106a874d513316f4e1fbd0ce
[portablexdr.git] / rpcgen_parse.y
1 /* rpcgen - Generate XDR bindings automatically.    -*- text -*-
2  * Copyright (C) 2008 Red Hat 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 of the License, or
7  * (at your option) 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., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18
19 %{
20 #include <config.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include "rpcgen_int.h"
24
25 extern void yyerror (const char *str);
26 %}
27
28 %union {
29   char *str;
30 }
31
32 %type <str> const
33
34 %token STRUCT
35 %token ENUM
36 %token CONST
37 %token TYPEDEF
38 %token UNION
39 %token SWITCH
40 %token CASE
41 %token DEFAULT
42 %token PROGRAM
43
44 %token UNSIGNED
45 %token SIGNED
46 %token CHAR
47 %token SHORT
48 %token INT
49 %token HYPER
50 %token DOUBLE
51 %token STRING
52 %token OPAQUE
53
54 /* This is sometimes lumped together with the other types, but
55  * the special keyword void can only occur after "default:" in
56  * union statements.
57  */
58 %token VOID
59
60 %token <str> IDENT
61 %token <str> INTLIT
62 %token <str> STRLIT
63
64 %%
65
66 file    : /* empty */
67         | stmts
68         ;
69
70 /* Statements. */
71 stmts   : stmt ';'
72         | stmts stmt ';'
73         ;
74
75 stmt    : ENUM IDENT '{' enum_values '}'
76         | STRUCT IDENT '{' decls '}'
77         | UNION IDENT SWITCH '(' decl ')' '{' union_cases '}'
78         | TYPEDEF decl
79         | CONST IDENT '=' const
80         | PROGRAM
81         ;
82
83 /* Declarations used inside structs and unions.  eg. "int foo;" */
84 decls   : decl ';'
85         | decls decl ';'
86         ;
87
88 decl    : simple_decl
89         | fixed_array_decl
90         | variable_array_decl
91         | pointer_decl
92         ;
93
94 simple_decl
95         : type_ident IDENT
96         ;
97
98 fixed_array_decl
99         : type_ident IDENT '[' const ']'
100         ;
101
102 variable_array_decl
103         : type_ident IDENT '<' const '>'
104         | type_ident IDENT '<' '>'
105         ;
106
107 pointer_decl
108         : type_ident '*' IDENT
109         ;
110
111 /* Enumerations. */
112 enum_values
113         : enum_value
114         | enum_values ',' enum_value
115         ;
116
117 enum_value
118         : IDENT
119         | IDENT '=' const
120         ;
121
122 /* Case list inside a union. */
123 union_cases
124         : union_case ';'
125         | union_cases union_case ';'
126         ;
127
128 union_case
129         : CASE const ':' decl
130         | DEFAULT ':' VOID
131         | DEFAULT ':' decl
132         ;
133
134 /* Constants, which may be integer literals or refer to previously
135  * defined constants (using "const" keyword).
136  * XXX In future we should probably allow computed constants.
137  */
138 const   : INTLIT
139         | IDENT
140         ;
141
142 /* Types. */
143 type_ident
144         : CHAR
145         | SIGNED CHAR
146         | UNSIGNED CHAR
147         | SHORT
148         | SIGNED SHORT
149         | UNSIGNED SHORT
150         | INT
151         | SIGNED INT
152         | UNSIGNED INT
153         | HYPER
154         | SIGNED HYPER
155         | UNSIGNED HYPER
156         | SIGNED
157         | UNSIGNED
158         | DOUBLE
159         | STRING
160         | OPAQUE
161         | IDENT
162         ;
163
164 %%
165
166 void
167 yyerror (const char *str)
168 {
169   error ("%s", str);
170 }