[cfe-dev] Understanding Clang parsing

Charles Davis cdavis at mymail.mines.edu
Wed Mar 3 19:19:10 PST 2010


On 3/3/10 7:57 PM, kalyan ponnala wrote:
> Thanks for the support Charles.
> Could you tell me a better way to understand how parser works. Or is
> "steping into" the code is the only solid way to understand it.
Clang's Parser class works like this:
1. Someone calls ParseTopLevelDecl() to parse a top-level declaration
from the source.
2. ParseTopLevelDecl() calls other methods inside the Parser class to
parse fragments of the source according to the grammar as defined in the
C and C++ standards. Most functions get called indirectly; for example,
when the Parser encounters a function declaration, the
ParseFunctionDeclaration() method gets called.
3. These parsing methods notify some object through the "Action"
interface every time something gets parsed. The default Action object
does nothing. The Semantic Analyzer (another really important part of
the compiler), or just "Sema" for short, provides an implementation
which not only builds the AST but also annotates it with types and
checks the semantics of the program specified by the source code.
4. Steps 1-3 are repeated for every top-level declaration in the
translation unit (a source file plus all its included headers).
5. When it does hit the end of the unit, ParseTopLevelDecl() calls
Action::ActOnEndOfTranslationUnit(). This lets the object on the other
side know that the translation unit has ended, and it's time to do
whatever. The Sema implementation invokes an ASTConsumer object with
this information.
> I would like to know some important files inside the solution file which
> are responsible for building the AST.
The Sema library is your best bet. Study it; that's where I started with
Clang. Trust me: it's one of the easiest parts of the compiler to
understand. Like most of Clang, it was designed to be relatively easy to
understand, even for someone with little or no prior experience with
compiler technology. In particular, look at lib/Sema/SemaDecl.cpp and
lib/Sema/SemaExpr.cpp .

Also, take a look at include/clang/Parse/Action.h . This is the file
that defines the Action interface.
> Thanks.
No problem.

Chip




More information about the cfe-dev mailing list