230 likes | 384 Views
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
E N D
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 • 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 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).
Syntax • It is the Form of the its (expressions, statement, program unit. • Some examples of syntax in C++ • While • For • Do…while • switch
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)
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)
Example of Lexeme & Token • Index = 2*count +17 ; • Lexeme token
Formal Methods of Describing Syntax • BNF: Backus-Naur Form and Context-Free Grammar 2. EBNF Extended 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: • Read the bool_xpr • If it is true: excute and repeat • Otherwise : go to the statements after while