GCSE · Computer Science · Eduqas · Spec C500QS

Semantic analysis

"The sandwich signed the mortgage." Perfect spelling, perfect grammar, zero meaning. Compilers meet the same problem with code, and they refuse to finish the job.

Computer Science · Compilers

Compile it. Read it. Fix it.

Every line of this program is written correctly. Press Run to compile it, read what the compiler reports, then choose a fix. Keep going until the compiler produces the finished program.

Java (a compiled language)
  1. public class Main {
  2. public static void main(String[] args) {
  3. int score = 10;
  4. String points = "20";
  5. int total = score + bonus;
  6. int lives = "three";
  7. int doubled = points * 2;
  8. }
  9. }

The error wording is what javac (Java 21) reports, shortened: its copy of the code line and its pointer are left out. In this lesson, Run means compile. The "finished program" line is added by the lesson.

Two different questions

Checking the formvsChecking the meaning

Same statement, two checks. Read across each row.

Focus

The question it asks

Checking the form

Is this statement written in a correct, grammatically valid form?

Checking the meaning

Does this statement make sense, and can it be carried out?

The insight

A statement can get a yes to the first question and a no to the second.

A fault it would catch

Checking the form

An expression left unfinished, like 5 + with nothing after it

Checking the meaning

A variable used without ever being declared

What counts as passing

Checking the form

The statement follows the rules for how code is written

Checking the meaning

Variables declared, data types consistent, operations on compatible types

The statement speed = speed + 1;

Checking the form

Correctly formed: nothing is wrong with how it is written

Checking the meaning

Invalid meaning if speed was never declared anywhere in the program

UK note

Eduqas GCSE Computer Science (C500QS): semantic analysis is listed in Component 1, Understanding Computer Science, in the Program construction section on translators.

Computer Science · Compilers

Which fault is it?

Sort each statement by the fault it contains. Some have none.

Still to sort

Variable not declared (0)

A variable is used but never declared.

Where the line is: The name appears with no declaration before it. A declared variable that holds the wrong kind of value is a different fault.

Value does not match declared type (0)

The value given is not the type the variable was declared to hold.

Where the line is: This is about assigning a value to a variable. Combining two values in arithmetic is a different fault.

Operation on incompatible types (0)

An operation is applied to values whose data types cannot be used together.

Where the line is: The operation itself is the problem, such as arithmetic on a string and an integer. Assigning to a variable is a different fault.

Form error, not semantic (0)

The statement is not written in a valid form.

Where the line is: A form error is about how the statement is written. The three semantic faults are about meaning, in statements that are written correctly.

No fault (0)

Passes all three meaning checks.

Where the line is: Everything it uses is declared, its types match and its operation is compatible.

8 of 8 still to sort.

Commit to a category for each statement, then read why.

Computer Science · Compilers

What do you reckon happens?

A programmer has written a program where every statement is correctly formed. One statement uses a variable that was never declared. They compile it.

Which of these is closest to what you think happens?
How sure are you?

Computer Science · Compilers

Explain it in your own words

Explain why a statement can be written correctly and still fail semantic analysis. Give one example. [3 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.

Code can look perfect and still be refused. Find out why.

What you need to know

  • A compiler translates a high-level language program, and it does the job in stages. Semantic analysis is one of those stages.
  • At this stage the compiler checks the meaning of the program: do the statements make sense, and can they be carried out?
  • Meaning goes beyond form. Form is only whether a statement is written in the correct way.
  • Have a goPriya says: "Every line of my program is written correctly, so the compiler has to accept it." Is that safe reasoning?

    No. Correct form does not guarantee valid meaning.

    Semantic analysis checks the meaning of the program as well as its form, so a correctly written statement can still fail that check.

  • Check one: every variable used in the program must have been declared before it is used.
  • Check two, type checking: data types must be used consistently, so a value assigned to a variable matches its declared type.
  • Check three: operations need compatible data types, so no arithmetic on a string and an integer as if both were numbers.
  • Have a goYour friend writes 5 * "apples". Which of the three checks should stop it: declared, types consistent, or compatible operations?

    Compatible operations: arithmetic on an integer and a string.

    A string like "apples" cannot be treated as a number, so the operation is applied to incompatible data types.

  • So a statement can be correctly formed, or grammatically valid, and still fail semantic analysis because its meaning is invalid.
  • Where the meaning is invalid, the compiler reports it to the programmer.
  • It does not produce a finished executable program until the problem has been corrected.
  • Have a goA friend says: "The compiler told me about the problem but built the program anyway, so I will fix it later." What does not add up?

    No finished executable is produced until the problem is corrected.

    When the meaning is invalid, the compiler reports it to the programmer and does not produce a finished executable until it is corrected.

The big picture

Semantic analysis is the stage of compilation where the compiler checks that a program's meaning is valid: variables declared, data types consistent and operations on compatible types. If it is not, the compiler reports it and produces no finished executable until it is corrected.

Key points

1Semantic analysis is a stage of compilation that checks meaning, not just form.
2Three meaning checks: variables declared first, data types consistent, operations on compatible types.
3Correctly formed does not mean valid meaning. A statement needs both.
4Invalid meaning is reported to the programmer, with no finished executable until it is corrected.

Worked example

Problem

The compiler meets these three statements, and all three are written correctly: int price = 4; int cost = price * quantity; String name = 7; Decide whether each one passes semantic analysis and, if it fails, which check it fails.

⚠ Watch out

Believing a program is safe from the compiler as long as every statement is written correctly. Correct form only answers the form question. Semantic analysis still asks whether the statement makes sense: variables declared, data types consistent, operations compatible.

🧠

Memory hook

Spelled right is not the same as makes sense. Put three questions to every statement: Is it declared? Do the types match? Can these values be used together?

✓

Check yourself

Cover the page. Name the three things semantic analysis checks. Then make up a statement that is written correctly but would still be refused, and say which check it fails.

Flashcards

(10)
What is semantic analysis?
A stage of compilation in which the compiler checks the meaning of the program: whether its statements make sense and can be carried out.
What is compilation?
What a compiler does when it translates a high-level language program. It works in stages, and semantic analysis is one of them.
Form or meaning: which does semantic analysis check?
Meaning. Form is only whether a statement is written correctly. Semantic analysis checks that it also makes sense.
Semantic check 1: variables
Every variable used in the program must have been declared before it is used.
What is type checking?
Checking that data types are used consistently, for example that a value assigned to a variable matches the variable's declared data type.
Semantic check 3: operations
Operations must be applied to values of compatible data types. No arithmetic on a string and an integer as if both were numbers.
Can a grammatically valid statement fail semantic analysis?
Yes. It can be correctly formed but have invalid meaning, such as an undeclared variable or mismatched data types.
What happens when semantic analysis finds invalid meaning?
The compiler reports it to the programmer and does not produce a finished executable program until it is corrected.
boolean ready = "yes"; Which check fails?
Type checking. ready is declared as a boolean, but the value assigned to it is a string.
A variable is used on line 5 but never declared. Where does the fix go?
A declaration of it earlier, before the line that uses it.

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.