문제

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?

도움이 되었습니까?

해결책

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).

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top