5
#include "stdio.h"

int main( )
{


    int x, y;

    y=x(5);
    return 0;
}

MSVC 2010 compiler gives the following error:

Error   1   error C2064: term does not evaluate to a function taking 1 arguments    c:\users\ae\documents\visual studio 2010\projects\text\text\text.cpp    13

2   IntelliSense: expression must have (pointer-to-) function type  c:\users\ae\documents\visual studio 2010\projects\text\text\text.cpp    13

Is this a semantic error or the syntax error ?

user207421
  • 305,947
  • 44
  • 307
  • 483
gpuguy
  • 4,607
  • 17
  • 67
  • 125
  • An error of kind invalid operand is semantic error, Syntax error is an error that is not valid according to language syntax (correct format). `y = x(5);` every token is valid and it can parse correctly (as `x` is a valid function name). but thing is `x` is not function it is `int` viable you can't call it a kind of invalid operand-so Semantic error.-- NOTE: `5(x)` will be a syntax error. – Grijesh Chauhan Nov 13 '13 at 09:14
  • Also adding one more example of semantic error `int a[10]; *p;` then `++a` would be semantic error but not syntax. While `++p` is valid. – Grijesh Chauhan Nov 13 '13 at 09:26
  • To proof it try this: write `#include int main(){ int y; y = x(5); return 0;}` (removed `x` from declaration) then compile with `-c` flag with gcc it it compile. but try to link it with a header file with declaration of `x` as int variable it will causes error. Hence `y = x(5);` is syntactically correct and not a syntax error. – Grijesh Chauhan Nov 13 '13 at 09:35
  • @GrijeshChauhan What makes you say that `5(x)` is a syntax error? The grammar in C99 6.5.2:1 is pretty clear (and it says that `5(x)`is a postfix-expression). – Pascal Cuoq Nov 13 '13 at 20:43
  • The tag `compiler` should be applied to questions concerning the programming of compilers or for questions about the detailed inner workings of compilers. Don't use `compiler` for questions about options and settings for a particular compiler, use the name of the compiler you are interested in instead. – user207421 Nov 14 '13 at 00:33
  • @PascalCuoq I commented for `C` (not for C99), Give it a try it will be syntax error for sure. I will read the section from C99. Thanks! – Grijesh Chauhan Nov 14 '13 at 05:57
  • @GrijeshChauhan The C language is defined by standards, identified for concision by the year they were published. The “C99 standard” defines a version of the C language. In what C standard do you claim that `5(x)` is a syntax error? PS: I gave it a try and `5(x)` results in a typing error, just like `x(5)`. You can see an example of syntax error in KepaniHaole's answer. – Pascal Cuoq Nov 14 '13 at 08:54
  • @PascalCuoq Yes I tried again and you are correct `5(x)` is semantic error. I read [this grammar](http://stackoverflow.com/questions/2068637/shift-reduce-problem-with-c-like-grammar-under-bison) and it says `5` is valid expression (I don't understand but may be 5 treated as function address, but not sure). Earlier I was tiring `y = (5)x;` that is syntax error. - Thanks to correct me. – Grijesh Chauhan Nov 14 '13 at 09:11
  • Who cares? It's an error either way. You still have to fix it. – Raymond Chen Nov 15 '13 at 14:36

4 Answers4

4

Semantic. It would be legit c syntax if x was a function that took 1 argument -- but it's just an int.

It'd be a syntax error if you did this:

int x, y;

y=x((5;
return 0;
yamafontes
  • 5,552
  • 1
  • 18
  • 18
4

I'd say it's a semantic error, specifically, a type error. The token sequence y = x(5) is well formed, and the x(5) part is parsed as a function call expression. The error is that x does not evaluate to a function pointer, but rather to an int.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • 1
    I would like to add to know `x(5)` is invalid it need to refer symbol-table/to know type of `x` that is postponed to semantic phase as can't be done in syntax analysis phase. – Grijesh Chauhan Nov 13 '13 at 09:10
  • @GrijeshChauhan: well, there are many ways you can write a compiler, so I don't want to presume too many details (e.g. "phases"). I imagine that a real-world C++ compiler doesn't use Joey Textbook's First Parser... – Kerrek SB Nov 13 '13 at 09:18
  • Yes but as I know 'C as formal language is a CSL', feature like Declare first then use variable make it context-sentitive instead context-free and as we don't know any efficient parsing technique for CSL every C compiler will post-pond type check work in semantic phase. So I added. – Grijesh Chauhan Nov 13 '13 at 09:23
  • @GrijeshChauhan: As far as I understand it, type checking has nothing to do with being context-free. Even in a context-free language you have to type-check your AST. The "context" comes from the fact that the parser needs to know the meaning of a symbol in order to parse a token sequence and determine *what kind of syntax* you have; luckily, C is "almost" context free and the only thing you need to disambiguate is whether a symbol is a variable or a type name, which can be solved quite easily. – Kerrek SB Nov 13 '13 at 09:28
2

If it was a syntax error it would say so. It's a semantic error. It's all about the meanings of the identifiers in your code.

user207421
  • 305,947
  • 44
  • 307
  • 483
2

It would clear the syntax analysis pass because it just check weather there any syntax error or not. I mean y=x(5); ,

it says that 5 in passed in function x and value returned to y.

but, the meaning is not assigned at parsing time that x is a integer variable not procedure. So, on semantic analysis when logical meanings are assigned it come to know this is not possible.

So, considering this as logical error we can say that is semantic error.

user
  • 130
  • 9