String Parser using BOOST.Spirit

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • krbyxtrm

    String Parser using BOOST.Spirit

    hi,

    i have problem implemting a string parser that parser comman delimited
    string:
    "str1,str2,str3 "
    INTO:
    1. str1
    2. str2
    3. str3
    *also strings are of any string (no specific string/keyword)

    I have this code below, what it does so far is to parse specific
    string: "x1", "x2", "y1", "y2"
    but what i need is a parser for any kind of string (comma delimited)

    bool
    parse_string(ch ar const* str, vector<string>& v)
    (
    return parse(str,
    (
    (str_p("x1"[push_back_a(v)]) |
    str_p("x2")[push_back_a(v)] )[color=blue][color=green]
    >> *(',' >> (str_p("y1")[push_back_a(v)] |[/color][/color]
    str_p("y2")[push_back_a(v)] ) )
    ),
    space_p).full;
    )

    example use: INPUT: "x1,y2,y1,y 2"
    OUTPUT:
    x1
    y2
    y1
    y2

    what i need is:
    example: INPUT: "this,is,a,test "
    OUTPUT:
    this
    is
    a
    test

    ps. i need to implement this using BOOST.Spirit
    --
    Thx

  • Jeff Flinn

    #2
    Re: String Parser using BOOST.Spirit

    krbyxtrm wrote:[color=blue]
    > hi,
    >
    > i have problem implemting a string parser that parser comman delimited
    > string:
    > "str1,str2,str3 "
    > INTO:
    > 1. str1
    > 2. str2
    > 3. str3
    > *also strings are of any string (no specific string/keyword)[/color]

    Your better off joining the boost.spirit mailing list at:
    Spirit is an object oriented recursive descent parser generator framework implemented using template meta-programming techniques.


    Also I'd be surprised if there weren't an example of doing exactly that in
    your spirit installation. Something like rule<> comma_delimited =
    (*(anychar_p-','))[push_back_a(a)] % ',';

    Jeff Flinn


    Comment

    • Gernot Frisch

      #3
      Re: String Parser using BOOST.Spirit


      [color=blue]
      > Also I'd be surprised if there weren't an example of doing exactly
      > that in your spirit installation. Something like rule<>
      > comma_delimited = (*(anychar_p-','))[push_back_a(a)] % ',';[/color]

      OT, but: What is that % operator used for?


      Comment

      • Jeff Flinn

        #4
        Re: String Parser using BOOST.Spirit

        Gernot Frisch wrote:[color=blue][color=green]
        >> Also I'd be surprised if there weren't an example of doing exactly
        >> that in your spirit installation. Something like rule<>
        >> comma_delimited = (*(anychar_p-','))[push_back_a(a)] % ',';[/color]
        >
        > OT, but: What is that % operator used for?[/color]

        From the docs at http://www.boost.org/libs/spirit/doc/operators.html,

        a % b ; Match a list of one or more repetitions of a separated by
        occurrences of b. This is the same as a >> *(b >> a). Note that a must not
        also match b.

        Jeff


        Comment

        • Phlip

          #5
          Re: String Parser using BOOST.Spirit

          > Gernot Frisch wrote:
          [color=blue][color=green]
          >> OT, but: What is that % operator used for?[/color][/color]

          Awesome expression metatemplate techniques are never off-topic!

          (And could we all learn to stand up to the chronic naggers, instead of
          cringing each time a thread strays off The C++ Standard?)

          --
          Phlip
          http://www.greencheese.org/ZeekLand <-- NOT a blog!!!


          Comment

          • Rolf Magnus

            #6
            Re: String Parser using BOOST.Spirit

            Phlip wrote:
            [color=blue][color=green]
            >> Gernot Frisch wrote:[/color]
            >[color=green][color=darkred]
            >>> OT, but: What is that % operator used for?[/color][/color]
            >
            > Awesome expression metatemplate techniques are never off-topic![/color]

            I don't consider them awesome. Actually, it's the worst case of operator
            abuse I've ever seen.

            Comment

            • Phlip

              #7
              Re: String Parser using BOOST.Spirit

              Rolf Magnus wrote:
              [color=blue]
              > I don't consider them awesome. Actually, it's the worst case of operator
              > abuse I've ever seen.[/color]

              Can you implement an efficient and appealing parser generator directly in
              C++ without abusing the occassional operator?

              --
              Phlip
              http://www.greencheese.org/ZeekLand <-- NOT a blog!!!

              Comment

              • Rolf Magnus

                #8
                Re: String Parser using BOOST.Spirit

                Phlip wrote:
                [color=blue]
                > Rolf Magnus wrote:
                >[color=green]
                >> I don't consider them awesome. Actually, it's the worst case of operator
                >> abuse I've ever seen.[/color]
                >
                > Can you implement an efficient and appealing parser generator directly in
                > C++ without abusing the occassional operator?[/color]

                No, and I wouldn't want to do that either.


                Comment

                • krbyxtrm

                  #9
                  Re: String Parser using BOOST.Spirit

                  Hello people,

                  I have this parser that parses string that represents a function,


                  CONSIDER:

                  bool
                  parse_info<> x_parser3(char const* str, FunctionSpec& spec)
                  {
                  // some rules...
                  rettype = lexeme_d[(alpha_p)[color=blue][color=green]
                  >> *(alnum_p | space_p | ch_p('*'))][&do_ret];[/color][/color]

                  // assign function name when match...
                  functionName = lexeme_d[(alpha_p | ch_p('_'))[color=blue][color=green]
                  >> *(alnum_p | ch_p('_'))][assign_a(spec.m _Name)];[/color][/color]
                  // other rules....
                  ....
                  return parse(str,top);
                  }

                  for the functionName rule it was easy, just assign the name.
                  but for the rettype rule its much harder since i should not be assigned
                  directly,
                  since the FunctionSpec struct look like this:

                  strcut FunctionSpec {
                  std::string m_Name;
                  enum eVarType {
                  VAR_VOID, VAR_BOOL, VAR_INT...
                  }
                  eVarType m_RetType;
                  }

                  How can i assign value for 'm_RetType' ? since [do_ret] callback on
                  rettype rule is like this: do_ret[char const*,char const*], is there a
                  way around here?

                  Comment

                  • Phlip

                    #10
                    Re: String Parser using BOOST.Spirit

                    krbyxtrm wrote:
                    [color=blue]
                    > I have this parser that parses string that represents a function,[/color]

                    Your post has two little problems:

                    - it's a new question, so it gets a new Subject
                    - the best place for Boost questions are its mailing lists

                    Just so you understand how topicality works, _I_ don't mind reading Boost
                    traffic here, because it's very important, and I'm not on that mailing
                    list. But _you_ will get the best answer on the narrowest possible
                    technical forum. There are too many people here who don't know enough about
                    Boost to qualify.

                    Try the Boost.Spirit mailing list for this one.

                    --
                    Phlip
                    http://www.greencheese.org/ZeekLand <-- NOT a blog!!!

                    Comment

                    • Gernot Frisch

                      #11
                      Re: String Parser using BOOST.Spirit


                      "Rolf Magnus" <ramagnus@t-online.de> schrieb im Newsbeitrag
                      news:e0rsab$m3v $00$1@news.t-online.com...[color=blue]
                      > Phlip wrote:
                      >[color=green][color=darkred]
                      >>> Gernot Frisch wrote:[/color]
                      >>[color=darkred]
                      >>>> OT, but: What is that % operator used for?[/color]
                      >>
                      >> Awesome expression metatemplate techniques are never off-topic![/color]
                      >
                      > I don't consider them awesome. Actually, it's the worst case of
                      > operator
                      > abuse I've ever seen.[/color]

                      I've used spirit since a very early version. Luckily I didn't choose
                      Lex/Yacc back then! If you get into the syntax of spirit, it's the
                      fastmost-awesome-incredibly-rapid way of programming complex parsers.


                      Comment

                      • krbyxtrm

                        #12
                        Re: String Parser using BOOST.Spirit

                        Do you have an idea on how to let the rules work with something like
                        this:

                        void do_ret()
                        {
                        //something to do here.
                        }
                        x_paser3(char const* str, FunctionSpec& spec/* struct */)
                        {
                        rule<> rettype = (<some rule...>)[<some_thing_sho uld_be_here>]
                        return pase(....)
                        }

                        rettype here returns the matched string in the actual code i made:
                        "void","voi d *","int","unsig ned int" etc.

                        in my current code, i just push this inside a vector... but as i said
                        earlier, the FunctionSpec member m_Rettype should be assigned with
                        defined type
                        defined by enum or any enumerated list...

                        info: FunctionSpec struct is structured representation of the function
                        string...

                        overall i think i need to used a class with operator(), as a
                        replacement for the <some_thing_sho uld_be_here> area in the above code.
                        with that will i pass to that class is the FunctionSpec struct....

                        Ayon kay Gernot Frisch:[color=blue]
                        > "Rolf Magnus" <ramagnus@t-online.de> schrieb im Newsbeitrag
                        > news:e0rsab$m3v $00$1@news.t-online.com...[color=green]
                        > > Phlip wrote:
                        > >[color=darkred]
                        > >>> Gernot Frisch wrote:
                        > >>
                        > >>>> OT, but: What is that % operator used for?
                        > >>
                        > >> Awesome expression metatemplate techniques are never off-topic![/color]
                        > >
                        > > I don't consider them awesome. Actually, it's the worst case of
                        > > operator
                        > > abuse I've ever seen.[/color]
                        >
                        > I've used spirit since a very early version. Luckily I didn't choose
                        > Lex/Yacc back then! If you get into the syntax of spirit, it's the
                        > fastmost-awesome-incredibly-rapid way of programming complex parsers.[/color]

                        Comment

                        • krbyxtrm

                          #13
                          Re: String Parser using BOOST.Spirit

                          ps. problem is that i can't make the operator() work>
                          i tried operator() (char const*,char const*)...

                          Ayon kay krbyxtrm:[color=blue]
                          > Do you have an idea on how to let the rules work with something like
                          > this:
                          >
                          > void do_ret()
                          > {
                          > //something to do here.
                          > }
                          > x_paser3(char const* str, FunctionSpec& spec/* struct */)
                          > {
                          > rule<> rettype = (<some rule...>)[<some_thing_sho uld_be_here>]
                          > return pase(....)
                          > }
                          >
                          > rettype here returns the matched string in the actual code i made:
                          > "void","voi d *","int","unsig ned int" etc.
                          >
                          > in my current code, i just push this inside a vector... but as i said
                          > earlier, the FunctionSpec member m_Rettype should be assigned with
                          > defined type
                          > defined by enum or any enumerated list...
                          >
                          > info: FunctionSpec struct is structured representation of the function
                          > string...
                          >
                          > overall i think i need to used a class with operator(), as a
                          > replacement for the <some_thing_sho uld_be_here> area in the above code.
                          > with that will i pass to that class is the FunctionSpec struct....
                          >
                          > Ayon kay Gernot Frisch:[color=green]
                          > > "Rolf Magnus" <ramagnus@t-online.de> schrieb im Newsbeitrag
                          > > news:e0rsab$m3v $00$1@news.t-online.com...[color=darkred]
                          > > > Phlip wrote:
                          > > >
                          > > >>> Gernot Frisch wrote:
                          > > >>
                          > > >>>> OT, but: What is that % operator used for?
                          > > >>
                          > > >> Awesome expression metatemplate techniques are never off-topic!
                          > > >
                          > > > I don't consider them awesome. Actually, it's the worst case of
                          > > > operator
                          > > > abuse I've ever seen.[/color]
                          > >
                          > > I've used spirit since a very early version. Luckily I didn't choose
                          > > Lex/Yacc back then! If you get into the syntax of spirit, it's the
                          > > fastmost-awesome-incredibly-rapid way of programming complex parsers.[/color][/color]

                          Comment

                          • Jeff Flinn

                            #14
                            Re: String Parser using BOOST.Spirit

                            krbyxtrm wrote:[color=blue]
                            > ps. problem is that i can't make the operator() work>
                            > i tried operator() (char const*,char const*)...[/color]

                            Now your taking advantage of the politeness of the responders here. You've
                            been directed twice to the boost mailing list, which if you read the link
                            provided in an earlier email, states how you can also view the mail list
                            using a news reader. Not only will you get more informed responses as philip
                            stated, but it your posting will help others in similar situations.

                            Jeff Flinn


                            Comment

                            Working...