Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Chpater 3. Outline The definition of Syntax The Definition of Semantic Most Common Methods of Describing Syntax."— Presentation transcript:

1 Chpater 3

2 Outline The definition of Syntax The Definition of Semantic Most Common Methods of Describing Syntax

3 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?

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

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

6 Syntax It is the Form of the its (expressions, statement, program unit. Some examples of syntax in C++ 1.While 2.For 3.Do…while 4.switch

7 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 word 2.Boolean expression 3.Body ( statements)

8 2. Example of Syntax : if Write the general form of if Syntax: if then What is the components of if Syntax in C++? 1.If word 2.Boolean expression 3.Body ( statements)

9 Example of Lexeme & Token Index = 2*count +17 ; Lexeme token

10 Formal Methods of Describing Syntax 1.BNF: Backus-Naur Form and Context-Free Grammar 2.EBNF Extended BNF

11 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

12 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

13 BNF indicate a nonterminal that needs to be further expanded, e.g. 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. ::= + | - This is all there is to “plain” BNF; but we will discuss extended BNF (EBNF) later in this lecture

14 BNF uses recursion ::= | or ::= | 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

15 BNF Examples I ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 ::= if ( ) | if ( ) else

16 BNF Examples II ::= | ::= | + | -

17 BNF Examples III ::= | | ::= { } ::= |

18 BNF Examples IV ::= | | | | | | | |...

19 BNF A= b+c*x

20 Parse A=b+c*x

21 Extended BNF The following are pretty standard: – [ ] enclose an optional part of the rule Example: ::= if ( ) [ else ] – { } mean the enclosed can be repeated any number of times (including zero) Example: ::= ( ) | ( {, } )

22 Semantic Define Why do we need to describe the Semantics? – Programmers need to know precisely what a statement in a language does.

23 Semantic Syntax: While (bool_expr) { statement} Semantics: 1.Read the bool_xpr 2.If it is true: excute and repeat 3.Otherwise : go to the statements after while


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

Similar presentations


Ads by Google