-
Notifications
You must be signed in to change notification settings - Fork 0
/
xml_parser.y
executable file
·158 lines (124 loc) · 3.23 KB
/
xml_parser.y
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
154
155
156
157
158
%{
import java.io.*;
import java.util.Stack;
%}
%token NL EQ
%token XML_START XML_CLOSE
%token<sval> XML_VER XML_ENCODING
%token DOCTYPE_START
%token<sval> DOCTYPE_DTD DOCTYPE_EXTID
%token TAG_OPEN TAG_CLOSE TAG_BEGIN_CLOSE TAG_EMPTY_CLOSE
%token<sval> NAME TEXT ATT_VALUE
%type<sval> prolog xml doctype
%type<sval> empty_element start_element end_element element
%type<sval> content chardata attribute
%%
document : prolog element TEXT { System.out.println("\n\n" + $1 + $2 + "\n\n"); }
;
prolog : xml doctype { $$ = $1 + $2; }
;
xml : XML_START XML_VER XML_ENCODING XML_CLOSE { $$ = ""; }
;
doctype : DOCTYPE_START NAME DOCTYPE_EXTID DOCTYPE_DTD TAG_CLOSE { $$ = ""; }
;
element : empty_element { $$ = $1; }
| start_element content end_element { $$ = $1 + "\n\"content\": [" + $2 + "\n]" + $3; }
;
empty_element : TAG_OPEN NAME attribute TAG_EMPTY_CLOSE
{
if(tags.empty())
$$ = "\n{\n\"tag\": \"" + $2 + "\"," + $3 + "\n}";
else
$$ = "\n{\n\"tag\": \"" + $2 + "\"," + $3 + "\n},";
}
;
start_element : TAG_OPEN NAME attribute TAG_CLOSE
{
tags.push($2);
$$ = "\n{\n\"tag\": \"" + $2 + "\"," + $3;
}
;
content : { $$ = ""; }
| element { $$ = $1; }
| chardata
{
if($1.trim().length() < 1)
$$ = "";
else
$$ = "\n\"" + $1.replaceAll("\n", "").trim() + "\"";
}
| chardata element content
{
if($1.trim().length() < 1)
$$ = $2 + $3;
else
$$ = "\n\"" + $1.replaceAll("\n", "").trim() + "\"," + $2 + $3;
}
;
chardata : TEXT { $$ = replaceSpecial($1.trim()); }
;
end_element : TAG_BEGIN_CLOSE NAME TAG_CLOSE
{
String temp = tags.pop();
if(tags.empty())
$$ = "\n}";
else
$$ = "\n},";
tags.push(temp);
if(tags.peek().equals($2))
tags.pop();
else {
yyerror("Overlapping or wrong closed tags detected");
return 1;
}
}
;
attribute : { $$ = ""; }
| NAME EQ ATT_VALUE attribute { $$ = "\n\"@" + $1 + "\": " + replaceSpecial($3) + "," + $4; }
;
%%
private Yylex lexer;
private Stack<String> tags;
public Parser(Reader r) {
lexer = new Yylex(r, this);
tags = new Stack<>();
//yydebug = true;
}
public static void main(String args[]) {
Parser yyparser;
if (args.length > 0) {
try {
yyparser = new Parser(new FileReader(args[0]));
yyparser.yyparse();
} catch (Exception e) {
e.printStackTrace();
}
} else
System.out.println("ERROR: Provide an input file as Parser argument");
}
private String replaceSpecial(String input) {
if(input.contains(">"))
input = input.replaceAll(">", ">");
if(input.contains("<"))
input = input.replaceAll("<", "<");
if(input.contains("'"))
input = input.replaceAll("'", "'");
if(input.contains("""))
input = input.replaceAll(""", "\"");
if(input.contains("&"))
input = input.replaceAll("&", "&");
return input;
}
private int yylex() {
int yyl_return = -1;
try {
yylval = new ParserVal(0);
yyl_return = lexer.yylex();
} catch (IOException e) {
System.err.println("IO error: " + e);
}
return yyl_return;
}
public void yyerror(String error) {
System.err.println ("Error: " + error);
}