Maps Grammars Based on slides by Marty Stepp & Alyssa Harding

Slides:



Advertisements
Similar presentations
CSci 143 Sets & Maps Adapted from Marty Stepp, University of Washington
Advertisements

Slide 1 Chapter 2-b Syntax, Semantics. Slide 2 Syntax, Semantics - Definition The syntax of a programming language is the form of its expressions, statements.
CSE 373 Data Structures and Algorithms Lecture 18: Hashing III.
CSE 143 Lecture 15 Sets and Maps reading: ; 13.2 slides created by Marty Stepp
CSE 143 Lecture 7 Sets and Maps reading: ; 13.2 slides created by Marty Stepp
Building Java Programs Chapter 11 Java Collections Framework.
1 Syntax and Semantics The Purpose of Syntax Problem of Describing Syntax Formal Methods of Describing Syntax Derivations and Parse Trees Sebesta Chapter.
CS324e - Elements of Graphics and Visualization Java Intro / Review.
Winter 2007SEG2101 Chapter 71 Chapter 7 Introduction to Languages and Compiler.
Syntax Specification and BNF © Allan C. Milne Abertay University v
The Java Collections Framework (Part 2) By the end of this lecture you should be able to: Use the HashMap class to store objects in a map; Create objects.
CSE 143 Lecture 11 Maps Grammars slides created by Alyssa Harding
CSS446 Spring 2014 Nan Wang.  Java Collection Framework ◦ Set ◦ Map 2.
ISBN Chapter 3 Describing Syntax and Semantics.
Building Java Programs Chapter 11 Lecture 11-1: Sets and Maps reading:
CSE 143 Lecture 14 (B) Maps and Grammars reading: 11.3 slides created by Marty Stepp
Building Java Programs Chapter 11 Java Collections Framework Copyright (c) Pearson All rights reserved.
CSE 143 Lecture 14 Maps/Sets; Grammars reading: slides adapted from Marty Stepp and Hélène Martin
Maps Nick Mouriski.
1 Maps, Stacks and Queues Maps Reading:  2 nd Ed: 20.4, 21.2, 21.7  3 rd Ed: 15.4, 16.2, 16.7 Additional references: Online Java Tutorial at
CSE 373: Data Structures and Algorithms Lecture 16: Hashing III 1.
CSE 143 Lecture 11: Sets and Maps reading:
CSE 143 Lecture 16 Iterators; Grammars reading: 11.1, 15.3, 16.5 slides created by Marty Stepp
Building Java Programs Chapter 11 Java Collections Framework Copyright (c) Pearson All rights reserved.
Chapter 3 – Describing Syntax CSCE 343. Syntax vs. Semantics Syntax: The form or structure of the expressions, statements, and program units. Semantics:
Introduction to python programming
Slides by Donald W. Smith
Building Java Programs
PROGRAMMING LANGUAGES
4. Java language basics: Function
CS170 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
Wednesday Notecards. Wednesday Notecards Wednesday Notecards.
Chapter 3 – Describing Syntax
Concepts of Programming Languages
Ch 16: Data Structures - Set and Map
slides created by Marty Stepp and Hélène Martin
Efficiency of in Binary Trees
What does it mean? Notes from Robert Sebesta Programming Languages
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
Syntax versus Semantics
CSE 3302 Programming Languages
Road Map CS Concepts Data Structures Java Language Java Collections
Week 6 Discussion Word Cloud.
Road Map - Quarter CS Concepts Data Structures Java Language
TCSS 342, Winter 2006 Lecture Notes
CSE 373: Data Structures and Algorithms
slides adapted from Marty Stepp
ADTs, Grammars, Parsing, Tree traversals
CSE 143 Lecture 2 Collections and ArrayIntList
Sets, Maps and Hash Tables
CSE 143 Lecture 14 More Recursive Programming; Grammars
"He's off the map!" - Eternal Sunshine of the Spotless Mind
Exercise Write a program that counts the number of unique words in a large text file (say, Moby Dick or the King James Bible). Store the words in a collection.
slides created by Marty Stepp and Alyssa Harding
CS2110: Software Development Methods
CSE 373 Java Collection Framework, Part 2: Priority Queue, Map
slides created by Marty Stepp
CSE 1020: The Collection Framework
slides created by Alyssa Harding
Plan for Lecture Review code and trace output
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
slides created by Marty Stepp
High-Level Programming Language
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
CSE 143 Lecture 15 Sets and Maps; Iterators reading: ; 13.2
slides adapted from Marty Stepp and Hélène Martin
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
Presentation transcript:

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

Example: studentGrades (Reminder – Our Midterm is Monday Nov 9th) 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: studentGrades We could keep another list of all the grades Student at index 0 has grade at index 0, and so on But that is tedious... 1 2 students Joe Sally Mike grades 32 87 51 3

Maps Solution: maps allow us to associate key/value pairs For example, a student name with a grade 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 student names grades Steve 32 Sally 51 Joe 87 Mike 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 containsKey(K key) // returns a Set of all keys in the map Set<K> keySet() // removes any existing mapping for the given key remove(K key) 5

Maps We will use two implementations of the Map interface: TreeMap: provides O(log(n)) access to elements stores keys in sorted order HashMap: provides O(1) access to elements stores keys in unpredictable order The SortedMap interface is also implemented by TreeMap 6

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

Example: studentGrades 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 : studentGrades.keySet() ) { System.out.println(name+" "+ studentGrades.get(name)); } 8

Example: wordCount 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 fileName = console.nextLine(); Scanner input = new Scanner(new File(fileName));

Example: wordCount Make a SortedMap to hold the words and their counts: SortedMap<String, Integer> wordCounts = new TreeMap<String, Integer>();

Example: wordCount Put the words into the map: SortedMap<String, Integer> wordCounts = new TreeMap<String, Integer>(); while (input.hasNext()) { String next = input.next().toLowerCase(); wordCounts.put(next, 1); }

Example: wordCount Put the words into the map: SortedMap<String, Integer> wordCounts = new TreeMap<String, Integer>(); while (input.hasNext()) { String next = input.next().toLowerCase(); wordCounts.put(next, 1); } But what if the word is already in the map? This would always keep its count at 1. 12

Example: wordCount Instead, we test whether it was there, and if so, increment it: while (input.hasNext()) { String next = input.next().toLowerCase(); if (!wordCounts.containsKey(next)) { wordCounts.put(next, 1); } else { wordCounts.put(next, wordCounts.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

Example: wordCount We can also print out all the word counts: for (String word : wordCounts.keySet()) { int count = wordCounts.get(word); System.out.println(count + "\t" + word); } Note that the keys (the words) occur in sorted order because we are using a SortedMap.

Grammars

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

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

An example BNF grammar Nonterminals: Terminals: <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

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

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

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

Grammars & Recursion We could just make an <adj> rule: <adj>: silly | invisible | loud | romantic But we want to allow multiple adjectives: <adjp>:<adj>|<adj> <adj>|<adj> <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.

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.

Sentence generation <s> <np> <vp> <pn> Fred <tv> honored <dp> <adjp> <n> the <adj> child green wonderful “Parse Tree”