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

Vivo X200 Price Drop
  • IT
  • Current Affairs
  • Tech News

Vivo X200: जाने कितनी कम कीमत पर मिल रहा ये 9400 मिडिया टेक प्रोसेसर वाला स्मार्टफोन

examhopeinfo@gmail.com December 23, 2025 0
Samsung Galaxy S25 Plus
  • IT
  • Current Affairs
  • Tech News

Samsung Galaxy S25 Plus पर मिल रही भारी छूट ,जाने सेल प्राइस

examhopeinfo@gmail.com December 22, 2025 0
Electricity bill saving Smart Plug
  • IT
  • Current Affairs
  • Tech News

AI के इस ज़माने में कैसे बिजली बचा रहे हैं यह स्मार्ट प्लग?

examhopeinfo@gmail.com December 21, 2025 0

Recent Posts

  • Vivo X200: जाने कितनी कम कीमत पर मिल रहा ये 9400 मिडिया टेक प्रोसेसर वाला स्मार्टफोन
  • Samsung Galaxy S25 Plus पर मिल रही भारी छूट ,जाने सेल प्राइस
  • AI के इस ज़माने में कैसे बिजली बचा रहे हैं यह स्मार्ट प्लग?
  • क्या है यह GhostPairing Scam और बिना पासवर्ड और सिम के क्यों हो रहा है व्हाट्सप्प अकाउंट हैक
  • Leica कैमरे के साथ जल्द लॉन्च हो सकता है Xiaomi Ultra 17

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

Vivo X200 Price Drop
  • IT
  • Current Affairs
  • Tech News

Vivo X200: जाने कितनी कम कीमत पर मिल रहा ये 9400 मिडिया टेक प्रोसेसर वाला स्मार्टफोन

examhopeinfo@gmail.com December 23, 2025 0
Samsung Galaxy S25 Plus
  • IT
  • Current Affairs
  • Tech News

Samsung Galaxy S25 Plus पर मिल रही भारी छूट ,जाने सेल प्राइस

examhopeinfo@gmail.com December 22, 2025 0
Electricity bill saving Smart Plug
  • IT
  • Current Affairs
  • Tech News

AI के इस ज़माने में कैसे बिजली बचा रहे हैं यह स्मार्ट प्लग?

examhopeinfo@gmail.com December 21, 2025 0
Ghost Pairing Scam on Whatsapp
  • IT
  • Current Affairs
  • Tech News

क्या है यह GhostPairing Scam और बिना पासवर्ड और सिम के क्यों हो रहा है व्हाट्सप्प अकाउंट हैक

examhopeinfo@gmail.com December 21, 2025 0
Copyright © All rights reserved for ExamHope. | MoreNews by AF themes.
Go to mobile version