Ada.Text_IO.Look_Ahead(c, EOL)
Overview
- Input Marker
- Get Routines and the Input Marker
- LookAhead vs Get
- Why LookAhead
- LookAhead and End of Line
- LookAhead and End of File
- How to Use LookAhead
- Look_Ahead Behavior
- Look_Ahead Examples
- Other Languages: Java, C, C++
Input Marker
- Indicates the next input character that will be read by a get routine
- We think of it as being between characters in the input
Get Routines and the Input Marker
-- Input: 10 20 30
x, y, z: Integer;
begin
get(x);
get(y);
get(z);
Example 2
-- Input: Help
a, b, c: Character;
begin
get(a);
get(b);
get(c);
Each call to get does two things:
- Assigns a value to the actual parameter
- Advances the input marker to immediately following the last character read
Ada.Text_io.LookAhead
-- Input: Help
EOL: Boolean;
c: Character;
-- Caution: This example ignores EOL, which is discussed later
begin
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'
lookAhead(c, EOL) assigns the character following the input marker to c (like get(c))
lookAhead(c, EOL) does NOT advance the input marker (unlike get(c))
Motivation for LookAhead
- Allows choosing the next get routine to call, based on the current input
-- Caution: This example ignores EOL, which is discussed later
-- Examine next character, without moving input marker
LookAhead(c, EOL);
-- Use get to input the next value and advance input marker
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;
Another motivation:
- allows current input character to be examined but left in the input
to be processed by some other code
- Example:
-- Caution: This example ignores EOL, which is discussed later
loop
LookAhead(c, EOL);
exit when is_Something(c);
-- do something with c
get(c);
end loop;
return something;
-- Now calling routine can handle next value of c
Example: compiling 123+456 allows + to signify that the end of the number
is reached, and the the plus to be recognized later in another section of code
We will see some examples of this later
End of Line, and Get
- Question: What does get do at the end of line?
- Answer: Skips it
- Example 1
-- Input Line 1: 10
-- Input Line 2: 20
-- Input Line 3: 30
x, y, z: Integer;
begin
get(x);
get(y);
get(z);
-- Input Line 1: D
-- Input Line 2: O
-- Input Line 3: G
a, b, c: Character;
begin
get(a);
get(b);
get(c);
End of Line, and LookAhead
- Question: What does LookAhead(c, EOL) do at end of line:
- Answer:
- It returns true in EOL
- The value in c is unpredictable
- The input marker is unchanged (where is it?)
- Remember: LookAhead NEVER advances the input marker
End of FILE, and LookAhead
- Question: What does LookAhead(c, EOL) do at end of FILE:
- Answer: Exactly the same as at EOL!
- Answer:
- It returns true in EOL
- The value in c is unpredictable
- The input marker is unchanged (where is it?)
- Remember: LookAhead NEVER advances the input marker
How to Use LookAhead
- EOL MUST be checked before c is used
- Example:
EOL: Boolean;
c: Character;
begin
loop
LookAhead(c, EOL);
if EOL then -- Input marker is at end of line
Ada.text_IO.Skip_line; -- Advance input marker
else
if is_digit(c) then
-- do a numeric get
else
-- do a character get
end if;
end if;
end loop;
Procedure Look_Ahead Signature
Procedure Look_Ahead Behavior
- If look_Ahead is called when the input marker is
at the end of the line, EOL will return true and C will not be valid
- If look_Ahead is called when the input marker is NOT at the end of the
line, EOL will return false and C will contain the next
value of input (ie the character that will be returned by the next get of a
character), without moving the input marker
- Repeated calls to look_Ahead without intervening calls to get (or
other input routines) return the same values
LookAhead and End of File
- What happens if a call to get finds EOF?
- What happens if a call to LookAhead finds EOF?
LookAhead and End of File
- What happens if a call to get finds EOF?
- What happens if a call to
LookAhead(c, EOL)
finds EOF?
- EOL is set ot true
- No error occurs
Procedure Look_Ahead Examples
Skipping White Space
- What would the code to skip white space look like?
Other Languages: 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.
Other Languages: C++
- C++ has unget and putback
Other Languages: C