1

tl;dr

I want to understand the logic by which I can make the following comments about a non-working code:

  • Oh, it's obvious it fails, there's syntactic error there at line 5
  • Mmm, look, I think between lines 10 and 13 there's a semantic error
  • No, you cannot call a variable a±b, that's a lexical error
  • At line 50 there's a X error, for any X type of error beside semantic and syntactic.

Original question

I add the C++ because I use an example from this language, and the language-lawyer tag in case the answer is language-dependent, even though my question is more general.

If I write this code in C++,

x x x;

it won't compile with error like this

$ g++ -std=c++17 uffa.cpp && ./a.out 
uffa.cpp: In function ‘int main()’:
uffa.cpp:4:6: error: expected ‘;’ before ‘x’
    4 |     x x x;
      |      ^~
      |      ;

My understanding is that x x x; is just syntactically wrong; ill-formed is a better term? Or syntactically ill-formed?

On the other hand, code this

    s.append("!");

can be valid, e.g. if it is preceded by std::string s{"Ciao"};, or invalid, e.g. if preceded by int s{3};, in the latter case the error being like this

$ g++ -std=c++17 uffa.cpp && ./a.out 
uffa.cpp: In function ‘int main()’:
uffa.cpp:5:7: error: request for member ‘append’ in ‘s’, which is of non-class type ‘int’
    5 |     s.append("!");
      |       ^~~~~~

Despite the errors have the same "shape", my understanding is that this latter error has to do with the code being invalid at a different "level" than it is in the first example. Is it semantically invalid/incorrect/ill-formed?

How does this "classification" of errors relate to SFINAE and if constexpr?

Enlico
  • 23,259
  • 6
  • 48
  • 102
  • Not sure, but maybe this answers your question: https://stackoverflow.com/q/22180312/1387438 (can't propose a dupe since with my privileges it will close question immediately). – Marek R Mar 08 '21 at 08:15
  • SF from SFINAE tends for "Substitution Failure", so replacing a template dependent `s`/`x` by a specific type(/value/template template parameter). indeed `x x x;` is invalide for any template, so would be hard error, whereas `s.append("!");` fails with *Substitution*. – Jarod42 Mar 08 '21 at 08:48
  • @Jarod42, that's the point. In my post I've just mentioned _syntax_ and _semantic_, but it looks like there's also _hardness/softness_, and other terms. – Enlico Mar 08 '21 at 08:51
  • There are also lexical errors, i.e. illegal characters, and ill-formed tokens; and also UB. – user207421 Mar 08 '21 at 09:12
  • 2
    @KamilCuk It is not about the ordinary English meanings of 'syntax' and 'semantics' since the Chomsky Hierarchy (1956) and Knuth *On the parsing of languages from left to right* (1965) and I guess van Weingarten. These are now terms of art in compiler construction and language design. – user207421 Mar 08 '21 at 09:16
  • @KamilCuk Of course it is about the meanings of 'syntax' and 'semantics'. What else could it be about? I quote: 'Is there any other level at which a program can be correct or incorrect?' – user207421 Mar 08 '21 at 09:18
  • @user207421, indeed it is about the meaning of "syntax" (about which I have probably no/few doubts), of "semantic" (about which I have many more doubts), and of "any other level". – Enlico Mar 08 '21 at 09:24
  • Well, lexical and syntax errors can be found mechanically and fairly trivially, given an understanding of scanning and parsing mechanics, without ascertaining the meaning ('semantics') of the program. Just a question of whether the lexical and grammatcial rules have been observed. Examples of semantic errors include, trivially, undeclared identifiers, and type errors. But it is also possible to write code which has undefined behaviour, or which doesn't link, or ... – user207421 Mar 08 '21 at 09:29
  • I seems you are trying to make a distinction between "a piece of code that can't possibly be well-formed in any context" and "a piece of code that may or may not be well-formed, depending on surrounding context". I'm not sure to what extent this distinction would be useful; the compiler definitely doesn't engage in such hypotheticals. – Igor Tandetnik Mar 08 '21 at 14:32
  • @IgorTandetnik, well, actually the reason for this question is that I'd like to be in a better position when someone explains me something about code and they use the adverbs _syntactically_, _semantically_, _lexically_ and so on. – Enlico Mar 08 '21 at 14:46
  • 1
    Well, since you tagged *language-lawyer*, the C++ standard has this to say: "**[defns.well.formed]** well-formed program: C++ program constructed according to the syntax rules, diagnosable semantic rules, and the one-definition rule (6.3)". And similarly "**[intro.compliance]/1** The set of diagnosable rules consists of all syntactic and semantic rules in this document except..." It doesn't appear to ever define the terms "syntactic rule" or "semantic rule"; there are many rules in the standard, but they are not clearly marked as being one kind or the other. – Igor Tandetnik Mar 08 '21 at 15:06

0 Answers0