1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
|
/* xml.y Bison file for limited XML parser (just tags, no text).
Last modified 1997-12-23
This is *NOT* a general-purpose XML parser.
Note that all lists except for the top level one are returned in
reverse order.
Copyright (C) 1997 David Flater.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
%{
#include "common.hh"
Dstr xmlfilename;
struct xmltag *xmlparsetree;
extern int xmllex ();
extern void xmlerror (const char *);
#define balance(lname,rname) { \
if ((*(lname)) != (*(rname))) { \
Dstr details ("Parse error in XML file "); \
details += xmlfilename; \
details += "\n<"; \
details += (*(lname)); \
details += "> ended by </"; \
details += (*(rname)); \
details += ">"; \
barf (XMLPARSE, details); \
} \
delete rname; }
%}
%union {
struct xmltag *tag;
struct xmlattribute *attribute;
Dstr *value;
}
%token <value> NAME ATTVALUE
%token XMLSTART
%type <tag> tag xml_file taglist
%type <attribute> attribute avlist
%%
xml_file: XMLSTART taglist
{ // Un-reverse the top-level list
struct xmltag *temp;
$$ = NULL;
while ($2) {
temp = $2->next;
$2->next = $$;
$$ = $2;
$2 = temp;
}
xmlparsetree = $$; }
;
taglist: tag
{ $$ = $1; }
| taglist tag
{ $$ = $2;
$$->next = $1; }
;
tag: '<' NAME avlist '>' '<' '/' NAME '>'
{
balance ($2, $7);
$$ = new xmltag;
$$->name = $2;
$$->attributes = $3;
$$->contents = NULL;
$$->next = NULL;
}
| '<' NAME avlist '>' taglist '<' '/' NAME '>'
{
balance ($2, $8);
$$ = new xmltag;
$$->name = $2;
$$->attributes = $3;
$$->contents = $5;
$$->next = NULL;
}
| '<' NAME avlist '/' '>'
{
$$ = new xmltag;
$$->name = $2;
$$->attributes = $3;
$$->contents = NULL;
$$->next = NULL;
}
| '<' NAME '>' '<' '/' NAME '>'
{
balance ($2, $6);
$$ = new xmltag;
$$->name = $2;
$$->attributes = NULL;
$$->contents = NULL;
$$->next = NULL;
}
| '<' NAME '>' taglist '<' '/' NAME '>'
{
balance ($2, $7);
$$ = new xmltag;
$$->name = $2;
$$->attributes = NULL;
$$->contents = $4;
$$->next = NULL;
}
| '<' NAME '/' '>'
{
$$ = new xmltag;
$$->name = $2;
$$->attributes = NULL;
$$->contents = NULL;
$$->next = NULL;
}
;
avlist: attribute
{ $$ = $1; }
| avlist attribute
{ $$ = $2;
$$->next = $1; }
;
attribute: NAME '=' ATTVALUE
{ $$ = new xmlattribute;
$$->name = $1;
$$->value = $3;
$$->next = NULL; }
;
%%
|