paul@0 | 1 | /* |
paul@0 | 2 | * Grammar for a simple interface description language. |
paul@28 | 3 | * |
paul@28 | 4 | * Copyright (C) 2019 Paul Boddie <paul@boddie.org.uk> |
paul@28 | 5 | * |
paul@28 | 6 | * This program is free software; you can redistribute it and/or |
paul@28 | 7 | * modify it under the terms of the GNU General Public License as |
paul@28 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@28 | 9 | * the License, or (at your option) any later version. |
paul@28 | 10 | * |
paul@28 | 11 | * This program is distributed in the hope that it will be useful, |
paul@28 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@28 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@28 | 14 | * GNU General Public License for more details. |
paul@28 | 15 | * |
paul@28 | 16 | * You should have received a copy of the GNU General Public License |
paul@28 | 17 | * along with this program; if not, write to the Free Software |
paul@28 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@28 | 19 | * Boston, MA 02110-1301, USA |
paul@0 | 20 | */ |
paul@0 | 21 | |
paul@0 | 22 | %{ |
paul@0 | 23 | #include <stdio.h> |
paul@7 | 24 | #include <string.h> |
paul@48 | 25 | #include "program.h" |
paul@15 | 26 | #include "parser.h" |
paul@7 | 27 | |
paul@8 | 28 | /* Standard signatures. */ |
paul@8 | 29 | |
paul@3 | 30 | int yylex(void); |
paul@3 | 31 | void yyerror(const char *); |
paul@21 | 32 | |
paul@33 | 33 | /* State information, also reset by the function defined below. */ |
paul@21 | 34 | |
paul@33 | 35 | struct interface *last_interface = NULL; |
paul@51 | 36 | struct include *last_include = NULL; |
paul@0 | 37 | %} |
paul@0 | 38 | |
paul@1 | 39 | %define parse.error verbose |
paul@1 | 40 | |
paul@3 | 41 | /* Define the identifier type employed in the scanner. */ |
paul@3 | 42 | |
paul@3 | 43 | %union { |
paul@38 | 44 | long num; |
paul@3 | 45 | char *str; |
paul@51 | 46 | struct include inc; |
paul@6 | 47 | struct interface iface; |
paul@6 | 48 | struct signature sig; |
paul@51 | 49 | struct identifier ident; |
paul@6 | 50 | struct parameter param; |
paul@25 | 51 | struct attribute attr; |
paul@3 | 52 | } |
paul@3 | 53 | |
paul@21 | 54 | /* Associate identifiers and similar values with the string union member. */ |
paul@3 | 55 | |
paul@38 | 56 | %type <str> IDENTIFIER DECIMAL HEXADECIMAL |
paul@4 | 57 | %type <str> interface_name |
paul@4 | 58 | %type <str> qualifier operation |
paul@21 | 59 | %type <str> HEADER |
paul@38 | 60 | %type <str> attr_param |
paul@4 | 61 | |
paul@30 | 62 | /* Specifiers and items have numeric values. */ |
paul@4 | 63 | |
paul@4 | 64 | %type <num> IN INOUT OUT |
paul@4 | 65 | %type <num> specifier |
paul@30 | 66 | %type <num> CAP FPAGE |
paul@30 | 67 | %type <num> item |
paul@4 | 68 | |
paul@18 | 69 | /* Associate rules with the appropriate union members. */ |
paul@4 | 70 | |
paul@21 | 71 | %type <inc> include |
paul@25 | 72 | %type <iface> q_interface |
paul@4 | 73 | %type <iface> interface |
paul@25 | 74 | %type <sig> q_signature |
paul@4 | 75 | %type <sig> signature |
paul@51 | 76 | %type <sig> signatures |
paul@51 | 77 | %type <attr> attribute |
paul@51 | 78 | %type <attr> attributes |
paul@4 | 79 | %type <param> parameter |
paul@51 | 80 | %type <param> parameters |
paul@51 | 81 | %type <ident> attr_params |
paul@51 | 82 | %type <ident> identifiers |
paul@3 | 83 | |
paul@3 | 84 | /* Scanner token types. */ |
paul@3 | 85 | |
paul@21 | 86 | %token INCLUDE HEADER |
paul@0 | 87 | %token INTERFACE |
paul@0 | 88 | %token BEGINSECTION ENDSECTION |
paul@0 | 89 | %token LPAR RPAR |
paul@25 | 90 | %token LBRACE RBRACE |
paul@0 | 91 | %token SEP TERM |
paul@0 | 92 | %token IN INOUT OUT |
paul@30 | 93 | %token CAP FPAGE |
paul@38 | 94 | %token IDENTIFIER DECIMAL HEXADECIMAL |
paul@0 | 95 | |
paul@3 | 96 | /* Starting rule. */ |
paul@3 | 97 | |
paul@1 | 98 | %start file |
paul@1 | 99 | |
paul@0 | 100 | %% |
paul@0 | 101 | |
paul@33 | 102 | file : statements |
paul@33 | 103 | { write_files(last_interface); } |
paul@33 | 104 | ; |
paul@1 | 105 | |
paul@21 | 106 | statements : statement statements |
paul@3 | 107 | | %empty |
paul@1 | 108 | ; |
paul@1 | 109 | |
paul@21 | 110 | statement : include |
paul@25 | 111 | | q_interface |
paul@33 | 112 | { $1.tail = last_interface; last_interface = copy_interface($1); } |
paul@21 | 113 | ; |
paul@21 | 114 | |
paul@21 | 115 | include : INCLUDE HEADER |
paul@51 | 116 | { $$.filename = $2; $$.tail = last_include; last_include = copy_include($$); } |
paul@21 | 117 | ; |
paul@21 | 118 | |
paul@25 | 119 | /* Interface qualified with or without attributes. */ |
paul@25 | 120 | |
paul@51 | 121 | q_interface : LBRACE attributes RBRACE interface |
paul@51 | 122 | { $4.attributes = copy_attribute($2); $$ = $4; } |
paul@25 | 123 | | interface |
paul@25 | 124 | { $$.attributes = NULL; } |
paul@25 | 125 | ; |
paul@25 | 126 | |
paul@10 | 127 | interface : INTERFACE interface_name BEGINSECTION signatures ENDSECTION TERM |
paul@155 | 128 | { $$.name = $2; $$.signatures = copy_signature($4); $$.includes = last_include; last_include = NULL; } |
paul@51 | 129 | | INTERFACE interface_name BEGINSECTION ENDSECTION TERM |
paul@155 | 130 | { $$.name = $2; $$.signatures = NULL; $$.includes = last_include; last_include = NULL; } |
paul@0 | 131 | ; |
paul@0 | 132 | |
paul@4 | 133 | interface_name : IDENTIFIER ; |
paul@0 | 134 | |
paul@51 | 135 | /* Signatures qualified with or without attributes. */ |
paul@25 | 136 | |
paul@51 | 137 | signatures : q_signature signatures |
paul@51 | 138 | { copy_tail(signature, $1, $2); $$ = $1; } |
paul@51 | 139 | | q_signature |
paul@51 | 140 | { $1.tail = NULL; $$ = $1; } |
paul@51 | 141 | ; |
paul@51 | 142 | |
paul@51 | 143 | q_signature : LBRACE attributes RBRACE signature |
paul@51 | 144 | { $4.attributes = copy_attribute($2); $$ = $4; } |
paul@51 | 145 | | signature |
paul@51 | 146 | { $1.attributes = NULL; $$ = $1; } |
paul@25 | 147 | ; |
paul@25 | 148 | |
paul@51 | 149 | signature : qualifier operation LPAR RPAR TERM |
paul@51 | 150 | { $$.qualifier = $1; $$.operation = $2; $$.parameters = NULL; } |
paul@51 | 151 | | qualifier operation LPAR parameters RPAR TERM |
paul@51 | 152 | { $$.qualifier = $1; $$.operation = $2; $$.parameters = copy_parameter($4); } |
paul@25 | 153 | ; |
paul@25 | 154 | |
paul@51 | 155 | qualifier : IDENTIFIER ; |
paul@51 | 156 | |
paul@51 | 157 | operation : IDENTIFIER ; |
paul@51 | 158 | |
paul@51 | 159 | /* Attributes with or without parameters. */ |
paul@51 | 160 | |
paul@51 | 161 | attributes : attribute SEP attributes |
paul@51 | 162 | { copy_tail(attribute, $1, $3); $$ = $1; } |
paul@51 | 163 | | attribute |
paul@51 | 164 | { $1.tail = NULL; $$ = $1; } |
paul@51 | 165 | ; |
paul@25 | 166 | |
paul@25 | 167 | attribute : IDENTIFIER LPAR attr_params RPAR |
paul@51 | 168 | { $$.attribute = $1; $$.identifiers = copy_identifier($3); } |
paul@25 | 169 | | IDENTIFIER |
paul@51 | 170 | { $$.attribute = $1; $$.identifiers = NULL; } |
paul@25 | 171 | ; |
paul@25 | 172 | |
paul@38 | 173 | attr_params : attr_param SEP attr_params |
paul@51 | 174 | { $$.identifier = $1; copy_tail(identifier, $$, $3); } |
paul@38 | 175 | | attr_param |
paul@25 | 176 | { $$.identifier = $1; $$.tail = NULL; } |
paul@25 | 177 | ; |
paul@25 | 178 | |
paul@38 | 179 | attr_param : IDENTIFIER |
paul@38 | 180 | | DECIMAL |
paul@38 | 181 | | HEXADECIMAL |
paul@38 | 182 | ; |
paul@38 | 183 | |
paul@51 | 184 | /* Signature parameters. */ |
paul@25 | 185 | |
paul@51 | 186 | parameters : parameter SEP parameters |
paul@51 | 187 | { copy_tail(parameter, $1, $3); $$ = $1; } |
paul@51 | 188 | | parameter |
paul@51 | 189 | { $1.tail = NULL; $$ = $1; } |
paul@3 | 190 | ; |
paul@3 | 191 | |
paul@30 | 192 | parameter : specifier item identifiers |
paul@57 | 193 | { $$.specifier = $1; $$.cls = $2; $$.identifiers = copy_identifier($3); } |
paul@30 | 194 | | specifier identifiers |
paul@57 | 195 | { $$.specifier = $1; $$.cls = WORD_CLASS; $$.identifiers = copy_identifier($2); } |
paul@18 | 196 | ; |
paul@0 | 197 | |
paul@18 | 198 | specifier : IN | INOUT | OUT ; |
paul@0 | 199 | |
paul@30 | 200 | item : CAP | FPAGE ; |
paul@30 | 201 | |
paul@25 | 202 | identifiers : IDENTIFIER |
paul@25 | 203 | { $$.identifier = $1; $$.tail = NULL; } |
paul@25 | 204 | | IDENTIFIER identifiers |
paul@51 | 205 | { $$.identifier = $1; copy_tail(identifier, $$, $2); } |
paul@25 | 206 | ; |
paul@25 | 207 | |
paul@0 | 208 | %% |
paul@0 | 209 | |
paul@7 | 210 | void *copy(void *value) |
paul@7 | 211 | { |
paul@25 | 212 | union YYSTYPE *v; |
paul@25 | 213 | |
paul@25 | 214 | if (value == NULL) |
paul@25 | 215 | return value; |
paul@25 | 216 | |
paul@25 | 217 | v = (union YYSTYPE *) malloc(sizeof(union YYSTYPE)); |
paul@7 | 218 | memcpy(v, value, sizeof(union YYSTYPE)); |
paul@7 | 219 | return v; |
paul@7 | 220 | } |
paul@7 | 221 | |
paul@33 | 222 | void reset(void) |
paul@33 | 223 | { |
paul@33 | 224 | last_interface = NULL; |
paul@33 | 225 | last_include = NULL; |
paul@33 | 226 | } |
paul@33 | 227 | |
paul@3 | 228 | void yyerror(const char *message) |
paul@0 | 229 | { |
paul@0 | 230 | printf("%s\n", message); |
paul@0 | 231 | } |