chapter 2 language description: syntactic structure

22
CHAPTER 2 CHAPTER 2 LANGUAGE DESCRIPTION: LANGUAGE DESCRIPTION: SYNTACTIC STRUCTURE SYNTACTIC STRUCTURE

Upload: gary-dean

Post on 02-Jan-2016

277 views

Category:

Documents


8 download

TRANSCRIPT

Page 1: CHAPTER 2 LANGUAGE DESCRIPTION: SYNTACTIC STRUCTURE

CHAPTER 2CHAPTER 2

LANGUAGE DESCRIPTION:LANGUAGE DESCRIPTION:

SYNTACTIC STRUCTURESYNTACTIC STRUCTURE

Page 2: CHAPTER 2 LANGUAGE DESCRIPTION: SYNTACTIC STRUCTURE

DescriptionDescription

►The state of art can be summarized by The state of art can be summarized by saying that a language is described by saying that a language is described by a combination of formal syntax and a combination of formal syntax and informal semantics.informal semantics.

►Syntax: How programs in the language Syntax: How programs in the language are built up, grammar.are built up, grammar.

►Semantics: what programs mean, Semantics: what programs mean, meaning.meaning.

Page 3: CHAPTER 2 LANGUAGE DESCRIPTION: SYNTACTIC STRUCTURE

EXAMPLEEXAMPLE

►Suppose that dates are built up from Suppose that dates are built up from digits represented by D and the digits represented by D and the symbol /, as follows:symbol /, as follows:

D D / D D / D D D DD D / D D / D D D D

01/02/2001 is a date, not identified by 01/02/2001 is a date, not identified by syntaxsyntax January 2, 2001 January 2, 2001 oror 1 1stst February, 2001 February, 2001

Page 4: CHAPTER 2 LANGUAGE DESCRIPTION: SYNTACTIC STRUCTURE

Organization of Language Organization of Language DescriptionsDescriptions

►Several Style of language descriptionSeveral Style of language description Tutorials : It provides impressions of what Tutorials : It provides impressions of what

the main constructs of the language are the main constructs of the language are and how they are meant to be usedand how they are meant to be used

Reference Manuals : describing the syntax Reference Manuals : describing the syntax and semantics of a language is and semantics of a language is traditionally organized around the syntax traditionally organized around the syntax of the languageof the language

Formal Definitions : a precise description Formal Definitions : a precise description of the syntax and semantics of a of the syntax and semantics of a language; it is aimed at specialists.language; it is aimed at specialists.

Page 5: CHAPTER 2 LANGUAGE DESCRIPTION: SYNTACTIC STRUCTURE

Over ViewOver View

►EXPRESSION NOTATIONSEXPRESSION NOTATIONS►ABSTRACT SYNTAX TREESABSTRACT SYNTAX TREES►LEXICAL SYNTAXLEXICAL SYNTAX►CONTEXT-FREE GRAMMARSCONTEXT-FREE GRAMMARS►GRAMMARS FOR EXPRESSIONSGRAMMARS FOR EXPRESSIONS►VARIANTS OF GRAMMARSVARIANTS OF GRAMMARS

Page 6: CHAPTER 2 LANGUAGE DESCRIPTION: SYNTACTIC STRUCTURE

Expression NotationsExpression Notations

► It serves 2 purposesIt serves 2 purposes Introducing notations for expressionsIntroducing notations for expressions Providing examples for the syntax description Providing examples for the syntax description

methodsmethods

► ExpressionsExpressions

► Notation:how to combine operators and Notation:how to combine operators and operandsoperands Prefix, Postfix, Infix, MixfixPrefix, Postfix, Infix, Mixfix

a

acbb

2

)4( 2

write as expression: (-b+sqrt(b*b-4.0*a*c)/(2.0*a)

Page 7: CHAPTER 2 LANGUAGE DESCRIPTION: SYNTACTIC STRUCTURE

Prefix NotationPrefix Notation

► + 3 5 is meaning summation of 3 and + 3 5 is meaning summation of 3 and 55

►Example 2.1Example 2.1

* + 20 30 60 = * 50 60 = 3000* + 20 30 60 = * 50 60 = 3000

* 20 + 30 60 = * 20 90 = 1800* 20 + 30 60 = * 20 90 = 1800►Try to do thisTry to do this

* 2 - 4 + 5 7 = ?* 2 - 4 + 5 7 = ?

Operator Expression1 Expression2

Page 8: CHAPTER 2 LANGUAGE DESCRIPTION: SYNTACTIC STRUCTURE

Postfix NotationPostfix Notation

►3 5 + is meaning summation of 3 and 53 5 + is meaning summation of 3 and 5►Example 2.2Example 2.2

20 30 + 60 * = 50 60 * = 300020 30 + 60 * = 50 60 * = 3000

20 30 60 + * = 20 90 * = 180020 30 60 + * = 20 90 * = 1800►Try to do thisTry to do this

45 37 - 69 * = ?45 37 - 69 * = ?

Expression1 Expression2 Operator

Page 9: CHAPTER 2 LANGUAGE DESCRIPTION: SYNTACTIC STRUCTURE

Infix NotationInfix NotationPrecedence and AssociativityPrecedence and Associativity

►3 + 5 is meaning summation of 3 and 53 + 5 is meaning summation of 3 and 5►ExampleExample

4 – 2 – 1 = ( 4 – 2 ) – 1 = 2 – 1 = 14 – 2 – 1 = ( 4 – 2 ) – 1 = 2 – 1 = 1

OrOr = 4 – ( 2 – 1 ) = 4 – 1 = 3 = 4 – ( 2 – 1 ) = 4 – 1 = 3

Try to do thisTry to do this

b*b – 4*a*c = ?b*b – 4*a*c = ?

Expression1 Operator Expression2

Page 10: CHAPTER 2 LANGUAGE DESCRIPTION: SYNTACTIC STRUCTURE

Mixfix NotationMixfix Notation

►Combination of symbols do not fit Combination of symbols do not fit neatly into the prefix, infix, postfix neatly into the prefix, infix, postfix classification.classification.

►Example the keywords if, then and Example the keywords if, then and elseelse

if a>b then a else bif a>b then a else b

Page 11: CHAPTER 2 LANGUAGE DESCRIPTION: SYNTACTIC STRUCTURE

ABSTRACT SYNTAX TREESABSTRACT SYNTAX TREES

► Identify the meaningful components of Identify the meaningful components of each construct in the language.each construct in the language. Prefix : + a bPrefix : + a b Postfix : a b +Postfix : a b + Infix : a + bInfix : a + b

►Corresponding tree representation isCorresponding tree representation is

+

a b

Page 12: CHAPTER 2 LANGUAGE DESCRIPTION: SYNTACTIC STRUCTURE

Tree Representation of Tree Representation of ExpressionExpression

►A tree consists of nodes, k>0A tree consists of nodes, k>0 A node with no children is called a leafA node with no children is called a leaf A node with no parent is called rootA node with no parent is called root

►A diagram for the tree isA diagram for the tree is Op Op denoted operator denoted operator EEk k denoted operanddenoted operand

Op

E1 E2

Page 13: CHAPTER 2 LANGUAGE DESCRIPTION: SYNTACTIC STRUCTURE

ExampleExample

►A tree of expression A tree of expression b*b – 4*a*cb*b – 4*a*c (b*b)-(4*a*c) (b*b)-(4*a*c) E1 – E2 E1 – E2

1.1. b*b b*b E1 * E2 E1 * E2

2.2. (4*a)*c (4*a)*c E1 * E2 E1 * E2*

b b*

4 a

*

c

-

Prefix : - * b b * * 4 a c

Infix: b * b – 4 * a * c

Postfix: b b * 4 a * c * -

Page 14: CHAPTER 2 LANGUAGE DESCRIPTION: SYNTACTIC STRUCTURE

LEXICAL SYNTAXLEXICAL SYNTAX

►A lexical syntax for a language A lexical syntax for a language specified the correspondence between specified the correspondence between the written representation of the the written representation of the language and the tokens or terminals language and the tokens or terminals in a grammar for the languagein a grammar for the language Alphabetic sequences as units in a Alphabetic sequences as units in a

language, called keywordslanguage, called keywords Occurrences of a token; using token name Occurrences of a token; using token name

for names and token number for integersfor names and token number for integers White space: blank not effect meaningWhite space: blank not effect meaning

Page 15: CHAPTER 2 LANGUAGE DESCRIPTION: SYNTACTIC STRUCTURE

CONTEXT-FREE GRAMMARSCONTEXT-FREE GRAMMARS

►The concrete syntax of a language The concrete syntax of a language describes its written representation.describes its written representation.

►Context-free grammars are a notation Context-free grammars are a notation for specifying concrete syntax.for specifying concrete syntax. BNF from: Backus-Naur FormBNF from: Backus-Naur Form

A way of writing grammars.A way of writing grammars.

Page 16: CHAPTER 2 LANGUAGE DESCRIPTION: SYNTACTIC STRUCTURE

GrammarsGrammars

►Parse treeParse tree

Real-number

Integer-part fraction

fractiondigitdigit

digit

3 . 1 4

Page 17: CHAPTER 2 LANGUAGE DESCRIPTION: SYNTACTIC STRUCTURE

Context-free grammarContext-free grammar

►Terminals are the atomic symbolsTerminals are the atomic symbols►Non-terminals are the variablesNon-terminals are the variables►Production: A set of rules for Production: A set of rules for

identifying the components of a identifying the components of a constructconstruct

►A starting non-terminal represent in A starting non-terminal represent in Main constructMain construct

Page 18: CHAPTER 2 LANGUAGE DESCRIPTION: SYNTACTIC STRUCTURE

BNF:Backus-Naur FormBNF:Backus-Naur Form

►TerminalsTerminals►Non-terminals enclosed between < and >Non-terminals enclosed between < and >►Production in formProduction in form

non-terminal ::= terminal | <non-non-terminal ::= terminal | <non-terminal>terminal> ::= “can be”::= “can be” | “or”| “or”

► A starting non-terminalA starting non-terminal

Page 19: CHAPTER 2 LANGUAGE DESCRIPTION: SYNTACTIC STRUCTURE

Example of BNFExample of BNF

►A BNF grammar for real numbersA BNF grammar for real numbers Terminals : 0,1,2,3,4,5,6,7Terminals : 0,1,2,3,4,5,6,7 Non-terminals : <real-number>,<integer-Non-terminals : <real-number>,<integer-

part>,<fraction>, and <digit>part>,<fraction>, and <digit> ProductionsProductions

<real-number><real-number> ::=<integer-part>.<fraction>::=<integer-part>.<fraction>

<integer-part> <integer-part> ::=<digit>|<integer-::=<digit>|<integer-part><digit>part><digit>

<fraction><fraction> ::=<digit>|<digit><fraction>::=<digit>|<digit><fraction>

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

Page 20: CHAPTER 2 LANGUAGE DESCRIPTION: SYNTACTIC STRUCTURE

Parse Trees Depict Concrete Parse Trees Depict Concrete SyntaxSyntax

►Each leaf is labeled with a terminal or Each leaf is labeled with a terminal or <empty><empty>

►Each non-leaf node is labeled with a Each non-leaf node is labeled with a non-terminalnon-terminal

►The root is labeled with the starting The root is labeled with the starting non-terminalnon-terminal

►Children of node, from left to right of Children of node, from left to right of right sideright side

Page 21: CHAPTER 2 LANGUAGE DESCRIPTION: SYNTACTIC STRUCTURE

ExampleExample

Real-number

Integer-part fraction.

Integer-part

Integer-part

digit digit fraction

digit fractiondigit

digit digit

1

2

3 7

8

9

Page 22: CHAPTER 2 LANGUAGE DESCRIPTION: SYNTACTIC STRUCTURE

AmbiguityAmbiguity

►Syntactic AmbiguitySyntactic Ambiguity►Dangling-Else AmbiguityDangling-Else Ambiguity