Syntax vs. Semantics
Syntax governs the formal structural rules for combining words or symbols into valid expressions, whereas semantics governs the meaning conveyed by those words and expressions.
Key Distinctions & Memory Anchors
- Form vs. meaning: syntax specifies which symbol or word arrangements are grammatical, whereas semantics specifies what valid expressions actually mean.
- Linguistics classic: 'Colorless green ideas sleep furiously' obeys English syntax perfectly while failing semantics.
- Programming parallel: a syntax error (a missing bracket) prevents parsing, whereas a semantic error runs valid code that computes the wrong result.
Academic Context Comparison
In Python, omitting a colon after a loop header triggers a syntax error before the script can even execute.
In formal logic, semantics assigns truth values to well-formed formulas under a given interpretation.