Difference Between Syntax and Semantics
Last Updated :
15 Nov, 2022
Syntax:
- It refers to the rules and regulations for writing any statement in a programming language like C/C++.
- It does not have to do anything with the meaning of the statement.
- A statement is syntactically valid if it follows all the rules.
- It is related to the grammar and structure of the language.
Semantics:
- It refers to the meaning associated with the statement in a programming language.
- It is all about the meaning of the statement which interprets the program easily.
- Errors are handled at runtime.
Program 1:
Below is the code to demonstrate the semantic error:
C++
#include <iostream>
using namespace std;
int main()
{
return 0;
cout << "GFG!" ;
}
|
Java
import java.util.*;
class GFG
{
public static void main(String[] args)
{
System.exit( 0 );
System.out.print( "GFG!" );
}
}
|
Python3
import sys
if __name__ = = '__main__' :
sys.exit( 0 );
print ( "GFG!" );
|
C#
using System;
class GFG
{
public static void Main(String[] args)
{
Environment.Exit(0);
Console.Write( "GFG!" );
}
}
|
Javascript
<script>
function fun()
{
return ;
document.write( "GFG!" );
}
fun();
</script>
|
Explanation:
- The output will be blank because the above program is semantically incorrect syntax.
- This program has no syntax error as it is following every programming rule but still, it will not print anything on the screen because the return statement is written before the cout statement which causes the program to terminate before printing anything on the screen. This type of situation is considered a semantic error.
Program 2:
Below is the correct code i.e, without any syntax and semantic errors.
C++
#include <iostream>
using namespace std;
int main()
{
cout << "GFG!" ;
return 0;
}
|
Java
class GFG{
public static void main(String[] args)
{
System.out.print( "GFG!" );
}
}
|
Python3
C#
using System;
public class GFG
{
public static void Main(String[] args)
{
Console.Write( "GFG!" );
}
}
|
Javascript
<script>
document.write( "GFG!" );
</script>
|
Tabular Difference between Syntax and Semantic Error:
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 is referred to as a 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 a 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. |
In linguistics |
The syntax is the arrangement or order of words, determined by both the writer’s style and grammar rules. |
There are two areas of semantics that are logical semantics and lexical semantics. |
Sensitivity |
the syntax is sensitive in most programming languages. |
Most of the semantics are case-insensitive. |
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...