SEASON OF KDE 2020 ( WEEKLY PROGRESS : Week 2-3 ).

After having published the grammer of KML file format, the task was to implement one parser with all the rules followed from the grammer in the previous post.

I wrote a sample impl, as given below :

#include "typenames.h"
#include<boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix1_operators.hpp>

#include<bits/stdc++.h>

namespace KmlParser{
    namespace qi = boost::spirit::qi;
    namespace ascii = boost::spirit::ascii;

    // qi grammer template usage is defined here :
    // https://www.boost.org/doc/libs/1_53_0/libs/spirit/doc/html/spirit/qi/reference/nonterminal/grammar.html
    template <typename Iterator>
    struct kmlprsr_grammer : boost::spirit::qi::grammar<Iterator, unsigned()>{

        kmlprsr_grammer() : kmlprsr_grammer::base_type(start){
        using qi::int_;
        using qi::string;
        using qi::char_;

        // the grammer as discussed is posted here:
        // https://sbalikondwar.blogspot.com/2020/01/season-of-kde-2020-weekly-progress.html#more
        start = graph_kwrd = ( string(<graph>) >> grph >> string(</graph>));

        type_kwrd = ( string(<type>) >> string(Directed) | string(Undirected) >> string(</type>));

        id = ( string(<id>) >> ( int_ |  +char_ ) >> string(</id>));

        connect_kwrd = ( string(<connect>) >> char_ >> char(',')  >> char_ >> string(</connect>) );

        color_kwrd = ( string(<color>) >> ( string('red') | string('green') ) >> string(</color));

        edge_kwrd = ( string(<edge>) >> ( +color_kwrd >> +connect_kwrd ) >> string(</edge>));

        node_kwrd = ( string(<node>) >> (id >> *color_kwrd ) >> string(</node>));

        grph = ( type_kwrd >> *node_kwrd >> *edge_kwrd );

        white_space = ascii::space;

        escapes = lexeme[*( white_space | eol )];
    }

boost::spirit::qi::rule<Iterator> escapes,id,color_kwrd,edge_kwrd,node_kwrd,graph_kwrd;
};

}


As the reader can see, I used qi grammer to declare a template which consists of all the rules defined at last. By using parse function the Document can be parsed.
Currently I am completing its impl, I would like to hear any suggestions.

I tried uploading same on my cl( fork ) but there were some config issues which might have taken me unnecessary time, so just posted here. I will upload it asap after this post.

Comments

Popular Posts