简体   繁体   English

这是语义错误还是语法错误?

[英]Is this a semantic error or the syntax error ?

#include "stdio.h"

int main( )
{


    int x, y;

    y=x(5);
    return 0;
}

MSVC 2010 compiler gives the following error: MSVC 2010编译器给出以下错误:

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 ? 这是语义错误还是语法错误?

Semantic. 语义。 It would be legit c syntax if x was a function that took 1 argument -- but it's just an int . 如果x是一个带有1个参数的函数,那将是合法的语法 - 但它只是一个int

It'd be a syntax error if you did this: 如果你这样做,那将是一个语法错误:

int x, y;

y=x((5;
return 0;

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. 令牌序列y = x(5)格式良好, x(5)部分被解析为函数调用表达式。 The error is that x does not evaluate to a function pointer, but rather to an int . 错误是x不计算函数指针,而是计算为int

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. 这完全取决于代码中标识符的含义。

It would clear the syntax analysis pass because it just check weather there any syntax error or not. 它会清除语法分析通过,因为它只是检查天气有没有语法错误。 I mean y=x(5); 我的意思是y=x(5); ,

it says that 5 in passed in function x and value returned to y. 它表示传入函数x的5和值返回y。

but, the meaning is not assigned at parsing time that x is a integer variable not procedure. 但是,在解析时没有指定x是整数变量而不是过程的含义。 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 . 因此,将此视为逻辑错误,我们可以说这是语义错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM