4

I'm trying to understand the ins and outs of Clang, and I'm not really sure about the "Sema" library. Is the semantic analysis in the path the compiler takes to compile a program? Or is it only used by the programmer to analyze his/her code?

From what I gather, the parser builds an AST, then there are "AST consumers" that use the AST to do different things. So, the code generation library turns the AST into IR. And the semantic analysis library uses the AST to analyze the code. Is this understanding correct, or is the semantic analyzer also used for compiling?

  • 1
    Clang is somewhat a weird thing: Sema does not only check the AST and insert implicit casts/declarations/whatever, but it is also responsible for building the AST itself. It is not a very typical arrangement for a compiler. – SK-logic Jul 18 '12 at 07:58
  • @SK-logic It's responsible for building the AST? But what does the parser do? And how does the parser communicate its findings with the semantic analyzer? –  Jul 18 '12 at 12:23
  • 1
    parser is calling Sema straight away, for each complete expression or statement. There are some intermediate structures involved (e.g., for representing parsed but not yet resolved types), but the final Clang AST is produced by Sema. – SK-logic Jul 18 '12 at 12:28
  • @SK-logic That's a pretty neat idea. So if the parser alone builds the AST, would "parse tree" be a better term? –  Jul 18 '12 at 12:40
  • yes, it would often be called a "parse tree", with all the further transformed intermediate representations being called "ASTs". Although this difference is quite vague. In clang, there is no distinct parse tree, it builds a semantically verified (and somewhat transformed) AST straight away (supposedly for performance reasons). – SK-logic Jul 18 '12 at 12:52
  • @SK-logic So in a more conventional compiler, the semantic analyzer would get a parse tree, then resolve the identifiers to variables, insert the implicit casts, etc? Then the resulting AST would go to the code generator? –  Jul 18 '12 at 13:06
  • Yes, most of the compilers will have separate parsing and semantic analysis passes, at least with a top level statements granularity (structures/functions in C). – SK-logic Jul 18 '12 at 13:10
  • @SK-logic Okay, thanks for answering my questions, it's been really helpful. –  Jul 18 '12 at 13:12

1 Answers1

3

Semantic analysis is part of compile analysis process, usually coming after lexical and syntax analysis. Semantic analyzer checks validity of used data types, does type casting etc, and reports errors if there are some.

In other words, when it comes to semantic analysis, compiler is already sure that valid words are used in program (lexical), and that sentences are built correctly, according to given grammar of language(syntax). There is only left to check if those sentences make sense - checking data types, return values, size boundaries, uninitialized variables, etc.

My knowledge of compile process is more general rather than specific about Clang, but I think semantic analysis is definitely present in the code analysis.

MatijaSh
  • 482
  • 4
  • 13
  • 1
    That would be correct if C++ syntax was completely independent of types. Unfortunately, you cannot even parse `a * b;` correctly without knowing if `a` is a type or not. – fredoverflow Jun 04 '16 at 14:55