1 History Formal Description of Syntax 7 8 1954 FORTRAN: First - - PDF document

1
SMART_READER_LITE
LIVE PREVIEW

1 History Formal Description of Syntax 7 8 1954 FORTRAN: First - - PDF document

1 2 Introduction to Programming Lecture 9: Describing the Syntax Michela Pedroni 16 Novem ber 2003 Chair of Softw are Engineering I ntro Lecture 9 Chair of Softw are Engineering I ntro Lecture 9 Why describe the syntax formally?


slide-1
SLIDE 1

1

I ntro – Lecture 9

1 Chair of Softw are Engineering

Introduction to Programming

Michela Pedroni

16 Novem ber 2003

I ntro – Lecture 9

2 Chair of Softw are Engineering

Lecture 9: Describing the Syntax

I ntro – Lecture 9

3 Chair of Softw are Engineering

Why describe the syntax formally?

We know syntax descriptions for human languages: e.g. gramm ar for German, French, … Expressed in natural language Good enough for human use Ambiguous, like human languages themselves

I ntro – Lecture 9

4 Chair of Softw are Engineering

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 m ore “Else if parts”, each of the form elseif condition then com pound. Zero or one “Else part” of the form else compound The keyword end. Here each condition is a boolean expression, and each com pound is a compound instruction.

I ntro – Lecture 9

5 Chair of Softw are Engineering

Why describe the syntax formally?

Programm ing languages need better descriptions: More precise: must tell us unambiguously whether given program text is legal or not Use formalism sim ilar to mathematics Can be fed into com pilers for automatic processing of programs

I ntro – Lecture 9

6 Chair of Softw are Engineering

Why describe the syntax formally?

Compilers use algorithm s to Determine if input is correct program text Analyse program text to extract specimens Translate program text to machine instructions Compilers need strict form al definition of programm ing language

slide-2
SLIDE 2

2

I ntro – Lecture 9

7 Chair of Softw are Engineering

Formal Description of Syntax

Use formal language to describe programm ing 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)

I ntro – Lecture 9

8 Chair of Softw are Engineering

History

1954 FORTRAN: First widely recognized programm ing 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 Naur for his contribution Backus-Naur- Form Many variants since then, e.g. graphical variant by Niklaus Wirth

I ntro – Lecture 9

9 Chair of Softw are Engineering

Formal description of a language

BNF lets us describe syntactical properties of a language Remember: Description of a programm ing language also includes lexical and semantic properties other tools

I ntro – Lecture 9

10 Chair of Softw are Engineering

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 gramm ar specifies which sequences are phrases and which are not BNF is used to define a gramm ar for a programming language

I ntro – Lecture 9

11 Chair of Softw are Engineering

Grammar

Definition A Gramm ar for a language is a finite set of rules for producing phrases, such that:

  • 1. Any sequence obtained by a finite num ber of

applications of rules from the gramm ar is a phrase

  • f the language.
  • 2. Any phrase of the language can be obtained by a

finite num ber of applications of rules from the gramm ar.

I ntro – Lecture 9

12 Chair of Softw are Engineering

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, end

  • r symbols such as the semicolon “;” or the

assignment “:= ”

slide-3
SLIDE 3

3

I ntro – Lecture 9

13 Chair of Softw are Engineering

Elements of a grammar: Nonterminals

Nonterm inals Names of syntactical structures or substructures used to build phrases.

I ntro – Lecture 9

14 Chair of Softw are Engineering

Elements of a grammar: Productions

Productions Rules that define nonterm inals of the grammar using a com bination of term inals and (other) nonterm inals

I ntro – Lecture 9

15 Chair of Softw are Engineering

An example production

Terminal Nonterminal Production

Conditional: if else end then Condition Instruction Instruction

I ntro – Lecture 9

16 Chair of Softw are Engineering

BNF Elements: Concatenation

Graphical representation: BNF: A B Meaning: A followed by B

A B

I ntro – Lecture 9

17 Chair of Softw are Engineering

Graphical representation: BNF: [ A ] Meaning: A or nothing

BNF Elements: Optional

A

I ntro – Lecture 9

18 Chair of Softw are Engineering

Graphical representation: BNF: A | B Meaning: either A or B

BNF Elements: Choice

A B

slide-4
SLIDE 4

4

I ntro – Lecture 9

19 Chair of Softw are Engineering

Graphical representation: BNF: { A } * Meaning: sequence of zero or more A

BNF Elements: Repetition

A

I ntro – Lecture 9

20 Chair of Softw are Engineering

Graphical representation: BNF: { A } + Meaning: sequence of one or more A

BNF Elements: Repetition, once or more A

I ntro – Lecture 9

21 Chair of Softw are Engineering

BNF elements: Overview

A

Repetition (at least once): { A }+ Repetition (zero or more): { A }*

A

Choice: A | B

A B A

Optional: [ A ]

A B

Concatenation: A B

I ntro – Lecture 9

22 Chair of Softw are Engineering

BNF Elements Combined

written in BNF: Conditional: if else end then condition instruction instruction

,

[

instruction instruction else end if then condition

]

Conditional

I ntro – Lecture 9

23 Chair of Softw are Engineering

BNF: Conditional with elseif

, , ,

Conditional Then_part_list Else_part Then_part_list if end

[ ]

Then_part elseif

} * {

Then_part Then_part , Boolean_expression then Compound Else_part else Compound

I ntro – Lecture 9

24 Chair of Softw are Engineering

Different Grammar for Conditional

Conditional If_part Then_part Else_list Elseif_part Boolean_expression if If_part Then_part Else_list end Compound then Boolean_expression Then_part elseif

, , , , ,

Elseif_part Compound

{ ]

else

} * [

slide-5
SLIDE 5

5

I ntro – Lecture 9

25 Chair of Softw are Engineering

BNF elements: Overview

A

Repetition (at least once): { A }+ Repetition (zero or more): { A }*

A

Choice: A | B

A B A

Optional: [ A ]

A B

Concatenation: A B

I ntro – Lecture 9

26 Chair of Softw are Engineering

BNF-E

Used in official description of Eiffel. Every Production is one of Concatenation A , B C [ D ] Choice A , B | C | D Repetition A , { B terminal ... } *

A , [ B { terminal B } * ]

I ntro – Lecture 9

27 Chair of Softw are Engineering

BNF-E Rules

Every nonterm inal m ust 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

I ntro – Lecture 9

28 Chair of Softw are Engineering

Conditional with elseif (BNF)

, , ,

Conditional Then_part_list Else_part Then_part_list if end

[ ]

Then_part elseif

} * {

Then_part Then_part , Boolean_expression then Compound Else_part else Compound

I ntro – Lecture 9

29 Chair of Softw are Engineering

BNF-E: Conditional

, , ,

Conditional Then_part_list Else_part Then_part_list if end

[ ]

Then_part , Boolean_expression then Compound Else_part else Compound elseif

} + {

Then_part

...

I ntro – Lecture 9

30 Chair of Softw are Engineering

Recursive grammars

Constructs may be nested Express this in BNF with recursive grammars Recursion: circular dependency of productions

slide-6
SLIDE 6

6

I ntro – Lecture 9

31 Chair of Softw are Engineering

Conditionals can be nested within conditionals:

Recursive grammars ,

Else_part else Compound Compound Instruction Instruction

… } * {

...

, ,

Call Conditional Loop

| | |

;

I ntro – Lecture 9

32 Chair of Softw are Engineering

Production nam e 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

I ntro – Lecture 9

33 Chair of Softw are Engineering

Guidelines for Grammars

Keep productions short. easier to read better assessm ent of language size Conditional , if Boolean_expression then Compound { elseif Boolean_expression then Com pound } * [ else Compound ] end

I ntro – Lecture 9

34 Chair of Softw are Engineering

Guidelines for Grammars

Treat lexical constructs like terminals Identifiers Constant values Identifier , Letter (Letter | Digit | "_ ")* Integer_constant , Digit + Floating_point , [ -] Digit* “." Digit + Letter , "A" | "B" | ... | "Z" | "a" | ... | "z" Digit , "0 " | "1 " | ... | "9 “

I ntro – Lecture 9

35 Chair of Softw are Engineering

Guidelines for Grammars

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

  • ne lexical element at a time

Conditional , if Then_part_list [ Else_part ] end Compound , { Instruction } * Instruction , Conditional | Loop | Call | ...

I ntro – Lecture 9

36 Chair of Softw are Engineering

Writing a Parser

One feature per Production Concatenation: Sequence of feature calls for Nonterm inals, checks for Term inals Choice: Conditional with Com pound per alternative Repetition: Loop

slide-7
SLIDE 7

7

I ntro – Lecture 9

37 Chair of Softw are Engineering

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

I ntro – Lecture 9

38 Chair of Softw are Engineering

Writing a Parser: Tools

Yooc Translates BNF-E to EiffelParse classes Yacc / Bison Translates BNF to C parser

I ntro – Lecture 9

39 Chair of Softw are Engineering

End lecture 9