📜  语法和语义之间的区别

📅  最后修改于: 2021-05-30 06:00:05             🧑  作者: Mango

句法:

  • 它指的是用诸如C / C++之类的编程语言编写任何语句的规则和规定。
  • 它与语句的含义无关。
  • 如果一条语句遵循所有规则,则在语法上是有效的。
  • 它与语言的语法和结构有关。

语义:

  • 它指的是与编程语言中的语句关联的含义。
  • 语句的含义全在于它可以轻松地解释程序。
  • 错误在运行时处理。

程序1:
下面是演示语义错误的代码:

C++
// C++ program to demonstrate semantic error
 
#include 
using namespace std;
 
// Driver Code
int main()
{
    // Return statement before cout
    return 0;
 
    // Print the value
    cout << "GFG!";
}


Java
// Java program to demonstrate semantic error
import java.util.*;
class GFG
{
 
// Driver Code
public static void main(String[] args)
{
   
    // exit() statement before cout
   System.exit(0);
 
    // Print the value
    System.out.print("GFG!");
}
}
 
// This code is contributed by aashish1995


Python3
# Python program to demonstrate semantic error
import sys
 
# Driver Code
if __name__ == '__main__':
 
    # exit() statement before cout
    sys.exit(0);
 
    # Prthe value
    print("GFG!");
     
# This code contributed by gauravrajput1


C#
// C# program to demonstrate semantic error
using System;
 
class GFG
{
 
// Driver Code
public static void Main(String[] args)
{
   
    // exit() statement before cout
    Environment.Exit(0);
 
    // Print the value
    Console.Write("GFG!");
}
}
 
// This code is contributed by gauravrajput1


C++
// C++ program to demonstrate basic operation
// without any syntax and semantic error
 
#include 
using namespace std;
 
// Driver Code
int main()
{
 
    // To print gfg
    cout << "GFG!";
 
    return 0;
}


Java
// Java program to demonstrate basic operation
// without any syntax and semantic error
class GFG{
 
// Driver Code
public static void main(String[] args)
{
     
    // To print gfg
    System.out.print("GFG!");
}
}
 
// This code is contributed by aashish1995


Python3
# Python3 program to demonstrate basic operation
# without any syntax and semantic error
 
# To print gfg
print("GFG!")
 
# This code is contributed by divyeshrabadiya07.


C#
// C# program to demonstrate basic operation
// without any syntax and semantic error
using System;
 
public class GFG
{
 
  // Driver Code
  public static void Main(String[] args)
  {
 
    // To print gfg
    Console.Write("GFG!");
  }
}
 
// This code contributed by Rajput-Ji


输出:

解释:

  • 由于上述程序在语义上不正确,因此输出将为空白。
  • 该程序没有语法错误,因为它遵循所有编程规则,但仍不会在屏幕上打印任何内容,因为return语句写在cout语句之前,这导致程序在打印屏幕上的任何内容之前终止。这种情况被认为是语义错误

程式2:
下面是正确的代码,即没有任何语法和语义错误。

C++

// C++ program to demonstrate basic operation
// without any syntax and semantic error
 
#include 
using namespace std;
 
// Driver Code
int main()
{
 
    // To print gfg
    cout << "GFG!";
 
    return 0;
}

Java

// Java program to demonstrate basic operation
// without any syntax and semantic error
class GFG{
 
// Driver Code
public static void main(String[] args)
{
     
    // To print gfg
    System.out.print("GFG!");
}
}
 
// This code is contributed by aashish1995

Python3

# Python3 program to demonstrate basic operation
# without any syntax and semantic error
 
# To print gfg
print("GFG!")
 
# This code is contributed by divyeshrabadiya07.

C#

// C# program to demonstrate basic operation
// without any syntax and semantic error
using System;
 
public class GFG
{
 
  // Driver Code
  public static void Main(String[] args)
  {
 
    // To print gfg
    Console.Write("GFG!");
  }
}
 
// This code contributed by Rajput-Ji
输出:
GFG!

语法和语义错误之间的表格差异

Basis

Syntax

Semantics

Meaning It refers to the rules of any statement in the programming language. It refers to the meaning associated with any statement in the programming language
Error It referred to as syntax error. It is generally encountered at the compile time. It occurs when a statement that is not valid according to the grammar of the programming language. Some examples are: missing semicolons in C++, using undeclared variables in Java, etc. It referred to as semantic error. It is generally encountered at run time. It occurs when a statement is syntactically valid but does not do what the programmer intended. This type of error is tough to catch.
想要从精选的最佳视频中学习和练习问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”