I wanna know what type of parser is used in the gcc and turbo c++ compilers. whether a top-down or bottom-up parser is used..... ?

Recommended Answers

All 5 Replies

parsers start at the top of the program (line 1) and work themselves down. The bottom up approach would be a little like you trying to read a book backwards.

I wanna know what type of parser is used in the gcc and turbo c++ compilers. whether a top-down or bottom-up parser is used..... ?

GCC uses a hand written recursive descent parser (ie. it's top-down). I wouldn't be surprised if Turbo C++ worked the same way, though it might use one of the parser generators rather than a hand written approach.

With compilers in general, we often talk about the front-end and the back-end. The front-end is the program that parses the source code, verifies all the grammar and syntax rules, and basically produces a translation of it in some intermediate language (not human-readable but readable for the back-end). The back-end takes the intermediate source code and performs most of the optimizations and then compiles to native machine code. So, the back-end is usually common to an entire compiler suite (like GCC, ICC, Clang, etc.), and then there is one specific front-end for each programming language supported by the compiler suite.

Examples:
- Borland's Delphi and C++Builder compilers share the same back-end (meaning you can also link C++ object files ".obj" with Delphi compiled units ".dcu" together).
- All GCC compilers (C, C++, Objective-C, Ada, Fortran, Java, etc.) share the same back-end, but with different front-ends.

A prominent back-end is LLVM which is used by the Clang front-ends and other compilers.

Prominent front-ends are those of EDG, which are used by Intel and Comeau.

Whatever front-end, parsing the C++ language is not an easy task, because its grammar is complex and context-dependent. Recursive descent parsers are used with plenty of modifications to really get reliable and accurate parsing. There are also tools for generating parsers, including YACC and Boost.Spirit.

GCC uses a hand written recursive descent parser (ie. it's top-down). I wouldn't be surprised if Turbo C++ worked the same way, though it might use one of the parser generators rather than a hand written approach.

Thanks a lot!! That was very helpful... :)

And thanks to everyone who replied :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.