itec 320 lecture 11 application part deux look ahead

14
ITEC 320 Lecture 11 Application Part Deux Look Ahead

Upload: george-sharp

Post on 14-Dec-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: ITEC 320 Lecture 11 Application Part Deux Look Ahead

ITEC 320

Lecture 11Application Part Deux

Look Ahead

Page 2: ITEC 320 Lecture 11 Application Part Deux Look Ahead

Applications / Look ahead

Review

• In-class coding• What did you learn?

Page 3: ITEC 320 Lecture 11 Application Part Deux Look Ahead

Applications / Look ahead

Outline

• Look ahead

Page 4: ITEC 320 Lecture 11 Application Part Deux Look Ahead

Applications / Look ahead

Input

• What are the problems with ADA input?

• What would need to be added to solve this?

Page 5: ITEC 320 Lecture 11 Application Part Deux Look Ahead

Applications / Look ahead

Example

EOL: Boolean; c: Character; -- Caution: This example ignores EOLbegin

lookAhead(c, EOL); put(c); -- Output is 'H' lookAhead(c, EOL); put(c); -- Output is 'H' get(c); lookAhead(c, EOL); put(c); -- Output is 'e’

end example;

Assumes input Hello

Page 6: ITEC 320 Lecture 11 Application Part Deux Look Ahead

Applications / Look ahead

Motivation

• Allows you to figure out how to read next bit of input

lookAhead(c, EOL); -- Use get to input if Ada.Characters.Handling.Is_Digit(c) then

Ada.Integer.Text_io.get(n); -- Get an integer else

Ada.Text_io.get(c); -- Get a character end if;

Page 7: ITEC 320 Lecture 11 Application Part Deux Look Ahead

Applications / Look ahead

Motivation

• Allows multiple portions of code to access the same input

• Loop conditionals

loop LookAhead(c, EOL); exit when is_Something(c); -- do something with c get(c);

end loop;

For example, when reading 123+456This can be used to help a compilerHow else could this be accomplished?

Page 8: ITEC 320 Lecture 11 Application Part Deux Look Ahead

Applications / Look ahead

Issue

• What happens when you hit the end of line?

• Value returned depends on which way the wind blows

EOL: Boolean; c: Character; begin

loop lookAhead(c, EOL); if EOL then – End of line

Ada.text_IO.Skip_line; else

if is_digit(c) then -- do a numeric get

else -- do a character get

end if; end if; end loop;

Page 9: ITEC 320 Lecture 11 Application Part Deux Look Ahead

Applications / Look ahead

Review

• EOL false– Doesn’t advance input stream–Will return same value until some form

of get is called

• EOL true–What it returns doesn’t matter

Page 10: ITEC 320 Lecture 11 Application Part Deux Look Ahead

Applications / Look ahead

Longer examples

• Web examples

Page 11: ITEC 320 Lecture 11 Application Part Deux Look Ahead

Applications / Look ahead

White space

• What is white space?• Code to skip white space?

Page 12: ITEC 320 Lecture 11 Application Part Deux Look Ahead

Applications / Look ahead

Others

• Java– java.io.PushbackInputStream(InputStream) - constructs

a stream that can be used to push back one byte – java.io.PushbackInputStream(InputStream, int size) -

constructs a stream that can be used to push back size bytes

– void unread(int b) pushes back the byte b which will be input again by the next read. Only one byte can be pushed back at a time.

• C++– unget and putback

• C– ungetc

Page 13: ITEC 320 Lecture 11 Application Part Deux Look Ahead

Applications / Look ahead

Example

• Given a string, compute recursively (no loops) the number of times lowercase "hi" appears in the string.

Page 14: ITEC 320 Lecture 11 Application Part Deux Look Ahead

Applications / Look ahead

Summary

• Application coding• Other languages