<p>In my programming language, <a href="https://flatassembler.github.io/AEC_specification.html#Branching" rel="nofollow noreferrer">AEC</a>, unlike in Ada or VHDL, there is no semicolon after the <code>EndIf</code> token. That is, you can put the semicolon <code>;</code> after <code>EndIf</code> if you want to, but you do not have to. I often put it so that ClangFormat does not get confused when formatting the code written in my programming language. When programming in VHDL, Ada or Pascal, it bothered me why I needed to put a semi-colon after <code>end if</code> (ADA and VHDL) or <code>end</code> (Pascal). I mean, <code>EndIf</code> by itself means an end of a statement, so why put another sign for ending a statement there? Is it not some kind of a pleonasm? I have asked a <a href="https://www.quora.com/Why-does-Pascal-require-a-semi-colon-after-end-and-Ada-and-VHDL-require-it-after-end-if-and-end-loop-Is-not-it-a-pleonasm-My-programming-language-allows-after-EndIf-and-EndWhile-but-it-does-not-require-it" rel="nofollow noreferrer">Quora question</a> about that, and I received an answer that it is to make recovery from parsing errors easier.</p>
<p>Can you please explain that answer in more details? I have hand-written a parser for my programming language, and I fail to see how it can make error recovery easier. You can see the parser for my programming language <a href="https://sourceforge.net/p/aecforwebassembly/code/ci/master/tree/parser.cpp" rel="nofollow noreferrer">here</a>, if it helps.</p>

<syntax><parsing>
How exactly does requiring a semicolon after `EndIf` and `EndWhile` make recovering from parsing errors easier?
<p>I'm not a maintainer of Ada or VHDL, but I somewhat disagree with the Quora answer. I have a few alternative hypotheses.</p>
<p>First, a couple clarifications.</p>
<p>An <strong>expression</strong> is any sequence of code that <em>returns</em> a value. A <strong>statement</strong> can be an expression, but it can also return nothing (such as an assignment in languages that don't return anything).</p>
<p>So in something like Rust, the <code>if</code> in this statement is an expression:</p>
<pre><code>let y = if 12 * 15 &gt; 150 {
    &quot;Bigger&quot;
} else {
    &quot;Smaller&quot;
};
</code></pre>
<p>(<a href="https://doc.rust-lang.org/reference/expressions/if-expr.html" rel="nofollow noreferrer">Original source</a>)</p>
<p>But also in Rust, the <code>if</code> in this statement is the statement itself:</p>
<pre><code>if x == 4 {
    println!(&quot;x is four&quot;);
} else if x == 3 {
    println!(&quot;x is three&quot;);
} else {
    println!(&quot;x is something else&quot;);
}
</code></pre>
<p>Allowing <code>if</code> to be an expression has consequences for parsing.This means that they can be embedded inside other expressions, which means that the end of the <code>if</code> may <em>not</em> be the end of a statement, so you may need some other way to disambiguate it. You see this in the Rust code, where the one that is an expression has a semicolon after, but the statement one does not.</p>
<p>IIRC, in Ada specifically, <code>if</code> statements can be <em>expressions</em>, so it uses semicolons to do this. Yes, this is not for recovery from parsing errors directly, but having the semicolon does help in that situation to let the parser know that that <code>if</code> is done, <em>really</em> done, and it can now reset.</p>
<p>VHDL is harder to figure out, but I have a guess.</p>
<p>First, the use of semicolons has two different flavors across languages. Some use it as a <em>terminator</em>, but some use it as a <em>separator</em>.</p>
<p>If you use the semicolon as a <em>terminator</em>, you expect it to be at the end of <em>every</em> statement for which it is needed. If you use it as a <em>separator</em>, you expect it to only be <em>between</em> statements.</p>
<p>So in a terminator language, you would write:</p>
<pre><code>x = 1;
y = 2;
</code></pre>
<p>But in a separator language, you could write:</p>
<pre><code>x = 1;
y = 2
</code></pre>
<p>if <code>y = 2</code> is the last statement in the block.</p>
<p>Rust uses this to its advantage; the last statement in a block becomes an expression <a href="https://web.mit.edu/rust-lang_v1.25/arch/amd64_ubuntu1404/share/doc/rust/html/book/first-edition/functions.html" rel="nofollow noreferrer">if it does not have a semicolon after</a>.</p>
<p>VHDL appears to be a <em>terminator</em> language, but this does not automatically mean that <em>every</em> statement needs a semicolon after.</p>
<p>Two examples of languages that are terminator languages, but that don't need a semicolon after every statement, are C and C++. They need a semicolon after every <em>non-compound</em> statement (including for single statement bodies of <code>if</code> and loops), but they do not need a terminator after compound statements like braced <code>if</code> and loop bodies.</p>
<p>This is purely a preference design decision, but it does have a <em>slight</em> consequence on the parser.</p>
<p>In C and C++, they have to check for two token types to see the end of a statement: right brace and semicolon. VHDL only needs to check for one.</p>
<p>Does it matter? Maybe, maybe not. Only the language designer can decide, and in your case, you decided that it does not matter. This is fine! I think you should feel confident in that decision.</p>

CC BY-SA 4.0
2023-05-17T19:43:16.543Z
2023-05-17T19:43:16.543Z
Gavin D. Howard
# How exactly does requiring a semicolon after `EndIf` and `EndWhile` make recovering from parsing errors easier?
Posted by: username_0 on 2023-05-17
In my programming language, [AEC](https://flatassembler.github.io/AEC_specification.html#Branching), unlike in Ada or VHDL, there is no semicolon after the `EndIf` token. That is, you can put the semicolon `;` after `EndIf` if you want to, but you do not have to. I often put it so that ClangFormat does not get confused when formatting the code written in my programming language. When programming in VHDL, Ada or Pascal, it bothered me why I needed to put a semi-colon after `end if` (ADA and VHDL) or `end` (Pascal). I mean, `EndIf` by itself means an end of a statement, so why put another sign for ending a statement there? Is it not some kind of a pleonasm? I have asked a [Quora question](https://www.quora.com/Why-does-Pascal-require-a-semi-colon-after-end-and-Ada-and-VHDL-require-it-after-end-if-and-end-loop-Is-not-it-a-pleonasm-My-programming-language-allows-after-EndIf-and-EndWhile-but-it-does-not-require-it) about that, and I received an answer that it is to make recovery from parsing errors easier.

Can you please explain that answer in more details? I have hand-written a parser for my programming language, and I fail to see how it can make error recovery easier. You can see the parser for my programming language [here](https://sourceforge.net/p/aecforwebassembly/code/ci/master/tree/parser.cpp), if it helps.
**Tags:** syntax parsing
---
## 1 answers
### Answer by username_1 on 2023-05-17. Score: 4 | Accepted answer
I'm not a maintainer of Ada or VHDL, but I somewhat disagree with the Quora answer. I have a few alternative hypotheses.
First, a couple clarifications.
An expression is any sequence of code that returns a value. A statement can be an expression, but it can also return nothing (such as an assignment in languages that don't return anything).
So in something like Rust, the if in this statement is an expression:
let y = if 12 * 15 > 150 {
    "Bigger"
} else {
    "Smaller"
};

(Original source)
But also in Rust, the if in this statement is the statement itself:
if x == 4 {
    println!("x is four");
} else if x == 3 {
    println!("x is three");
} else {
    println!("x is something else");
}

Allowing if to be an expression has consequences for parsing.This means that they can be embedded inside other expressions, which means that the end of the if may not be the end of a statement, so you may need some other way to disambiguate it. You see this in the Rust code, where the one that is an expression has a semicolon after, but the statement one does not.
IIRC, in Ada specifically, if statements can be expressions, so it uses semicolons to do this. Yes, this is not for recovery from parsing errors directly, but having the semicolon does help in that situation to let the parser know that that if is done, really done, and it can now reset.
VHDL is harder to figure out, but I have a guess.
First, the use of semicolons has two different flavors across languages. Some use it as a terminator, but some use it as a separator.
If you use the semicolon as a terminator, you expect it to be at the end of every statement for which it is needed. If you use it as a separator, you expect it to only be between statements.
So in a terminator language, you would write:
x = 1;
y = 2;

But in a separator language, you could write:
x = 1;
y = 2

if y = 2 is the last statement in the block.
Rust uses this to its advantage; the last statement in a block becomes an expression if it does not have a semicolon after.
VHDL appears to be a terminator language, but this does not automatically mean that every statement needs a semicolon after.
Two examples of languages that are terminator languages, but that don't need a semicolon after every statement, are C and C++. They need a semicolon after every non-compound statement (including for single statement bodies of if and loops), but they do not need a terminator after compound statements like braced if and loop bodies.
This is purely a preference design decision, but it does have a slight consequence on the parser.
In C and C++, they have to check for two token types to see the end of a statement: right brace and semicolon. VHDL only needs to check for one.
Does it matter? Maybe, maybe not. Only the language designer can decide, and in your case, you decided that it does not matter. This is fine! I think you should feel confident in that decision.
---
