Maurice's Notes
Blog
Low Level Computing
Low Level Computing
  • Operating Systems
    • General Operating Systems
      • OS Structure
      • Main Memory
        • Basic Hardware
        • Address Binding
        • Memory Address Register
      • Booting
        • MBR (Master Boot Record)
        • Global Descriptor Table
      • Direct Memory Access (DMA)
        • DMA
      • Processes
        • Basics
        • Process Scheduling
    • Linux Operating System
      • Linker Scripts
      • Position Independent Code/Executable
      • Relocation
      • Understanding PLT and GOT
    • Windows Operating System
      • Page 1
    • Real-Time Embedded Systems
      • Real-Time Scheduling
        • Cyclic Executive
  • Computer Architecture
    • Architecture Fundamentals
      • Introduction
      • Cache Basics
      • Cache Memory
      • A Few CPU Formulas
    • RISC Architectures
      • ARM
        • ARM Design Philosophy
        • RISC Review
        • Exceptions, Interrupts, & Vector Table
        • ARM Pipelines
        • ARM Registers
        • ARM Branch Instructions
        • ARM CSPR (Instructions)
        • ARM Data Processing Instructions
        • Load/Store Instructions
        • Profiling Cycle Counter
        • Compiler Optimizations
      • RISCV
    • CISC Architectures
    • Cache Coherency
      • Basic Introduction
      • Memory Sequential Consistency
  • Exploits
    • Walkthrough: Return-to-Libc
    • Access Physical Memory
  • Compilers & Runtime Systems
    • Introduction
      • Programming Language Basics
      • Static Scope
    • Syntax Translation
      • Syntax Defined
      • Parsing
    • Algorithms
      • FIRST FOLLOW (Top-Down) Parsing
      • Building a Recursive Descent Parser
      • Construction: Regular Expression -> NFA
Powered by GitBook
On this page

Was this helpful?

  1. Compilers & Runtime Systems
  2. Algorithms

FIRST FOLLOW (Top-Down) Parsing

PreviousAlgorithmsNextBuilding a Recursive Descent Parser

Last updated 7 months ago

Was this helpful?

For FIRST and FOLLOW, we keep applying rules until nothing else can be added to the FIRST and FOLLOW sets.

FIRST(α)FIRST(\alpha)FIRST(α)

The set of terminals that BEGIN strings derived from α\alphaα. If α\alphaα is ϵ\epsilonϵ or generates ϵ\epsilonϵ, then ϵ\epsilonϵ is also in FIRST(α)FIRST(\alpha)FIRST(α).f(x)=x∗e2piiξxf(x) = x * e^{2 pi i \xi x}f(x)=x∗e2piiξx

  1. If X is a terminal, the FIRST(X) = {X}

  2. If X →aα,add a  to FIRST(X)\rightarrow a\alpha, add ~a ~~to ~FIRST(X)→aα,add a  to FIRST(X)

  3. If X →ϵ,add ϵ to FIRST(X)\rightarrow \epsilon, add ~ \epsilon ~to ~ FIRST(X)→ϵ,add ϵ to FIRST(X)

  4. If a production has multiple Symbols like X →Y1,Y2,...,Yk\rightarrow Y_1, Y_2, ..., Y_k→Y1​,Y2​,...,Yk​

    1. Add every non−ϵ  in  FIRST(Yi)  to  FIRST(X)non-\epsilon ~~in ~~ FIRST(Y_i) ~~to ~~FIRST(X)non−ϵ  in  FIRST(Yi​)  to  FIRST(X)

    2. If Y1⇒∗ϵ,add  ϵ  FIRST(Y2) to FIRST(X)Y_1 \Rightarrow^* \epsilon, add ~~\epsilon ~~FIRST(Y_2) ~to ~FIRST(X)Y1​⇒∗ϵ,add  ϵ  FIRST(Y2​) to FIRST(X)

    3. Continue for each subsequent YiY_iYi​, if Yi⇒∗ϵ add FIRST(Yi+1) to FIRST(X)Y_i \Rightarrow^*\epsilon ~add ~FIRST(Y_{i+1}) ~to ~FIRST(X)Yi​⇒∗ϵ add FIRST(Yi+1​) to FIRST(X)

    to FIRST(X).

  5. If all symbols Y1,Y2,...,YkY_1, Y_2, ..., Y_kY1​,Y2​,...,Yk​ can derive ϵ\epsilonϵ, then add ϵ\epsilonϵ

FOLLOW(α)FOLLOW(\alpha)FOLLOW(α)

The set of terminals that can immediately follow A in sentential form (i.e. ).

  1. If S is the start symbol, add $ to FOLLOW(S)

  2. Search for A in a production body and apply rules to that production:

    1. Add FOLLOW(A) to FOLLOW(B) if:

If A→αBβ,add FIRST(β)−ϵ to FOLLOW(B)A \rightarrow \alpha B \beta, add ~FIRST(\beta) - {\epsilon} ~to ~FOLLOW(B)A→αBβ,add FIRST(β)−ϵ to FOLLOW(B)

A→αBA \rightarrow \alpha BA→αB

A→αBβ and β⇒∗ϵA \rightarrow \alpha B \beta ~and ~\beta \Rightarrow^*\epsilonA→αBβ and β⇒∗ϵ