0% found this document useful (0 votes)
2 views13 pages

Syntax and Semantics

The document discusses the syntax and semantics of programming languages, emphasizing the importance of language description and the distinction between syntax (structure) and semantics (meaning). It covers key concepts such as lexemes, syntax analyzers, grammars, and the challenges of ambiguity in language rules. Additionally, it introduces Backus-Naur Form (BNF) and Extended BNF for specifying programming language syntax.

Uploaded by

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

Syntax and Semantics

The document discusses the syntax and semantics of programming languages, emphasizing the importance of language description and the distinction between syntax (structure) and semantics (meaning). It covers key concepts such as lexemes, syntax analyzers, grammars, and the challenges of ambiguity in language rules. Additionally, it introduces Backus-Naur Form (BNF) and Extended BNF for specifying programming language syntax.

Uploaded by

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

Syntax and Semantics

Principles of Programming Language

S.Venkatesan

Reference: Robert W. Sebesta, Concepts of Programming Languages, 10th Edition, Pearson


Basics
• Description of a language is very important - understandable

• The study of language can be divided into


– Syntax – expressions, statements and program units.
– Semantics – meaning of those

• Example
while(boolean_expr) statement

– Syntax is as above.
– Semantic is when the boolean_expr is true, the embedded
statement get executed otherwise control goes after the while
statement.
Syntax
• Strings of a language are called as sentences or statements.

• The syntax rules of a language specify which strings of


character from the language’s alphabet are in the language.

• English, for example, has a large and complex collection of


rules for specifying the syntax of its sentences.

• The program as strings of lexemes rather than characters.


Lexemes
• The small units.

• The description of lexemes can be given by a lexical specification.

• It includes, literals, operators, and special words, etc.

• Partitioned into groups – Each group will be represented by a


name or token.
Lexemes Tokens
Index, count identifier
• For example = Equal sign
index = 2 * count + 17 2, 17 Int literal
+,* Operators
Syntax Analyser
• Language Recognizers
– is the sentence part of the language or not
• Language Generators
– Generate the sentences of the language.
Describing Syntax
• Grammars (Noam Chomsky – 1956 and 1959)
– Context-free – Syntax of whole programming language with
minor exceptions.
– Regular – the forms of tokens of programming languages can
be described by regular grammars.

• Backus-Naur Form (John Backus and Peter Naur)


– A formal notation for specifying programming language syntax.
– BNF is natural notation to describe the syntax.
– Panini used similar syntax for Sanskrit that is several hundred
years before Christ.
Grammar - Fundamentals
• A metalanguage – BNF

• Example
<assign> -> <var> = <expression>

• It is called as production or rule

• <assign> <expression> are the abstractions.

• Non-terminal and Terminal


Derivations
• Rightmost derivations
• Leftmost derivations
• Sentential form
Parse Trees
• Hierarchical Structure

• Eg.
A = B * (A + C)
Problems
• Ambiguity
– operator precedence - order
– Associativity – An expression that have the same
precedence – a rule is required to specify the
precedence – recursion
– if then else - with matched and unmatched if
– Non-Deterministic – if then else
Example
if done == true
then if denom == 0
then quotient = 0
else quotient = num/denom

• Grammar
I -> i E t s | i E t S e S

• Deterministic
I -> i E t s S’
S’ -> e S | epsilon
Ambiguity
Extended BNF
• It increases its readability and writability.

• Non-Deterministic
I -> i E t s | i E t S e S

• Deterministic
I -> i E t s S’
S’ -> e S | epsilon
----- Simplification-----

• EBNF
I -> i E t s [e S]

• EBNF
<term> -> <term> (* | / | %) <factor>

You might also like