6

There are different stages of compilation

Such as

  • Preprocessing stage
  • Syntax analysis
  • Semantic analysis
  • Intermediate Code generation
  • Machine Code generation
  • Optimization
  • Linking

My question here is in which stage of compilation "typedef" is resolved.

7
  • It depends upon what you call "syntax analysis" and "semantic analysis" but it is one of them. Also, your use of "resolved" is strange... Aug 26, 2014 at 11:52
  • You mean to say it depends on compiler??
    – Adarsh
    Aug 26, 2014 at 11:54
  • 1
    No, I mostly mean that syntax analysis and semantic analysis are imprecise wording. What one calls "syntax analysis" may be called "semantic analysis" by another person. Read about symbol tables; and what "typedef" is resolved really means is also very ambiguous. Aug 26, 2014 at 11:55
  • Syntax analysis breaks down your code into token to generate the parse tree and Semantic analysis does the type checking. How could they both be same?
    – Adarsh
    Aug 26, 2014 at 11:59
  • 2
    Because it is not that simple. In practice, parsing (that is, building some AST) requires a lot of "semantics" (managing the symbol table is definitely more "semantics" than "syntax") .... And in practice, programming languages are not context-free languages but are parsed using context-free techniques mixed to a lot of ad-hoc semantics. Aug 26, 2014 at 12:00

2 Answers 2

6

C and C++ don't have context-free grammars. Some of their grammar rules are ambiguous without knowing whether particular identifier identifies a type or not. Therefore the "syntactic" and "semantic" analysis are intermingled. Each statement that is parsed is immediately checked to see whether it introduces any new types and the information is fed back into the lexer so it can classify tokens in reads as either identifiers or type-identifiers. Templates present similar ambiguity with < and > tokens, so template declarations also need to be fed back to lexer.

This also explains why dependent names in templates have to be annotated with typename and/or template keywords. Compilers that build complete abstract syntax tree of templates when they are declared can't do so without knowing which identifiers will identify types and/or templates and they don't know yet when those identifiers depend on the template parameters.

3

The meaning of typedef is defined in the standard at n1256 S6.7.7. It says:

A typedef declaration does not introduce a new type, only a synonym for the type so specified.

In other words, a typedef is complete and meaningful as soon as the declaration is complete. Nothing is left to be analysed later.

Although giving things meanings is part of semantic analysis, this particular action must take place during syntactic analysis, because this information is used later to choose between possibly ambiguous syntactic interpretations. For example S6.7.3:

If the same qualifier appears more than once in the same specifier-qualifier-list, either directly or via one or more typedefs, the behavior is the same as if it appeared only once.

So the answer is: during syntax analysis, with the reservation that this might be taken to overlap with semantic analysis.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.