0% found this document useful (0 votes)
101 views23 pages

CSC305 Chapter 2 (Part 1)

Uploaded by

ustazdanshirfan
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)
101 views23 pages

CSC305 Chapter 2 (Part 1)

Uploaded by

ustazdanshirfan
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/ 23

Chapter 2:

Language
Design
Principles
(Part 1)
Topics

• Describing syntax and semantics


• Lexical and syntax analysis
• Names, Data types
• Binding, type checking and scopes
• Expressions & assignment statements
• Statement-level control structures
• Subprograms
• Abstract data types and encapsulation constructs
• Support for object-oriented programming
Describing syntax and semantic

• The study of programming languages, like the study of natural languages, can be divided
into examinations of syntax and semantics.

Programming language
Syntax Semantic
• The syntax of a programming language is • Its semantics is the meaning of those
the form of its expressions, statements, expressions, statements, and program
and program units. units.
• Set of rules how language element may be • How each grammatically correct sentence
grammatically combined is to be interpreted
Describing syntax and semantic
Describing syntax and semantic (cont.)

• Example:
• According to the syntax of C programming language:
Z = 100;
✓ Syntax: Assigning Z with 100
✓ Semantic: Replace the value of Z with 100
Describing syntax and semantic (cont.)

Analysis & Implementation (program coding) Output


Algorithm

Define the Syntax error Run-time Logic error Correct


meaning of a error output
program
Lexical and semantic
syntax
Describing syntax
Features of a good syntax

• Readable
• Writeable
• Lack of ambiguity
• Suggestive of correct meaning
• Ease of translation
Language:
Sentence:
Set of string of
String of a language
characters from
aka statements
some alphabet.

Describing
Syntax Lexeme:

basic lexical unit of Token:


a language. Includes
of identifier, literals, Category of its
operator, special lexeme. Example
word. identifier.
Describing Syntax (cont.)

• Consider the following Java statement:

number = 2 * count + 17;

Lexemes Tokens
number identifier
= equal-sign
2 int_literal
* mult_op
count identifier
+ plus_op
17 int_literal
; semicolon
Describing Syntax (cont.)

• Language can be formally defined by recognition & generation

• A device capable of reading string of characters from a language & determine the
Language character, in effect a device capable of recognize all the characters from the
language
recognizers: • A recognizer determines whether the given programs are in the language and
syntactically correct.

Language • A device that is used to generate sentence of a language.


generators:
Grammar

• Grammars are commonly used to describe the syntax of programming languages.

Type 0
Grammar
Unrestricted grammar

Type 1
Grammar
Context sensitive grammar

Type 2 Programming language used Type


Grammar
Context-free grammar 2 Grammar

Type 3
Grammar
Regular grammar or restrictive grammar
Grammar (cont.)

• The context-free grammar (Type 2


Grammar) is useful for describing
the syntax of programming
languages.
• The revised method of context-
free grammar is known as Backus-
Naur Form, or simply BNF –
introduced by Peter Naur and John
Backus.
• BNF is a popular method for
describing programming language
syntax.
Grammar (cont.)

• BNF syntax:

❖Any word or words enclosed in angle brackets, < > , is a


nonterminal; it stands for some string (it’s like a string
variable)
❖The symbol ::= means “is defined as”
❖The symbol | means “or”
❖Anything else is a terminal ; it stands for itself
Grammar (cont.)

• Examples:

LHS: Nonterminal RHS: terminal (mixture of token, lexeme etc.

<vowel> ::= a|e|i|o|u

<consonant> ::= b|c|d|f|g|h|j|k|l|m|n|p|q|r|s|t|v|w|x|y|z

<digit> ::= 0|1|2|3|4|5|6|7|8|9


Grammar (cont.)

• Recursion in BNF – example:


Suppose that (in some computer language) a variable consists of letters,
digits, or underscores, but must begin with a letter

<variable> ::= <letter> | <variable><letter> | <variable><digit> |<variable> _


Grammar (cont.)
• Example:
<assign> ::= <id> = <expr>
<id>::= A | B| C Definitions
<expr> ::= <id> * <expr> | <id> + <expr> | (<expr>) | <id>

A = B * (A + C)

<assign> => <id> = <expr>


=> A = <expr>
=> A = <id> * <expr>
=> A = B * <expr>
=> A = B * (<expr>)
=> A = B * ( <id> + <expr>)
=> A = B * ( A + <expr>)
=> A = B * ( A + <id>)
=> A = B * ( A + C )
Input string

Parse tree

• Also known as derivation tree


• Naturally describe the syntactic
structure of the language define.
• Every internal node labeled as non-
terminal symbol.
• Every leaf is labeled with a terminal
symbol
• Every subtree describes one
abstraction instances.
<assign>

Parse tree (cont.) <id> = <expr>

A
<assign> ::= <id> = <expr>
<id>::= A | B| C *
<expr> ::= <id> * <expr> | <id> + <expr> | (<expr>) | <id>
<id> <expr>

B
A = B * (A + C) <expr>
( )

<id> <expr>
+

A <id>

C
Describing semantic
Describing semantic

• Generally, semantics reveals the meaning of the syntax or grammar.


• Semantics can be categorized into two types:

Static Semantic Dynamic Semantic

is only indirectly
related to the meaning
of programs during
executions;

Reveals the meaning Reveals the meaning


at compile time at run time
Dynamic semantic

Operational Semantics Axiomatic Semantics Denotational Semantics

Defines a programming language Defines a programming language


Describe the meaning of a program
behavior by applying mathematical behavior by applying mathematical
by executing its statement on a
logic and the development of method functions to programs and program
machine, either real or simulated.
to prove the correctness of programs. components to represent their meaning.

The changes that occur in the Axiomatic semantics is based on


machine’s state when it executes a mathematical logic. The logical
It is based on recursive function theory.
given statement define the meaning expressions are called predicates, or
of that statement. assertions.

You might also like