What Is a Programming Language? Machine Code, Compilers and Interpreters: Programming Fundamentals (3)
Learn what a programming language is, how machine code, compilers and interpreters work, and how source code becomes a running program.
A programming language is a standard way to write instructions so that a machine can understand them and run them. It has its own words, symbols and grammar rules. A processor cannot read it directly, so another program translates it into machine code first. That translator is a compiler or an interpreter.
After this lesson, you can explain how the text you write (or the text an AI writes for you) turns into something a computer runs. You will also know what each kind of error message is telling you.
In this lesson you will:
- Tell machine code, low level languages and high level languages apart.
- Explain the difference between a compiler and an interpreter.
- Follow the phases a compiler goes through, from source code to executable.
- Run your first StepCode program and break it on purpose to see syntax and semantic errors.
- Run the same kind of program in Python and spot a bug that only shows up when it runs.
This lesson takes about 40 minutes. You need a web browser for the StepCode editors in this page. Python is optional.
How computers understand us
A computer only understands two states: on and off. We write them as 1 and 0. Every instruction a processor runs is a pattern of those two digits. People do not think in patterns of 0 and 1. We think in words, like “add these two numbers”.
Programming languages exist to close that gap. Each language is a compromise. It is close enough to human language that a person can write it and read it, and strict enough that a program can translate it into 0 and 1 with no doubt about the meaning.
Today there is one more layer on top. Many people now ask an AI agent for code in plain English, and the agent writes the program. That makes it look like the gap is gone. It is not. The prompt still becomes code in a real language, and that code is what runs. If you cannot read that code, you cannot tell whether it does what you asked. Programming did not disappear. As the first lesson put it, the job moved from typing code to understanding it.
Three levels of languages
The course splits languages into three groups. The split depends on how close each one is to the machine.
Machine code
Machine code is the only language a processor runs directly. It is made of binary instructions (0 and 1) that stand for operations and memory addresses. Each family of processors has its own machine code. It is very hard for people to read or write, because it sits at the level of the hardware itself.
Low level languages
A low level language, like assembly language, is still tied to the hardware, but it is easier for people to read. It uses short words called mnemonics instead of raw bits. For example, add eax, ebx means “add the value in one processor register to another”. A register is a tiny, very fast storage spot inside the processor.
Low level languages give you direct control over the hardware. The cost is that you need to know the processor in detail, and a program for one processor often has to be rewritten for another. Today they are used in small devices with few resources, like microcontrollers (the chips inside appliances, cars and toys).
High level languages
A high level language, like Python, uses words and phrases close to English. It adds abstraction. Abstraction means the language hides the hardware details, so you can focus on the problem you want to solve.
High level languages are easier to read and faster to work with, and the same code can run on many platforms. The cost is that they use more resources. You find them almost everywhere: web apps, mobile apps, data analysis and artificial intelligence.
The newest layer: prompts
You can see the full chain in the drawing below. The same task, adding two numbers, appears at every level.
The top row is not a programming language. A prompt can mean several things, and the AI picks one meaning for you. The rows below it cannot be read in two ways. That is why the code is the layer you must be able to check.
Compilers and interpreters
Compilers and interpreters are the tools that take a program written in a programming language and make it run. They do the same job in two different ways.
- A compiler is a program that translates the whole source code into object code before anything runs. Object code is lower level code made for one specific platform. The result is an executable file that the operating system can run directly.
- An interpreter is a program that reads the source code and runs it one line, or one instruction, at a time. It does not translate everything in advance.
The class slides for this unit show the difference with a small game. The first list of instructions is in Spanish, the language of the class: raise your right hand, raise your left hand, wave your arms, say “I am a robot”. You can read the whole list before you start, so you already know every step. That is like a compiled program. The second list has the same instructions in Greek. Now someone has to translate one line, do it, and then translate the next one. That is like an interpreter.
Press Run the program below to watch both ways run the robot list. Then press Run again.
An animation that runs the robot program in two ways. The compiler goes through lexical, syntax and semantic analysis, intermediate code, optimization, object code and the linker, and only then shows the four lines of output, at t = 9. The interpreter translates and runs one line at a time, so its first line appears at t = 2.5, but it finishes at t = 10. On a second run the executable already exists, so the compiler finishes at t = 2, while the interpreter translates every line again and finishes at t = 10.
Here is how they compare.
| Compiler | Interpreter | |
|---|---|---|
| When it translates | Everything, before running | One line at a time, while running |
| Speed when running | Usually faster | Usually slower, it translates every time |
| What you need to run it | Only the executable | The interpreter installed on every computer |
| Finding errors | Harder, errors live in the object code | Easier, each error points to a line of your code |
| Platforms | Compile once for each architecture (a 64 bit build does not run on a 32 bit machine) | Runs wherever the interpreter is installed |
Today, almost every language runtime mixes both ideas to get the best of each. A famous method is JIT compilation (just in time), which compiles code while the program runs. Python itself first turns your file into bytecode, a simpler intermediate code, and then runs that bytecode in a virtual machine.
The phases of compilation
From the outside, compiling looks like one step. Inside, the compiler works in phases. Each phase checks one kind of problem.
- Lexical analysis. The compiler reads the code character by character and splits it into tokens. A token is the smallest piece with meaning: a keyword, an identifier (a name you chose), a constant or a symbol. This phase also removes blank spaces and comments.
- Syntax analysis. The compiler checks the grammar. It makes sure the tokens appear in an order the language allows, and it builds a tree that shows the structure of the program. A missing semicolon is a syntax error.
- Semantic analysis. The compiler checks the meaning. Are the variables and functions declared? Does every value have the right type? Putting a number with decimals into a variable that only holds whole numbers is a semantic error.
- Intermediate code generation. The compiler translates the program into a simpler intermediate language.
- Code optimization. The compiler looks for ways to make that intermediate code faster or smaller.
- Object code generation. The compiler turns the optimized code into low level code or machine code.
Finally, a tool called the linker takes the object code and builds the executable. Then you run it.
Why pseudocode and Python
In this course we write every algorithm in two forms.
The first form is pseudocode. Pseudocode is a simple language between plain English and a real programming language. It lets you think about the logic without fighting a strict syntax. I built StepCode for my students at UCAB (Universidad Católica Andrés Bello), where I taught this course in 2023 and 2024. StepCode runs pseudocode in the browser. It checks the whole program first, like the analysis phases of a compiler, and only then runs it one instruction at a time, like an interpreter. That way your pseudocode is not just text on paper. You can run it and see what happens.
The second form is Python. Python is a high level, interpreted language that is easy to read. It is also one of the languages AI agents write most often, so it is a good language to learn to read.
Paradigms in one table
A programming paradigm is a style of building programs. You will use the imperative style in this whole course, but it helps to know the names.
| Paradigm | Main idea | Strength | Weakness |
|---|---|---|---|
| Imperative (procedural) | A list of steps that change variables, with loops, conditionals and procedures | Easy to understand, precise control of the flow | Errors when many parts change the same data |
| Declarative | Describe the problem and its rules, not how to solve it | Short, expressive code for complex problems | Can be slower, needs a good grasp of logic |
| Object oriented | Objects that hold data and behavior and talk to each other | Modular, reusable code, good for big teams | More overhead, harder to learn |
| Functional | Programs built from functions, with no changing state | Clear code, easier to run tasks in parallel | Strange at first for people used to the imperative style |
Guided practice
Now you will run a program, read it the way a compiler does, and break it twice.
1. Run your first StepCode program
This program is the robot game from class. Every line between Program and EndProgram is one instruction. Write shows text on the screen. Lines that start with // are comments, notes for people that the computer ignores.
Press Run.
You should see:
Raise your right hand
Raise your left hand
Wave your arms
Say: I am a robot
2. Read a program like a lexical analyzer
This program adds two whole numbers. Define creates variables, Read takes a value from the keyboard, and <- stores a value. The next lesson covers variables in detail.
Run it and type 3 and then 4.
You should see:
Enter n1:
Enter n2:
The result is: 7
Now read it the way the lexical analysis phase does. Sort every token into a group:
- Keywords (words of the language):
Program,Define,As,Integer,Write,Read,EndProgram. - Identifiers (names you chose):
SumTwoNumbers,n1,n2,result. - Constants (fixed values written in the code): the texts
"Enter n1: ","Enter n2: "and"The result is: ". - Symbols:
<-,+,,and;.
The two comments and the extra spaces disappear in this phase. The compiler never sees them.
The SumTwoNumbers program split into tokens. Keywords: Program, Define, As, Integer, Write, Read, EndProgram. Identifiers: SumTwoNumbers, n1, n2, result. Constants: "Enter n1: ", "Enter n2: ", "The result is: ". Symbols: , ; <- +. The two comments and the extra spaces are removed by the lexical analysis phase.
Keywords: Program Define As Integer Write Read EndProgram (7 different, 10 in total)
Identifiers: SumTwoNumbers n1 n2 result (4 different, 10 in total)
Constants: "Enter n1: " "Enter n2: " "The result is: " (3 different, 3 in total)
Symbols: , ; <- + (4 different, 12 in total)
3. Break the grammar: a syntax error
Delete the semicolon at the end of the line result <- n1 + n2;. The program below already has it removed. Press Run.
You should see this error, and the program does not run:
This statement is missing its ";".
This is the syntax analysis phase at work, and the same syntax error you met in the first lesson. Every word is valid, but the grammar is broken: StepCode needs a ; at the end of every instruction. Add the semicolon back and the program runs again.
4. Break the meaning: a semantic error
Now change the + to /, so the program divides instead of adding. The grammar is fine. Press Run.
You should see:
A Real cannot be stored where a Integer is expected: "Div" gives the whole division.
This is the semantic analysis phase. In StepCode, / always gives a Real, a number with decimals (7 divided by 2 is 3.5). The variable result is an Integer, so it can only hold whole numbers. The sentence is grammatically correct, but its meaning is wrong. The message even suggests the fix: Div gives the whole part of the division. You will learn all the operators in the operators lesson.
5. Run the same idea in Python, and find the hidden bug
Here is a Python version of the sum program. Imagine an AI agent wrote it for you after the prompt “ask for two numbers and add them”. It looks right.
n1 = input("Enter n1: ")
n2 = input("Enter n2: ")
result = n1 + n2
print("The result is:", result)
Save it as sum.py, run python3 sum.py in a terminal, and type 3 and 4.
You should see:
Enter n1: 3
Enter n2: 4
The result is: 34
The program runs, but the answer is wrong. In Python, input() always returns text. Adding two texts joins them, so "3" + "4" is "34". Python is interpreted and does not ask you to declare types, so no phase stopped this before it ran. The StepCode version could not have this bug, because Integer was declared and checked first. You will meet the same trap in Files, where everything a program reads from a file is text too.
The fix is to convert the text to a whole number with int():
n1 = int(input("Enter n1: "))
n2 = int(input("Enter n2: "))
result = n1 + n2
print("The result is:", result)
You should see:
Enter n1: 3
Enter n2: 4
The result is: 7
You catch this kind of mistake only if you can read the code. The AI did what the prompt said, not what you meant.
Here are the four versions from this practice side by side. Pick one and watch which phase stops it.
Four versions of the sum program, run with 3 and 4. The correct StepCode program passes the lexical, syntax and semantic phases, runs and prints The result is: 7. The version missing a semicolon is stopped by syntax analysis. The version with a division is stopped by semantic analysis, because a Real cannot go into an Integer. The Python version without int() has no type check before it runs, so it runs and prints The result is: 34, a wrong answer that no phase catches.
This statement is missing its ";". Syntax analysis stops it. The program never runs.
Common mistakes
If you see This statement is missing its ";"., it means an instruction has no semicolon at the end. Go to the line the editor marks and add the ;.
If you see a message like I did not expect ""Hello"" here., look at the word just before the mark. A common cause is a misspelled keyword. For example, Wrtie "Hello"; produces exactly that message: StepCode does not know Wrtie, so it reads it as a name, and then a text right after a name makes no sense. Fix the spelling to Write.
If you see "result" is not declared., it means you used a name that was never created with Define. Declare it at the top of the program, or fix the spelling of the name.
If a program runs but prints a wrong answer, like 34 instead of 7, no message will help you. That is a logic error, the third kind of error from the first lesson. Test with small inputs where you already know the answer.
Boundaries
A programming language is a set of rules, not the tool that runs it. Python is a language; python3 is the interpreter you install.
“Compiled” and “interpreted” describe how a language is usually run, not a fixed property. Many runtimes do both, like Python with its bytecode.
Pseudocode is not a programming language in the strict sense. It helps people think and share an algorithm. StepCode makes it runnable for learning, but real projects use languages like Python.
A prompt to an AI is not a programming language either, because it can be read in more than one way. The code the AI produces has one exact meaning, so that is the part you review.
Practice exercises
These exercises come from the practice guide of this unit. Try them first, then open the solution.
Exercise 1. Lexical analysis. List the keywords, identifiers, constants and symbols of this Python code, and write the code the way the lexical phase leaves it.
# this is a comment
n1: int
n2: int
result: int
# another comment
n1 = int(input("Enter n1: "))
n2 = int(input("Enter n2: "))
result = n1 + n2
print("The result is: ", result)
# remember the blank spaces
Solution
- Words of the language:
int,input,print. The guide calls them keywords. Strictly, Python calls them built in names, but they belong to the language, not to you. - Identifiers:
n1,n2,result. - Named constants: none.
- Symbols:
:,=,+,,and the parentheses.
The code with comments and extra spaces removed:
n1:int
n2:int
result:int
n1=int(input("Enter n1: "))
n2=int(input("Enter n2: "))
result=n1+n2
print("The result is: ",result)Exercise 2. Syntax analysis. Find the syntax errors in this Python code.
1n: int
n2: int
result: xyz
1n = int(inut("Enter 1n: "))
n2 = int(inut("Enter n2: "))
result = 1n + n2;
1n++
print("The result is: ", result)
Solution
1nis not a valid identifier. In Python, a name must start with a letter or an underscore. Usen1.inutis a misspelled function name. It should beinput.xyzis not a data type in Python. Useint.1n++is not valid Python. Python has no++operator. Usen1 += 1.- The
;at the end ofresult = 1n + n2;is not needed in Python. It is the only error here a compiler could simply ignore.
Exercise 3. Semantic analysis. Find the type errors.
n1: int
n2: float
result: int
n1 = input("Enter n1: ")
n2 = input("Enter n2: ")
result = n1 + n2
print("The result is: ", result)
Solution
input()returnsstr(text). Storing it inn1, declared asint, is a type error.- The same happens with
n2, declared asfloat(a real number). resultis declared asint, but adding anintand afloatgives afloat.
Python does not stop at these type hints when it runs the program. A type checker or a careful reader catches them.
Exercise 4. Paradigms. Name the paradigm for each description.
- Programs run instructions in sequence and change state through variables, with loops and conditionals.
- Programs are built around objects, which are instances of classes and send messages to each other.
- Programs are defined by logical rules and facts, and the computer searches for solutions that meet them.
- Programs are made of pure functions combined together, with no change of state.
Solution
- Imperative. 2. Object oriented. 3. Declarative. 4. Functional.
Exercise 5. Triangle area in two languages. This was the first graded activity of the course. You wrote the pseudocode in the pseudocode lesson, so this time the new part is Python. Write a StepCode program that asks for the base and the height of a triangle and shows its area (base times height divided by 2). Then translate it to Python.
Solution
With 3 and 4 as inputs, you should see:
Enter the base:
Enter the height:
The area of the triangle is: 6The Python translation:
# Read the base and the height as real numbers
base = float(input("Enter the base: "))
height = float(input("Enter the height: "))
# Compute and show the area
area = base * height / 2
print("The area of the triangle is:", area)With the same inputs, you should see:
Enter the base: 3
Enter the height: 4
The area of the triangle is: 6.0What you learned
- A programming language is a set of rules to write instructions a machine can run after translation.
- Machine code is the only thing a processor runs. Low level languages sit close to it, and high level languages hide the hardware.
- A compiler translates the whole program before it runs. An interpreter translates and runs one instruction at a time.
- A compiler works in phases: lexical, syntax and semantic analysis, intermediate code, optimization and object code, then a linker builds the executable.
- A prompt to an AI is the newest layer, but the code it produces is what runs, and reading it is your job.
Next steps
In the next lesson, Variables and data types, you will see how a program stores data in memory, and why every value has a type.
- Review Pseudocode if the StepCode syntax still feels new.
- Learn more about the editor in the StepCode project page.