UNIHIVE

Study Hub UNIPORT MEG 202

MEG 202 · Basic Software Engineering

Programming languages: syntax, semantics and translation

Machine, assembly and high-level languages, formal languages and BNF, and compilers and interpreters.

Our notes

2 min read

Levels of language

  • Machine language: binary instructions the CPU executes directly (for example 10110000 01100001). Fast, but hard to write and specific to one processor family.
  • Assembly language: short mnemonics for machine instructions (MOV, ADD, JMP) and symbolic addresses. An assembler translates it to machine code. Used for device drivers, embedded systems and speed-critical code.
  • High-level languages: close to human language and mathematics, and portable across machines: FORTRAN (1957, scientific computing, still used for engineering simulation), COBOL (business), Pascal (teaching, structured programming), C (1972, systems and embedded programming), C++, Java, Visual Basic (Windows applications), Python and MATLAB (engineering analysis today).

Example: the statement a = b + c in a high-level language may become, in assembly:
    LOAD R1, b
    ADD R1, c
    STORE R1, a

Paradigms

  • Procedural (imperative): step-by-step instructions grouped into procedures (FORTRAN, Pascal, C).
  • Object-oriented: data and the functions acting on it grouped into objects (C++, Java, Python).
  • Functional, logic and event-driven (Visual Basic forms respond to clicks) styles.

Formal languages

A programming language is a formal language: a set of strings built from an alphabet by precise rules (a grammar), unlike natural languages, which are ambiguous.

  • Syntax: the rules of form: which sequences of symbols are valid programs. A missing semicolon in C is a syntax error.
  • Semantics: the meaning: what a valid program does. x = x + 1 is valid syntax; its meaning is "add one to x and store it".
  • Pragmatics: how the language is used in practice (style, conventions).

BNF (Backus-Naur Form)

A notation for grammars (introduced for ALGOL 60). Rules define non-terminals in angle brackets in terms of terminals and other non-terminals, with ::= meaning "is defined as" and the bar meaning "or":
    <digit> ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
    <integer> ::= <digit> | <integer> <digit>
The second rule is recursive: an integer is a digit, or an integer followed by a digit. So 407 is an integer: 4 is an integer, 40 is integer + digit, 407 is integer + digit. Compilers use such grammars to parse programs.

Translators

  • Compiler: translates the whole source program into machine code before running (C, FORTRAN, Pascal). Fast execution; errors are reported for the whole program; the executable can run without the compiler.
  • Interpreter: translates and runs one statement at a time (classic BASIC, Python, MATLAB). Easier testing; slower execution.
  • Stages of compilation: lexical analysis (split into tokens) → syntax analysis (parse with the grammar) → semantic analysis (type checks) → optimisation → code generation → linking with libraries.

Types of errors

  • Syntax errors: found by the compiler.
  • Run-time errors: occur while running (division by zero, reading past an array's end).
  • Logic errors: the program runs but gives wrong answers (wrong formula); found only by testing.

Exams usually ask

  • Compare machine, assembly and high-level languages.
  • Distinguish syntax and semantics; write a BNF rule.
  • Compare compilers and interpreters.

Common mistake

Thinking a program that compiles is correct. Compilation catches syntax errors only; logic errors need testing.

Written by the UniHive team in our own words. Spotted a mistake? Tell us on WhatsApp.

Read

Watch

We link to free material and never copy it. Every link was opened by our team before it went up; video data is a rough guide at 360p.

All MEG 202 topics

Report

Sign in to report this. It is free.

Is this your work (a lecturer, author or publisher)? Ask us to take it down.