Semantics of Prog Languages
Semantics of Prog Languages
The semantics of a programming language describes what programs mean and what they
do. It answers the questions: is this sentence valid? If so, what does the sentence mean?
Syntax is the formal grammar of the language, which specifies a well-formed statement the
compiler will recognize. While syntax describes how it looks, the semantics describes what it
should do.
Syntax:
Syntax is about the structure or the grammar of the language. It answers the
question: how do I construct a valid sentence? All languages, even English and other
human or natural languages have grammars, that is, rules that define whether or not
the sentence is properly constructed.
The syntax of a programming language is a collection of rules to specify the
structure or form of code whereas semantics refers to the interpretation of the
code or the associated meaning of the symbols, characters or any part of a program.
Syntax refers to the rules that define the structure of a language. Without syntax,
the meaning or semantics of a language is nearly impossible to understand. Syntax in
computer programming means the rules that control the structure of the symbols,
punctuation, and words of a programming language. It is related to the grammar
and structure of the language.
It refers to the rules and regulations for writing any statement in a programming
language like C, C++, Java, JavaScript, etc.
It does not have to do anything with the meaning of the statement.
A statement is syntactically correct if it follows all the rules.
1
Syntax error occurs when a sentence that is not valid according to the grammar of
the programming language. In this case, we can say that the program or sentence
is syntactically incorrect. Some examples are; missing semicolons in C++, using
undeclared variables in Java, etc.
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.
Semantic error occurs when a statement is syntactically correct but does not do
what the programmer intended i.e., the program runs without producing error
messages but doesn’t do the right thing. This type of error is tough to catch.
o Undeclared variable
#include <iostream>
Int main() {
2
Return 0;
Int main() {
Return 0;
sys.exit(0)
Class HelloWorld {
System.exit(0);
This java program has no syntax error as it is following every programming rule but
still, it will not print anything on the screen because the exit statement is written
before the print statement which causes the program to terminate before printing
anything on the screen.