Syntax error

from Wikipedia, the free encyclopedia
Syntax error in a scientific calculator .

Syntax Error ( English syntax error ) are generally violations of the syntax rules of a natural or artificial language. In grammar the term refers to the arrangement of the parts of a sentence, in computer science it usually refers to the so-called context-free part of the syntactic rules of a programming language . Programs with syntax errors are rejected by a compiler or interpreter . A syntactically correct program can lead to runtime errors if it contains semantic errors.

definition

Syntax errors in the grammar of natural languages , for example German, are violations of the rules of the sentence structure, according to which words are combined into larger functional units ( sentences ) and relationships such as part-whole, dependency, etc. are formulated between these clauses .

Syntax errors in programming languages ​​are recognized by the parser in the analysis phase of a compiler run, more precisely in the syntactic analysis . The parser not only has to recognize that the analyzed source code does not correspond to the formal grammar of the programming language, but also output an understandable error message. Since not all properties of a programming language can be described by context-free grammars, some errors, e.g. B. Violations of the type rules of the programming language, also only recognized by the compiler in the semantic analysis . Other errors in thinking in the program cannot be recognized at all at compile time; However, compilers often insert check code there, which leads to a runtime error only during program execution . In contrast to this, semantic errors, i.e. errors in the intended meaning of a program text, cannot be detected automatically at all.

Examples

A common example of syntax errors in German are formulations like

compared the sexually documented frequency of Jack London with that of Erich Mühsams

In grammatical terms , the adverbial definition "sexual" refers to the participle "documented", ie it describes a circumstance of documenting. The intended meaning, however, is different: It is not the documenting that is sexual, but the frequency. So it has to be correct: the documented sexual frequency .

Syntax errors can also arise from an incorrect combination of the meaning levels of the words ( semantics ). For example:

The response times of the security providers are often very fast ...

Times are not fast or slow, but short or long. It has to be correct either The reaction times of the S. are often very short or the reactions of the S. are often very fast ...

Syntax errors in computer programs are often caused by incorrectly placed or missing characters such as semicolons , commas and brackets or meaningful words such as foror BEGIN. Integrated development environments are usually designed in such a way that such errors are recognized immediately and signaled to the programmer.

An example of a syntax error in Java :

1 public class Beispiel {
2     public static void main(String[] args) {
3         System.out.println("Hallo Welt!")
4     }
5 }

In this example, a typical Java compiler would criticize the missing semicolon after the closing bracket in line 3, as required by the Java syntax . The output of the compiler gives the programmer information on the type and approximate location of the error:

Syntax error, insert ";" to complete BlockStatements Beispiel.java line 3

The file name of the source text , the line and the actual error message are usually output. The order and number of outputs varies from compiler to compiler.

Individual evidence

  1. ^ Alfred V. Aho, Ravi Seti, Jeffrey D. Ullmann: Compilerbau . tape 1 . Addison-Wesley, Bonn 1988, ISBN 3-89319-150-X , p. 194 .
  2. Rüdiger Bernhardt in a review of Florian Illies: 1913 - What I really wanted to tell. Our time 11/30/2018
  3. Christian Fenselau: Virus scanners can be dangerous. t-online.de 6.4.2016 ( online )