# Syntax error

> Mediated Wiki article. Canonical URL: https://mediated.wiki/source/Syntax_error
> Markdown URL: https://mediated.wiki/source/Syntax_error.md
> Source: https://en.wikipedia.org/wiki/Syntax_error
> Source revision: 1344928863
> License: Creative Commons Attribution-ShareAlike 4.0 International (https://creativecommons.org/licenses/by-sa/4.0/)

{{Short description|Computer science concept}}
{{for|the 2003 Australian film|Syntax Error (film)}}
[[File:Codebeispiel - Syntax error.png|thumb|A [BASIC](/source/BASIC) program failing due to a syntax error in its code]]
A '''syntax error''' is a mismatch in the [syntax](/source/Syntax_(programming_languages)) of [data](/source/data) [input](/source/input_data) to a [computer system](/source/computer_system) that requires a specific syntax. For [source code](/source/source_code) in a [programming language](/source/programming_language), a [compiler](/source/compiler) detects syntax errors before the software is run (at compile-time), whereas an [interpreter](/source/interpreter) detects syntax errors at [run-time](/source/Run_time_(program_lifecycle_phase)). 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](/source/Java_(programming_language)) is a syntax error, but others disagree<ref>[https://stackoverflow.com/questions/8803718/issue-of-syntax-or-semantics/8803765#8803765 Issue of syntax or semantics?]</ref><ref name="uninitialized var" /> {{endash}} classifying it as a [static semantic](/source/Programming_language) error.<ref name="uninitialized var">[http://www.dummies.com/how-to/content/semantic-errors-in-java.html Semantic Errors in Java]</ref><ref>{{cite book|last=Aho|first=Alfred V.|author2=Monica S. Lam|author3=Ravi Sethi|author4=Jeffrey D. Ullman|title=Compilers: Principles, Techniques, and Tools|publisher=Addison Wesley|date=2007|edition=2nd|isbn=978-0-321-48681-3|url-access=registration|url=https://archive.org/details/compilers00alfr_0}} Section 4.1.3: Syntax Error Handling, pp.194&ndash;195.</ref><ref>{{cite book|last=Louden|first=Kenneth C.|title=Compiler Construction: Principles and Practice|publisher=Brooks/Cole|date=1997|isbn=981-243-694-4}} Exercise 1.3, pp.27&ndash;28.</ref>

==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.

<syntaxhighlight lang="java">
System.out.println(Hello World);
</syntaxhighlight>

Valid syntax is:

<syntaxhighlight lang="java">
System.out.println("Hello World");
</syntaxhighlight>

===In Lisp===
The code <code>(add 1 1)</code> is a syntactically valid [Lisp](/source/Lisp_(programming_language)) program (assuming the 'add' function exists) that adds 1 and 1. 

However, {{code|(_ 1 1)}} results in syntax error {{code|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, {{code|(add 1 1}} results in syntax error {{code|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](/source/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](/source/Programming_language) errors instead.<ref>{{cite book|last=Aho|first=Alfred V.|author2=Monica S. Lam|author3=Ravi Sethi|author4=Jeffrey D. Ullman|title=Compilers: Principles, Techniques, and Tools|publisher=Addison Wesley|date=2007|edition=2nd|isbn=978-0-321-48681-3|url=https://archive.org/details/compilers00alfr_0}}Section 4.1.3: Syntax Error Handling, pp.194&ndash;195.</ref><ref>{{cite book|last=Louden|first=Kenneth C.|title=Compiler Construction: Principles and Practice|publisher=Brooks/Cole|date=1997|isbn=981-243-694-4}} Exercise 1.3, pp.27&ndash;28.</ref><ref name="uninitialized var">[http://www.dummies.com/how-to/content/semantic-errors-in-java.html Semantic Errors in Java]</ref>

===In Python===

For [Python](/source/Python_(programming_language)) code, {{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 {{endash}} during parsing (phrase analysis) {{endash}} 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 {{code|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===

thumb|Syntax error in a scientific calculator
<!-- This section may need sources so it is not just original research, as well as better wording on what a syntax error is on calculators, or be removed, though it is probably important to cover this type of syntax error.-->
A syntax error can occur on a [calculator](/source/calculator) (especially a [scientific](/source/scientific_calculator) or [graphing](/source/graphing_calculator) calculator) when the input [equation](/source/equation) is incorrect in ways such as:
*Invalid number or operation
*Open [bracket](/source/Bracket) without closing
*Using [minus sign](/source/minus_sign) instead of negative symbol (or vice versa)

==See also==
*[Tag soup](/source/Tag_soup)

==References==
{{Reflist}}

Error
Category:Computer errors
Category:Parsing
Category:Programming language theory

---
Adapted from the Wikipedia article [Syntax error](https://en.wikipedia.org/wiki/Syntax_error) by Wikipedia contributors ([contributor history](https://en.wikipedia.org/wiki/Syntax_error?action=history)). Available under [Creative Commons Attribution-ShareAlike 4.0 International](https://creativecommons.org/licenses/by-sa/4.0/). Changes may have been made.
