ISBN 0-321-19362-8 Chapter 6 Data Types Character Strings Pattern Matching.

Slides:



Advertisements
Similar presentations
Chapter 25 Perl and CGI (Common Gateway Interface)
Advertisements

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Introduction to Ruby.
JavaScript I. JavaScript is an object oriented programming language used to add interactivity to web pages. Different from Java, even though bears some.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Strings.
Programming Languages and Paradigms
 2003 Prentice Hall, Inc. All rights reserved Fundamentals of Characters and Strings Character constant –Integer value represented as character.
Strings.
Current Assignments Homework 5 will be available tomorrow and is due on Sunday. Arrays and Pointers Project 2 due tonight by midnight. Exam 2 on Monday.
Names and Bindings.
Chapter 6 Structured Data Types Arrays Records. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Definitions data type –collection of data objects.
ISBN Regular expressions Mastering Regular Expressions by Jeffrey E. F. Friedl –(on reserve.
Chapter 9 Subprograms Sections 1-5 and 9. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Introduction Two fundamental abstraction facilities.
CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.
ISBN Lecture 06 Data Types. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-2 Lecture 06 Topics Introduction Primitive Data.
COS 381 Day 19. Agenda  Assignment 5 Posted Due April 7  Exam 3 which was originally scheduled for Apr 4 is going to on April 13 XML & Perl (Chap 8-10)
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
ISBN Chapter 6 Data Types: Structured types.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes Names Variables The Concept of Binding Type Checking Strong Typing Type Compatibility.
Regular expressions Mastering Regular Expressions by Jeffrey E. F. Friedl Linux editors and commands (e.g.
Primitive Data Types: Numbers Strings Ordinal Types Pointers
ISBN Chapter 6 Data Types. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-2 Figure 6.1 IEEE floating-point formats: (a)
JavaScript, Third Edition
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.3 The Class String.
Chapter 6 Structured Data Types Arrays Records. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Definitions data type –collection of data objects.
CS 355 – Programming Languages
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
ISBN 0-321— Chapter 6 sections 1-4, 9 Primitive Data Types Numbers Strings Ordinal Types Pointers.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
2440: 211 Interactive Web Programming Expressions & Operators.
CMSC 202 Arrays. Aug 6, Introduction to Arrays An array is a data structure used to process a collection of data that is all of the same type –An.
ISBN Chapter 5 Names, Bindings, and Scopes.
ISBN Chapter 5 Names, Bindings, and Scopes.
 Character set is a set of valid characters that a language can recognise.  A character represents any letter, digit or any other sign  Java uses the.
Expressions and Assignment Statements
Overview A regular expression defines a search pattern for strings. Regular expressions can be used to search, edit and manipulate text. The pattern defined.
Working with Forms and Regular Expressions Validating a Web Form with JavaScript.
When you read a sentence, your mind breaks it into tokens—individual words and punctuation marks that convey meaning. Compilers also perform tokenization.
Introduction to Java Java Translation Program Structure
ISBN Chapter 6 Data Types Introduction Primitive Data Types User-Defined Ordinal Types.
ISBN Chapter 6 Data Types. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-2 Chapter 6 Topics Introduction Primitive Data.
CS 330 Programming Languages 10 / 02 / 2007 Instructor: Michael Eckmann.
Names, Bindings, and Scope Session 3 Course : T Programming Language Concept Year : February 2011.
1 CS Programming Languages Class 08 September 19, 2000.
ISBN Chapter 6 Structured Data Types Array Types Associative Arrays Record Types Union Types.
Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1.
ISBN Chapter 6 Data Types. Copyright © 2006 Addison-Wesley. All rights reserved. 6-2 Chapter 6 Topics Introduction Primitive Data Types.
1 Data Types Primitive Data Types Character String Types User-Defined Ordinal Types Array Types Associative Arrays Record Types Union Types Pointer Types.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Fluency with Information Technology Third Edition by Lawrence Snyder Chapter.
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
Chapter 6 © 2002 by Addison Wesley Longman, Inc Introduction - Evolution of Data Types: FORTRAN I (1957) - INTEGER, REAL, arrays … Ada (1983) -
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
ISBN Chapter 6 Data Types. Copyright © 2006 Addison-Wesley. All rights reserved.2 Primitive Data Types Almost all programming languages.
CS 330 Programming Languages 09 / 30 / 2008 Instructor: Michael Eckmann.
Chapter 8 Statement-Level Control Structures. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 8 Topics Introduction Selection Statements.
Structure of Programming Language Data Types. 2 A data type defines a collection of data objects and a set of predefined operations on those objects An.
Chapter 6 – Data Types CSCE 343.
Type Checking Generalizes the concept of operands and operators to include subprograms and assignments Type checking is the activity of ensuring that the.
6.1 Introduction 6.2 Primitive Data Types - Evolution of Data Types:
Strings, Characters and Regular Expressions
Chapter 6: Data Types Lectures # 10.
Instructor : Ahmed Alalawi Slides from Chung –Ta King
Concepts of Programming Languages
Complex Data Types One very important measure of the “goodness” of a PL is the capability of its data types to model the problem space variables Design.
CMSC 202 Java Primer 2.
Introduction to Primitive Data types
Names, Bindings, and Scopes
Introduction to Primitive Data types
Presentation transcript:

ISBN Chapter 6 Data Types Character Strings Pattern Matching

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-2 Character String Types Values are sequences of characters Design issues: 1.Is it a primitive type or just a special kind of array? 2.How is it stored in memory? 3.Is the length of a string variable static or dynamic?

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-3 Character String Operations Assignment Comparison (==, >, etc.) Catenation –Sometimes an operator is provided (+ in Java,. in perl) –Some languages have a repetition operator (x in perl, * in python and ruby) Substring reference Pattern matching

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-4 Actual String Implementations Pascal –Not primitive; assignment and comparison only (of packed arrays) Ada, FORTRAN 90, and BASIC –Somewhat primitive –Assignment, comparison, catenation, substring reference –FORTRAN has an intrinsic for pattern matching –Ada code N := N1 & N2 (catenation) N(2..4) (substring reference)

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-5 Character String Implementations C and C++ –Not primitive - implemented as arrays of characters terminated by null character –Use char arrays and a library of functions that provide operations SNOBOL4 (a string manipulation language) –Primitive –Many operations, including elaborate pattern matching

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-6 Character String Implementations Java - String class (not arrays of char ) –Objects cannot be changed (immutable) –StringBuffer is a class for changeable string objects Javascript, Ruby – strings are objects with many operations Perl, PHP – strings are primitive

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-7 String Length Options 1. Static - FORTRAN 77, Ada, COBOL e.g. (FORTRAN 90) CHARACTER (LEN = 15) NAME; 2. Limited Dynamic Length - C and C++ actual length is indicated by a null character 3. Dynamic - SNOBOL4, Perl, JavaScript

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-8 Evaluation of String Types Aid to writability As a primitive type with static length, they are inexpensive to provide--why not have them? Dynamic length is nice, but is it worth the expense?

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-9 Character String Implementation Static length - compile-time descriptor Limited dynamic length - may need a run-time descriptor for length (but not in C and C++) Dynamic length - need run-time descriptor; allocation/deallocation is the biggest implementation problem

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-10 Character String Descriptors Compile-time descriptor for static strings Run-time descriptor for limited dynamic strings

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-11 Character String Interpolation Two literal representations of strings in many scripting languages –Single quoted strings are literals Every character inside is stored as written. (In some languages, a few characters may be treated specially.) These are like the double quoted strings in Java –Double quoted strings are interpolated Special characters have their regular meaning unless they have a backslash in front of them. Variable names are expanded, replaced by the value of the variable.

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-12 Pattern Matching A useful operation for strings Usually based on regular expressions Some languages have pattern matching built into the language (perl, python, ruby, …) Some languages implement pattern matching via external libraries or classes –Java has Pattern and Matcher classes

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-13 Recursive definition of a regular expressions Individual terminals are regular expressions If a and b are regular expressions so are –a | bchoice –absequence –(a)grouping –a*zero or more repetitions Nothing else is a regular expression

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-14 Examples Identifiers –letter(letter | digit)* Binary strings –(0 | 1)(0 | 1)* Binary strings divisible by 2 –(0 | 1)*0

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-15 Regular Expressions and Pattern Matching in Perl The operators =~ and !~ check for match and no match respectively. A pattern is enclosed between slashes as in /pattern/ If a pattern appears by itself, the variable $_ is checked for a match ^ at the beginning of the pattern means it must start at the beginning of the string $ at the end of a pattern means it must end at the end of the string

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-16 Example The following Perl script checks for the regular expression a+b+ #!/usr/bin/perl # This is a Perl script $_ = ; # Read into $_ if (/^a+b+$/) # Match $_ with a+b+ { print "yes\n";} else { print "no\n"; }

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-17 Pattern symbols Case insensitive\i Grouping() Choice| Between i and j occurrences{i, j} None of enclosed characters[^abc] One of enclosed characters[abc] 0 or 1 occurrences of previous character? 1 or more occurrences+ 0 or more occurrences* Any single character (except '\n'). MeaningSymbol

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-18 Character Classes There are several classes of characters that have special names \S \W \D Exclude Any whitespace\s Any letter, digit, or underscore \w Any digit\d Match

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-19 Anchors Used to specify position within a string \bpattern\b matches the word pattern but not patterned Not at word boundary\B Word boundary\b End of string$ Beginning of string^ PositionSymbol

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-20 String Manipulation split( /pattern/, string) splits a string into tokens using the pattern as the delimiter split( /, /, $fullName) tr/a..z/A..Z/ transliterates characters s/pattern/replacement/ replaces one occurrences of pattern in a string with replacement. A g after the last slash replaces all occurrences. –Use this same syntax for substitution in the vi editor

Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-21 Pattern Memory Any part of a pattern that has parentheses around it will cause the matching text to be stored in pattern memory Within a pattern, you can use \1, \2, \3 to refer back to an earlier part of the pattern. This is called a back reference. After a match has completed, the variables $1, $2, … $9 contain the pieces from the last match.