Einführung in die Programmierung Introduction to Programming

Similar documents
An example (1) - Conditional. An example (2) - Conditional. An example (3) Nested conditional

Semi-Lossless Text Compression: a Case Study

Professional Communication

COP 3402 Systems Software Syntax Analysis (Parser)

Syntax. A. Bellaachia Page: 1

Theoretical Part. Chapter one:- - What are the Phases of compiler? Answer:

Chapter 3: CONTEXT-FREE GRAMMARS AND PARSING Part 1

CNT4406/5412 Network Security

This book is licensed under a Creative Commons Attribution 3.0 License

Syntax. In Text: Chapter 3

CPS 506 Comparative Programming Languages. Syntax Specification

Dr. D.M. Akbar Hussain

CSE 3302 Programming Languages Lecture 2: Syntax

UNIT I Programming Language Syntax and semantics. Kainjan Sanghavi

Einführung in die Programmierung Introduction to Programming

The impossible patent: an introduction to lossless data compression. Carlo Mazza

History of Typography. (History of Digital Font)

CSSE SEMESTER 1, 2017 EXAMINATIONS. CITS1001 Object-oriented Programming and Software Engineering FAMILY NAME: GIVEN NAMES:

announcements CSE 311: Foundations of Computing review: regular expressions review: languages---sets of strings

Chapter 3. Describing Syntax and Semantics

CSCE 314 Programming Languages

Lecture 4: Syntax Specification

Chapter 3. Describing Syntax and Semantics ISBN

programming languages need to be precise a regular expression is one of the following: tokens are the building blocks of programs

CMSC 330: Organization of Programming Languages. Context Free Grammars

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

EECS 6083 Intro to Parsing Context Free Grammars

Programming Language Definition. Regular Expressions

CSCI312 Principles of Programming Languages!

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

3. Context-free grammars & parsing

Principles of Programming Languages COMP251: Syntax and Grammars

A simple syntax-directed

Part 5 Program Analysis Principles and Techniques

CSE 311 Lecture 21: Context-Free Grammars. Emina Torlak and Kevin Zatloukal

CS323 Lecture - Specifying Syntax and Semantics Last revised 1/16/09

flex is not a bad tool to use for doing modest text transformations and for programs that collect statistics on input.

Chapter 3: Describing Syntax and Semantics. Introduction Formal methods of describing syntax (BNF)

2.2 Syntax Definition

CMPS Programming Languages. Dr. Chengwei Lei CEECS California State University, Bakersfield

Chapter 3. Describing Syntax and Semantics ISBN

Syntax Intro and Overview. Syntax

Chapter 3. Describing Syntax and Semantics

Syntax and Semantics

Department of Image Processing and Computer Graphics University of Szeged. Fuzzy Techniques for Image Segmentation. Outline.

Consider a description of arithmetic. It includes two equations that define the structural types of digit and operator:

3. DESCRIBING SYNTAX AND SEMANTICS

Computer Security & Privacy. Why Computer Security Matters. Privacy threats abound (identity fraud, etc.) Multi-disciplinary solutions

CSE450 Translation of Programming Languages. Lecture 4: Syntax Analysis

Defining Program Syntax. Chapter Two Modern Programming Languages, 2nd ed. 1

Wednesday, September 9, 15. Parsers

Parsers. What is a parser. Languages. Agenda. Terminology. Languages. A parser has two jobs:

Principles of Programming Languages COMP251: Syntax and Grammars

CS 315 Programming Languages Syntax. Parser. (Alternatively hand-built) (Alternatively hand-built)

Describing Syntax and Semantics

Syntax. Syntax. We will study three levels of syntax Lexical Defines the rules for tokens: literals, identifiers, etc.

A programming language requires two major definitions A simple one pass compiler

EDAN65: Compilers, Lecture 04 Grammar transformations: Eliminating ambiguities, adapting to LL parsing. Görel Hedin Revised:

Chapter 3. Syntax - the form or structure of the expressions, statements, and program units

COP 3402 Systems Software Top Down Parsing (Recursive Descent)

Chapter 2 - Programming Language Syntax. September 20, 2017

Architecture of Compilers, Interpreters. CMSC 330: Organization of Programming Languages. Front End Scanner and Parser. Implementing the Front End

Programming Language Specification and Translation. ICOM 4036 Fall Lecture 3

Chapter 4. Syntax - the form or structure of the expressions, statements, and program units

CMSC 330: Organization of Programming Languages

Programming Languages

CMSC 330: Organization of Programming Languages

Appendix A: Syntax Diagrams

ICOM 4036 Spring 2004

LANGUAGE PROCESSORS. Presented By: Prof. S.J. Soni, SPCE Visnagar.

Formal Languages and Grammars. Chapter 2: Sections 2.1 and 2.2

Defining syntax using CFGs

A Simple Syntax-Directed Translator

Semantics via Syntax. f (4) = if define f (x) =2 x + 55.

Syntax Analysis. COMP 524: Programming Language Concepts Björn B. Brandenburg. The University of North Carolina at Chapel Hill

CS /534 Compiler Construction University of Massachusetts Lowell. NOTHING: A Language for Practice Implementation

Last time. What are compilers? Phases of a compiler. Scanner. Parser. Semantic Routines. Optimizer. Code Generation. Sunday, August 29, 2010

Introduction to Parsing

Theory and Compiling COMP360

Programming Languages Third Edition

Syntax Analysis. Chapter 4

Syntax and Grammars 1 / 21

CMSC 330: Organization of Programming Languages. Architecture of Compilers, Interpreters

CMSC 330: Organization of Programming Languages

Homework. Lecture 7: Parsers & Lambda Calculus. Rewrite Grammar. Problems

Building Compilers with Phoenix

Where We Are. CMSC 330: Organization of Programming Languages. This Lecture. Programming Languages. Motivation for Grammars

ISO/IEC INTERNATIONAL STANDARD. Information technology - Syntactic metalanguage - Extended BNF

Programming Language Syntax and Analysis

CMSC 330: Organization of Programming Languages. Context Free Grammars

Syntax. 2.1 Terminology

Chapter 3: CONTEXT-FREE GRAMMARS AND PARSING Part2 3.3 Parse Trees and Abstract Syntax Trees

Chapter 4. Lexical and Syntax Analysis

CIT Lecture 5 Context-Free Grammars and Parsing 4/2/2003 1

Formal Languages. Formal Languages

CMSC 330: Organization of Programming Languages. Context Free Grammars

Lexical Analysis. COMP 524, Spring 2014 Bryan Ward

Introduction to Parsing. Lecture 8

MIT Specifying Languages with Regular Expressions and Context-Free Grammars

Transcription:

Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Michela Pedroni Lecture 11: Describing the Syntax

Goals of today s lecture Learn about languages that describes other languages Read and understand the syntax description for Eiffel Write simple syntax descriptions 2

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 compound The keyword end. Here each condition is a Boolean expression, and each compound is a compound instruction. 3

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 4

The power of human mind I cdnoult blvelee taht I cluod aulacity uesdnatnrd waht I was rdgnieg. The Paomnnehal Pweor of the Hmuan Mnid Aoccdrnig 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 ltteer be in the rghit pclae. The rset can be a taotl mses and you can sitll raed it wouthit any porbelm. Tihs is bcuseae the huamn mnid deos not raed ervey lteter by istlef, but the wrod as a wlohe. Ptrety Amzanig Huh? 5

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 6

Why describe the syntax formally? Compilers use algorithms to Determine if input is correct program text (parser) Analyze program text to extract specimens for abstract syntax tree Translate program text to machine instructions Compilers need strict formal definition of programming language 7

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) 8

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 Naur for his contribution Backus-Naur-Form Many variants since then, e.g. graphical variant by Niklaus Wirth 9

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 10

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 11

Example of phrases class PERSON feature age: INTEGER -- Age end class age: INTEGER -- Age end PERSON feature 12

Grammar Definition A Grammar for a language is a finite set of rules for producing 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. 13

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 or symbols such as the semicolon ; or the assignment : 14

Elements of a grammar: Nonterminals Nonterminals Names of syntactical structures or substructures used to build phrases. 15

Elements of a grammar: Productions Productions Rules that define nonterminals of the grammar using a combination of terminals and (other) nonterminals 16

An example production Conditional: else Instruction if Condition then Instruction end Terminal Nonterminal Production 17

BNF Elements: Concatenation Graphical representation: A B BNF: A B Meaning: A followed by B 18

BNF Elements: Optional Graphical representation: A BNF: [ A ] Meaning: A or nothing 19

BNF Elements: Choice Graphical representation: A B BNF: A B Meaning: either A or B 20

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

BNF Elements: Repetition, once or more Graphical representation: A BNF: { A } + Meaning: sequence of one or more A 22

BNF elements: Overview Concatenation: A B A B Optional: [ A ] Choice: A B Repetition (zero or more): { A }* A A B A Repetition (at least once): { A } + A 23

A simple example digit: 0 1 2 3 4 5 6 7 8 9 float_number: - digit Example phrases:.76 -.76 1.56 12.845-1.34 13.0 Translate it to written form!. digit 24

A simple example written in BNF: digit 0 1 2 3 4 5 6 7 8 9 float_number [ - ] { digit }*. { digit } + 25

BNF Elements Combined Conditional: else instruction if condition then instruction end written in BNF: Conditional if condition then instruction [ else instruction end ] 26

BNF: Conditional with elseif Conditional if Then_part_list [ Else_part ] end Then_part_list Then_part { elseif Then_part } * Then_part Boolean_expression then Compound Else_part else Compound 27

Different Grammar for Conditional Conditional If_part Then_part Else_list end If_part if Boolean_expression Then_part then Compound Else_list { Elseif_part } * [ else Compound ] Elseif_part elseif Boolean_expression Then_part 28

Simple BNF example Sentence I [ don t ] Verb Names Quant Names Name {and Name}* Name tomatoes shoes books football Verb like hate Quant a lot a little Which of the following phrases are correct? I like tomatoes and football I don t like tomatoes a little I hate football a lot I like shoes and tomatoes a little I don t hate tomatoes, football and books a lot Rewrite the BNF to include the incorrect phrases 29

Simple BNF example (Solution) Which of the following phrases are correct? - I like tomatoes and football I don t like tomatoes a little I hate football a lot I like shoes and tomatoes a little - I don t hate tomatoes, football and books a lot Rewrite the BNF to include the incorrect phrases Sentence I [ don t ] Verb Names [ Quant ] Names Name [{, Name}* and Name] Name tomatoes shoes books football Verb like hate Quant a lot a little 30

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 delimiter... } * A { B delimiter... } + Interpreted as A [ B { delimiter B } * ] Interpreted as A B { delimiter B } * 31

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 32

Conditional with elseif (BNF) Conditional if Then_part_list [ Else_part ] end Then_part_list Then_part { elseif Then_part } * Then_part Boolean_expression then Compound Else_part else Compound 33

BNF-E: Conditional Conditional if Then_part_list [ Else_part ] end Then_part_list { Then_part elseif... } + Then_part Boolean_expression then Compound Else_part else Compound 34

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

Recursive grammars Conditionals can be nested within conditionals: Else_part else Compound Compound { Instruction ; } * Instruction Conditional Loop Call... 36

Recursive grammars Production name can be used in its own definition Definition of Then_part_list with repetition: Then_part_list { Then_part elseif } * Recursive definition of Then_part_list: Then_part_list Then_part [ elseif Then_part_list ] 37

Conditional if a b then a : a - 1 b : b + 1 elseif a > b then a : a + 1 else b : b + 1 end Conditional if Then_part_list [ Else_part ] end Then_part_list elseif } + { Then_part... Then_part Boolean_expression then Compound Else_part else Compound 38

BNF for simple arithmetic expressions Assume Number is defined as a positive integer number, and Variable is a one-letter alphabetical word. Is this a recursive grammar? How would the same grammar in BNF-E look like? 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)) Expr Term { Add_op Term }* Term Factor { Mult_op Factor}* Factor Number Variable Nested Nested ( Expr ) Add_op + Mult_op * / 39

BNF for simple arithmetic expressions (Solution) 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)) - Expr {Term Add_op } + Term { Factor Mult_op } + Factor Number Variable Nested Nested ( Expr ) Add_op + Mult_op * / 40

Guidelines for Grammars Keep productions short. easier to read better assessment of language size Conditional if Boolean_expression then Compound { elseif Boolean_expression then Compound } * [ else Compound ] end 41

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 42

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

Eiffel syntax description The syntax of Eiffel is described in BNF-E One production per Nonterminal Production is one of concatenation, choice, repetition Special semantics of repetition Recursion allowed Terminal (lexical) constructs do not use BNF-E for description reserved words(e.g. if, end, class) manifest constants (237, -12.93) symbols (+, ;) Identifiers (LINKED_LIST, put) 44

Eiffel syntax description (lexical level) Lexical constructs described with BNF-like regular grammar Mixing production types allowed Use parentheses for disambiguating, e.g. Letter (Letter Digit Underscore)* No recursion permitted Uses symbols and character intervals, e.g. a.. z Simple repetition rules (see BNF) Concatenation does not assume breaks between concatenated elements 45

One more exercise Define a recursive grammar in BNF-E for boolean expressions with the following description: Simple expressions limited to the variable identifiers x, y, or z, that contain operations of unary not, and binary and, or, and implies together with parentheses. Valid phrases would include not x and not y (x or y implies z) y or (z) (x) 46

Solution binary expressions B_expr With_par Expr With_par ( Expr ) Expr Not_term Bin_term Variable Bin_term B_expr Bin_op B_expr Bin_op implies or and Not_term not B_expr Variable x y z Note: Here we are using x to denote terminals 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 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 49

Writing a Parser: Tools Yooc: Translates BNF-E to EiffelParse classes Yacc / Bison: Translates BNF to C parser 50

BNF alike syntax descriptions DTD: Description of XML documents <!ELEMENT collection (recipe*)> <!ELEMENT recipe (title, ingredient*,preparation)> <!ELEMENT title (#PCDATA)> <!ELEMENT ingredient (ingredient*,preparation)?> <!ATTLIST ingredient name CDATA #REQUIRED amount CDATA #IMPLIED unit CDATA #IMPLIED> <!ELEMENT preparation (step*)> <!ELEMENT step (#PCDATA)> 51

BNF alike syntax descriptions Unix/Linux: Synopsis of commands SYNOPSIS man [-acdffhkktww] [--path] [-m system] [-p string] [-C config_file] [-M pathlist] [-P pager] [-S section_list] [section] name... 52

Eiffel syntax http://www.ecma-international.org/publications/files/ ECMA-ST/Ecma-367.pdf http://www.gobosoft.com/eiffel/syntax/ 53

What we have seen A way to describe syntax: BNF Three variants: BNF, BNF-E, graphical A glimpse into recursion 54

Exercise Describe BNF-E with the help of BNF-E. Assume that the lexical constructs Keyword and Symbol (for Terminals) and Identifier (for Nonterminals) are given: Terminal Keyword Symbol Nonterminal Identifier 55