GCSE · Computer Science · Eduqas · Spec C500QS
Lexical analysis
Your code is full of tidy spacing, comments and blank lines. Lovely for you. The compiler's first job is to keep only the parts that actually mean something.
Lexical analysis · Keep or bin?
One line of code. Eight fragments. What happens to each?
Here is one line of source code, followed by a blank line: if age >= 16: # old enough? Each fragment of it is below. Is it kept as a token (and which kind?) or removed?
Still to sort
Keyword token (0)
A word the programming language itself gives a fixed meaning.
Where the line is: Keyword: the language decided what it means. Identifier: the programmer chose the name.
Identifier token (0)
A name chosen by the programmer, such as a variable name.
Where the line is: An identifier is a name for something. A literal is the value itself.
Operator token (0)
A symbol that works on values, such as adding or comparing them.
Literal token (0)
A value written directly into the code.
Where the line is: A literal is the value itself. An identifier is a name that is used for a value.
Punctuation token (0)
A punctuation symbol that helps lay out the code.
Removed (0)
A comment or unneeded white space.
Where the line is: Removed: a human needs it, but the translated program does not.
Make the call before you read the reason.
Lexical analysis · Dry run
Watch the lexer read pay = 8 + tip
This table is the lexer reading text. Nothing is being run.
Press Next. At the marked steps, make the call before the table shows it.
pay = 8 + tip
| Character read | Fragment so far | Token emitted | Stream so far |
|---|---|---|---|
| p | p |
Step 1. Worked for you: the lexer reads p and starts a fragment.
Ask: does the next character carry on the fragment, or end it?
WHAT YOU'VE LEARNED
A quick recap of today's lesson.
The first stage of compilation: keep what means something, bin what only a human needs.
What you need to know
- Lexical analysis is the first stage of compilation, when a compiler translates a high-level language program.
- It works directly on the source code, reading it one character at a time.
- As it reads, it breaks the code up into tokens.
- A token is the smallest meaningful unit of a program.
Have a goSam says rate=7 is ONE token, because there are no spaces in it. Is Sam right? How many tokens do you think it holds?
Sam is wrong. It holds three tokens: rate, = and 7.
A token is the smallest meaningful unit, and rate, = and 7 are each one. Spaces don't decide where tokens end.
- Tokens come in kinds: keywords, identifiers (such as variable names), operators, literal values and punctuation symbols.
Have a goName the kind of token for each of these: speed, +, 30.
speed is an identifier, + is an operator, 30 is a literal.
speed is a variable-style name, + is an operator, and 30 is a literal value written straight into the code.
- Comments are removed, because they are for human readers and the program doesn't need them.
- Unneeded white space goes too: extra spaces, tabs and blank lines carry no meaning for the translated program.
Have a goTwo programmers type exactly the same code. One adds loads of comments and blank lines; the other adds none. Which one gets the shorter token stream?
Neither. Both streams are the same.
Comments and unneeded white space are removed, so they never reach the stream of tokens.
- What comes out is a stream of tokens, handed on to the next stage of compilation.
The big picture
Lexical analysis is the first stage of compilation. It reads the source code one character at a time, breaks it up into tokens, removes comments and unneeded white space, and passes a stream of tokens on to the next stage.
Key points
Worked example
Problem
Lexical analysis reads this line: limit = 5 # max tries. What stream of tokens does it pass on, and what does it throw away?
⚠ Watch out
Thinking a token is a single character, or whatever sits between two spaces. A token is the smallest meaningful unit, so count=count+10 gives five tokens (count, =, count, +, 10) even though it has no spaces and fourteen characters.
Memory hook
Tokens go through. Notes and gaps go in the bin.
Check yourself
Close your eyes (or cover the page). What goes into lexical analysis, what comes out, and what gets thrown away on the way?
Flashcards
(9)Where does lexical analysis come in compilation?
How does lexical analysis read the source code?
What is a token?
Name the five kinds of token.
Keyword or identifier: which one does the programmer choose?
What is a literal?
Why are comments removed?
Which white space is removed, and why?
What does lexical analysis produce?
Tap any card to flip it, or use Study as deck to go through them one at a time. In the full lesson these run as a spaced-repetition deck — you rate each card Hard, Good or Easy and the tricky ones keep coming back until they stick.
Learning with Lightbulb is opening soon
You can use this lesson now. Join the waitlist and we'll let you know when the full Lightbulb experience is ready.
Keep me postedMore Eduqas GCSE Computer Science topics
How this lesson was checked. This Eduqas GCSE Computer Science (specification C500QS)lesson was published through Lightbulb Learning's human-designed editorial process — the educational standards, accuracy rules and publication checks it must pass were authored and approved by Philip Halpin. It passed subject-specific assessment, automated educational checks and technical publication verification before going live (publication checks completed 16 November 2026). Published pages are monitored, human spot-checking is ongoing across the lesson library, and anything found wrong is corrected or withdrawn. How our lessons are made and checked. Spotted a mistake? Email hello@lightbulblearning.co and we'll review it.