ArticleslgStudy

computer science

Syntax error

Syntax error is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Syntax error rather than just read about it. In short: A syntax error is a mismatch in the syntax of data input to a computer system that requires a specific syntax. For source code in a programming language, a compiler detects syntax errors before the software is run (at compile-time), whereas an interpreter detects syntax errors at run-time.

Syntax error — main illustration
Syntax error — illustration

Key takeaways

  • Syntax error belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Syntax error to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Syntax error from memory before moving on to harder problems.

Reference excerpt

A syntax error is a mismatch in the syntax of data input to a computer system that requires a specific syntax. For source code in a programming language, a compiler detects syntax errors before the software is run (at compile-time), whereas an interpreter detects syntax errors at run-time. A syntax error can occur based on syntax rules other than those defined by a programming language. For example, typing an invalid equation into a calculator (an interpreter) is a syntax error. Some errors that occur during the translation of source code may be considered syntax errors by some but not by others. For example, some say that an uninitialized variable in Java is a syntax error, but others disagree – classifying it as a static semantic error.

Examples

In Java The Java compiler generates a syntax error for the following code since the string is not quoted. The compilation process fails and does not produce a usable executable.

Valid syntax is:

In Lisp The code (add 1 1) is a syntactically valid Lisp program (assuming the 'add' function exists) that adds 1 and 1. However, (_ 1 1) results in syntax error lexical error: '_' is not valid. The lexer is unable to identify the first error – all it knows is that, after producing the token LEFT_PAREN, '(' the remainder of the program is invalid, since no word rule begins with '_'. And, (add 1 1 results in syntax error parsing error: missing closing ')'. The parser identifies the "list" production rule due to the '(' token (as the only match), and thus gives an error message; in general, it may be ambiguous grammar. Type errors and undeclared variable errors are sometimes considered to be syntax errors when they are detected at compile-time (which is usually the case when compiling strongly-typed languages), though it is common to classify these kinds of error as semantic errors instead.

In Python For Python code, 'a' + 1 contains a type error because it adds a string literal to an integer literal. A type error like this can be detected at compile-time – during parsing (phrase analysis) – if the compiler uses separate rules that allow "integer-literal + integer-literal" but not "string-literal + integer-literal", though it is more likely that the compiler will use a parsing rule that allows expressions of the form "literal-or-identifier + literal-or-identifier" and then the error will be detected during contextual analysis (when type checking occurs). In some cases, this validation is not done by the compiler, and these errors are only detected at runtime. In a dynamically typed language, where type can only be determined at runtime, many type errors can only be detected at runtime. For example, for Python a + b is syntactically valid at the phrase level, but the correctness of the types of a and b can only be determined at runtime, as variables do not have types in Python, only values do. Whereas there is disagreement about whether a type error detected by the compiler should be called a syntax error (static semantic), type errors which can only be detected at program execution time are always regarded as semantic rather than syntax errors.

On a calculator

A syntax error can occur on a calculator (especially a scientific or graphing calculator) when the input equation is incorrect in ways such as:

Invalid number or operation Open bracket without closing Using minus sign instead of negative symbol (or vice versa)

See also Tag soup

References

Illustrations

Syntax error: A BASIC program failing due to a syntax error in its code
A BASIC program failing due to a syntax error in its code
Syntax error: Syntax error in a scientific calculator
Syntax error in a scientific calculator

Worked examples

Example 1 — a first encounter with Syntax error

Start with the simplest possible case. Write down what Syntax error claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Syntax error before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Syntax error ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Syntax error

In research
Syntax error appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Syntax error in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Syntax error is common in secondary-school and first-year university syllabi. It links to neighbouring topics Computer errors, Parsing, Programming language syntax, so understanding it makes those chapters shorter.
In everyday life
Look for Syntax error outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Syntax error” →

Affiliate

Preply — study more efficiently by working with a personal tutor. 50% off.

How to study Syntax error in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Syntax error means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Syntax error out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Syntax error in simple terms?

A syntax error is a mismatch in the syntax of data input to a computer system that requires a specific syntax. For source code in a programming language, a compiler detects syntax errors before the software is run (at compile-time), whereas an interpreter detects syntax errors at run-time.

Why does Syntax error matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Syntax error?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Syntax error.

Tags

  • Computer errors
  • Parsing
  • Programming language syntax
  • Programming language theory

Keep exploring