CSE411

CSE411: Introduction to Compilers

Abstract Syntax Tree

Jooyong Yi (UNIST)

2026 Fall
CSE411

Where We Are

         |-------|            |--------|        
Source → | Lexer | → tokens → | Parser | → parse
Program  |       |            |        |   tree 
         |-------|            |--------|        
                                                        
2026 Fall
CSE411

Semantic Analyzer

  • Given a parse tree, the semantic analyzer detects semantic errors such as type errors.
                       =
                      / \
                <ID,1>   +
                        / \
                   <ID,2>  *
                          / \
                    <ID,3>   <INT,60>           
                           ↓                    
        |------------------------------------|
        |  Semantic Analyzer (Type Checking) |
        |------------------------------------|
                           ↓                            
                       =
                      / \
                <ID,1>   +
                  INT   INT
                        / \
                   <ID,2>  *
                     INT   INT
                          / \
                    <ID,3>   <INT,60>
                     INT       INT

2026 Fall
CSE411

IR (Intermediate Representation) Generator

  • At this phase, we do not restrict ourselves to a specific hardware architecture.
                       =
                      / \
                <ID,1>   +
                  INT   INT
                        / \
                   <ID,2>  *
                     INT   INT
                          / \
                    <ID,3>   <INT,60>
                     INT       INT
                           ↓                            
        |------------------------------------|
        |            IR Generator            |
        |------------------------------------|
                           ↓                            
                       t1 = 60
                       t2 = ID3 * t1
                       t3 = ID2 + t2
                       ID1 = t3
2026 Fall
CSE411
E → E + T | T
T → T * F | F
F → (E) | ID | INT
                                    E
x + y * 60    --------->        /   |   \
                               E    +    T
                               |       / | \  
                               T      T  *  F
                               |      |     |
                               F      F     INT
                               |      |
                               ID    ID
2026 Fall
CSE411
E → E + T | T
T → T * F | F
F → (E) | ID | INT
                                    E                                  BinExp(Op: +)
x + y * 60    --------->        /   |   \        --------->            /           \
                               E    +    T                           ID(x)      BinExp(Op: *)
                               |       / | \                                     /       \
                               T      T  *  F                                  ID(y)     INT(60)
                               |      |     |
                               F      F     INT
                               |      |
                               ID    ID
2026 Fall
CSE411
E → E + T | T
T → T * F | F
F → (E) | ID | INT
                               Parse Tree                          
                        Concrete Syntax Tree (CST)               Abstract Syntax Tree (AST)

                                    E                                  BinExp(Op: +)
x + y * 60    --------->        /   |   \        --------->            /           \
                               E    +    T                           ID(x)      BinExp(Op: *)
                               |       / | \                                     /       \
                               T      T  *  F                                  ID(y)     INT(60)
                               |      |     |
                               F      F     INT
                               |      |
                               ID    ID
2026 Fall
CSE411

How do we convert a parse tree to an AST?

2026 Fall
CSE411

Rule-based conversion from a parse tree to an AST

Production Rule
E → E + T E.node = BinExp('+', E.node, T.node)
E → T E.node = T.node
T → T * F BinExp('*', T.node, F.node)
T → F T.node = F.node
F → ( E ) F.node = E.node
F → ID F.node = ID(/*input corresponding to ID token*/)
F → INT F.node = INT(/*input corresponding to INT token*/)
2026 Fall
CSE411

Rule-based conversion from a parse tree to an AST

                                                  Parse Tree                          
                                           Concrete Syntax Tree (CST)               Abstract Syntax Tree (AST)
E: E '+' T { $$ = BinExp('+', $1, $3); }
 | T       { $$ = $1; }                                E                                  BinExp(Op: +)
 ;                                                 /   |   \        --------->            /           \
T: T '*' F { $$ = BinExp('*', $1, $3); }          E    +    T                           ID(x)      BinExp(Op: *)
 | F       { $$ = $1; }                           |       / | \                                     /       \
 ;                                                T      T  *  F                                  ID(y)     INT(60)
F: '('E')' { $$ = $2; }                           |      |     |
 | id      { $$ = ID($1); }                       F      F     INT  
 | int     { $$ = INT($1); }                      |      |
 ;                                                ID     ID
2026 Fall
CSE411

Applications of ASTs

  • Type checking
  • IR generation
2026 Fall
CSE411

Applications of ASTs

  • Program analysis, including type checking
  • Program transformation, including IR generation
  • Code interpretation
2026 Fall
CSE411

Code Interpretation Using ASTs


            BinExp(Op: +)
            /           \
         ID(x)      BinExp(Op: *)
                     /       \
                  ID(y)     INT(60)
2026 Fall
CSE411

Code Interpretation Using ASTs

                   If
               /       \
      BinExp(Op: <)     Assign
          /    \        /   \
       ID(x)  INT(0)  ID(y) INT(1)
2026 Fall
CSE411

Program Analysis Using ASTs

  • Example: MISRA C
    • A set of coding guidelines designed to improve code safety and reliability in embedded systems
  • Example rules
    • Do not use goto
    • Every switch statement must have a default case.
    • Do not ignore the return value of a non-void function.
      • e.g., In f(); x = f();, the first call for f should not be allowed.
2026 Fall
CSE411

Program Transformation Using ASTs

  • Code generation (AST -> IR)
  • Source code instrumentation (AST -> AST')
  • Transpilation (source-to-source compilation)
2026 Fall

![width:400px](img/overview/semantic-analyzer.png)