A Simple Ompass Compiler — Syntax definition

🧠 Why Do We Need a Syntax Definition?

Imagine reading a sentence like:

Runs dog the fast very

You can understand the words, but the order is all wrong.

A compiler faces the same problem.

So, we give it the rules — just like a teacher explaining where nouns and verbs go in a sentence. That’s what syntax definition does for a programming language.


🧩 How We Define Syntax (The Friendly Way)

Most teachers define syntax using BNF (Backus–Naur Form) or syntax rules.
But don’t feel scared — these rules are simply patterns that show what is allowed.

Let’s build the syntax for our simple Ompass compiler step by step.


🛠️ Example: Simple Syntax Rules for Ompass

We’ll define a tiny language where:

  • You can assign values
  • You can do simple arithmetic
  • You can write expressions

Here’s what the syntax might look like in a beginner-friendly style:

<program>      → <statement_list>

<statement_list> → <statement> | <statement> <statement_list>

<statement>    → <id> = <expression>

<expression>   → <term> | <term> + <expression> | <term> - <expression>

<term>         → <factor> | <factor> * <term> | <factor> / <term>

<factor>       → <number> | <id> | ( <expression> )

Don’t worry if this looks new — we’ll break it down.

🔍 What do these rules mean?

  • <program>
    The whole code you write is made of one or more statements.
  • <statement>
    A statement is like:
    x = 5 + y
  • <expression>
    Expressions describe math operations like addition or subtraction.
  • <term>
    These handle multiplication and division.
  • <factor>
    The smallest pieces: numbers, identifiers (like variable names), or expressions inside brackets.

These rules allow the compiler to understand whatever the programmer writes.


🌳 Diagram: Syntax Structure for Ompass (Parse Tree Style)

Below is a simple diagram to help you imagine how the compiler sees a statement like:

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

This tree shows how the compiler breaks down a statement into smaller and smaller parts until everything is understood.


🧩 Why This Syntax Definition Matters

A good syntax definition helps the compiler:

✔ understand the order of operations
✔ catch errors like missing brackets or wrong symbols
✔ build a syntax tree
✔ translate the code into lower-level instructions

Think of syntax definition as giving the compiler a blueprint so it can safely build meaning from your code.