CSC305 Chapter 2 (Part 1)
CSC305 Chapter 2 (Part 1)
Language
Design
Principles
(Part 1)
Topics
• 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.)
• 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:
Lexemes Tokens
number identifier
= equal-sign
2 int_literal
* mult_op
count identifier
+ plus_op
17 int_literal
; semicolon
Describing Syntax (cont.)
• 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.
Type 0
Grammar
Unrestricted grammar
Type 1
Grammar
Context sensitive grammar
Type 3
Grammar
Regular grammar or restrictive grammar
Grammar (cont.)
• BNF syntax:
• Examples:
A = B * (A + C)
Parse tree
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
is only indirectly
related to the meaning
of programs during
executions;