0% found this document useful (0 votes)
51 views5 pages

Semantics of Prog Languages

The document discusses the differences between syntax and semantics in programming languages. Syntax refers to the structure and formal grammar rules of a language, while semantics describes the meaning and behavior of programs. The document provides examples of syntactically correct but semantically incorrect "Hello World" programs in C++, Python, and Java to illustrate how programs can compile without errors but fail to operate as intended due to semantic issues.

Uploaded by

Dess
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views5 pages

Semantics of Prog Languages

The document discusses the differences between syntax and semantics in programming languages. Syntax refers to the structure and formal grammar rules of a language, while semantics describes the meaning and behavior of programs. The document provides examples of syntactically correct but semantically incorrect "Hello World" programs in C++, Python, and Java to illustrate how programs can compile without errors but fail to operate as intended due to semantic issues.

Uploaded by

Dess
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

CSC 801

CONTEMPORARY TOPICS IN COMPUTER SCIENCE

ASSIGNMENT ON: SEMANTICS OF PROGRAMMING


LANGUAGES
SEMANTICS OF PROGFRAMMING 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?

Semantics is about meaning while Syntax refers to the structure of a language. Semantics is


what your code means--what you might describe in pseudo-code. Syntax is the actual
structure--everything from variable names to semi-colons. Syntax refers to the structure/form
of the code that a specific programming language specifies but Semantics deal with the
meaning assigned to the symbols, characters and words.

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.

For example: undeclared or multiple declared identifiers. Type mismatched is


another compile time error. The semantic error can arises using the wrong variable
or using wrong operator or doing operation in wrong order.

Semantic errors can also be as a result of:

o Incompatible types of operands

o Undeclared variable

o Not matching of actual argument with formal argument

Syntax, semantics and semantic error in 3 programming languages

Hello World in C++

Syntactically correct C++ code

#include <iostream>

using namespace std;

Int main() {

cout << “Hello World!”;

2
Return 0;

Semantically incorrect C++ code


#include <iostream>

using namespace std;

Int main() {

Return 0;

cout << “Hello World!”;

 The output will be blank because the above program is semantically incorrect.


 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.

Hello World in Python

Syntactically correct python code

Print ("Hello World!")

Semantically incorrect java code


import sys

sys.exit(0)

Print ("Hello World!")

Hello World in Java


3
Syntactically correct java code

Class HelloWorld {

Public static void main (String[] args) {

System.out.println (“Hello World!”);

Semantically incorrect java code


Class HelloWorld {

Public static void main (String[] args) {

System.exit(0);

System.out.println (“Hello World!”);

 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.

You might also like