Skip to content
ExamHope Logo

examhope

Primary Menu
  • Digital Logic
    • Arithmetic Operations
    • Asynchronous/Ripple Counters
    • Basic Gates
    • Boolean Algebraic Theorems
    • Codes
  • Data Structures
    • Binary Heaps
    • Binary Search
    • Binary Search Trees
    • Binary Tree
    • Binary Tree Sort
    • Bipartite Graphs
    • Complete Graph
  • Theory of Computation
    • Finite Automata
    • Finite Automaton First Example
  • Current Affairs
    • Sports News
    • Tech News
    • Bollywood News
    • Daily News
  • Database
  • Computer Network
  • Computer Organization and Architecture
  • C Language
  • Operating Systems
  • Software Engineering
  • Theory of Computation
  • About us
  • Contact Us
  • Privacy Policy
  • DMCA Policy
  • Terms and Conditions
  • Home
  • IT
  • A Simple Ompass Cempiler — Syntax-directed translation
  • IT
  • Compiler Design
  • syntax-directed translation

A Simple Ompass Cempiler — Syntax-directed translation

examhopeinfo@gmail.com December 4, 2025 3 minutes read
Syntax-directed translation A Simple Ompass Compiler

Syntax-directed translation A Simple Ompass Compiler

🧠 What Does “Syntax-Directed” Really Mean?

Let’s take a simple idea:

  • When a compiler sees an addition like a + 3,
    it should compute or generate code for that.
  • When it sees an assignment like x = ...,
    it should store the result.

Every piece of work the compiler does is attached to a rule in the syntax.

So, the syntax (grammar) decides the structure,
and the translation rules decide the meaning or action.

It’s like:

  • Grammar = the skeleton
  • Translation rules = the muscles that move the skeleton

Both work together.


🧩 How Syntax-Directed Translation Works (Friendly Breakdown)

Let’s create a tiny Ompass language again, just like earlier:

  • It supports assignments
  • It allows arithmetic expressions
  • It uses variables and numbers

Here’s a simple grammar, but this time with translation rules added:

<statement> → <id> = <expression>     { print("assign " + id.name) }

<expression> → <expression> + <term>  { print("add") }
<expression> → <expression> - <term>  { print("sub") }
<expression> → <term>

<term> → <term> * <factor>            { print("mul") }
<term> → <term> / <factor>            { print("div") }
<term> → <factor>

<factor> → <number>                   { print("push " + number.value) }
<factor> → <id>                       { print("push " + id.name) }
<factor> → ( <expression> )

Don’t worry about the code in curly braces — it simply describes what the compiler does when that rule matches.

For example:

  • When it sees a number like 7, the rule says:
    → push 7
    meaning: “Store this value for later use.”
  • When it sees +, the rule says:
    → add
    meaning: “Combine the top two values.”

This is the heart of syntax-directed translation.


Translation

Let’s translate this tiny program:

x = a + 3

What happens?

  1. The compiler sees a → push a
  2. Then sees 3 → push 3
  3. Then sees + → add
  4. Then the assignment rule prints assign x

So the final output (or intermediate code) looks like:

push a
push 3
add
assign x

This is how a simple Ompass compiler would understand and translate code.


🌳 Diagram: Syntax-Directed Translation Tree

Below is a simple, hand-drawn style diagram showing how translation happens alongside the parse tree.

                       <statement>
                           |
         --------------------------------------------------
         |                                                |
       <id>                                        <expression>
        |                                              |
        x                        ----------------------------------------
                                   |                                    |
                                 <expression>                         + <term>
                                     |                                      |
                                   <term>                                 <factor>
                                      |                                      |
                                    <factor>                                 3
                                      |
                                      a

Attached translation actions (shown in order they fire):

push a
push 3
add
assign x

The compiler processes the tree bottom-up.
As it reaches each node, it triggers the action connected to that rule.


🧩 Why Syntax-Directed Translation Is Important

Syntax-directed translation helps a compiler:

✔ generate intermediate code
✔ evaluate expressions
✔ type-check values
✔ build symbol tables
✔ simplify later stages like optimization and code generation

It’s like giving the compiler a map that says:

“Whenever you see this, produce that.”

This keeps everything organized and predictable.


About the Author

examhopeinfo@gmail.com

Administrator

Visit Website View All Posts

Post navigation

Previous: A Simple Ompass Compiler — Syntax definition
Next: Parsing — A Simple Onepass Compiler

Related News

India Squad for Afghanistan Series
  • IT

India Squad for Afghanistan Series Likely to Witness Major Changes, Leadership Reshuffle Possible

examhopeinfo@gmail.com May 19, 2026 0
Brazil Football Team
  • IT
  • Current Affairs
  • Sports News

Brazil Unveils 26-Man Squad for 2026 FIFA World Cup Under Carlo Ancelotti

examhopeinfo@gmail.com May 19, 2026 0
CSK Vs SRH Ipl match
  • IT
  • Current Affairs
  • Sports News

Ruturaj Gaikwad Highlights Squad Challenges After CSK’s Defeat Hurts IPL 2026 Playoff Hopes

examhopeinfo@gmail.com May 19, 2026 0

Recent Posts

  • India Squad for Afghanistan Series Likely to Witness Major Changes, Leadership Reshuffle Possible
  • Brazil Unveils 26-Man Squad for 2026 FIFA World Cup Under Carlo Ancelotti
  • Ruturaj Gaikwad Highlights Squad Challenges After CSK’s Defeat Hurts IPL 2026 Playoff Hopes
  • MS Dhoni Misses CSK Clash Against SRH Due to Fitness Concerns, Ruturaj Gaikwad Shares Update
  • IPL 2026 Playoff Race Heats Up: Rajasthan Royals’ Defeat to Delhi Capitals Changes Top-4 Battle

At ExamHope, we understand that preparing for exams can be challenging, overwhelming, and sometimes stressful. That’s why we are dedicated to providing high-quality educational resources, tips, and guidance to help students and aspirants achieve their goals with confidence. Whether you are preparing for competitive exams, school tests, or professional certifications, ExamHope is here to make your learning journey smarter, easier, and more effective.

Quick links

  • About us
  • Contact Us
  • Privacy Policy
  • Terms and Conditions
  • Disclaimer
  • DMCA Policy

Category

  • Computer Network
  • Computer Organization and Architecture
  • Data Structures
  • C Language
  • Theory of Computation
  • Database

You may have missed

India Squad for Afghanistan Series
  • IT

India Squad for Afghanistan Series Likely to Witness Major Changes, Leadership Reshuffle Possible

examhopeinfo@gmail.com May 19, 2026 0
Brazil Football Team
  • IT
  • Current Affairs
  • Sports News

Brazil Unveils 26-Man Squad for 2026 FIFA World Cup Under Carlo Ancelotti

examhopeinfo@gmail.com May 19, 2026 0
CSK Vs SRH Ipl match
  • IT
  • Current Affairs
  • Sports News

Ruturaj Gaikwad Highlights Squad Challenges After CSK’s Defeat Hurts IPL 2026 Playoff Hopes

examhopeinfo@gmail.com May 19, 2026 0
MS Dhoni News
  • IT
  • Current Affairs
  • Sports News

MS Dhoni Misses CSK Clash Against SRH Due to Fitness Concerns, Ruturaj Gaikwad Shares Update

examhopeinfo@gmail.com May 18, 2026 0
Copyright © All rights reserved for ExamHope. | MoreNews by AF themes.
Go to mobile version