In this lecture, we begin by getting a context for what Syntax and Semantics are in programming languages to appropriately set ourselves up for learning Rust. We then take a 'sneak peak' at the Rust language as is solves the Change Making Problem.
Lecture Date
๐ January 14, 2026
Standard
Concurrent Programming
Topics Covered
SyntaxSemanticsPython vs RustChange Making Problem
"The study of how words and morphemes combine to form larger units like phrases or sentences."
Syntax is all about structureโthe rules that govern how symbols can be arranged. Think of it like the grammar of a language, but stripped of all meaning.
A Natural Language Example
Consider this English sentence:
"The cat likes to eat fish."
We can break this down into grammatical components:
Fragment
Classification
The cat
Noun phrase
likes
Verb
to eat
Infinitive
fish
Noun
This reveals the grammatical structure of the sentence.
โ ๏ธ WAIT! Before we continue, it's important to note that we are NOT talking about the meaning of the sentence. We are only talking about the structure. Do your best to ignore the meaning of the words!
We can abstract away the meaning entirely:
"The cat likes to eat fish." โ "Noun-Phrase Verb Infinitive Noun"
When Syntax Breaks Down
Consider this malformed sentence:
"Bob fan a Lakers is of the"
Is this problematic because of what the words mean? Or because of how they're arranged?
The malformed grammar creates ambiguity when we try to parse it:
Was it trying to say "Bob is a fan of the Lakers"?
Or perhaps "The Lakers are a fan of Bob"?
Maybe even "fan is a bob of the lakers"? ๐คท
We simply can't tell! And that's the pointโsyntax is about structure, not meaning.
Syntax in Programming Languages
Good news: you're already familiar with programming language syntax! Let's look at Python:
total = previous + current
Thanks to syntax highlighting in your editor, we can identify the tokens:
Statements control program flowโthey don't "simplify" to anything:
# Conditional: skip code block if condition is falseif x > 10:
print("x is greater than 10")
# Loop: repeat code block while condition holdswhile x < 10:
x += 1# Function definition: bind a name to executable codedefadd(a, b):
return a + b
Why This Matters
Understanding syntax helps us answer questions like:
What part of this line gets evaluated first?
Does this line have a side effect?
Is this code structurally valid?
We don't need to know what's stored in the variables to understand what's about to happen!
๐ Key Insight: Syntax gives us rules for how to validly organize tokens in a programming language. A token is the smallest unit of meaningful informationโa single "piece" of the language.
Malformed Syntax Example
this is a variable = 23
If your editor's coloring looks wrong, you may have made a syntax error!
Semantics
"The meaning transported through symbols."
While syntax is about structure, semantics is about meaningโthe actual stuff we store in our language's symbols.
The Symbol-Meaning Relationship
We would never say that the characters r, e, d, c, a, rare themselves a red car. Instead, we recognize:
red โ stores information about the color red
car โ stores information about vehicles with four wheels and a motor
We create syntax rules to define how tokens can be arranged. We then assign meaning to those arrangements, so that when someone else reads our code, they can extract that meaning.
A Semantic Analysis Example
Consider this Python code:
print("Hello, World!")
Syntactic Breakdown
Token
Classification
print
Variable (identifier)
(...)
Call operator
"Hello, World!"
String literal
Semantic Execution
Here's what actually happens when this code runs:
Lookup: Find the object stored at the variable named print
Identify: It's a function (callable object)
Call: Open a new stack frame, pass "Hello, World!" as an argument
Execute: Run the function body...
Return: Close the stack frame, return None
Continue: Move to the next line of code
EOF: End of file reached โ terminate program
Case Study: Making Change
Let's see how Python and Rust express the same algorithm with different syntax. This classic problem counts coins needed for a given amount.
Python Implementation
defmake_change(amount):
denominations = [25, 10, 5, 1]
change = []
for denomination in denominations:
count = 0while amount >= denomination:
amount -= denomination
count += 1
change.append(count)
return change
if __name__ == "__main__":
amount = 410
change = make_change(amount)
print(f"Change for {amount} is: {change}")
Notice how Rust's syntax carries more information:
Feature
Python
Rust
Type annotations
Implicit
Explicit and Implicit (i16, Vec<i16>)
Mutability
Implicit
Explicit (mut)
Return type
Implicit
Explicit (-> Vec<i16>)
Memory management
Garbage collected
Ownership system
๐ก Observation: Rust's larger syntax vocabulary allows it to store more specific semantic information directly in the code structure!
Summary
Syntax is the structure and grammar of code โ the rules for how tokens can be arranged
Semantics is the meaning and behavior of code โ the actual information stored in those arrangements
Expressions evaluate to a single value; Statements command actions without producing one
Tokens are the smallest meaningful units in a programming language
Different languages can express the same semantics with different syntax โ the Making Change case study showed Python and Rust implementing identical logic with different syntactic vocabularies
More verbose syntax (like Rust's type annotations and mut keyword) often carries more semantic information directly in the code
๐ Lecture Notes
Key Takeaways:
Syntax = Structure and grammar of code (how it looks)
Semantics = Meaning and behavior of code (what it does)
Expressions evaluate to values; Statements command actions
Tokens are the smallest meaningful units in a language
Different languages can express the same semantics with different syntax
More verbose syntax often carries more semantic information