CSE 143 Lecture 14 Maps Grammars Based on

CSE 143 Lecture 14 Maps Grammars Based on slides by Marty Stepp & Alyssa Harding 1

Example: student. Grades • (Reminder – Our Midterm is Monday Nov 9 th) • We want to write a very simple program to keep track of each student’s grade on the midterm. • In other words, to associate a numeric grade with the name of each person in the class: Joe 32 Sally 87 Mike 51 2

Example: student. Grades • We could keep another list of all the grades – Student at index 0 has grade at index 0, and so on 0 1 2 students Joe Sally Mike grades 32 87 51 But that is tedious. . . 3

Maps • Solution: maps allow us to associate key/value pairs – For example, a student name with a grade student names grades Steve Sally 32 51 Joe Mike 87 • Also known as a dictionary, associative array, hash – Can think of it as an array where you can have indexes of any type of Object instead of just ints 4

Maps • Java’s Map<K, V> interface that uses generic key/value types // adds a mapping from the given key to the given value void put(K key, V value) // returns the value mapped to the given key (null if none) V get(K key) // returns true if map contains a mapping for the given key boolean contains. Key(K key) // returns a Set of all keys in the map Set<K> key. Set() // removes any existing mapping for the given key remove(K key) 5

Maps • We will use two implementations of the Map interface: • Tree. Map: – provides O(log(n)) access to elements – stores keys in sorted order • Hash. Map: – provides O(1) access to elements – stores keys in unpredictable order • The Sorted. Map interface is also implemented by Tree. Map 6

Example: student. Grades • Using this, can solve our problem of grading by making a map: Map<String, Integer> student. Grades = new Hash. Map<String, Integer>(); • And storing the grades in it: Random r = new Random(); for (String name : students) { int grade = r. next. Int(100); student. Grades. put(name, grade); } 7

Example: student. Grades • How can we see the grades? • We can get a Set of all the keys – we don’t know anything about a Set – but it’s Iterable so we can use a foreach loop for (String name : student. Grades. key. Set() ) { System. out. println(name+" "+ student. Grades. get(name)); } 8

Example: word. Count • Let’s try a tougher problem now • Given some text file, we want to count how many times each word occurs // open the file Scanner console = new Scanner(System. in); System. out. print("What is the name of the text file? "); String file. Name = console. next. Line(); Scanner input = new Scanner(new File(file. Name)); 9

Example: word. Count • Make a Sorted. Map to hold the words and their counts: Sorted. Map<String, Integer> word. Counts = new Tree. Map<String, Integer>(); 10

Example: word. Count • Put the words into the map: Sorted. Map<String, Integer> word. Counts = new Tree. Map<String, Integer>(); while (input. has. Next()) { String next = input. next(). to. Lower. Case(); word. Counts. put(next, 1); } 11

Example: word. Count • Put the words into the map: Sorted. Map<String, Integer> word. Counts = new Tree. Map<String, Integer>(); while (input. has. Next()) { String next = input. next(). to. Lower. Case(); word. Counts. put(next, 1); But what if the word is already in the map? } This would always keep its count at 1. 12

Example: word. Count • Instead, we test whether it was there, and if so, increment it: while (input. has. Next()) { String next = input. next(). to. Lower. Case(); if (!word. Counts. contains. Key(next)) { word. Counts. put(next, 1); } else { word. Counts. put(next, word. Counts. get(next) + 1); } } Note that each key can only map to one value. When we put a key in multiple times, only the last value is recorded 13

Example: word. Count • We can also print out all the word counts: for (String word : word. Counts. key. Set()) { int count = word. Counts. get(word); System. out. println(count + "t" + word); } Note that the keys (the words) occur in sorted order because we are using a Sorted. Map. 14

Grammars 15

Languages and grammars • (formal) language: A set of words or symbols. • grammar: A description of a language that describes which sequences of symbols are allowed in that language. – describes language syntax (rules) but not semantics (meaning) – can be used to generate strings from a language, or to determine whether a given string belongs to a given language 16

Backus-Naur (BNF) • Backus-Naur Form (BNF): A syntax for describing language grammars in terms of transformation rules, of the form: <symbol> : : = <expression> | <expression>. . . | <expression> – terminal: A fundamental symbol of the language. – non-terminal: A high-level symbol describing language syntax, which can be transformed into other non-terminal or terminal symbol(s) based on the rules of the grammar. – developed by two Turing-award-winning computer scientists in 1960 to describe their new ALGOL programming language 17

An example BNF grammar <s> : : = <n> <v> <n> : : = Ruth | Morgan | Jordan | Alyssa <v> : : = cried | slept | belched • Nonterminals: – <s>, <n>, <v> – will not appear in an actual English sentence – can be transformed into one or more nonterminals or terminals • Terminals: – “Ruth”, “Alyssa”, and “slept” – they can appear in sentences – they only appear on the right side of rules 18

An example BNF grammar <s> : : = <n> <v> <n> : : = Ruth | Morgan | Jordan | Alyssa <v> : : = cried | slept | belched • Some sentences that could be generated from this grammar: Alyssa slept Morgan belched Ruth cried 19

BNF grammar version 2 <s> : : = <np> <v> <np> : : = <pn> | <dp> <n> <pn> : : = Ruth | Morgan | Jordan | Alyssa <dp> : : = a | the <n> : : = ball | hamster | carrot | computer <v> : : = cried | slept | belched • Some sentences that could be generated from this grammar: the carrot cried Ruth belched a computer slept 20

BNF grammar version 3 <s> : : = <np> <v> <np> : : = <pn> | <dp> <adj> <n> <pn> : : = Ruth | Morgan | Jordan | Alyssa <dp> : : = a | the <adj> : : = silly | invisible | loud | romantic <n> : : = ball | hamster | carrot | computer <v> : : = cried | slept | belched • Some sentences that could be generated from this grammar: the invisible carrot cried Alyssa cried a computer slept a romantic ball belched 21

Grammars & Recursion • We could just make an <adj> rule: <adj>: silly | invisible | loud | romantic • But we want to allow multiple adjectives: <adjp>: <adj>|<adj>…? ? ? OR we can use recursion to generate any number of adjectives: <adjp>: <adj>|<adj> <adjp> 22

Grammars and recursion <s> : : = <np> <v> <np> : : = <pn> | <dp> <adjp> <n> <pn> : : = Ruth | Morgan | Jordan | Alyssa <dp> : : = a | the <adjp> : : = <adj> | <adj> <adjp> <adj> : : = silly | invisible | loud | romantic <n> : : = ball | hamster | carrot | computer <v> : : = cried | slept | belched • Grammar rules can be defined recursively, so that the expansion of a symbol can contain that same symbol. – There must also be expressions that expand the symbol into something non-recursive, so that the recursion eventually ends. 23

Grammar, final version <s>: : =<np> <vp> <np>: : =<dp> <adjp> <n>|<pn> <dp>: : =the|a <adjp>: : =<adj>|<adj> <adjp> <adj>: : =big|fat|green|wonderful|faulty|subliminal <n>: : =dog|cat|man|university|father|mother|child <pn>: : =John|Jane|Sally|Spot|Fred|Elmo <vp>: : =<tv> <np>|<iv> <tv>: : =hit|honored|kissed|helped <iv>: : =died|collapsed|laughed|wept • Could this grammar generate the following sentences? Fred honored the green wonderful child big Jane wept the fat man fat • Generate a random sentence using this grammar. 24

Sentence generation <s> <np> <pn> <vp> <tv> <np> <dp> <adj> <n> <adjp> <adj> Fred honored the green wonderful child 25
- Slides: 25