Pascal Schärli

Ähnliche Dokumente
17. Rekursion 2. Bau eines Taschenrechners, Ströme, Formale Grammatiken, Extended Backus Naur Form (EBNF), Parsen von Ausdrücken

15. Rekursion. Rekursive Funktionen, Korrektheit, Terminierung, Aufrufstapel, Bau eines Taschenrechners, BNF, Parsen

Programmier-Befehle - Woche 10

15. Rekursion 2. Motivation: Taschenrechner. Naiver Versuch (ohne Klammern) Analyse des Problems (15 7 3) = Eingabe * 3 = Ergebnis 15

Programmier-Befehle - Woche 08

17. Rekursion 2. Bau eines Taschenrechners, Formale Grammatiken, Ströme, Backus Naur Form (BNF), Parsen

15. Rekursion 2. Bau eines Taschenrechners, Ströme, Formale Grammatiken, Extended Backus Naur Form (EBNF), Parsen von Ausdrücken

Informatik für Mathematiker und Physiker Woche 7. David Sommer

Informatik - Übungsstunde

a < &a[2] a < &a[2] Wert/Value: true

Aufgabe 1.1. Wählen Sie jene Variablendeklarationen mit Initialisierungen aus, die in Java hinsichtlich der Typen korrekt sind:

D-BAUG Informatik I. Exercise session: week 1 HS 2018

Introduction to Python. Introduction. First Steps in Python. pseudo random numbers. May 2016

16. Structs und Klassen I. Rationale Zahlen, Struct-Definition, Operator-Überladung, Datenkapselung, Klassen-Typen

Informatik für Mathematiker und Physiker Woche 2. David Sommer

Übungsstunde: Informatik 1 D-MAVT

Prüfung Informatik D-MATH/D-PHYS :00 11:00

Informatik D-MATH/D-PHYS Self-Assessment IV,

Prüfung Informatik D-MATH/D-PHYS :00 17:00

Kontrollfluss. man Verzweigungen und Sprünge. o bisher linear (von oben nach unten) o Für interessante Programme braucht

Übungsstunde: Informatik 1 D-MAVT

Angewandte Mathematik und Programmierung

Übungsstunde 10. Einführung in die Programmierung

Prüfung A Informatik D-MATH/D-PHYS :15 14:55

Das folgende Kleingedruckte finden Sie auch auf einer "scharfen" Prüfung. 1. Dauer der Prüfung: 15 Minuten. Exam duration: 15 minutes.

12. Felder (Arrays) II. Caesar-Code, Strings, Lindenmayer-Systeme, Mehrdimensionale Felder, Vektoren von Vektoren, Kürzeste Wege

Introduction to Python. Introduction. First Steps in Python. pseudo random numbers. May 2018

Übungsstunde 10. Einführung in die Programmierung I

Lindenmayer-Systeme: Fraktale rekursiv zeichnen

Informatik für Mathematiker und Physiker Woche 6. David Sommer

Übungsstunde 11. Einführung in die Programmierung

Tutoraufgabe 1 (Implementierung eines ADTs):

Programmier-Befehle - Woche 9

Übersetzen des Quelltexts in ausführbaren Maschinen-Code Translation of source code into executable machine code

Automatentheorie und formale Sprachen reguläre Ausdrücke

Vorsemesterkurs Informatik Sommersemester Aufgabenblatt Nr. 5A. Lösung zu Aufgabe 1 (Fehler in Haskell-Quelltext: Parsefehler)

Programmier-Befehle - Woche 09

Der Grid-Occam-Compiler. Übersicht über die OCCAM-Sprachdefinition

Informatik Prüfung Lösung B. Gärtner

Prüfung Informatik D-MATH/D-PHYS :00 11:00

Informatik 1 ( ) D-MAVT F2011. Operatoren, Auswertungsreihenfolge, Fliesskommazahlen. Yves Brise Übungsstunde 2

Informatik 1 Kurzprüfung 2 LÖSUNG

Programmier-Befehle - Woche 8

17. Klassen. Überladen von Funktionen. Funktionsüberladung. Operator-Überladung (Operator Overloading) operatorop

Informatik 1 ( ) D-MAVT F2010. Operatoren, Auswertungsreihenfolge, Fliesskommazahlen. Yves Brise Übungsstunde 2

Informatik 1 ( ) D-MAVT F2011. Pointer, Structs. Yves Brise Übungsstunde 6

Informatik I Übung, Woche 38

Informatik II Übung 3. Pascal Schärli

Computational Models

Algorithmen und Datenstrukturen Musterlösung 5

Prüfung Informatik D-MATH/D-PHYS :00 11:00

Grundlegende Datentypen

12/18/12 // POST: values of a and b are interchanged void swap (int& a, int& b) { int c = a; a = b; b = c;

Funktionale Programmierung Grundlegende Datentypen

4.3 Keller und Warteschlangen

Grundlegende Datentypen

Dynamische Datentypen. Destruktor, Copy-Konstruktor, Zuweisungsoperator, Dynamischer Datentyp, Vektoren

18. Vererbung und Polymorphie

Informatik Prüfung Lösung B. Gärtner

19. Vererbung und Polymorphie

Software Entwicklung 1. Fallstudie: Arithmetische Ausdrücke. Rekursive Klassen. Überblick. Annette Bieniusa / Arnd Poetzsch-Heffter

Informatik I (D-ITET)

Ganze Zahlen. Die Typen int, unsigned int; Auswertung arithmetischer Ausdrücke, arithmetische Operatoren

Verzweigungen. Prof. Dr. Markus Gross Informatik I für D-MAVT (FS 2014)

18. Vererbung und Polymorphie

Problem: Keine Integers in JavaCard. ToDo: Rechnen mit Bytes und Shorts

Repetitive Strukturen

4. Verzweigungen. Prof. Dr. Markus Gross Informatik I für D-ITET (WS 03/04)

Finite Difference Method (FDM)

Grundlegende Datentypen

Woche 6. Cedric Tompkin. April 11, Cedric Tompkin Woche 6 April 11, / 29

Felder (Arrays) und Zeiger (Pointers) - Teil II. Zeichen, Texte, Caesar-Code, String Matching; Mehrdimensionale Felder; kürzeste Wege

Software Entwicklung 1

Java Tools JDK. IDEs. Downloads. Eclipse. IntelliJ. NetBeans. Java SE 8 Java SE 8 Documentation

Informatik I Übung, Woche 38

6. Kontrollanweisungen II

12. Felder (Arrays) II

Visuelle Kryptographie. Anwendung von Zufallszahlen

Vorsemesterkurs Informatik

5. Kontrollanweisungen II

Programmieren in Java

Magic Figures. We note that in the example magic square the numbers 1 9 are used. All three rows (columns) have equal sum, called the magic number.

Lindenmayer-Systeme: Fraktale rekursiv zeichnen. Definition. Definition. Definition. o Alphabet Σ ( Beispiel: {F, +, -} )

7. Vererbung und Polymorphie

Beispiel: Temperaturumwandlung. Imperative Programmierung. Schwerpunkte. 3. Grundlegende Sprachkonstruktionen imperativer Programme

Grundlagen der Informatik 6. Arrays I

Quiz und Übungen. C++ Übung am 19. Mai 2016

3.1 Reservierte Wörter

Datenkapselung: public / private

Unit 4. The Extension Principle. Fuzzy Logic I 123

12. Felder (Arrays) II. Caesar-Code, Strings, Lindenmayer-Systeme, Mehrdimensionale Felder, Vektoren von Vektoren, Kürzeste Wege

TECHNISCHE UNIVERSITÄT MÜNCHEN FAKULTÄT FÜR INFORMATIK

Algorithmen zur Datenanalyse in C++

1. Der Begriff Informatik 2. Syntax und Semantik von Programmiersprachen - 1 -

Transkript:

Informatik I - Übung 8 Pascal Schärli pascscha@student.ethz.ch 12.04.2019 1

Was gibts heute? Best-Of Vorlesung: Prefix / Infix EBNF Vorbesprechung Problem of the Week 2

Vorlesung 3. 1

Prefix Notation Infix (normal) Expression1 Operator Expression2 Prefix Operator Expression1 Expression2 Infix Prefix a + b + a b a * b - c - * a b c 3. 2

Prefix Notation (a + b) * (c - d / e) Gegen Uhrzeigersinn um den Baum Gehen und wenn man links von einer Node ist aufschreiben. a + b * - c / d e * + a b - c / d e 3. 3

Infix vs Prefix Notation Infix Prefix a * b e / f * a b / e f (a * b) / (c * d) / * a b * c d a * b + c * d a + b * c + d (a + b) * (c - d / e) + * a b * c d + + a * b c d * + a b - c / d e 3. 4

BNF Die Backus-Naur-Form (BNF) ist ein Rekursiver Weg um Elemente aus einem "Alphabet" mit gegebenen Regeln zusammenzusetzen. Die BNF ist eine Sammlung von Substitutionsregeln, mit welchen alle erlaubten Sätze erstellt werden können. 3. 5

BNF - Beispiel Alphabet: {A, a, _ Regeln: "A" kann nur als das Erste Symbol oder direkt nach "_" kommen. "_" kann nicht als das Erste oder Letzte Symbol Kommen "_" kommt nur einzeln Vor. 1 2 3 seq = term term "_" seq term = "A" "A" lowerterm lowerterm lowerterm = "a" "a" lowerterm Beispiel Aaa_aa Aaaa aaaa 3. 6

BNF - Beispiel 1 2 3 seq = term term "_" seq term = "A" "A" lowerterm lowerterm lowerterm = "a" "a" lowerterm Kann man "Aaa_A" mit dieser BNF ausdrücken? term "_" seq "A" lowerterm "_" seq "Aa" lowerterm "_" seq "Aaa_" seq "Aaa_" term "Aaa_A" 3. 7

EBNF Die Extended-Backus-Naur-Form hat zusätzliche Syntax -> kompaktere Notationen {... -> Inhalt kann beliebig viel (inkl. 0) Mal wiederholt werden. [...] -> Inhalt wird entweder 0 oder 1 Mal wiederholt BNF 1 2 3 seq = term term "_" seq term = "A" "A" lowerterm lowerterm lowerterm = "a" "a" lowerterm EBNF 1 2 seq = term [ "_" seq ] term = "A" { "a" "a" { "a" 3. 8

EBNF EBNF 1 2 seq = term [ "_" seq ] term = "A" { "a" "a" { "a" A aaaa a _ Aaaa A_A Aa_Aa Aa a 3. 9

EBNF in C++ 1 2 seq = term [ "_" seq ] term = "A" { "a" "a" { "a" Für Jede Regel machen wir eine Funktion vom Typ bool, welche prüft ob die Regel erfüllt ist oder nicht. // PRE: Valid stream is. // POST: Returns true if stream contains seq and // extracts it, otherwise false. bool seq(std::istream& is); // PRE: Valid stream is. // POST: Returns true if stream contains term and // extracts it, otherwise false. bool term(std::istream &is); 3. 10

EBNF in C++ Wir erstellen eine lookahead Funktion, welche schaut, was das nächste Zeichen ist, ohne dies vom String zu entfernen. // PRE: Valid stream is. // POST: Leading whitespace characters are extracted from is, and the // first non-whitespace character is returned (0 if there is none). char lookahead (std::istream& is) { is >> std::ws; // skip whitespaces if (is.eof()) return 0; // end of stream else return is.peek(); // next character in is 3. 11

EBNF in C++ Wir erstellen eine consume Funktion, welche einen Charakter vom Stream entfernt. // PRE: Valid stream is. // POST: Reads char from stream and returns true if that char matches // the expected char, otherwise false is returned. bool consume(std::istream& is, char expected) { char actual; is >> actual; return actual == expected; 3. 12

EBNF in C++ Die Sequenz Funktion prüft zuerst, ob wir einen Term haben. Danach prüft es ob noch ein optionales '_' kommt, falls ja folgt eine weitere Sequenz. // PRE: Valid stream is. // POST: Returns true if stream contains seq and extracts it, otherwise fals bool seq(std::istream& is){ // seq = term [ "_" seq ] bool valid = term(is); // Every seq has to start with a term if(!valid){ // If it does not start with a term, return false return false; if(lookahead(is) == '_'){ // check if we have another seq consume(is, '_'); // "consume" the "_" from the instream return seq(is); // Check if the rest forms a valid seq else{ // If there is no following seq return true; 3. 13

EBNF in C++ Ein Term liest zuerst ein Buchstabe ein, dieser Kann 'A' oder 'a' sein. Danach liest dieser alle darauf folgenden 'a' ein. // PRE: Valid stream is. // POST: Returns true if stream contains term and extracts it, // otherwise false. bool term(std::istream &is){ // term = "A" { "a" "a" { "a" char first = lookahead(is); if(first == 'A' first == 'a'){ consume(is, first); while(lookahead(is) == 'a'){ consume(is, 'a'); return true; else{ return false; 3. 14

EBNF in C++ A a a _ A 1 2 3 4 5 6 7 8 9 10 11 12 13 1 2 3 4 5 6 7 8 9 10 11 12 13 bool seq(std::istream& is){ bool valid = term(is); if(!valid){ return false; if(lookahead(is) == '_'){ consume(is, '_'); return seq(is); else{ return true; bool term(std::istream &is){ char first = lookahead(is); if(first == 'A' first == 'a'){ consume(is, first); while(lookahead(is) == 'a'){ consume(is, 'a'); return true; else{ return false; 3. 15

EBNF in C++ Die Main-Funktion liest eine Zeile ein und übergibt diese dann der Prüfenden Funktion. Danach müssen wir mit lookahead noch prüfen, ob wir alles gelesen haben int main() { std::cout << "please enter a sequence:\n"; std::string sequence; std::cin >> sequence; std::stringstream sequence_in(sequence); if (seq(sequence_in) && lookahead(sequence_in) == 0) { std::cout << "valid\n"; else { std::cout << "invalid\n"; return 0; 3. 16

Vorbesprechung 4. 1

1: Trains train = open compositions open = loco cars loco = "*" "*" loco cars = "-" "-" cars compositions = composition { composition composition = "<" open loco ">" Schreibt ein Programm, welches bestimmen kann ob ein eingelesener Input dieser EBNF entspricht. Tipp: Analog Zu unserem Beispiel 4. 2

1: Trains train = open compositions open = loco cars loco = "*" "*" loco cars = "-" "-" cars compositions = composition { composition composition = "<" open loco ">" <*-*><***-----******> <*-*> <***-----******> *- * ***----- ****** * - *** ----- 4. 3

2: Money $$$ Wie Viele Münz/Noten-Kombinationen gibt es für X Rappen? Beispiel 20 Rappen - 4 Lösungen: Schreibt ein Rekursives Programm, welches die Anzahl Möglichen Kombinationen für beliebige X Rappen berechnet. 4.4

2: Money $$$ Die Main-Funktion ist bereits geschrieben, alles was Ihr schreiben müsst ist eine Funktion partitions im File partitions.cpp // PRE: 0 <= end <= v.size(), and // 0 < denominations[0] < denominations[1] <.. denominators[end-1] // describes a (potentially empty) // sequence of denominations // POST: return value is the number of ways to partition amount // using denominations from denominations[0],..., denominations[end-1] unsigned int partitions (unsigned int amount, const std::vector<unsigned int> & denominations, unsigned int end) { // Please write your code here.. Anzahl Partitionen: Denominations Geldstücke Es gibt 1 Weg kein Geld zu haben Initiere Anzahl Partitionen mit 0 Wiederhole für alle denominations von 0 bis end: 1. Prüfe ob die denomination überhaupt Platz hat 2. Falls ja, Berechne die Anzahl Partitionen bei welchen alle Geldstücke höchstens so viel Wert sind wie die aktuelle denomination. (rekrursion) 3. Addiere den Wert von 2. zu deiner Anzahl Partitionen. 4. 5

3: Prefix To Infix Schreibt ein Programm, welches Prefix zu Infix umwandeln kann. Input: Output: * + a b - c / d e (a + b) * (c - d / e) 4. 6

3: Prefix To Infix // PRE: valid stream is with operator (+-*/) as first char // precedence_operator is the precedence of the operator // precedence_left is precedence of left sub-expression // precedence_right is precedence of right sub-expression // POST: Read operator followed by two consecutive prefix expressions e_1 and // e_2 from is. // Print e_1 operator_name e_2 in infix notation with parenthesis around // if precedence_operator < precedence_left void print_operator(std::istream& is, char operator_name, int precedence_operator, int precedence_left, int precedence_right){ // TODO // PRE: valid stream is, // int precedence describes the precedence of the expression to the left // POST: Converts prefix expression e from input stream and prints it in infix // notation with according parenthesis void convert(std::istream& is, int precedence_left){ // TODO int main () { convert(std::cin, 0); return 0; 4. 7

3: Prefix To Infix void print_operator(std::istream& is, char operator_name, int precedence_operator, int precedence_left, int precedence_right) void convert(std::istream& is, int precedence_left) Precedences: Operator Right + 0 0-0 1 / 1 1 * 1 2 - a + b c a - ( b + c ) precedence_left > precedence_operator convert(is, 0) print_operator(is, "-", 0, 0, 1) convert(is, 0) convert(is, 1) print_operator(is, "+", 0, 1, 0) convert(is, 1) convert(is, 0) 4. 8

Program of the Week 5

Viel Spass! 6