Syntax error: Difference between revisions

From Wikipedia, the free encyclopedia
Jump to navigation Jump to search
imported>Kvng
destub
imported>ClueBot NG
m Reverting possible vandalism by Thaliaoliviasiboro to version by Frood. Report False Positive? Thanks, ClueBot NG. (4426866) (Bot)
 
Line 2: Line 2:
{{for|the 2003 Australian film|Syntax Error (film)}}
{{for|the 2003 Australian film|Syntax Error (film)}}


In [[computer science]], a '''syntax error''' is an error in the [[Syntax (programming languages)|syntax]] of a sequence of characters that is intended to be written in a particular [[programming language]].
A '''syntax error''' is a mismatch in the [[Syntax (programming languages)|syntax]] of [[data]] [[input 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 (program lifecycle phase)|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.


For [[compiled language]]s, syntax errors are detected at [[compiler|compile-time]]. A program will not compile until all syntax errors are corrected. For [[interpreted language]]s, a syntax error may be detected during [[Run time (program lifecycle phase)|program execution]], and an interpreter's error messages might not differentiate syntax errors from errors of other kinds.
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 (programming language)|Java]] 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 [[Programming language#Static semantics|static semantic]] 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>


There is some disagreement as to just what errors are "syntax errors". For example, some would say that the use of an uninitialized variable's value in Java code is a syntax error, but many others would disagree<ref>[https://stackoverflow.com/questions/8803718/issue-of-syntax-or-semantics/8803765#8803765 Issue of syntax or semantics?]</ref><ref name="uninitialized var" /> and would classify this as a [[Programming language#Static semantics|(static) semantic]] error.
==Examples==


In 8-bit [[home computer]]s that used [[BASIC]] interpreter as their primary user interface, the {{mono|SYNTAX ERROR}} [[error message]] became somewhat notorious, as this was the response to any command or user input the interpreter could not parse.
===In Java===
A syntax error can occur or take place, when an invalid equation is being typed on a calculator. This can be caused, for instance, by opening brackets without closing them, or less commonly, entering several [[decimal point]]s in one number.
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.


In [[Java (programming language)|Java]] the following is a syntactically correct statement:
<syntaxhighlight lang="java">
:<syntaxhighlight lang="java">System.out.println("Hello World");</syntaxhighlight>
System.out.println(Hello World);
</syntaxhighlight>


while the following is not:
Valid syntax is:
:<pre>System.out.println(Hello World);</pre>


The second example would theoretically print the variable <code>Hello World</code> instead of the [[string literal|words]] "Hello World". A variable in Java cannot have a space in between, so the syntactically correct line would be <code>System.out.println(Hello_World)</code>.
<syntaxhighlight lang="java">
System.out.println("Hello World");
</syntaxhighlight>


A compiler will flag a syntax error when given source code that does not meet the requirements of the language's grammar.
===In Lisp===
The code <code>(add 1 1)</code> is a syntactically valid [[Lisp (programming language)|Lisp]] program (assuming the 'add' function exists) that adds 1 and 1.  


Type errors (such as an attempt to apply the ++ increment operator to a Boolean variable in Java) and undeclared variable errors are sometimes considered to be syntax errors when they are detected at compile-time. It is common to classify such errors as [[Programming language#Static semantics|(static) semantic]] errors instead.<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>
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]].
 
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 [[Programming language#Semantics|semantic]] 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 (programming language)|Python]] 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===


==Syntax errors on calculators==
[[File:Syntax error.JPG|thumb|Syntax error in a scientific calculator]]
[[File:Syntax error.JPG|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 is one of several types of errors on [[calculator]]s (most commonly found on [[scientific calculator]]s and [[graphing calculator]]s), representing that the [[equation]] that has been input has incorrect syntax of numbers, operations and so on. It can result in various ways, including but not limited to:
<!-- 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.-->
*An open [[Bracket#usage in mathematics|bracket]] without closing parenthesis (unless missing closing parenthesis is at very end of equation)
A syntax error can occur on a [[calculator]] (especially a [[scientific calculator|scientific]] or [[graphing calculator|graphing]] calculator) when the input [[equation]] is incorrect in ways such as:
*Using [[minus sign]] instead of negative symbol (or vice versa), which are distinct on most scientific calculators. Note that while some scientific calculators allow a minus sign to stand in for a negative symbol, the reverse is less common.
*Invalid number or operation
*Open [[Bracket#usage in mathematics|bracket]] without closing
*Using [[minus sign]] instead of negative symbol (or vice versa)


==See also==
==See also==

Latest revision as of 14:11, 29 September 2025

Template:Short description Script error: No such module "For".

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]

  1. REDIRECT Template:En dash

Template:R protected 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");

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.[5][6][2]

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

  1. REDIRECT Template:En dash

Template:R protected during parsing (phrase analysis)

  1. REDIRECT Template:En dash

Template:R protected 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

File:Syntax error.JPG
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

<templatestyles src="Reflist/styles.css" />

  1. Issue of syntax or semantics?
  2. a b c Semantic Errors in Java
  3. Script error: No such module "citation/CS1". Section 4.1.3: Syntax Error Handling, pp.194–195.
  4. Script error: No such module "citation/CS1". Exercise 1.3, pp.27–28.
  5. Script error: No such module "citation/CS1".Section 4.1.3: Syntax Error Handling, pp.194–195.
  6. Script error: No such module "citation/CS1". Exercise 1.3, pp.27–28.

Script error: No such module "Check for unknown parameters".