Question

I wrote simple class code generator, that creates code basing on xml file.

The xml file looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<classes>
    <class name="Klient">
        <attr type="int">id</attr>
        <attr type="String">imie</attr>
        <attr type="String">nazwisko</attr>
        <attr type="Date">dataUr</attr>
    </class>
    <class name="Wizyta">
        <attr type="int">id</attr>
        <attr type="Klient">klient</attr>
        <attr type="Date">data</attr>
    </class>
</classes>

If I wanted to define this simple xml as a BNF grammar, how it would look like?

Was it helpful?

Solution

This depends on how loose you want the grammar to be. For example, making some clear assumptions:

data       -> version '<classes>' classes '</classes>'
version    -> '<?xml version=' quotedString 'encoding=' quotedString '?>'
classes    -> '<class name=' quotedString '>' attributes '</class>' classes
attributes -> '<attr type=' quotedString '>' string '</attr>' attributes

(quotedString and string are terminals and all other terminals are between quotes)

Note how you could eliminate some productions from the grammar above, but that wouldn't improve readability.

As for the actual usage of this grammar: consider using an XML parsing library, that will probably be much more easier than using an actual parser generator (let alone manually implementing one).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top