Re: On the fly XML parser

Alex Rousskov <[email protected]> Thu, 3 Jun 2004 09:30:29 -0600 (MDT)
Newsgroups gmane.comp.parsers.hapy.user
Message-ID <[email protected]>
On Thu, 3 Jun 2004, Romain Behar wrote:

> The attached example is an attempt for an "on the fly" XML parser.
> XML files tend to get very big: the idea is not to load the entire
> file into memory but to parse it when loading the file.

That's a reasonable approach provided you need to extract something
small from the XML file as opposed to load and manipulate the entire
file.

> The parser was modified to use prefix parsing, and the rNode and
> rElement merged into a new rXML rule. We loose the element nesting
> information: a stack needs to be set up to keep track of nesting
> level or skip entire blocks.
>
> Could the prefix parser stop on defined rules, that would avoid
> changing the grammar?

Do you mean stop if any of the specified rules match, as opposed
to stopping when the top-most rule (the grammar) matches? It sounds
like you may want to use semantic actions attached to the rules you
are interested in:

	http://www.hapy.org/actions.html

We probably need more experience with this, but for now the following
rules of thumb seem accurate to me:

	- Use prefix parsing to handle a stream of "objects",
	  where each object has the same grammar

	- Use actions to handle a complex "object" where
	  parts of the object are have different grammars

The two approaches can be combined, of course.

If you mark the rules as both committed and verbatim, then the parser
will discard the parsing tree formed by those rules, after calling
your action. Thus, memory overhead would be reduced.

Do we need to add a feature to the action interface to allow the
action to dynamically mark the parsed result as discardable?

> There is another tweak in the prefix parsing loop:
>
> char c = file.get();
> while(c != ' ' && c != '\n' && c != '\r' && c != '\t')
> {
>  ...
> }
>
> helps the parser not to stop on string parsing (e.g.
> "<!--" in rComment);
>
> is there a better way to handle this trick?

Hmm... As far as I can see, your parser should only stop when the
entire comment (rComment) is mathed. It should not stop when only
"<!--" part matched, unless it stops because more input is needed. Are
you sure that the parser with "<-- ... -->" as available input stops
after "<--"? That would be a bug, I think.

Thanks,

Alex.

-- 
Protocol performance, functionality, and reliability testing.
Tools, services, and know-how.
http://www.measurement-factory.com/


-------------------------------------------------------
This SF.Net email is sponsored by the new InstallShield X.
From Windows to Linux, servers to mobile, InstallShield X is the one
installation-authoring solution that does it all. Learn more and
evaluate today! http://www.installshield.com/Dev2Dev/0504