0% found this document useful (0 votes)
7 views9 pages

Syntax and Semantics

The document discusses the criteria for language evolution, focusing on readability, writability, and reliability, along with the concepts of machine and human readability. It elaborates on syntax and semantics, defining their roles in programming languages, and detailing syntax types, semantics types, and their relationship. The document concludes with practical applications of these concepts in compiler design, static analysis, and software verification.

Uploaded by

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

Syntax and Semantics

The document discusses the criteria for language evolution, focusing on readability, writability, and reliability, along with the concepts of machine and human readability. It elaborates on syntax and semantics, defining their roles in programming languages, and detailing syntax types, semantics types, and their relationship. The document concludes with practical applications of these concepts in compiler design, static analysis, and software verification.

Uploaded by

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

Syntax and Semantics

Language evolution criteria:


Readability:

The ease with which programs can be read and understood.

Writability:

The ease with which programs can be developed for a given problem
domain.

Reliability:

The extent to which a program will perform according to its


specifications.

What do we mean by machine readability?

A language is considered machine readable if it can be translated


efficiently into a form that the computer can execute. This requires
that:

 A translation algorithm exists.


 The algorithm is not too complex.

We can ensure machine readability be requiring that programming


languages be context-free languages.

What did we mean by human readability?

 It is harder to define human readability in precise terms.


 Generally, this requires a programming language to provide
enough abstractions to make the algorithm clear to someone who
is not familiar with the program's details.
 As programs gets larger, making our language readable requires
that the amount of detail is reduced, so that changes in one part of
a program have a limited effect on other parts of the program.

What contributes to readability?

There are five characteristics of programming language that


contribute to readability:

1. Simplicity
2. Orthogonality
3. Controls statements
4. Data types and Structures
5. Syntax

1. Simplicity:
 Programming language with a large number of basic components
are harder to learn. Most programmers using these language tend
to learn and use subsets of the whole language.
 Complex language have multiplicity (more than one way to
accomplish an operation).
 Overloading operators can reduce the clarity of program's meaning.

Example of multiplicity:
All of the following add one to the variable count in C:

count = count+1;

count +=1;

count++;

++count;

Do they mean the same thing?


2. Orthogonality:
 For a programming language to be orthogonal, language
constructs should not behave differently in different contexts.
 The facts that Modula-2's constant Expressions may not include
function calls can be viewed as a non-orthogonality.

Examples of non-orthogonalities:
 In pascal functions can only return scalar values or pointers.
 In C/C++, arrays types cannot be returned from a function.
 In C, Local variables must be at the beginning of a block.
 C Passes ALL parameters by value except arrays (passed by
reference).

3. Control Statements:
 In 1950s and 1960s, the goto was most common control
mechanism in a program; however, it could make programs less
readable.
 The introduction of while for and if-then-else eliminate the need
for goto and led to more readable programs.

4. Data Types and Structures:


 A More diverse set of data types and ability of programmers to
create their own increased program readability:
 Booleans make programs more readable:

TimeOut=1 vs TimeOut=True

 The use of records to store complex data objects make


programs more readable:

CHARACTER*30 NAME(100)

INTEGER AGE(100), EMPLOYEE_NUM(100)

REAL SALARY(100)
Would not it better if these were an array of records instead of four
parallel arrays?

5. Syntax:
 Most Syntactic features in a programming language can enhance
readability:
 Identifier forms: Older language(Like Fortran) Restrict the
length of identifiers, Which become less meaningful.
 Special words: In addition to a while, do and for, Some
languages use special words to close structures such as endif
and endwhile.
 Form and meaning: In C a stactic variable within a Function
and outside a function mean mean two different things this is
undesirable.

Now, let's deeply understand Syntax and Semantics in the


Theory of Programming Languages.:
1. Introduction
Programming languages are defined by two fundamental aspects:
syntax and semantics. These aspects ensure that a language is both
structured correctly and behaves as expected.

 Syntax: Refers to the formal structure and rules of a language,


determining how programs should be written.
 Semantics: Refers to the meaning behind correctly written
programs, ensuring that they execute as intended.

Understanding syntax and semantics is crucial for language design,


compiler construction, and effective programming.

2. Syntax in Programming Languages


Definition
Syntax refers to the set of rules that define the structure of valid
programs in a language. It determines how symbols, keywords, and
expressions should be arranged.

Types of Syntax:
1) Concrete Syntax:

The actual representation of code as written by a programmer.

Example (Python if-statement):

if x > 5:

print("Hello")

2) Abstract Syntax:

A simplified internal representation used by compilers and


interpreters.

Example (Abstract Syntax Tree for x = x + 1):

Assign
├── Variable: x
└── Expression: x + 1

Syntax Specification Methods


 Backus-Naur Form (BNF):

A formal notation for describing programming language grammar.

Example BNF rule for an if statement:

<if-statement> ::= "if" <condition> ":" <statement>


 Syntax Trees:

A hierarchical representation of program structure, essential for


parsing.

 Syntax Errors
Syntax errors occur when the written code does not adhere to the
grammar rules of the language.

Example (Python):

if x > 5

print("Hello") # SyntaxError: missing colon

3. Semantics in Programming Languages


Definition

Semantics defines the meaning and behavior of syntactically correct


programs. It ensures that statements and expressions produce
expected results.

Types of Semantics
1) Static Semantics

Defines rules that can be checked at compile-time, such as type


checking.

Example:

x = "hello" + 5 # Type Error: cannot concatenate str and int

2) Dynamic Semantics

Defines rules that are enforced at runtime, such as variable scope and
memory allocation.
Example:

def func():
print(x) # NameError if x is not defined in global scope

Semantics Specification Methods


 Operational Semantics

Describes program execution in terms of state changes.

Example:

x=x+1

Reads x from memory.

Adds 1 to x.

Stores the new value back in memory.

 Denotational Semantics

Maps programming constructs to mathematical functions.

Example:

f(x) = x + 1

 Axiomatic Semantics

Uses logic-based methods with preconditions and postconditions.

Example:

{x = 5} x = x + 1 {x = 6}

 Semantic Errors

Semantic errors occur when code is syntactically correct but produces


unintended results.
Example:

print(5 + "hello") # Syntax correct but semantically incorrect (type


mismatch)

4. Relationship Between Syntax and Semantics


Syntax ensures code follows correct structural rules, while semantics
ensures correct behavior.

 Incorrect syntax prevents program execution.


 Incorrect semantics leads to unintended results.

Aspect Definition
Syntax Structure and rules of a language
Semantics Meaning and behavior of a language
Syntax Error Code structure violation
Semantic Error Incorrect Logic or meaning

5. Practical Applications
 Compiler Design: Syntax analysis (parsing) and semantic
analysis are critical in compilation.
 Static Analysis: Identifies potential semantic errors before
execution.
 Programming Language Design: Ensures consistency and
correctness in language rules.
 Software Verification: Formal methods use axiomatic semantics
to verify program correctness.
 Interpreter Development: Operational semantics helps define
how interpreters execute code.

6. Conclusion
Syntax and semantics are fundamental to programming languages,
ensuring that programs are both correctly structured and meaningfully
executed. Mastery of these concepts is essential for software
development, compiler construction, and language design.

References:
These notes are based on the following books:

 Concepts of Programming Languages by Robert W. Sebesta (10th


edition, 2012)
 Programming Language Pragmatics by Michael L. Scott (2nd
edition, 2005)
 Introduction to Programming Languages by Anthony A. Aaby
(2004)
 Principles of Programming Languages by Mike Grant Zachary
Palmer Scott Smith (Johns Hopkins University, 2016)

You might also like