Jump to content

Syntax error

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Stevebroshar (talk | contribs) at 15:09, 9 July 2025 (Edit for brevity). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

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[1][2] – classifying it as a static semantic error.[2][3][4]

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.

System.out.println(Hello World);

Valid syntax is:

System.out.println("Hello World");

On a calcualtor

Syntax error in a scientific 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

References

  1. ^ Issue of syntax or semantics?
  2. ^ a b Semantic Errors in Java
  3. ^ Aho, Alfred V.; Monica S. Lam; Ravi Sethi; Jeffrey D. Ullman (2007). Compilers: Principles, Techniques, and Tools (2nd ed.). Addison Wesley. ISBN 978-0-321-48681-3. Section 4.1.3: Syntax Error Handling, pp.194–195.
  4. ^ Louden, Kenneth C. (1997). Compiler Construction: Principles and Practice. Brooks/Cole. ISBN 981-243-694-4. Exercise 1.3, pp.27–28.