23
Chpater 3 DESCRIBING SYNTAX , SEMANTICS

Chpater 3. Outline The definition of Syntax The Definition of Semantic Most Common Methods of Describing Syntax

Embed Size (px)

Citation preview

Chpater 3

DESCRIBINGSYNTAX , 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• 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?

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.

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).

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

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)

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)

Example of Lexeme & Token

• Index = 2*count +17 ;

• Lexeme token

Formal Methods of Describing Syntax

1. BNF: Backus-Naur Form and Context-Free Grammar

2. EBNFExtended BNF

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

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

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

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

BNF Examples I

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

• <if statement> ::= if ( <condition> ) <statement> | if ( <condition> ) <statement> else <statement>

BNF Examples II

• <unsigned integer> ::= <digit> | <unsigned integer> <digit>

• <integer> ::= <unsigned integer> | + <unsigned integer> | - <unsigned integer>

BNF Examples III

• <identifier> ::= <letter> | <identifier> <letter> | <identifier> <digit>

• <block> ::= { <statement list> }• <statement list> ::=

<statement> | <statement list> <statement>

BNF Examples IV

• <statement> ::= <block> | <assignment statement> | <break statement> | <continue statement> | <do statement> | <for loop> | <goto statement> | <if statement> | . . .

BNF

• A= b+c*x

Parse

• A=b+c*x

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> )

Semantic

• Define

• Why do we need to describe the Semantics?– Programmers need to know precisely what a

statement in a language does.

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