Compiler Design (2170701)

BE | Semester-7   Winter-2018 | 15/11/2018

Q1) (b)

Write a brief note on input buffering techniques.

There are mainly two techniques for input buffering,

  • Buffer pair
  • Sentinels

1. Buffer pair:
  • The lexical analysis scans the input string from left to right one character at a time.
  • So, specialized buffering techniques have been developed to reduce the amount of overhead required to process a single input character.
  • We use a buffer divided into number of character on one disk block
  • We read N input character into each half of the buffer.
  • Two pointers to the input are maintained and string between two pointers is the current lexemes.
  • Pointer Lexeme Begin
  • Pointer Forward, scans ahead until a pattern
  • If forward pointer is at the end of first buffer half then second is filled with N input character.
  • If forward pointer is at the end of second buffer half then first is filled with N input character

code to advance forward pointer is given below :
if forward at end of first half then begin
  reload second half;
   forward := forward + 1
end
else if forward at end of second half then begin
  reload first half
  move forward to beginning of first half;
end
else forward := forward + 1;

2. Sentinels:
  • If we use the scheme of Buffer pairs we must check, each time we pointer that we have not moved off one of the buffers; if we do, then we must reload the other buffer. Thus, for each character read, we make two tests.
  • We can combine the buffer-end test with the test for the character if we extend each buffer to hold a sentinel character at the end.
Look ahead code with sentinels is given below :
forward := forward + 1
if forward = eof then begin
   if forward at end of first half then begin
      reload second half;
      forward := forward + 1;
end
else if forward at the second half then begin
      reload first half;
      move forward to beginning of first half;
end
else terminate lexical analysis;
end