CSE411

CSE411: Introduction to Compilers

Introduction to Parsing

Jooyong Yi (UNIST)

2026 Fall
CSE411

Parsing

        position  =  initial  +  rate   *   60
                           ↓
        |------------------------------------|
        |      Lexical Analyzer (Lexer)      |
        |------------------------------------|
                           ↓
        <id, 1> <=> <id, 2> <+> <id, 3> <*> <int, 60>
                           ↓
        |------------------------------------|
        |      Syntax Analyzer (Parser)      |
        |------------------------------------|
                           ↓
                       =
                      / \
                <id,1>   +
                        / \
                   <id,2>  *
                          / \
                    <id,3>   <int,60>                   
2026 Fall
CSE411

Why a Tree?

2026 Fall
CSE411

Why a Tree?


            IfStmt                                  
        /           \                             
      Cond          Then                      
        |             |                       
      BinExp        Assign                    
      /  | \       /       \                   
     ID OP   Num   Left     Right                
     |   |   |     |         | 
     x   >    0    ID      BinExp
                   |     /    |     \    
                   y   Left  Op  Right
                        |     |      |
                        ID    +      Num
                        |            |
                        x            1
2026 Fall
CSE411

Parse Tree and Abstract Syntax Tree (AST)

          Parse Tree                                  AST

            IfStmt                                  IfStmt
        /           \                             /       \
      Cond          Then                      BinOp(>)     Assign
       |             |                          /  \      /   \
      BinOp        Assign                      x    0    y   BinOp(+)
      /  \       /       \                                   /  \
     ID  Num   Left     Right                               x    1
     |    |     |         | 
     x    0     ID      BinExp
                |    /    |      \    
                y   Left  BinOp  Right
                     |    |       |
                     ID   +       Num
                     |            |
                     x            1
2026 Fall
CSE411

How to write a parser to generate a parse tree?

2026 Fall
CSE411

Recall how a lexer is obtained

RE -> NFA -> DFA

2026 Fall
CSE411

What to give as the specification for a parser?

2026 Fall
CSE411

What to give as the specification for a parser?

Recall that a parser is also known as a syntax analyzer, which detects syntactic errors.

2026 Fall
CSE411

Is a regular expression sufficient to describe the syntax of a programming language?

2026 Fall
CSE411

INT: 0 | [1-9] [0-9]*
VALID: INT

1  : valid
10 : valid
5  : valid
0  : valid
01 : invalid
2026 Fall
CSE411

INT : 0 | [1-9] [0-9]*
PLUSEXP:
VALID: INT | PLUSEXP

1+1   : valid
10+5  : valid
5+0   : valid
0+01  : invalid
01+55 : invalid
2026 Fall
CSE411

INT : 0 | [1-9] [0-9]*
PLUSEXP: INT '+' INT
VALID: INT | PLUSEXP

1+1   : valid
10+5  : valid
5+0   : valid
0+01  : invalid
01+55 : invalid
2026 Fall
CSE411

INT : 0 | [1-9] [0-9]*
PLUSEXP: INT '+' INT
PARENEXP: '(' PLUSEXP ')'
VALID: INT | PLUSEXP | PARENEXP

(1+1)   : valid
(10+5)  : valid
(5+0)   : valid
(0+01)  : invalid
(01+55) : invalid
2026 Fall
CSE411

INT : 0 | [1-9] [0-9]*
PLUSEXP: INT '+' INT
PARENEXP: '(' PLUSEXP ')'
VALID: INT | PLUSEXP | PARENEXP

(1+(10+5)) : valid
(10+5))    : invalid

How to change the regular expression?

2026 Fall
CSE411

INT : 0 | [1-9] [0-9]*
PLUSEXP: EXP '+' EXP
PARENEXP: '(' EXP ')'
EXP: INT | PLUSEXP | PARENEXP
VALID: EXP

(1+(10+5)) : valid
(10+5))    : invalid
2026 Fall
CSE411

INT : 0 | [1-9] [0-9]*
PLUSEXP: EXP '+' EXP
PARENEXP: '(' EXP ')'
EXP: INT | PLUSEXP | PARENEXP
VALID: EXP

(1+(10+5)) : valid
(10+5))    : invalid

Is this a regular expression?

2026 Fall
CSE411

Context-Free Grammar (CFG)

INT → 0 | [1-9] [0-9]*
PLUSEXP → EXP '+' EXP
PARENEXP → '(' EXP ')'
EXP → INT | PLUSEXP | PARENEXP
VALID → EXP
  • CFG
    • : a finite set of non-terminals (e.g., VALID, EXP, PARENEXP, PLUSEXP).
    • : a finite set of terminal symbols (e.g., '(', ')', '+', INT)
    • : a start non-terminal (e.g., VALID)
    • : a finite set of productions.
      • Each production has the form
        • where and
2026 Fall
CSE411

What does "context-free" mean?

  • CFG
    • : a finite set of non-terminals (e.g., VALID, EXP, PARENEXP, PLUSEXP).
    • : a finite set of terminal symbols (e.g., '(', ')', '+', INT)
    • : a start non-terminal (e.g., VALID)
    • : a finite set of productions.
      • Each production has the form
        • where and
2026 Fall
CSE411

Context-Free Grammar vs. Context-Sensitive Grammar

  • CFG , CSG
    • : a finite set of non-terminals (e.g., exp).
    • : a finite set of terminals (e.g., '(', ')', PLUS)
    • : a start non-terminal (e.g., start)
    • : a finite set of productions.
      • CFG
        • Each production has the form
          • where and
      • CSG
        • Each production has the form
          • where , and
2026 Fall
CSE411

Recall

  • Regular expression specifies what to recognize as legitimate tokens.
  • There exists a NFA that recognizes the tokens specified by .
  • Given an NFA , there exists a DFA that recognizes the same language as .
2026 Fall
CSE411

Recall

  • What: regular expression
  • How: NFA and DFA
2026 Fall
CSE411
  • What: CFG specifies what to recognize as syntactically valid programs.
  • How:
2026 Fall
CSE411
  • What: CFG specifies what to recognize as syntactically valid programs.
  • How: Non-deterministic Pushdown Automata (NPDA)
2026 Fall
CSE411

CFG and NPDA

Theorem: For any context-free grammar , there exists an NPDA accepting .

2026 Fall
CSE411

NPDA

Automaton with a stack

2026 Fall
CSE411

NPDA Example

INT → 0 | [1-9] [0-9]*
PLUSEXP → EXP '+' EXP
PARENEXP → '(' EXP ')'
EXP → INT | PLUSEXP | PARENEXP
START → EXP

2026 Fall
CSE411

2026 Fall
CSE411

2026 Fall
CSE411

2026 Fall
CSE411

2026 Fall
CSE411

2026 Fall
CSE411

2026 Fall
CSE411

2026 Fall
CSE411

2026 Fall
CSE411

2026 Fall
CSE411

2026 Fall
CSE411

2026 Fall
CSE411

Non-deterministic PDA (NPDA)

  • NPDA
    • : a finite set of states
    • : the input alphabet
    • : the stack alphabet
    • : the transition function
    • : the initial state
    • $ : stack start symbol
    • : set of final states
2026 Fall
CSE411

Acceptance Condition of NPDA

  • Suppose
  • if and
    1. When all symbols of the input are consumed, the final state is reached.
    2. The state of the stack is irrelevant to the acceptance.
2026 Fall
CSE411

Deterministic PDA (DPDA)

  • DPDA
    • : a finite set of states
    • : the input alphabet
    • : the stack alphabet
    • : the transition function
      • Note: If is defined, then should not be defined for every .
    • : the initial state of the control unit
    • $ : stack start symbol
    • : the set of final states
2026 Fall
CSE411

Is it a DPDA?

2026 Fall
CSE411

DPDA Example

'#' indicates the end of the input.

2026 Fall
CSE411

NPDAs are more expressive than DPDAs.

2026 Fall
CSE411

NPDAs are more expressive than DPDAs.

  • Multiple states in an NFA ⟼ a single state in a DFA (possible)
  • Multiple stack configurations in an NPDA ⟼ a single stack configuration in a DPDA (not always possible)
2026 Fall
CSE411

CFG NPDA DPDA

  • This does not work.
  • We need a different approach to obtain a deterministic parser.
2026 Fall
CSE411

Science and Engineering

  • Tony Hoare (Turing Award Laureate, 1980) once said something along these lines:
    • Engineers seek the 'it works!' moment, while scientists seek the 'Aha!' moment.
  • Which department are you in?
2026 Fall