Best lex/yacc for C++?

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

    Best lex/yacc for C++?

    Hi,

    I am searching for the best lex and yacc combination (or something similar)
    that can be used in combination with C++ and that can contain C++ code. I
    have the regular flex/bison port working but then I, of course, can't use
    C++ constructs in the actions.

    I have tried spirit although I could make a simple command line parser
    (after too many hours), creating a parser tree with it was not possible in
    an easy way (the compiler choked while compiling the include files), so back
    to a lex/yacc clone I guess.

    Does anyone have any opinion (especially if you have or are using some
    implemtation yourself).

    Any help appreciated.

    Regards, Ron AF Greve.



  • Kris Wempa

    #2
    Re: Best lex/yacc for C++?


    "Moonlit" <alt.spam@jupit er.universe> wrote in message
    news:3f842da7$0 $58703$e4fe514c @news.xs4all.nl ...[color=blue]
    > Hi,
    >
    > I am searching for the best lex and yacc combination (or something[/color]
    similar)[color=blue]
    > that can be used in combination with C++ and that can contain C++ code. I
    > have the regular flex/bison port working but then I, of course, can't use
    > C++ constructs in the actions.[/color]

    You can write "wrapper" functions that link to and call the actual C++ code.
    What C++ code are you looking to use in the actions ?


    Comment

    • tom_usenet

      #3
      Re: Best lex/yacc for C++?

      On Wed, 8 Oct 2003 17:30:56 +0200, "Moonlit"
      <alt.spam@jupit er.universe> wrote:
      [color=blue]
      >Hi,
      >
      >I am searching for the best lex and yacc combination (or something similar)
      >that can be used in combination with C++ and that can contain C++ code. I
      >have the regular flex/bison port working but then I, of course, can't use
      >C++ constructs in the actions.
      >
      >I have tried spirit although I could make a simple command line parser
      >(after too many hours), creating a parser tree with it was not possible in
      >an easy way (the compiler choked while compiling the include files), so back
      >to a lex/yacc clone I guess.[/color]

      There is a spirit mailing list I believe:

      Spirit is an object oriented recursive descent parser generator framework implemented using template meta-programming techniques.


      Compatibility information is here:



      Tom

      Comment

      • Moonlit

        #4
        Re: Best lex/yacc for C++?

        Hi,

        Thanks for your reply.

        Ah, they should have linked that on their front page I think. Now I see my
        vc6 is apparently
        not supported (well not the parse trees, but without them ....).

        I tried actually their news server but for some reason my news post won't
        arrive there though I can read the other posts.

        Has someone got experience with some kind of language parser (doesn't have
        to be big but at least with more than say 20 rules) with vc7.1 and spirit.

        I must admit the way you can write your parser just in C++ code appeals to
        me a lot, that's why I spend already so much time on it and I would be
        willing to buy the 7.1 compiler just for that, if I would be at least
        recently confident that I could use it then (with parse trees of reasonable
        size).

        Thanks for your reply again.


        Regards, Ron AF Greve.







        "tom_usenet " <tom_usenet@hot mail.com> wrote in message
        news:44j8ovghf0 pm501togtafjjid 8rlnsa66n@4ax.c om...[color=blue]
        > On Wed, 8 Oct 2003 17:30:56 +0200, "Moonlit"
        > <alt.spam@jupit er.universe> wrote:
        >[color=green]
        > >Hi,
        > >
        > >I am searching for the best lex and yacc combination (or something[/color][/color]
        similar)[color=blue][color=green]
        > >that can be used in combination with C++ and that can contain C++ code. I
        > >have the regular flex/bison port working but then I, of course, can't use
        > >C++ constructs in the actions.
        > >
        > >I have tried spirit although I could make a simple command line parser
        > >(after too many hours), creating a parser tree with it was not possible[/color][/color]
        in[color=blue][color=green]
        > >an easy way (the compiler choked while compiling the include files), so[/color][/color]
        back[color=blue][color=green]
        > >to a lex/yacc clone I guess.[/color]
        >
        > There is a spirit mailing list I believe:
        >
        > https://lists.sourceforge.net/lists/...spirit-general
        >
        > Compatibility information is here:
        >
        > http://www.boost.org/libs/spirit/doc/portability.html
        >
        > Tom[/color]


        Comment

        • Moonlit

          #5
          Re: Best lex/yacc for C++?

          Hi,

          Well amongst other things I would like to fill STL map templates with thing
          like var[ "name" ] = etc, also I would call some classes to log info like
          "Output << Debuginfo << End;" (the "End" is not a typo). But of course other
          things too. In the past I just used a flex/bison scanner and then afterwards
          I parsed the tree and created a tree of classes of it. Yes, that works and
          it is easy to write but it feals like doing everything twice.

          Actually I must admit I have never tried to call C++ from C I know of course
          your can wrap stuff like in extern "C" { FILE* yyin; }. Would extern "C++"
          { CWhatEver *ClassFactory() ; } let you do that the other way around? It
          would be a solution although of course you get a lot of type casting and
          loose the type checking.

          Well its worth trying if there isn't a better solution.

          Thanks for your reply.

          Regards, Ron AF Greve.





          "Kris Wempa" <calmincents(NO _SPAM)@yahoo.co m> wrote in message
          news:bm1hi5$87s 17@kcweb01.netn ews.att.com...[color=blue]
          >
          > "Moonlit" <alt.spam@jupit er.universe> wrote in message
          > news:3f842da7$0 $58703$e4fe514c @news.xs4all.nl ...[color=green]
          > > Hi,
          > >
          > > I am searching for the best lex and yacc combination (or something[/color]
          > similar)[color=green]
          > > that can be used in combination with C++ and that can contain C++ code.[/color][/color]
          I[color=blue][color=green]
          > > have the regular flex/bison port working but then I, of course, can't[/color][/color]
          use[color=blue][color=green]
          > > C++ constructs in the actions.[/color]
          >
          > You can write "wrapper" functions that link to and call the actual C++[/color]
          code.[color=blue]
          > What C++ code are you looking to use in the actions ?
          >
          >[/color]


          Comment

          • Rob Williscroft

            #6
            Re: Best lex/yacc for C++?

            Moonlit wrote in news:3f845c39$0 $58704$e4fe514c @news.xs4all.nl :
            [color=blue]
            > Ah, they should have linked that on their front page I think. Now I
            > see my vc6 is apparently
            > not supported (well not the parse trees, but without them ....).
            >
            > I tried actually their news server but for some reason my news post
            > won't arrive there though I can read the other posts.[/color]

            I belive you have too register for there mailing list's before you
            can post.


            [color=blue]
            >
            > Has someone got experience with some kind of language parser (doesn't
            > have to be big but at least with more than say 20 rules) with vc7.1
            > and spirit.
            >
            > I must admit the way you can write your parser just in C++ code
            > appeals to me a lot, that's why I spend already so much time on it and
            > I would be willing to buy the 7.1 compiler just for that, if I would
            > be at least recently confident that I could use it then (with parse
            > trees of reasonable size).
            >[/color]

            Last time I checked the .NET SDK it came with a version (comandline only)
            of the 7.1 compiler.

            Also see/ask in: news://comp.compilers from what I see the topic is
            mostly parsing.


            Rob.
            --

            Comment

            • Kris Wempa

              #7
              Re: Best lex/yacc for C++?


              "Moonlit" <alt.spam@jupit er.universe> wrote in message
              news:3f845fa7$0 $58706$e4fe514c @news.xs4all.nl ...[color=blue]
              > Hi,
              >
              > Well amongst other things I would like to fill STL map templates with[/color]
              thing[color=blue]
              > like var[ "name" ] = etc, also I would call some classes to log info like
              > "Output << Debuginfo << End;" (the "End" is not a typo). But of course[/color]
              other[color=blue]
              > things too. In the past I just used a flex/bison scanner and then[/color]
              afterwards[color=blue]
              > I parsed the tree and created a tree of classes of it. Yes, that works and
              > it is easy to write but it feals like doing everything twice.
              >[/color]

              The only way you can use maps this way is to either:

              1) create a map instance in a C++ compiler, write C functions to call the
              map methods, give these C functions C linkage with: extern "C" {} and call
              them from the flex/bison generated C program.

              2) write the C only flex/bison code and generate the .c files, manually
              edit the .c files to insert your C++ code, compile the resulting code with a
              C++ compiler

              Step 2 can be messy if your grammar is very large. I haven't looked at
              flex/bison output in a long time, so I don't remember how ugly it can get.
              Step 1 is easier, but you will also lose the ease of using the maps
              directly. You'd essentially be calling a wrapper insert() function instead
              of simply assigning the field/value like you have shown above. In other
              words:

              var[(string) "field"] = ((string) "value");

              would become something like

              mapinsert("fiel d","value");

              I hope these give you some ideas.


              Comment

              • Kris Wempa

                #8
                Re: Best lex/yacc for C++?


                "Kris Wempa" <calmincents(NO _SPAM)@yahoo.co m> wrote in message
                news:bm1tqj$87g 16@kcweb01.netn ews.att.com...[color=blue]
                >
                > 1) create a map instance in a C++ compiler, write C functions to call the[/color]

                Sorry, that should be "in a C++ file" .... not compiler


                Comment

                • Xenos

                  #9
                  Re: Best lex/yacc for C++?


                  "Moonlit" <alt.spam@jupit er.universe> wrote in message
                  news:3f842da7$0 $58703$e4fe514c @news.xs4all.nl ...[color=blue]
                  > Hi,
                  >
                  > I am searching for the best lex and yacc combination (or something[/color]
                  similar)[color=blue]
                  > that can be used in combination with C++ and that can contain C++ code. I
                  > have the regular flex/bison port working but then I, of course, can't use
                  > C++ constructs in the actions.
                  >[/color]

                  I absolutely love ANLTr. It will generate Java, C++, and C#. I use it for
                  C++. It has a steep learning curve, but is very powerful. It will generate
                  LL(k) grammars, and has a lot of neat features like semantic and syntactic
                  predicates. It generates surprising tight code--looks like a hand-generated
                  parser. The parse and lexer creation process is well integrated.





                  DrX.


                  Comment

                  • Moonlit

                    #10
                    Re: Best lex/yacc for C++?

                    Hi,

                    I didn't actually know you could call c from c++ that way, so thanks for the
                    ideas. However I usually build a tree from the input. In C I creates structs
                    (with a number assigned so I know what it represents) in most actions an
                    return pointers to it, what I end up then is a tree that represents the
                    input. Though it is not impossible to do the same using your first method, I
                    think it would be not easy to maintain but indeed you would only have to
                    build a tree once unlike twice as I do it now (first C then convert it to
                    C++).

                    Editing the generated code I would rather not, at some point you always
                    change the grammar and have to redo the changes.

                    Thanks for the reply.

                    Regards, Ron AF Greve.


                    "Kris Wempa" <calmincents(NO _SPAM)@yahoo.co m> wrote in message
                    news:bm1tqj$87g 16@kcweb01.netn ews.att.com...[color=blue]
                    >
                    > "Moonlit" <alt.spam@jupit er.universe> wrote in message
                    > news:3f845fa7$0 $58706$e4fe514c @news.xs4all.nl ...[color=green]
                    > > Hi,
                    > >
                    > > Well amongst other things I would like to fill STL map templates with[/color]
                    > thing[color=green]
                    > > like var[ "name" ] = etc, also I would call some classes to log info[/color][/color]
                    like[color=blue][color=green]
                    > > "Output << Debuginfo << End;" (the "End" is not a typo). But of course[/color]
                    > other[color=green]
                    > > things too. In the past I just used a flex/bison scanner and then[/color]
                    > afterwards[color=green]
                    > > I parsed the tree and created a tree of classes of it. Yes, that works[/color][/color]
                    and[color=blue][color=green]
                    > > it is easy to write but it feals like doing everything twice.
                    > >[/color]
                    >
                    > The only way you can use maps this way is to either:
                    >
                    > 1) create a map instance in a C++ compiler, write C functions to call the
                    > map methods, give these C functions C linkage with: extern "C" {} and call
                    > them from the flex/bison generated C program.
                    >
                    > 2) write the C only flex/bison code and generate the .c files, manually
                    > edit the .c files to insert your C++ code, compile the resulting code with[/color]
                    a[color=blue]
                    > C++ compiler
                    >
                    > Step 2 can be messy if your grammar is very large. I haven't looked at
                    > flex/bison output in a long time, so I don't remember how ugly it can get.
                    > Step 1 is easier, but you will also lose the ease of using the maps
                    > directly. You'd essentially be calling a wrapper insert() function[/color]
                    instead[color=blue]
                    > of simply assigning the field/value like you have shown above. In other
                    > words:
                    >
                    > var[(string) "field"] = ((string) "value");
                    >
                    > would become something like
                    >
                    > mapinsert("fiel d","value");
                    >
                    > I hope these give you some ideas.
                    >
                    >[/color]


                    Comment

                    • Moonlit

                      #11
                      Re: Best lex/yacc for C++?

                      Hi,

                      "Xenos" <dont.spam.me@s pamhate.com> wrote in message
                      news:bm1ubu$meu 4@cui1.lmms.lmc o.com...[color=blue]
                      >
                      > "Moonlit" <alt.spam@jupit er.universe> wrote in message
                      > news:3f842da7$0 $58703$e4fe514c @news.xs4all.nl ...[color=green]
                      > > Hi,
                      > >
                      > > I am searching for the best lex and yacc combination (or something[/color]
                      > similar)[color=green]
                      > > that can be used in combination with C++ and that can contain C++ code.[/color][/color]
                      I[color=blue][color=green]
                      > > have the regular flex/bison port working but then I, of course, can't[/color][/color]
                      use[color=blue][color=green]
                      > > C++ constructs in the actions.
                      > >[/color]
                      >
                      > I absolutely love ANLTr. It will generate Java, C++, and C#. I use it[/color]
                      for[color=blue]
                      > C++. It has a steep learning curve, but is very powerful. It will[/color]
                      generate[color=blue]
                      > LL(k) grammars, and has a lot of neat features like semantic and syntactic
                      > predicates. It generates surprising tight code--looks like a[/color]
                      hand-generated[color=blue]
                      > parser. The parse and lexer creation process is well integrated.
                      >[/color]


                      Ok, I think I have some reading to then. I already went over all the spirit
                      documentation. And despite that my VC6 compiler, generated invalid object
                      files, stack overlows and internal compiler errors.. well apart from that I
                      liked it.

                      But I think I just give antlr a try as well.

                      Thanks for the link and the reply.

                      Regards, Ron AF Greve.
                      [color=blue]
                      >
                      > http://www.antlr.org
                      >
                      >
                      > DrX.
                      >
                      >[/color]


                      Comment

                      • David B. Held

                        #12
                        Re: Best lex/yacc for C++?

                        "Moonlit" <alt.spam@jupit er.universe> wrote in message
                        news:3f845c39$0 $58704$e4fe514c @news.xs4all.nl ...[color=blue]
                        > [...]
                        > I must admit the way you can write your parser just in C++
                        > code appeals to me a lot, that's why I spend already so
                        > much time on it and I would be willing to buy the 7.1
                        > compiler just for that, if I would be at least recently
                        > confident that I could use it then (with parse trees of
                        > reasonable size).[/color]

                        If you're willing to switch compilers, try gcc/cygwin.
                        Unless your grammar is very large. Then compile times
                        or instantiation depth might slay you. You can always
                        break your grammar up into parts, which helps, but Spirit
                        can't really compete with traditional compiler compilers
                        for very large projects. For small and medium size stuff,
                        though, it's very very sweet. I'm using the ASTs with gcc,
                        and while they aren't as well-developed as they could be,
                        it's amazing how much power you get from just a few
                        lines of code. It's almost like coding in VB without all
                        the nasty stuff you get with VB.

                        Dave



                        ---
                        Outgoing mail is certified Virus Free.
                        Checked by AVG anti-virus system (http://www.grisoft.com).
                        Version: 6.0.521 / Virus Database: 319 - Release Date: 9/23/2003


                        Comment

                        • Moonlit

                          #13
                          Re: Best lex/yacc for C++?

                          Hi,

                          "David B. Held" <dheld@codelogi cconsulting.com > wrote in message
                          news:bm36vo$o8t $1@news.astound .net...[color=blue]
                          > "Moonlit" <alt.spam@jupit er.universe> wrote in message
                          > news:3f845c39$0 $58704$e4fe514c @news.xs4all.nl ...[color=green]
                          > > [...]
                          > > I must admit the way you can write your parser just in C++
                          > > code appeals to me a lot, that's why I spend already so
                          > > much time on it and I would be willing to buy the 7.1
                          > > compiler just for that, if I would be at least recently
                          > > confident that I could use it then (with parse trees of
                          > > reasonable size).[/color]
                          >
                          > If you're willing to switch compilers, try gcc/cygwin.
                          > Unless your grammar is very large. Then compile times
                          > or instantiation depth might slay you. You can always
                          > break your grammar up into parts, which helps, but Spirit
                          > can't really compete with traditional compiler compilers
                          > for very large projects. For small and medium size stuff,
                          > though, it's very very sweet. I'm using the ASTs with gcc,
                          > and while they aren't as well-developed as they could be,
                          > it's amazing how much power you get from just a few
                          > lines of code. It's almost like coding in VB without all
                          > the nasty stuff you get with VB.[/color]

                          Yes, it appeared to me that with spirit you could do things with just a few
                          lines of code and without the hassle of first compiling with bison and flex.
                          Unfortunately the ast stuff doesn't work for vc6.

                          I tried the gcc/cygwin solution one day (I do use g++ for unix platforms),
                          many years ago and things might have improved. But at that time, though I
                          could create a window, it really was lacking the IDE that goes with gcc. I
                          know there is ddd and such but VC6's IDE is real easy to use. It really
                          reduces the development time a lot (not to mention all the documentation
                          that you get with it)

                          Thanks for your reply,

                          Ron AF Greve.
                          [color=blue]
                          >
                          > Dave
                          >
                          >
                          >
                          > ---
                          > Outgoing mail is certified Virus Free.
                          > Checked by AVG anti-virus system (http://www.grisoft.com).
                          > Version: 6.0.521 / Virus Database: 319 - Release Date: 9/23/2003
                          >
                          >[/color]


                          Comment

                          • Jerry Coffin

                            #14
                            Re: Best lex/yacc for C++?

                            In article <3f842da7$0$587 03$e4fe514c@new s.xs4all.nl>,
                            alt.spam@jupite r.universe says...[color=blue]
                            > Hi,
                            >
                            > I am searching for the best lex and yacc combination (or something similar)
                            > that can be used in combination with C++ and that can contain C++ code. I
                            > have the regular flex/bison port working but then I, of course, can't use
                            > C++ constructs in the actions.[/color]

                            Flex (at least in recent versions) produces output that can be compiled
                            as C++. In fact, with the "-+" flag, it'll produce real C++ output
                            (i.e. the parser as a class instead of just C that happens to be
                            compilable as C++).

                            BYACC produces output that can be compiled as C++, and I've used C++
                            specific "stuff" in actions, including using a map for a symbol table,
                            much as you've mentioned.

                            One thing I should point out is that when I'm using C++, I generally do
                            not use %union and the built-in type system -- instead, I normally use
                            the C++ type system, typically with a polymorphic type.

                            --
                            Later,
                            Jerry.

                            The universe is a figment of its own imagination.

                            Comment

                            • Moonlit

                              #15
                              Re: Best lex/yacc for C++?

                              Hi,

                              I knew some flex implementations could do C++ (and some bison). However I
                              hoped there was some strong opionion on what to use on what not.

                              Apparently, there isn't one very good solution. I haven't made up my mind
                              yet but willl add byacc to it.

                              For mysef I made the following list:

                              spririt very tight code, is C++ code, no extra dependecies, know how it
                              works, unfortunately only got very small, simple command line parsers
                              working (migh be due to my VC6 version, certainly have to buy vc7.1 then)

                              Antlr Don't know anything about this, seems widely supported for different
                              languages, user java to generate code (I think).

                              Flex++/Bison++/byacc different versions on the net, lot of experience with
                              flex/bison so probably the most easy to use, more code than with spirit but
                              very likely more compatible with any compiler arround..

                              Hmmm, still difficult to choose, ok at some point I just have to pick one I
                              guess.

                              I just decided to order my copy of vc++ 2003, hope it works with spirit and
                              well, I could always backtrack to flex++/byacc/bison++ if not.


                              Thanks for your help.

                              Regards, Ron AF Greve.


                              "Jerry Coffin" <jcoffin@taeus. com> wrote in message
                              news:MPG.19ef2a cab07fe68f989b6 5@news.clspco.a delphia.net...[color=blue]
                              > In article <3f842da7$0$587 03$e4fe514c@new s.xs4all.nl>,
                              > alt.spam@jupite r.universe says...[color=green]
                              > > Hi,
                              > >
                              > > I am searching for the best lex and yacc combination (or something[/color][/color]
                              similar)[color=blue][color=green]
                              > > that can be used in combination with C++ and that can contain C++ code.[/color][/color]
                              I[color=blue][color=green]
                              > > have the regular flex/bison port working but then I, of course, can't[/color][/color]
                              use[color=blue][color=green]
                              > > C++ constructs in the actions.[/color]
                              >
                              > Flex (at least in recent versions) produces output that can be compiled
                              > as C++. In fact, with the "-+" flag, it'll produce real C++ output
                              > (i.e. the parser as a class instead of just C that happens to be
                              > compilable as C++).
                              >
                              > BYACC produces output that can be compiled as C++, and I've used C++
                              > specific "stuff" in actions, including using a map for a symbol table,
                              > much as you've mentioned.
                              >
                              > One thing I should point out is that when I'm using C++, I generally do
                              > not use %union and the built-in type system -- instead, I normally use
                              > the C++ type system, typically with a polymorphic type.
                              >
                              > --
                              > Later,
                              > Jerry.
                              >
                              > The universe is a figment of its own imagination.[/color]


                              Comment

                              Working...