chpater 3
DESCRIPTION
Chpater 3. Describing Syntax , Semantics. Outline. The definition of Syntax The Definition of Semantic Most Common Methods of Describing Syntax. Introduction. We usually break down the problem of defining a programming language into two parts. Defining the PL’s syntax - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Chpater 3](https://reader036.vdocuments.us/reader036/viewer/2022062322/56814dba550346895dbb0d3e/html5/thumbnails/1.jpg)
Chpater 3
DESCRIBINGSYNTAX , SEMANTICS
![Page 2: Chpater 3](https://reader036.vdocuments.us/reader036/viewer/2022062322/56814dba550346895dbb0d3e/html5/thumbnails/2.jpg)
Outline
• The definition of Syntax• The Definition of Semantic• Most Common Methods of Describing Syntax
![Page 3: Chpater 3](https://reader036.vdocuments.us/reader036/viewer/2022062322/56814dba550346895dbb0d3e/html5/thumbnails/3.jpg)
Introduction
• We usually break down the problem of defining a programming language into two parts.
• Defining the PL’s syntax• Defining the PL’s semantics
• In other words, In Order to understand any Programming Language, you need to understand:
• Syntax • Semantics
– What is the Syntax & the Semantics?
![Page 4: Chpater 3](https://reader036.vdocuments.us/reader036/viewer/2022062322/56814dba550346895dbb0d3e/html5/thumbnails/4.jpg)
What is the Syntax & the Semantics?
Syntax: It is the Form of the its (expressions, statement,
program unit. Semantic:
It the meaning of those (expressions, statements, Program units)
The boundary between the two is not always clear.
![Page 5: Chpater 3](https://reader036.vdocuments.us/reader036/viewer/2022062322/56814dba550346895dbb0d3e/html5/thumbnails/5.jpg)
Syntax
• It is the Form of the its (expressions, statement, program unit.
• A sentence is a string of characters over some alphabet.
• A language is a set of sentences.
• A lexeme is the lowest level syntactic unit of a language (e.g., *, sum, begin).
• A token is a category of lexemes (e.g., identifier).
![Page 6: Chpater 3](https://reader036.vdocuments.us/reader036/viewer/2022062322/56814dba550346895dbb0d3e/html5/thumbnails/6.jpg)
Syntax
• It is the Form of the its (expressions, statement, program unit.
• Some examples of syntax in C++1. While 2. For3. Do…while4. switch
![Page 7: Chpater 3](https://reader036.vdocuments.us/reader036/viewer/2022062322/56814dba550346895dbb0d3e/html5/thumbnails/7.jpg)
1. Example of Syntax : While
• Write the general form of while Syntax:While ( Boolean Expression )
{ Statement ;}
• What is the components of While Syntax?1. While word2. Boolean expression3. Body ( statements)
![Page 8: Chpater 3](https://reader036.vdocuments.us/reader036/viewer/2022062322/56814dba550346895dbb0d3e/html5/thumbnails/8.jpg)
2. Example of Syntax : if
• Write the general form of if Syntax:if <boolean expression> then <statement>
• What is the components of if Syntax in C++?1. If word2. Boolean expression3. Body ( statements)
![Page 9: Chpater 3](https://reader036.vdocuments.us/reader036/viewer/2022062322/56814dba550346895dbb0d3e/html5/thumbnails/9.jpg)
Example of Lexeme & Token
• Index = 2*count +17 ;
• Lexeme token
![Page 10: Chpater 3](https://reader036.vdocuments.us/reader036/viewer/2022062322/56814dba550346895dbb0d3e/html5/thumbnails/10.jpg)
Formal Methods of Describing Syntax
1. BNF: Backus-Naur Form and Context-Free Grammar
2. EBNFExtended BNF
![Page 11: Chpater 3](https://reader036.vdocuments.us/reader036/viewer/2022062322/56814dba550346895dbb0d3e/html5/thumbnails/11.jpg)
Metalanguages
• A metalanguage is a language used to talk about a language (usually a different one)
• We can use English as its own metalanguage (e.g. describing English grammar in English)
• It is essential to distinguish between the metalanguage terms and the object language terms
• Ex: BNF
![Page 12: Chpater 3](https://reader036.vdocuments.us/reader036/viewer/2022062322/56814dba550346895dbb0d3e/html5/thumbnails/12.jpg)
BNF
• BNF stands for either Backus-Naur Form or Backus Normal Form
• BNF is a metalanguage used to describe the grammar of a programming language
• BNF is formal and precise– BNF is a notation for context-free grammars
• BNF is essential in compiler construction• There are many dialects of BNF in use, but…• …the differences are almost always minor
![Page 13: Chpater 3](https://reader036.vdocuments.us/reader036/viewer/2022062322/56814dba550346895dbb0d3e/html5/thumbnails/13.jpg)
BNF
• < > indicate a nonterminal that needs to be further expanded, e.g. <variable>
• Symbols not enclosed in < > are terminals; they represent themselves, e.g. if, while, (
• The symbol ::= means is defined as• The symbol | means or; it separates alternatives,
e.g. <addop> ::= + | -
• This is all there is to “plain” BNF; but we will discuss extended BNF (EBNF) later in this lecture
![Page 14: Chpater 3](https://reader036.vdocuments.us/reader036/viewer/2022062322/56814dba550346895dbb0d3e/html5/thumbnails/14.jpg)
BNF uses recursion
• <integer> ::= <digit> | <integer> <digit> or<integer> ::= <digit> | <digit> <integer>
• Recursion is all that is needed (at least, in a formal sense)
• "Extended BNF" allows repetition as well as recursion• Repetition is usually better when using BNF to
construct a compiler
![Page 15: Chpater 3](https://reader036.vdocuments.us/reader036/viewer/2022062322/56814dba550346895dbb0d3e/html5/thumbnails/15.jpg)
BNF Examples I
• <digit> ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
• <if statement> ::= if ( <condition> ) <statement> | if ( <condition> ) <statement> else <statement>
![Page 16: Chpater 3](https://reader036.vdocuments.us/reader036/viewer/2022062322/56814dba550346895dbb0d3e/html5/thumbnails/16.jpg)
BNF Examples II
• <unsigned integer> ::= <digit> | <unsigned integer> <digit>
• <integer> ::= <unsigned integer> | + <unsigned integer> | - <unsigned integer>
![Page 17: Chpater 3](https://reader036.vdocuments.us/reader036/viewer/2022062322/56814dba550346895dbb0d3e/html5/thumbnails/17.jpg)
BNF Examples III
• <identifier> ::= <letter> | <identifier> <letter> | <identifier> <digit>
• <block> ::= { <statement list> }• <statement list> ::=
<statement> | <statement list> <statement>
![Page 18: Chpater 3](https://reader036.vdocuments.us/reader036/viewer/2022062322/56814dba550346895dbb0d3e/html5/thumbnails/18.jpg)
BNF Examples IV
• <statement> ::= <block> | <assignment statement> | <break statement> | <continue statement> | <do statement> | <for loop> | <goto statement> | <if statement> | . . .
![Page 19: Chpater 3](https://reader036.vdocuments.us/reader036/viewer/2022062322/56814dba550346895dbb0d3e/html5/thumbnails/19.jpg)
BNF
• A= b+c*x
![Page 20: Chpater 3](https://reader036.vdocuments.us/reader036/viewer/2022062322/56814dba550346895dbb0d3e/html5/thumbnails/20.jpg)
Parse
• A=b+c*x
![Page 21: Chpater 3](https://reader036.vdocuments.us/reader036/viewer/2022062322/56814dba550346895dbb0d3e/html5/thumbnails/21.jpg)
Extended BNF
• The following are pretty standard:– [ ] enclose an optional part of the rule
• Example:<if statement> ::= if ( <condition> ) <statement> [ else <statement> ]
– { } mean the enclosed can be repeated any number of times (including zero)• Example:
<parameter list> ::= ( ) | ( { <parameter> , } <parameter> )
![Page 22: Chpater 3](https://reader036.vdocuments.us/reader036/viewer/2022062322/56814dba550346895dbb0d3e/html5/thumbnails/22.jpg)
Semantic
• Define
• Why do we need to describe the Semantics?– Programmers need to know precisely what a
statement in a language does.
![Page 23: Chpater 3](https://reader036.vdocuments.us/reader036/viewer/2022062322/56814dba550346895dbb0d3e/html5/thumbnails/23.jpg)
Semantic
• Syntax: While (bool_expr) { statement}
• Semantics:1. Read the bool_xpr 2. If it is true: excute and repeat3. Otherwise : go to the statements after while