0% found this document useful (0 votes)
14 views

SEN308 Lecture 3 1

Uploaded by

aikhomuremen
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)
14 views

SEN308 Lecture 3 1

Uploaded by

aikhomuremen
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/ 19

LECTURE 3

SYNTAX AND SEMANTIC OF PROGRAMMING LANGUAGES

Concepts of Programming Languages - NUN 2024 Austin Olom Ogar


LECTURE OUTLINE

Introduction to Syntax and Semantics.


The General Problem of Describing Syntax
Formal Method of Describing Syntax
Attributes Grammars.
Describing the meaning of Programs: Dynamic Semantic

Concepts of Programming Languages - NUN 2024 Austin Olom Ogar


Introduction to Syntax and Semantics

.
• The Building Blocks of Programming Language
• Syntax which defines the form and structure of codes
• semantics which gives meaning to that structure

Concepts of Programming Languages - NUN 2024 Austin Olom Ogar


Syntax: The Grammar of Programming

.
• Syntax defines the rules that determine what is considered valid code in a language.
• It encompasses the lexicon (keywords, symbols) and how they can be combined.
• Key Elements of Syntax
• Keywords: Reserved words with special meanings (e.g., "if", "for", "while")
• Identifiers: Programmer-chosen names for variables, functions, etc.
• Operators: Symbols for calculations, comparisons, logic (+, -, *, >, etc.)
• Punctuation: Semicolons, commas, braces, etc., to separate elements.
• Program Structure: How code is organised into functions, modules, etc.
Concepts of Programming Languages - NUN 2024 Austin Olom Ogar
Syntax Errors

• Syntax errors occur when code violates the language's rules.


• Examples: Misspelled keywords, incorrect punctuation, or invalid structure.
• Compilers and interpreters detect syntax errors and provide messages

Concepts of Programming Languages - NUN 2024 Austin Olom Ogar


Semantics: The Meaning Behind the Code

.
 Semantics determines the meaning and behaviour of syntactically correct code.
 It tells the computer how to execute the instructions.
 Focuses on logic, computations, and intended results.
Types of Semantic
• Operational Semantic
• Operational semantics describes how code executes.
• Explains how a program's state changes as each instruction is carried out.
• Involves concepts like variables, memory, and control flow.

• Denotational Semantics
.
• Uses mathematical models to describe program meaning.
• Maps code to mathematical objects and functions.
• Helps prove correctness and reason about program behaviour
The General Problem of Describing Syntax

Concepts of Programming Languages - NUN 2024 Austin Olom Ogar


The General Problem of Describing Syntax: Terminology
• Sentence: A valid string of characters in the language (a correct Python program,
for example).
• Language: The set of all possible sentences (all correct Python programs).
• Lexeme: The smallest building block of syntax (e.g., the keyword "if", the
variable name "x").

.
• Token: A category of lexemes (e.g., "keyword", "identifier", "operator").
• Recognizer:
• A recognizer is a theoretical machine, or a more practical program, that can
determine if a given string of characters belongs to a specific language.
• It acts as a gatekeeper – if it accepts the code, the code is syntactically correct
according to the rules.
• Recognizers are fundamental parts of compilers and interpreters
• Generator:
• A device that generates sentences of a language
• One can determine if the syntax of a particular sentence is syntactically correct
Formal Method of Describing Syntax
• Context-free grammars (CFGs): The most common way to define syntax,
using rules to describe how to build valid sentences in the language.
• Developed by Noam Chomsky in the mid-1950s
• Language generator meant to describe the syntax of natural languages
• Define a class of languages called context-free languages

.
• Regular Expressions: Used to define patterns for matching text (like finding all
phone numbers in a document).

• Backus-Naur Form (1959)


• Invented by Joh Backus to describe the syntax of Algol 58
• BNF is equivalent to context-free grammars
BNF Fundamentals
• A metalanguage is a language that is used to describe another language

• BNF is a metalanguage for programming languages

• In BNF, abstractions are used to represent classes of syntactic structures


.
• A simple Java assignment statement, for example, might be represented by the
abstraction <assign>

• The abstraction in BNF can be terminals or non-terminals

• Terminals are lexemes or token


BNF Fundamentals
• A simple Java assignment statement, for example, might be represented by the
abstraction <assign>

• The actual definition of <assign> can be given by


• <assign> <var> = <expression>
.
• Nonterminal Terminals and/or Nonterminal

• Abstraction Mixtures of lexemes, tokens and references to other


abstraction
BNF Fundamentals
• BNF rules are hierarchical – nonterminal defined in one rule are used in other rules

• Example of BNF Rules:


• <assignment> ::= <variable> "=" <expression>
• <expression> ::= <term> "+" <term> | <term>
• <term> ::= <variable> | <number>
.
• Grammar: a finite non-empty set of rules

• A start symbol is a special element of the nonterminal of a grammar


BNF Fundamentals
• Nonterminal symbols can have two or more distinct definitions, representing two or
more possible syntactic forms in the language. (two or more RHS)
• <if_stmt> if ( <logic_expr> ) <stmt>
• <if_stmt> if ( <logic_expr> ) <stmt> else <stmt>

.
• Multiple definitions can be written as a single rule, with the different definitions
separated by the symbol |, meaning logical OR

• <if_stmt> if ( <logic_expr> ) <stmt>


| if ( <logic_expr> ) <stmt> else <stmt>
Grammars and Derivations
• Grammar in programming is a set of formal rules that define how you construct valid
expressions, statements, and programs in a specific language. Think of it as the
language's rulebook for proper structure
• Example: A Simplified Grammar
• <program> ::= <statement> | <program> <statement>
• <statement> ::= <assign_stmt> | <if_stmt>
.
• <assign_stmt> ::= <identifier> "=" <expression> ";"
• <if_stmt> ::= "if" "(" <expression> ")" <statement>
• <expression> ::= <identifier> | <number>
• <identifier> ::= "x" | "y" | "result"
• <number> ::= "1" | "20"

• A derivation is a repeated application of rules, starting with the start symbols and
ending with a sentence (all terminal symbols)
Grammars and Derivations
• A derivation is a step-by-step process of starting with a grammar's start symbol (like
<program> in our example) and applying production rules to generate a valid sentence
in the language (i.e., a concrete piece of code).

• Example Derivation
• Start with: <program>
.
• Apply rule: <program> ::= <program> <statement>
• Now we have: <program> <statement>
• Apply rule to <statement>: <program> <if_stmt>
• Apply rule to <if_stmt> : <program> "if" "(" <expression> ")" <statement>
• Focus on <expression>: <program> "if" "(" <identifier> ")" <statement>
• Apply rule to <identifier>: <program> "if" "(" "x" ")" <statement> ... Continue until
only terminal symbols remain
• Final Result of Derivation: if (x) y = 20;
An Example Grammar (2)
• <program> begin <stmt_list> end

• <stmt_list> <stmt>
| <stmt>; <stmt_list>

• <stmt> <var> = <expression>

• <var> . A|B|C

• <expression> <var> + <var>


| <var> - <var>
| <var>
An Example Derivation (2)
• Derivate “begin A = B + C ; B = C end”

<program> => begin <stmt_list> end


=> begin <stmt> ; <stmt_list> end
=> begin <var> = <expression> ; <stmt_list> end
=> begin A = <expression> ; <stmt_list> end
=> begin A = <var> + <var> ; <stmt_list> end
.
=> begin A = B + <var> ; <stmt_list> end
=> begin A = B + C ; <stmt_list> end
=> begin A = B + C ; <var> = <expression> end
=> begin A = B + C ; B = <expression> end
=> begin A = B + C ; B = <var> end
=> begin A = B + C ; B = C end
An Example Parse Tree
if x > 5:
print(x)

if
/ \
<condition> <statement>
/\
x > 5
. \
print
\
<expression>
|
x

You might also like