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.

8 of 8 still to sort.

Make the call before you read the reason.

Exam line: A fragment either becomes a token of one of five kinds, or it is removed because it carries no meaning for the translated program.
Watch out: Two characters can still be ONE token: >= is a single operator, and 16 is a single literal.

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 readFragment so farToken emittedStream so far
pp

Step 1. Worked for you: the lexer reads p and starts a fragment.

Step 1 of 14Called right: 0 of 3

Ask: does the next character carry on the fragment, or end it?

Lexical analysis · Which idea is yours?

Three ideas about tokens that sound sensible

Two versions of the same line: count=count+10 and count = count + 10 # add ten.

Which is closest to what you think right now?
How sure are you?

Lexical analysis · Say it yourself

Now put it in your own words

Describe what happens during lexical analysis. [5 marks]

0 words · your answer stays on this page and is not sent anywhere.

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

1Lexical analysis is the first stage of compilation and works directly on the source code.
2The code is read character by character and broken up into tokens.
3A token is the smallest meaningful unit: a keyword, identifier, operator, literal or punctuation symbol.
4Comments and unneeded white space are removed because they carry no meaning for the translated program.
5The output is a stream of tokens, passed to the next stage.

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?
It is the first stage, working directly on the source code.
How does lexical analysis read the source code?
Character by character, breaking it up into tokens as it goes.
What is a token?
The smallest meaningful unit of a program.
Name the five kinds of token.
Keyword, identifier, operator, literal, punctuation symbol.
Keyword or identifier: which one does the programmer choose?
The identifier, such as a variable name. A keyword is a word the language itself gives a fixed meaning.
What is a literal?
A value written directly in the code, such as 16.
Why are comments removed?
They are for human readers and are not needed to produce the program.
Which white space is removed, and why?
Unneeded spaces, tabs and blank lines, because they carry no meaning for the translated program.
What does lexical analysis produce?
A stream of tokens, passed on to the next stage of compilation.

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 posted

More Eduqas GCSE Computer Science topics

See the full Eduqas Computer Science curriculum →

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.