Syntax error: Difference between revisions
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)}} | ||
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 [[ | 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–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–28.</ref> | ||
==Examples== | |||
In | ===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 (programming language)|Lisp]] 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]]. | |||
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–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–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=== | |||
[[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 | <!-- 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]] (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) | *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]
- 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
- REDIRECT Template:En dash
Template:R protected during parsing (phrase analysis)
- 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
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" />
- ↑ Issue of syntax or semantics?
- ↑ a b c Semantic Errors in Java
- ↑ Script error: No such module "citation/CS1". Section 4.1.3: Syntax Error Handling, pp.194–195.
- ↑ Script error: No such module "citation/CS1". Exercise 1.3, pp.27–28.
- ↑ Script error: No such module "citation/CS1".Section 4.1.3: Syntax Error Handling, pp.194–195.
- ↑ Script error: No such module "citation/CS1". Exercise 1.3, pp.27–28.
Script error: No such module "Check for unknown parameters".