Skip to content
Tutorials Center
My learning

Topics

Lesson 19 of 55 ยท Logic and Programs

Programming Languages, Compilers and Interpreters

Understand high-level and low-level languages, compilers, interpreters and bytecode virtual machines, and watch a tokenizer, parser and interpreter turn text into results.

In the last lesson you saw that a processor only understands numeric instructions. Almost nobody writes programs that way. We write in languages such as Python, JavaScript, Java, C or Rust, which use words, symbols and rules that people can read. Something has to translate that text into something the machine can act on. The tools that do the translating are compilers and interpreters, and the choice between them shapes speed, portability and how errors appear.

This lesson explains the types of languages, the three main ways a program gets executed, and then lets you build a miniature tokenizer, parser, interpreter and virtual machine yourself. It follows Machine Code and Assembly.

High-level and low-level languages

Languages sit on a scale of abstraction:

  • Machine code and assembly are low-level: one statement is one hardware instruction, and you manage registers and addresses yourself. They are tied to a specific processor.
  • High-level languages describe what you want in terms of ideas such as lists, text, objects and functions. One line may become dozens of machine instructions, and the same source can often run on different processors.
  • Mid-level languages such as C sit in between: readable, but close enough to memory that you can control it directly.

Higher abstraction costs some control and speed but gains safety, readability and productivity. No level is "the best"; each suits different jobs: an operating system kernel, a data analysis script and a web page all call for different trade-offs. Most programs you write will be high-level, and you can see more of them in tutorials such as Python or JavaScript.

Three ways to run a program

Compiled

A compiler translates the whole program ahead of time into machine code for a target processor, producing an executable file. You compile once, then run the result as often as you like with no translator present. C, C++, Rust and Go work this way. Typical consequences:

  • Fast execution, because the translation work is already done and the compiler can optimise.
  • Many mistakes (such as using a name that does not exist or mixing incompatible types) are reported before the program ever runs.
  • The executable is specific to an operating system and processor type.

Interpreted

An interpreter reads the source and carries out each statement directly while the program runs, without producing a separate executable. Classic examples are shell scripts, early JavaScript engines and the standard Python implementation in its simplest form. Typical consequences:

  • You can run code immediately and experiment, which is great for learning.
  • Errors in a branch that never ran may stay hidden until it does.
  • Slower in raw speed, because translation work happens during the run.

Bytecode and virtual machines

Many modern languages use a middle path. A compiler first translates source into bytecode, a compact instruction set for an imaginary machine. A virtual machine (VM) then runs that bytecode. Java and C# work this way, and so does Python (it compiles to bytecode behind the scenes). Bytecode is portable: any computer with the VM can run it. Fast engines add a just-in-time (JIT) compiler that turns frequently used bytecode into real machine code while the program runs, which is how JavaScript engines in browsers become fast.

flowchart LR
  S["Source code"] --> C["Compiler"]
  C --> M["Machine code<br/>runs on the CPU"]
  S --> I["Interpreter<br/>runs source directly"]
  S --> B["Compiler to bytecode"]
  B --> V["Virtual machine<br/>may JIT-compile hot code"]

Three routes from source code to execution; real languages often combine them.

The line between "compiled" and "interpreted" is a property of an implementation, not of a language. There are interpreters for C and compilers for Python-like languages. Treat the labels as descriptions of typical use.

What a compiler or interpreter does

Whatever the route, the front part of the work is similar. It reads text and turns it into a structure it can reason about:

  1. Lexing (tokenizing): split characters into tokens, the smallest meaningful pieces, such as numbers, names, operators and brackets. The text price * 3 becomes three tokens.
  2. Parsing: check that the tokens follow the grammar and build a tree, the abstract syntax tree (AST), which records structure and precedence. 2 + 3 * 4 becomes a tree where the multiplication sits below the addition because it binds tighter.
  3. Analysis: check names and types, and report errors.
  4. Optimisation: simplify, such as computing 2 * 60 at translation time.
  5. Code generation or execution: a compiler emits machine code or bytecode; an interpreter walks the tree and acts.

A syntax error such as a missing bracket is caught in steps 1 and 2; a name that was never defined is caught in step 3 (compiled languages) or at run time (many interpreted ones).

Build a tokenizer

Step one is easy to demonstrate. The tokenizer scans through the text, skips spaces, and groups characters into labelled tokens. It is a stand-in for a real lexer, which handles many more token kinds, strings and comments.

JS A tokenizer

Press Run (or Ctrl + Enter) to see the console output here.

No output.

A tiny interpreter

Now add a parser and an evaluator. The parser uses precedence: * and / bind tighter than + and -, and brackets override everything. Each function handles one level, calling the next tighter level for its operands. The interpreter then walks the resulting tree and keeps variables in a table.

JS Tokens to a result

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Look at the first two lines: 2 + 3 * 4 gives 14 and (2 + 3) * 4 gives 20, which shows the parser building different trees for different structures. The last two lines show the two broad kinds of failure: using a name that does not exist, and breaking the grammar. A real language has far more, but the pipeline (tokens, tree, evaluation) is the same one used by real engines.

From tree to bytecode

A compiler targeting a VM walks the same tree but, instead of computing, emits instructions. A typical VM is a stack machine: numbers are pushed on a stack and operators pop their operands and push the result. This is a stand-in for something like the JVM, whose instruction set is far larger.

JS Compile to bytecode and run on a stack VM

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The compile step happens once; execute could then run the same bytecode again and again without re-reading any source, which is the idea behind .class files in Java or .pyc files in Python.

Real engines do much more: type checks, optimisation passes, garbage collection and JIT compilation. Treat these stand-ins as a map of the territory rather than a copy of any actual language implementation.

Choosing between them

Compiled languages are common for operating systems, games and anything where speed or small size counts. Interpreted or VM-based languages are popular for scripts, web back ends, data analysis and learning, where quick feedback and portability matter. Many projects combine both: a Python program that calls fast compiled libraries, or a web page that runs JavaScript compiled by the browser while you watch. For the next step in the chapter, move from translating instructions to designing the steps themselves: Algorithms and Problem Solving.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is a token?

    Lexing splits source code into tokens, which the parser then arranges into a tree.
  2. 2. Which description fits a compiled language implementation?

    A compiler produces an executable in advance, so running it needs no translator.
  3. 3. What is bytecode?

    Bytecode is portable: any computer with the matching VM can run it.
  4. 4. Why does 2 + 3 * 4 evaluate to 14 and not 20 in our interpreter?

    Precedence is encoded in the tree: the multiplication is evaluated before the addition that contains it.

Summary

  • Languages range from low-level (close to hardware) to high-level (close to human ideas).
  • Programs run by being compiled to machine code, interpreted directly, or compiled to bytecode and run on a virtual machine (often with JIT compilation).
  • The labels describe implementations, not languages, and many systems mix them.
  • Translators tokenize, parse into a tree, analyse, optimise and then generate code or execute.
  • Syntax errors are found while parsing; undefined names and type problems are found at analysis or at run time.
  • A stack-based VM runs a flat list of simple instructions, which is what a compiler emits from the tree.

Next: Algorithms and Problem Solving.