<p>In the strictest sense, there is <strong>no real difference</strong> between syntax errors and semantics errors, at least as far as <a href="https://en.wikipedia.org/wiki/Formal_language" rel="noreferrer">language theory</a> is concerned: the only salient difference is the <strong>complexity of the automaton</strong> required to recognize that language, with, e.g.</p>

<ul>
<li>Context-free languages only requiring pushdown automata (PDA)</li>
<li>General recursive languages requiring full Turing machines (TM)</li>
</ul>

<p>If every "syntactic task" merely required pushdown automata and every "semantic task" something more, then the matter would be settled, but unfortunately there are many tasks that are associated with programming language syntax that cannot be handled by PDA alone, and conversely, some semantic tasks which seem to require only weak computation power. Still, the distinction provides some intuition.</p>

<p>Pragmatically, compilers are designed in phases, which makes managing the task of generating executable code manageable. I particularly like this diagram from the Compcert website.</p>

<p><a href="https://i.sstatic.net/Wz8Gs.png" rel="noreferrer"><img src="https://i.sstatic.net/Wz8Gs.png" alt="Compcert Arch"></a></p>

<p>Typically, the first 1-3 phases turn what is essentially a stream of bytes or tokens into something more naturally seen as a tree structure. Everything before the tree creation will be labelled as "syntax" and everything after as "semantics".</p>

<p>In C, the syntactic phases are typically <a href="https://en.wikipedia.org/wiki/Lexical_analysis#Tokenization" rel="noreferrer">tokenization</a>, <a href="https://en.wikipedia.org/wiki/C_preprocessor" rel="noreferrer">macro expansion</a> and <a href="https://en.wikipedia.org/wiki/Parsing" rel="noreferrer">parsing</a>, everything after that will be semantics. Again, opinions may differ, since there is no accepted formal distinction.</p>

<hr>

<p>I think there's a larger discussion to be had about how to distinguish syntactic notions from semantic notions in the field of formal logic, but I'm not sure I feel qualified to have it.</p>

CC BY-SA 4.0
2018-05-15T20:35:46.683Z
Thanks for your reply! About the language theory perspective you mentioned, can I say argument like below?

Since there's a compiler that accepts strings that pass syntax & semantics checking and rejects strings that don't, then it means there is such a program that can do this, which means there's a recursive language for it (even including things like type checking)
Rui Liu
CC BY-SA 4.0
2018-05-15T22:24:55.000Z
Yes, that is a correct reasoning, any compiler which can accept or reject an input string that may represent a program is effectively a proof that the well-formed programs are a recursive language.
cody
CC BY-SA 4.0
2018-05-15T19:16:23.223Z
2018-05-15T19:16:23.223Z
cody
<p>It is hard to say. The boundary between "syntactic errors" and "<em>static</em> semantic errors" can be really blurred.</p>

<p>One appropriate example would be Curry-style (extrinsic typing) and Church-style (intrinsic typing) variants of the same programming language, say System F. Failing to include type annotations in the Church-style variant of the language would be a syntactic error as well as a semantic error (because types are a part of the syntax <em>and</em> the semantics), while failing to include them in the Curry-style variant might or might not be a semantic error (type inference for System F is undecidable, so who knows if this is an error), but never a syntax error.</p>

<p>Now you'd see that, in many cases, it's hard to draw the line between certain classes of static semantic and syntactic errors. We might like to think of syntactically incorrect programs as "not even wrong" and semantically incorrect programs as "meaningless", but there are certain tricky cases. You can check for re-declaration of variables in either the parsing (I'm actually not sure but theoretically should be possible) or the static semantic check phase. Is it a syntactic or semantic issue? I don't know. It depends on the formal description of syntax and semantics if there exists one, but in the absence of one it all becomes very difficult.</p>

CC BY-SA 4.0
2018-05-16T08:07:01.930Z
2018-05-16T08:07:01.930Z
xrq
<p>To simplify all that I've read about, basically:
The syntax of a programming language is a collection of rules to specify the structure or form of code whereas semantics refers to the interpretation of the code or the associated meaning of the symbols, characters or any part of a program.</p>

<p>True??</p>

CC BY-SA 4.0
2020-02-21T07:48:09.553Z
2020-02-21T07:48:09.553Z
Nigel Veneto
<p>When compilers generate errors for a specific programming language, there's distinction between syntax &amp; semantic errors. E.g. <code>) + f 3</code> has ill-formed syntax, but type checking is considered as part of semantic analysis.</p>

<p>My intuition tells me that, by setting more constraints on the language syntax, some semantic errors can become syntax errors. And my question is if there are any intrinsic differences between syntax and semantics? Or are they just checking performed on different levels, for example syntax checking is a rather local checking while semantic checking is global? Is there anything fundamental that makes semantics checking different from syntax checking? Is it possible to embed all semantic checking within syntax? Are there any theories related to these questions?</p>

<p>It may also be the case that my intuition is wrong - if that's the case, please also explain to me why that is wrong.</p>

<pl.programming-languages><semantics><parsing><compilers>
Difference between syntax and semantic error in programming languages
Rui Liu
# Difference between syntax and semantic error in programming languages

(Asked by: username_0 on 2018-05-15)

When compilers generate errors for a specific programming language, there's distinction between syntax & semantic errors. E.g. `) + f 3` has ill-formed syntax, but type checking is considered as part of semantic analysis.

My intuition tells me that, by setting more constraints on the language syntax, some semantic errors can become syntax errors. And my question is if there are any intrinsic differences between syntax and semantics? Or are they just checking performed on different levels, for example syntax checking is a rather local checking while semantic checking is global? Is there anything fundamental that makes semantics checking different from syntax checking? Is it possible to embed all semantic checking within syntax? Are there any theories related to these questions?

It may also be the case that my intuition is wrong - if that's the case, please also explain to me why that is wrong.

**Tags:** pl.programming-languages, semantics, parsing, compilers


---

## 3 answers

### Answer by username_1 on 2018-05-15. Score: 7 | Accepted answer

In the strictest sense, there is **no real difference** between syntax errors and semantics errors, at least as far as [language theory](https://en.wikipedia.org/wiki/Formal_language) is concerned: the only salient difference is the **complexity of the automaton** required to recognize that language, with, e.g.

* Context-free languages only requiring pushdown automata (PDA)
* General recursive languages requiring full Turing machines (TM)

If every "syntactic task" merely required pushdown automata and every "semantic task" something more, then the matter would be settled, but unfortunately there are many tasks that are associated with programming language syntax that cannot be handled by PDA alone, and conversely, some semantic tasks which seem to require only weak computation power. Still, the distinction provides some intuition.

Pragmatically, compilers are designed in phases, which makes managing the task of generating executable code manageable. I particularly like this diagram from the Compcert website.

[![Compcert Arch](https://i.sstatic.net/Wz8Gs.png)](https://i.sstatic.net/Wz8Gs.png)

Typically, the first 1-3 phases turn what is essentially a stream of bytes or tokens into something more naturally seen as a tree structure. Everything before the tree creation will be labelled as "syntax" and everything after as "semantics".

In C, the syntactic phases are typically [tokenization](https://en.wikipedia.org/wiki/Lexical_analysis#Tokenization), [macro expansion](https://en.wikipedia.org/wiki/C_preprocessor) and [parsing](https://en.wikipedia.org/wiki/Parsing), everything after that will be semantics. Again, opinions may differ, since there is no accepted formal distinction.

---

I think there's a larger discussion to be had about how to distinguish syntactic notions from semantic notions in the field of formal logic, but I'm not sure I feel qualified to have it.

> **Comments:**
> **username_0**: Thanks for your reply! About the language theory perspective you mentioned, can I say argument like below?  Since there's a compiler that accepts strings that pass syntax & semantics checking and rejects strings that don't, then it means there is such a program that can do this, which means there's a recursive language for it (even including things like type checking)  
> **username_1**: Yes, that is a correct reasoning, any compiler which can accept or reject an input string that may represent a program is effectively a proof that the well-formed programs are a recursive language.  

---

### Answer by username_2 on 2018-05-16. Score: 2

It is hard to say. The boundary between "syntactic errors" and "*static* semantic errors" can be really blurred.

One appropriate example would be Curry-style (extrinsic typing) and Church-style (intrinsic typing) variants of the same programming language, say System F. Failing to include type annotations in the Church-style variant of the language would be a syntactic error as well as a semantic error (because types are a part of the syntax *and* the semantics), while failing to include them in the Curry-style variant might or might not be a semantic error (type inference for System F is undecidable, so who knows if this is an error), but never a syntax error.

Now you'd see that, in many cases, it's hard to draw the line between certain classes of static semantic and syntactic errors. We might like to think of syntactically incorrect programs as "not even wrong" and semantically incorrect programs as "meaningless", but there are certain tricky cases. You can check for re-declaration of variables in either the parsing (I'm actually not sure but theoretically should be possible) or the static semantic check phase. Is it a syntactic or semantic issue? I don't know. It depends on the formal description of syntax and semantics if there exists one, but in the absence of one it all becomes very difficult.


---

### Answer by username_3 on 2020-02-21. Score: -2

To simplify all that I've read about, basically:
The syntax of a programming language is a collection of rules to specify the structure or form of code whereas semantics refers to the interpretation of the code or the associated meaning of the symbols, characters or any part of a program.

True??


---

