lecture 8: describing the syntaxse.inf.ethz.ch/old/teaching/ws2006/0001/slides/08_bnf_3up.pdf ·...

18
1 Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer, Michela Pedroni October 2006 – February 2007 Chair of Software Engineering Lecture 8: Describing the Syntax Intro. to Programming, lecture 8: Describing the syntax 2 Intro. to Programming, lecture 8: Describing the syntax 3 Goals of today’s lecture Read and understand the syntax description for Eiffel Write simple syntax descriptions

Upload: others

Post on 09-Aug-2020

12 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 8: Describing the Syntaxse.inf.ethz.ch/old/teaching/ws2006/0001/slides/08_bnf_3up.pdf · Intro. to Programming, lecture 8: Describing the syntax 20 Elements of a grammar:

1

Einführung in die ProgrammierungIntroduction to Programming

Prof. Dr. Bertrand Meyer,

Michela PedroniOctober 2006 – February 2007

Chair of Software Engineering

Lecture 8: Describing the Syntax

Intro. to Programming, lecture 8: Describing the syntax 2

Intro. to Programming, lecture 8: Describing the syntax 3

Goals of today’s lecture

Read and understand the syntax description for Eiffel

Write simple syntax descriptions

Page 2: Lecture 8: Describing the Syntaxse.inf.ethz.ch/old/teaching/ws2006/0001/slides/08_bnf_3up.pdf · Intro. to Programming, lecture 8: Describing the syntax 20 Elements of a grammar:

2

Intro. to Programming, lecture 8: Describing the syntax 4

An example (1) - Conditional

fare (half, first: BOOLEAN; base_price: REAL): REAL is-- Ticket price -- Base_price: 2nd class, no halbtax

doResult := base_price

-- If half price: divide price by 2

-- If first class: multiply price by 2

end

if half thenResult := Result / 2

end

if first thenResult := Result * 2

end10.--5.--1/220.--10.--1/11st2nd

Intro. to Programming, lecture 8: Describing the syntax 5

An example (2) - Conditional

fare (half, first: BOOLEAN; base_price: REAL): REAL is-- Ticket price -- Base_price: 2nd class, no halbtax

do-- If 1/2 price, not first: base_price/2

-- If not 1/2, first: base_price * 2

-- Else: base_price is the result

end

if not half and first thenResult := base_price * 2

elseif half and not first thenResult := base_price / 2

elseResult := base_price

end 10.--5.--1/220.--10.--1/11st2nd

Intro. to Programming, lecture 8: Describing the syntax 6

An example (3) – Nested conditionalga_price (first: BOOLEAN; age: INTEGER): REAL is

-- Price for GA depending on agerequire

old_enough: age >= 16do

end

if first thenif age <= 25 then Result := 3450.0elseif age >= 64 then Result := 3550.0else Result := 4700.0end

elseif age <= 25 then Result := 2200.0elseif age >= 64 then Result := 2250.0else Result := 2990.0end

end

4700.--2990.--26-633550.--2250.--64-…

3450.--2200.--16-251st2nd

Page 3: Lecture 8: Describing the Syntaxse.inf.ethz.ch/old/teaching/ws2006/0001/slides/08_bnf_3up.pdf · Intro. to Programming, lecture 8: Describing the syntax 20 Elements of a grammar:

3

Intro. to Programming, lecture 8: Describing the syntax 7

Syntax: Conditional

A conditional instruction consists, in order, of:An “If part”, of the form if condition.A “Then part” of the form then compound.Zero or more “Else if parts”, each of the form

elseif condition then compound.Zero or one “Else part” of the form else compoundThe keyword end.

Here each condition is a boolean expression, and eachcompound is a compound instruction.

Intro. to Programming, lecture 8: Describing the syntax 8

The power of human mind

I cdnoult blvelee taht I cluod aulacity uesdnatnrd waht I was rdgnieg. The Paomnnehal Pweor of the Hmuan MnidAoccdrnig to a rscheearch at Cmabrigde Uinervtisy, is deosn't mttaer in waht oredr the ltteers in a wrod are, the olny iprmoatnt tihng is taht the frist and lsat ltteerbe in the rghit pclae. The rset can be a taotl mses and you can sitll raed it wouthit porbelm. Tihs is bcuseae the huamn mnid deos not raed ervey lteter by istlef, but the wrod as a wlohe. Ptrety Amzanig Huh?

Intro. to Programming, lecture 8: Describing the syntax 9

Why describe the syntax formally?

We know syntax descriptions for human languages:

e.g. grammar for German, French, …

Expressed in natural language

Good enough for human use

Ambiguous, like human languages themselves

Page 4: Lecture 8: Describing the Syntaxse.inf.ethz.ch/old/teaching/ws2006/0001/slides/08_bnf_3up.pdf · Intro. to Programming, lecture 8: Describing the syntax 20 Elements of a grammar:

4

Intro. to Programming, lecture 8: Describing the syntax 10

Why describe the syntax formally?

Programming languages need better descriptions:

More precise: must tell us unambiguously whether given program text is legal or not

Use formalism similar to mathematics

Can be fed into compilers for automatic processing of programs

Intro. to Programming, lecture 8: Describing the syntax 11

Why describe the syntax formally?

Compilers use algorithms to

Determine if input is correct program text (parser)

Analyze program text to extract specimens

Translate program text to machine instructions

Compilers need strict formal definition of programming language

Intro. to Programming, lecture 8: Describing the syntax 12

Formal Description of Syntax

Use formal language to describe programming languages.

Languages used to describe other languages are called Meta-Languages

Meta-Language used to describe Eiffel:BNF-E (Variant of the Backus-Naur-Form, BNF)

Page 5: Lecture 8: Describing the Syntaxse.inf.ethz.ch/old/teaching/ws2006/0001/slides/08_bnf_3up.pdf · Intro. to Programming, lecture 8: Describing the syntax 20 Elements of a grammar:

5

Intro. to Programming, lecture 8: Describing the syntax 13

History

1954 FORTRAN: First widely recognized programming language (developed by John Backus et Al.)

1958 ALGOL 58: Joint work of European and American groups

1960 ALGOL 60: Preparation showed a need for a formal description John Backus (member of ALGOL team) proposed Backus-Normal-Form (BNF)

1964: Donald Knuth suggested acknowledging Peter Naurfor his contribution Backus-Naur-Form

Many variants since then, e.g. graphical variant by NiklausWirth

Intro. to Programming, lecture 8: Describing the syntax 14

Formal description of a language

BNF lets us describe syntactical properties of a language

Remember: Description of a programming language also includes lexical and semantic properties other tools

Intro. to Programming, lecture 8: Describing the syntax 15

Formal Description of Syntax

A language is a set of phrases

A phrase is a finite sequence of tokens from a certain “vocabulary”

Not every possible sequence is a phrase of the language

A grammar specifies which sequences are phrases and which are not

BNF is used to define a grammar for a programming language

Page 6: Lecture 8: Describing the Syntaxse.inf.ethz.ch/old/teaching/ws2006/0001/slides/08_bnf_3up.pdf · Intro. to Programming, lecture 8: Describing the syntax 20 Elements of a grammar:

6

Intro. to Programming, lecture 8: Describing the syntax 16

Example of phrases

class PERSONfeature

age: INTEGER-- Age

end

classage: INTEGER

-- Ageend PERSONfeature

Intro. to Programming, lecture 8: Describing the syntax 17

Grammar

DefinitionA Grammar for a language is a finite set of rules forproducing phrases, such that:

1. Any sequence obtained by a finite number of applications of rules from the grammar is a phrase of the language.

2. Any phrase of the language can be obtained by a finite number of applications of rules from the grammar.

Intro. to Programming, lecture 8: Describing the syntax 18

Elements of a grammar: Terminals

Terminals

Tokens of the language that are not defined by a production of the grammar.E.g. keywords from Eiffel such as if, then, endor symbols such as the semicolon “;” or the assignment “:=”

Page 7: Lecture 8: Describing the Syntaxse.inf.ethz.ch/old/teaching/ws2006/0001/slides/08_bnf_3up.pdf · Intro. to Programming, lecture 8: Describing the syntax 20 Elements of a grammar:

7

Intro. to Programming, lecture 8: Describing the syntax 19

Elements of a grammar: Nonterminals

Nonterminals

Names of syntactical structures or substructures used to build phrases.

Intro. to Programming, lecture 8: Describing the syntax 20

Elements of a grammar: Productions

Productions

Rules that define nonterminals of the grammar using a combination of terminals and (other) nonterminals

Intro. to Programming, lecture 8: Describing the syntax 21

An example production

Terminal

Nonterminal

Production

Conditional:

if

else

endthenCondition Instruction

Instruction

Page 8: Lecture 8: Describing the Syntaxse.inf.ethz.ch/old/teaching/ws2006/0001/slides/08_bnf_3up.pdf · Intro. to Programming, lecture 8: Describing the syntax 20 Elements of a grammar:

8

Intro. to Programming, lecture 8: Describing the syntax 22

BNF Elements: Concatenation

Graphical representation:

BNF: A B

Meaning: A followed by B

A B

Intro. to Programming, lecture 8: Describing the syntax 23

Graphical representation:

BNF: [ A ]

Meaning: A or nothing

BNF Elements: Optional

A

Intro. to Programming, lecture 8: Describing the syntax 24

Graphical representation:

BNF: A | B

Meaning: either A or B

BNF Elements: Choice

A

B

Page 9: Lecture 8: Describing the Syntaxse.inf.ethz.ch/old/teaching/ws2006/0001/slides/08_bnf_3up.pdf · Intro. to Programming, lecture 8: Describing the syntax 20 Elements of a grammar:

9

Intro. to Programming, lecture 8: Describing the syntax 25

Graphical representation:

BNF: { A }*

Meaning: sequence of zero or more A

BNF Elements: Repetition

A

Intro. to Programming, lecture 8: Describing the syntax 26

Graphical representation:

BNF: { A }+

Meaning: sequence of one or more A

BNF Elements: Repetition, once or more

A

Intro. to Programming, lecture 8: Describing the syntax 27

BNF elements: Overview

ARepetition (at least once): { A }+

Repetition (zero or more): { A }* A

Choice: A | BA

B

AOptional: [ A ]

A BConcatenation: A B

Page 10: Lecture 8: Describing the Syntaxse.inf.ethz.ch/old/teaching/ws2006/0001/slides/08_bnf_3up.pdf · Intro. to Programming, lecture 8: Describing the syntax 20 Elements of a grammar:

10

Intro. to Programming, lecture 8: Describing the syntax 28

A simple example

.- digit

digit

float_number:0123456789

digit:

Example phrases:.76-.761.5612.845-1.3413.0

Intro. to Programming, lecture 8: Describing the syntax 29

BNF Elements Combined

written in BNF:

Conditional:

if

else

endthencondition instruction

instruction

,

[ instructioninstruction else endif thencondition ]

Conditional

Intro. to Programming, lecture 8: Describing the syntax 30

BNF: Conditional with elseif

,

,

,

Conditional

Then_part_list

Else_partThen_part_listif end[ ]

Then_part elseif }*{ Then_part

Then_part , Boolean_expression then Compound

Else_part else Compound

Page 11: Lecture 8: Describing the Syntaxse.inf.ethz.ch/old/teaching/ws2006/0001/slides/08_bnf_3up.pdf · Intro. to Programming, lecture 8: Describing the syntax 20 Elements of a grammar:

11

Intro. to Programming, lecture 8: Describing the syntax 31

Different Grammar for Conditional

Conditional

If_part

Then_part

Else_list

Elseif_part

Boolean_expressionif

If_part Then_part Else_list end

Compoundthen

Boolean_expression Then_partelseif

,

,

,

,

,

Elseif_part Compound{ ]else}* [

Intro. to Programming, lecture 8: Describing the syntax 32

Simple BNF exampleSentence I [ don’t ] Verb Names QuantNames Name {and Name}*Name tomatoes | shoes | books | footballVerb like | hateQuant a lot | a little

Which of the following phrases are correct?I like tomatoes and footballI don’t like tomatoes a littleI hate football a lotI like shoes and tomatoes a littleI don’t hate tomatoes, football and books a lot

Rewrite the BNF to include the incorrect phrases

,,,,,

Intro. to Programming, lecture 8: Describing the syntax 33

Simple BNF example (Solution)

Which of the following phrases are correct?- I like tomatoes and football

I don’t like tomatoes a littleI hate football a lotI like shoes and tomatoes a little

- I don’t hate tomatoes, football and books a lot

Rewrite the BNF to include the incorrect phrasesSentence I [ don’t ] Verb Names [ Quant ]Names Name [{, Name}* and Name]Name tomatoes | shoes | books | footballVerb like | hateQuant a lot | a little

,,,,,

Page 12: Lecture 8: Describing the Syntaxse.inf.ethz.ch/old/teaching/ws2006/0001/slides/08_bnf_3up.pdf · Intro. to Programming, lecture 8: Describing the syntax 20 Elements of a grammar:

12

Intro. to Programming, lecture 8: Describing the syntax 34

BNF-E

Used in official description of Eiffel.Every Production is one of

ConcatenationA , B C [ D ]

ChoiceA , B | C | D

RepetitionA , { B terminal ... }*

(also with +)

A , [ B { terminal B }* ]

Intro. to Programming, lecture 8: Describing the syntax 35

BNF-E Rules

Every nonterminal must appear on the left-hand side of exactly one production, called its defining production

Every production must be of one kind: Concatenation, Choice or Repetition

Intro. to Programming, lecture 8: Describing the syntax 36

Conditional with elseif (BNF)

,

,

,

Conditional

Then_part_list

Else_partThen_part_listif end[ ]

Then_part elseif }*{ Then_part

Then_part , Boolean_expression then Compound

Else_part else Compound

Page 13: Lecture 8: Describing the Syntaxse.inf.ethz.ch/old/teaching/ws2006/0001/slides/08_bnf_3up.pdf · Intro. to Programming, lecture 8: Describing the syntax 20 Elements of a grammar:

13

Intro. to Programming, lecture 8: Describing the syntax 37

BNF-E: Conditional

,

,

,

Conditional

Then_part_list

Else_partThen_part_listif end[ ]

Then_part , Boolean_expression then Compound

Else_part else Compound

elseif }+{ Then_part ...

Intro. to Programming, lecture 8: Describing the syntax 38

Recursive grammars

Constructs may be nested

Express this in BNF with recursive grammars

Recursion: circular dependency of productions

Intro. to Programming, lecture 8: Describing the syntax 39

Conditionals can be nested within conditionals:

Recursive grammars

,Else_part else Compound

Compound

Instruction

Instruction …}*{

...

,

, CallConditional Loop| | |

;

Page 14: Lecture 8: Describing the Syntaxse.inf.ethz.ch/old/teaching/ws2006/0001/slides/08_bnf_3up.pdf · Intro. to Programming, lecture 8: Describing the syntax 20 Elements of a grammar:

14

Intro. to Programming, lecture 8: Describing the syntax 40

Production name can be used in its own definition

Definition of Then_part_list with repetition:

Recursive definition of Then_part_list:

Recursive grammars

,Then_part_list …}*{ Then_part

,Then_part_list Then_part elseif ][ Then_part_list

elseif

Intro. to Programming, lecture 8: Describing the syntax 41

Conditional

if a = b thena := a - 1b := b + 1

elseif a > b thena := a + 1

elseb := b + 1

end

,

,

,

Conditional

Then_part_list

Else_partThen_part_listif end[ ]

Then_part , Boolean_expression then Compound

Else_part else Compound

elseif }+{ Then_part ...

Intro. to Programming, lecture 8: Describing the syntax 42

BNF for simple arithmetic expressions

Expr Term { Add_op Term }*Term Factor { Mult_op Factor}*Factor Number | Variable | Nested Nested ( Expr )Add_op + | –Mult_op * | /

Is this a recursive grammar?How would the same grammar in BNF-E look like?Which of the following phrases are correct?aa + b-a + ba * 7 + b7 / (3 * 12) – 7(3 * 7)(5 + a ( 7 * b))

,,,,,

,

Page 15: Lecture 8: Describing the Syntaxse.inf.ethz.ch/old/teaching/ws2006/0001/slides/08_bnf_3up.pdf · Intro. to Programming, lecture 8: Describing the syntax 20 Elements of a grammar:

15

Intro. to Programming, lecture 8: Describing the syntax 43

BNF for simple arithmetic expressions(Solution)

Expr {Term Add_op …}+

Term { Factor Mult_op …}+

Factor Number | Variable | Nested Nested ( Expr )Add_op + | –Mult_op * | /

Is this a recursive grammar? Yes (see Nested)How would the same grammar in BNF-E look like?(see yellow box below)Which of the following phrases are correct?a a + b -a + b -a * 7 + b 7 / (3 * 12) – 7 (3 * 7) (5 + a ( 7 * b)) -

,,,,,

,

Intro. to Programming, lecture 8: Describing the syntax 44

Guidelines for Grammars

Keep productions short.

easier to readbetter assessment of language size

Conditional ,if Boolean_expression then Compound{ elseif Boolean_expression then Compound }*

[ else Compound ] end

Intro. to Programming, lecture 8: Describing the syntax 45

Guidelines for Grammars

Treat lexical constructs like terminalsIdentifiersConstant values

Identifier , Letter (Letter | Digit | "_")*Integer_constant , Digit+

Floating_point , [-] Digit* “." Digit+

Letter , "A" | "B" | ... | "Z" | "a" | ... | "z"Digit , "0" | "1" | ... | "9“

Page 16: Lecture 8: Describing the Syntaxse.inf.ethz.ch/old/teaching/ws2006/0001/slides/08_bnf_3up.pdf · Intro. to Programming, lecture 8: Describing the syntax 20 Elements of a grammar:

16

Intro. to Programming, lecture 8: Describing the syntax 46

Guidelines for Grammars

Use unambiguous productions.Applicable production can be found by looking at one lexical

element at a time

Conditional , if Then_part_list [ Else_part ] end

Compound , { Instruction }*

Instruction , Conditional | Loop | Call | ...

Intro. to Programming, lecture 8: Describing the syntax 47

Writing a Parser

One feature per Production

Concatenation:Sequence of feature calls for Nonterminals, checks for Terminals

Choice:Conditional with Compound per alternative

Repetition:Loop

Intro. to Programming, lecture 8: Describing the syntax 48

Writing a Parser: EiffelParse

Automatic generation of abstract syntax tree for phrase

Based on BNF-E

One class per production

Classes inherit from predefined classes AGGREGATE, CHOICE, REPETITION, TERMINAL

Feature production defines Production

Page 17: Lecture 8: Describing the Syntaxse.inf.ethz.ch/old/teaching/ws2006/0001/slides/08_bnf_3up.pdf · Intro. to Programming, lecture 8: Describing the syntax 20 Elements of a grammar:

17

Intro. to Programming, lecture 8: Describing the syntax 49

Writing a Parser: Tools

Yooc

Translates BNF-E to EiffelParse classes

Yacc / Bison

Translates BNF to C parser

Intro. to Programming, lecture 8: Describing the syntax 50

BNF similar syntax descriptions

Unix/Linux: Synopsis of commandsSYNOPSISman [-acdfFhkKtwW] [--path] [-m system] [-p string] [-C

config_file] [-M pathlist] [-P pager] [-S section_list] [section] name ...

Intro. to Programming, lecture 8: Describing the syntax 51

What we have seen

Page 18: Lecture 8: Describing the Syntaxse.inf.ethz.ch/old/teaching/ws2006/0001/slides/08_bnf_3up.pdf · Intro. to Programming, lecture 8: Describing the syntax 20 Elements of a grammar:

18

Intro. to Programming, lecture 8: Describing the syntax 52

What we have seen

A way to describe syntax: BNF

Three variants: BNF, BNF-E, graphical

A glimpse into recursion

A preview of tomorrow: conditional

Intro. to Programming, lecture 8: Describing the syntax 53

End of lecture 8