source: trunk/markup_stats.cxx @ 135

Last change on this file since 135 was 135, checked in by lindanl, 11 years ago

SymbolTable? class and end tag checking.

File size: 11.3 KB
Line 
1/*  markup_stats.c - parabix demo program
2    Copyright (c) 2007, Robert D. Cameron.
3    Licensed to the public under the Open Software License 3.0.
4    Licensed to International Characters, Inc., under the Academic
5    Free License 3.0.
6
7*/
8
9#include <stdio.h>
10#include <stdlib.h>
11#include <string.h>
12#include <errno.h>
13#include <sys/types.h>
14#include <sys/stat.h>
15#include <string>
16#include <iostream>
17using namespace std;
18
19#ifdef PAPI
20#include "../code_clocker/clocker/cc.h"
21#include "../code_clocker/clocker/cc.cxx"
22
23CC * code_clocker;
24
25#define CHARSET_VALIDATION 1
26#define WS_CONTROL 2
27#define MARKUP_STREAMS 3
28#define BITLEX_ALL 4
29#define BITPLEX 5
30#define BYTEPLEX 6
31#define ADVANCE_BUFFERS 7
32#define BUFFER_TOTAL 8
33
34#define CODE_CLOCKING BUFFER_TOTAL
35
36#endif
37
38//#include "src/ilax.h"
39#include "src/engine.h"
40
41#ifndef REPEAT_RUNS
42#define REPEAT_RUNS 1
43#endif
44
45/* Internals */
46#include "src/xmlmodel.h"
47#include "src/xml_error.h"
48#include "src/bitplex.h"
49#include "src/byteplex.h"
50#include "src/xmldecl.h"
51#include "src/bitlex.h"
52
53
54#include "src/xmlmodel.c"
55#include "src/xml_error.c"
56#include "src/bitplex.c"
57#include "src/byteplex.c"
58#include "src/xmldecl.c"
59#include "src/bitlex.c"
60#include "src/engine.c"
61#include "src/symtab.c"
62
63/* Global declarations of parsing engine. */
64Parser_Interface * parser;
65
66/* Global declarations for statistics. */
67
68int comment_count = 0;
69int comment_length = 0;
70int CDATA_start_count = 0;
71int CDATA_start_pos = 0;
72int CDATA_length = 0;
73int CDATA_end_count = 0;
74int PI_count = 0;
75int PI_length = 0;
76int empty_elem_count = 0;
77int empty_elem_length = 0;
78int start_tag_count = 0;
79int start_tag_length = 0;
80int attribute_count = 0;
81int end_tag_count = 0;
82int end_tag_length = 0;
83int reference_count = 0;
84int reference_length = 0;
85int text_item_count = 0;
86int text_item_length = 0;
87int error_item_count = 0;
88int error_item_length = 0;
89int nesting_depth = 0;
90int max_nesting_depth = 0;
91int total_attribute_count = 0;
92int total_att_name_length = 0;
93int total_att_value_length = 0;
94int namespace_count = 0;
95int total_namespace_name_length = 0;
96int total_namespace_URI_length = 0;
97
98int last_item_start = 0;
99int last_item_stop = 0;
100int last_buffer_rel_pos = 0;
101
102
103/* Action routine for an XML comment in "<!--"  "-->" brackets. */
104template <CodeUnit_Base C>
105inline void ParsingEngine<C>::Comment_action(unsigned char * item, int lgth) {
106        comment_count +=1;
107        comment_length += lgth;
108}
109
110/* Action routine called upon recognizing "<![CDATA[" to start a CDATA section. */
111template <CodeUnit_Base C>
112inline void ParsingEngine<C>::CDATA_start_action(unsigned char * CDATA_ptr){
113        CDATA_start_pos = AbsPos() - 9;
114        CDATA_start_count +=1;
115}
116
117/* Action routine called upon recognizing "]]>" to end a CDATA section. */
118template <CodeUnit_Base C>
119inline void ParsingEngine<C>::CDATA_end_action(unsigned char * CDATA_end_ptr) {
120        CDATA_end_count +=1;
121        CDATA_length += AbsPos() - CDATA_start_pos;
122}
123
124/* Action routine for an XML processing instruction enclosed in "<?" and "?>" brackets. */
125template <CodeUnit_Base C>
126inline void ParsingEngine<C>::PI_action(unsigned char * item, int lgth) {
127        PI_count +=1;
128        PI_length += lgth;
129}
130
131/* Action routine for an empty element enclosed in "<" and "/>" brackets. */
132template <CodeUnit_Base C>
133inline void ParsingEngine<C>::EmptyElement_action(unsigned char * item, int lgth) {
134        empty_elem_count +=1;
135        empty_elem_length += lgth;
136}
137
138/* Action routine for a start tag enclosed in "<" and ">" brackets. */
139template <CodeUnit_Base C>
140inline void ParsingEngine<C>::StartTag_action(unsigned char * item, int lgth) {
141        start_tag_count +=1;
142        start_tag_length += lgth;
143        nesting_depth += 1;
144        if (nesting_depth > max_nesting_depth) max_nesting_depth = nesting_depth;
145//      cout << string((char *) item, lgth) << endl;
146}
147
148/* Action routine for an end tag enclosed in "</" and ">" brackets. */
149template <CodeUnit_Base C>
150inline void ParsingEngine<C>::EndTag_action(unsigned char * item, int lgth) {
151        end_tag_count +=1;
152        end_tag_length += lgth;
153        nesting_depth -= 1;
154}
155
156/* Action routine for an error item */
157template <CodeUnit_Base C>
158inline void ParsingEngine<C>::Error_action(unsigned char * item, int lgth) {
159        error_item_count +=1;
160        error_item_length += lgth;
161        fprintf(stderr, "Error: illegal markup at positions %i of length %i.\n", AbsPos()-lgth, lgth);
162        cerr << string((char *) item, lgth) << endl;
163}
164
165/* Action routine for a text item */
166template <CodeUnit_Base C>
167inline void ParsingEngine<C>::Text_action(unsigned char * item, int lgth) {
168        text_item_count +=1;
169        text_item_length += lgth;
170}
171
172template <CodeUnit_Base C>
173inline void ParsingEngine<C>::Reference_action(unsigned char * item, int lgth) {
174        reference_count +=1;
175        reference_length += lgth;
176}
177
178
179
180
181/* Three action routines for markup components are defined as follows.
182|ElementName_action| is the action routine called upon recognition of
183an element name immediately after the opening angle bracket of a start
184tag or empty element tag.  {\bf OR MAYBE THIS SHOULD BE DEFERRED UNTIL
185AFTER ATTRIBUTE PROCESSING SO THAT NAMESPACES ARE SET?}
186It is called with two parameters identifying the
187first and last character positions of the expected XML_name.
188Similarly, |PI_Target_action| is the action routine called upon recognition
189of the XML Name that occurs immediately after the opening "<?"
190delimiter of a processing instruction.
191
192 The third action routine for markup components is Attribute_Value_action,
193which takes three parameters rather than two.  {\bf OR POSSIBLY JUST
194THE QUOTE MARK ITEMS, RELYING ON THE END OF THE LAST COMPONENT PROCESSED
195TO MARK THE SPACE BEFORE THE ATT NAME.- REQUIRES ELEMENT_NAME_ACTION}
196*/
197
198/* Semantic action routines for markup components. */
199/* Action routine for an element name occurring immediately after the
200   opening "<" of a start tag or empty element tag. */
201template <CodeUnit_Base C>
202inline void ParsingEngine<C>::ElementName_action(unsigned char * item, int lgth) {
203}
204
205/* Action routine for a processing instruction target name occurring immediately
206   after the opening "<?" of a processing instruction. */
207template <CodeUnit_Base C>
208inline void ParsingEngine<C>::PI_Target_action(unsigned char * item, int lgth) {
209}
210
211/* Action routine for an individual attribute/value pair occurring in
212   a element start tag or an empty element tag. */
213template <CodeUnit_Base C>
214inline void ParsingEngine<C>::AttributeValue_action(unsigned char * name, int name_lgth, 
215                                 unsigned char * val, int val_lgth) {
216        total_attribute_count+=1;
217        total_att_name_length += name_lgth;
218        total_att_value_length += val_lgth;
219}
220
221/* Action routine for an individual attribute/value pair occurring in
222   a element start tag or an empty element tag. */
223template <CodeUnit_Base C>
224inline void ParsingEngine<C>::Namespace_action(unsigned char * name, int name_lgth,
225                             unsigned char * URI, int URI_lgth) {
226        namespace_count+=1;
227        total_namespace_name_length += name_lgth;
228        total_namespace_URI_length += URI_lgth;
229}
230
231
232template <CodeUnit_Base C>
233inline void ParsingEngine<C>::FinalizeBuffer_action() {
234       
235       
236#ifdef DEBUG
237        printf ("FinalizeBuffer; last 16 bytes + lookahead 16 =\n");
238        cout << string((char *) GetCodeUnitPtr(AbsPos()-16), 16) << "::" << string((char *) GetCodeUnitPtr(AbsPos()), 16) << endl;
239#endif
240        #if defined(PAPI) and defined(CODE_CLOCKING) and (CODE_CLOCKING == BUFFER_TOTAL)
241                code_clocker->cc_end_interval(BUFFER_SIZE);
242        #endif
243        #if defined(PAPI) and defined(CODE_CLOCKING) and (CODE_CLOCKING == BUFFER_TOTAL)
244                code_clocker->cc_start_interval();
245        #endif
246       
247}
248
249
250template <CodeUnit_Base C>
251inline void ParsingEngine<C>::DocumentStart_action() {
252        #if defined(PAPI) and defined(CODE_CLOCKING) and (CODE_CLOCKING == BUFFER_TOTAL)
253                code_clocker->cc_start_interval();
254        #endif
255}
256
257template <CodeUnit_Base C>
258inline void ParsingEngine<C>::DocumentEnd_action() {
259        #if defined(PAPI) and defined(CODE_CLOCKING) and (CODE_CLOCKING == BUFFER_TOTAL)
260                code_clocker->cc_end_interval(buffer_rel_pos);
261        #endif
262}
263
264template <CodeUnit_Base C>
265inline void ParsingEngine<C>::Doctype_action(unsigned char * item, int lgth) {
266#ifdef SHOW_DTD_ACTIONS
267        printf("Document Type:\n");
268        cout << string((char *) item, lgth) <<endl;
269#endif
270}
271
272template <CodeUnit_Base C>
273inline void ParsingEngine<C>::PEReference_action(unsigned char * item, int lgth) {
274}
275
276
277template <CodeUnit_Base C>
278inline void ParsingEngine<C>::ExtSubsetDecl_action(unsigned char * item, int lgth) {
279#ifdef SHOW_DTD_ACTIONS
280        printf("ExtSubsetDecl:\n");
281        cout << string((char *) item, lgth) <<endl;
282#endif
283}
284
285template <CodeUnit_Base C>
286inline void ParsingEngine<C>::Prolog_action(unsigned char * item, int lgth) {
287#ifdef SHOW_DTD_ACTIONS
288        printf("Prolog:\n");
289        cout << string((char *) item, lgth) <<endl;
290#endif
291}
292
293#define print_stats(stat_string, count, total_lgth) \
294        printf("%i %s", count, stat_string);\
295        if (count == 0) printf("s.\n");\
296        else if (count == 1) printf(" of length %i.\n", total_lgth);\
297        else printf("s of avg. lgth %i.\n", total_lgth/count);
298
299
300
301
302int
303main(int argc, char * argv[]) {
304        if (argc != 2) {
305        printf("Usage: %s <filename>\n", argv[0]);
306                exit(-1);
307        }
308        char * filename = argv[1];
309
310        #ifdef PAPI
311                #define NUM_EVENTS 2
312                int Events[NUM_EVENTS] = {PAPI_TOT_CYC, PAPI_BR_MSP};
313                int cal_size = 1000;
314                code_clocker = new CC(Events,NUM_EVENTS,cal_size);
315                code_clocker->cc_set_cmd(argv[0]);
316                code_clocker->cc_set_param("Mhz","2127.997");
317        #endif
318
319        for (int run = 0; run < REPEAT_RUNS; run++) {
320//      #ifdef PAPI
321//              code_clocker->cc_start_interval();
322//      #endif
323
324        parser = Parser_Interface::ParserFactory(filename);
325       
326        /*             
327        if (!parser->has_ByteOrderMark()) printf("No ");
328        printf("Byte Order Mark found.\n");
329
330        if (parser->get_version() == XML_1_0) printf("XML version 1.0 declared.\n");
331        else if (parser->get_version() == XML_1_1) printf("XML version 1.1 declared.\n");
332        else printf ("XML version 1.0 implied by default.\n");
333        if (parser->has_EncodingDecl()) {
334                printf("XML encoding declared:  %s\n", parser->get_Encoding());
335        }
336        if (parser->standalone_status() == Standalone_yes)
337                printf("XML standalone = yes declared.\n");
338        else if (parser->standalone_status() == Standalone_no)
339                printf("XML standalone = no declared.\n");
340        else printf ("XML standalone = no by default.\n");
341        */
342       
343        parser->Parse_Prolog();
344
345        parser->Parse_DocumentContent();
346
347        parser->~Parser_Interface();
348        printf("Run %i complete.\n", run);
349//      #ifdef PAPI
350//              int elems = 0;
351//              code_clocker->cc_end_interval(100);
352//      #endif
353       
354       
355        }
356
357        #ifdef PAPI
358                code_clocker->cc_display();
359                code_clocker->cc_write_xml_file();
360                code_clocker->cc_write_csv_file();
361                delete code_clocker;
362        #endif 
363       
364        print_stats("comment", comment_count, comment_length);
365        print_stats("CDATA section", CDATA_end_count, CDATA_length);
366        print_stats("processing instruction", PI_count, PI_length);
367        print_stats("empty element", empty_elem_count, empty_elem_length);
368        print_stats("start tag", start_tag_count, start_tag_length);
369        printf("%i total attributes\n", attribute_count);
370        print_stats("attribute name", total_attribute_count, total_att_name_length);
371        print_stats("attribute value", total_attribute_count, total_att_value_length);
372        print_stats("namespace name", namespace_count, total_namespace_name_length);
373        print_stats("namespace URI", namespace_count, total_namespace_URI_length);
374        print_stats("end tag", end_tag_count, end_tag_length);
375        print_stats("text item", text_item_count, text_item_length);
376        print_stats("reference", reference_count, reference_length);
377        print_stats("error item", error_item_count, error_item_length);
378        printf("Maximum nesting depth = %i\n", max_nesting_depth);
379       
380        return(0);
381}
Note: See TracBrowser for help on using the repository browser.