Syntax and Semantics
Syntax and Semantics
Writability:
The ease with which programs can be developed for a given problem
domain.
Reliability:
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;
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.
TimeOut=1 vs TimeOut=True
CHARACTER*30 NAME(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.
Types of Syntax:
1) Concrete Syntax:
if x > 5:
print("Hello")
2) Abstract Syntax:
Assign
├── Variable: x
└── Expression: x + 1
Syntax Errors
Syntax errors occur when the written code does not adhere to the
grammar rules of the language.
Example (Python):
if x > 5
Types of Semantics
1) Static Semantics
Example:
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
Example:
x=x+1
Adds 1 to x.
Denotational Semantics
Example:
f(x) = x + 1
Axiomatic Semantics
Example:
{x = 5} x = x + 1 {x = 6}
Semantic Errors
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: