1 / 23

Chpater 3

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

suchi
Download Presentation

Chpater 3

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chpater 3 Describing Syntax , Semantics

  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 sentenceis a string of characters over some alphabet. • A languageis a set of sentences. • A lexemeis the lowest level syntactic unit of a language (e.g., *, sum, begin). • A tokenis 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++ • While • For • Do…while • 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? • While word • Boolean expression • Body ( statements)

  8. 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++? • If word • Boolean expression • Body ( statements)

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

  10. Formal Methods of Describing Syntax • 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. <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

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

  15. BNF Examples I • <digit> ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 • <if statement> ::= if ( <condition> ) <statement> | if ( <condition> ) <statement> else <statement>

  16. BNF Examples II • <unsigned integer> ::= <digit> | <unsigned integer> <digit> • <integer> ::= <unsigned integer> | + <unsigned integer> | - <unsigned integer>

  17. BNF Examples III • <identifier> ::= <letter> | <identifier> <letter> | <identifier> <digit> • <block> ::= { <statement list> } • <statement list> ::= <statement> | <statement list> <statement>

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

  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 statement> ::= if ( <condition> ) <statement> [ else <statement> ] • { } mean the enclosed can be repeated any number of times (including zero) • Example:<parameter list> ::= ( ) | ( { <parameter> , } <parameter> )

  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: • Read the bool_xpr • If it is true: excute and repeat • Otherwise : go to the statements after while

More Related