5

I think Bison and Yacc are very often used for parsing grammars of programming languages. (and lex/flex for tokenizing...)

My Question is: Are all compilers made with this tools or are there people who write their parsers from scratch? (I do it mostly without "compiler compilers", but I know them)

Is it "luctrative" to build parsers without these tools?

Are there alternatives for yacc/bison and lex/flex that are more "open" and not so strict. Are libraries for C existing to do that for me (the parsing)? Is there another option to build parsers without yacc but also not to write it from scratch?

Best regards Lukas

lukkkkkas
  • 61
  • 1
  • 2
  • 2
    There are lots of parser generators out there. All of them have been used for something and probably most of them continue to be used.. Sometimes for a very simple syntax it is just easier to parse a domain specific language (often a configuration file of sorts) by hand written code. – Ken Clement Dec 27 '15 at 00:19
  • 1
    Nope. Consider JavaCC and ANTLR. Some people roll their own parser generators. – erip Dec 27 '15 at 00:38
  • 1
    There are lots of parser generators out there. Here's a partial list: https://en.wikipedia.org/wiki/Comparison_of_parser_generators. – Ira Baxter Dec 31 '15 at 12:44
  • SO closers: I don't think this question asked for resource or a recommendation. He is asking about what is the state of the practice. I think your close request inappropriate. (Vote to re-open). – Ira Baxter Dec 31 '15 at 12:45

3 Answers3

3

Are all Parsers made with yacc or bison (and lex/flex)?

No. For example, GCC don't use them.

c - Are GCC and Clang parsers really handwritten? - Stack Overflow

Community
  • 1
  • 1
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
3

Many high performance parsers for big ticket products, like C++ compilers are handwritten, or at least pre generated by a tool. and then modified by hand.

Unfortunately, this significantly increases maintenance effort and introduces a much larger possibility for bugs.

A well written grammar and a well chosen parser generator tool can yield performance almost as good, or in many cases, as good as a handwritten parser.

For this reason, a parser generator is preferable if you don't have the development and testing resources that a larger organization might.

There are many parsing and lexing tools available, both general and specialized depending on platform, parser type (LLk, LALR(1), etc), and output language.

There are also general parsing engines like Programmar that take an input grammar and interpret it at runtime in order to allow an app to analyze inputs that conform to that grammar.

Gold Parser is a free and easy to use LALR(1) parser generator that runs on Windows operating systems.

The Spirit Framework ships with the C++ Boost library and uses a combination of preprocessing macros and clever generics to allow the developer to produce a grammar in a C++ file that is compiled by the C++ compiler itself despite looking quite a bit like a standard EBNF grammar file. Naturally, its output targets C++

Elk is a toolkit for creating C++ based GLR parsers. GLR is a particularly efficient and flexible type of parser often used for complex grammars, like C++ itself.

There are many other tools out there, including ANTLR and ACCENT, each of which have their own strengths and weaknesses.

  • 2
    See http://stackoverflow.com/questions/6319086/are-gcc-and-clang-parsers-really-handwritten for a discussion about using grammars effectively for complex languages such as C++. – Ira Baxter Dec 31 '15 at 12:42
3

In grad school I wrote a C compiler using an LRR parser that was built from the ground up -- first generated the table and then wrote an engine to use it.

This was not hard to do and is quite well explained in the literature. I suggest you read the dragon book if you want to learn more.

https://en.wikipedia.org/wiki/Compilers:_Principles,_Techniques,_and_Tools

Hogan
  • 69,564
  • 10
  • 76
  • 117