Computer Science Engineering (CSE) Exam  >  Computer Science Engineering (CSE) Notes  >  Input Buffering - Lexical Analysis, Computer Science and IT Engineering

Input Buffering - Lexical Analysis, Computer Science and IT Engineering - Computer Science Engineering (CSE) PDF Download

INPUT BUFFERING

The lexical analyzer scans the characters of the source program one a t a time to discover tokens. Often, however, many characters beyond the next token many have to be examined before the next token itself can be determined. For this and other reasons, it is desirable for the lexical analyzer to read its input from an input buffer. Fig. 1.9 shows a buffer divided into two halves of, say 100 characters each. One pointer marks the beginning of the token being discovered. A look ahead pointer scans ahead of the beginning point, until the token is discovered .we view the position of each pointer as being between the character last read and the character next to be read. In practice each buffering scheme adopts one convention either a pointer is at the symbol last read or the symbol it is ready to read.

The distance which the lookahead pointer may have to travel past the actual token may be large. For example, in a PL/I program we may see:

DECLARE (ARG1, ARG2… ARG n)

Without knowing whether DECLARE is a keyword or an array name until we see the character that follows the right parenthesis. In either case, the token itself ends at the second E. If the look ahead pointer travels beyond the buffer half in which it began, the other half must be loaded with the next characters from the source file.

Since the buffer shown in above figure is of limited size there is an implied constraint on how much look ahead can be used before the next token is discovered. In the above example, if the look ahead traveled to the left half and all the way through the left half to the middle, we could not reload the right half, because we would lose characters that had not yet been grouped into tokens. While we can make the buffer larger if we chose or use another buffering scheme, we cannot ignore the fact that overhead is limited.

 

BUFFER PAIRS

  • A buffer is divided into two N-character halves, as shown below

Input Buffering - Lexical Analysis, Computer Science and IT Engineering - Computer Science Engineering (CSE)

  •  Each buffer is of the same size N, and N is usually the number of characters on one block. E.g., 1024 or 4096 bytes.
  •  Using one system read command we can read N characters into a buffer.
  •  If fewer than N characters remain in the input file, then a special character, represented by eof, marks the end of the source file.
  • Two pointers to the input are maintained:

1. Pointer lexeme_beginning, marks the beginning of the current lexeme, whose extent we are attempting to determine.

2. Pointer forward scans ahead until a pattern match is found.

Once the next lexeme is determined, forward is set to the character at its right end.

  •  The string of characters between the two pointers is the current lexeme.

After the lexeme is recorded as an attribute value of a token returned to the parser, lexeme_beginning is set to the character immediately after the lexeme just found.


Advancing forward pointer:

Advancing forward pointer requires that we first test whether we have reached the end of one of the buffers, and if so, we must reload the other buffer from the input, and move forward to the beginning of the newly loaded buffer. If the end of second buffer is reached, we must again reload the first buffer with input and the pointer wraps to the beginning of the buffer.

Code to advance forward pointer: 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 second half;

move forward to beginning of first half end

else forward := forward + 1;

 

Sentinels

  • For each character read, we make two tests: one for the end of the buffer, and one to determine what character is read. We can combine the buffer-end test with the test for the current character if we extend each buffer to hold a sentinel character at the end.
  • The sentinel is a special character that cannot be part of the source program, and a natural choice is the character eof.
  • The sentinel arrangement is as shown below:

: : E : : = : : M : * : eof C : * : : * : 2 : eof : : : eof

             lexeme_beginning forward
Fig. 1.10 Sentinels at end of each buffer half

 

Note that eof retains its use as a marker for the end of the entire input. Any eof that appears other than at the end of a buffer means that the input is at an end.

Code to advance forward pointer: 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 end of second half then begin reload first half;

move forward to beginning of first half end

else /* eof within a buffer signifying end of input */ terminate lexical analysis

end

The document Input Buffering - Lexical Analysis, Computer Science and IT Engineering - Computer Science Engineering (CSE) is a part of Computer Science Engineering (CSE) category.
All you need of Computer Science Engineering (CSE) at this link: Computer Science Engineering (CSE)

Top Courses for Computer Science Engineering (CSE)

FAQs on Input Buffering - Lexical Analysis, Computer Science and IT Engineering - Computer Science Engineering (CSE)

1. What is input buffering in lexical analysis?
Ans. Input buffering in lexical analysis refers to the process of reading and storing a chunk of input data in a buffer before it is analyzed and processed by the lexical analyzer. This helps in efficient handling of input data by reducing the number of I/O operations and improving the performance of the lexical analysis phase.
2. How does input buffering improve the performance of lexical analysis?
Ans. Input buffering improves the performance of lexical analysis by reducing the number of I/O operations. Instead of reading one character at a time, input buffering allows the lexical analyzer to read and store a chunk of input data in a buffer. This reduces the overhead of frequent I/O operations and improves the overall speed of the analysis process.
3. What is the role of input buffering in computer science and IT engineering?
Ans. Input buffering plays a crucial role in computer science and IT engineering by optimizing the processing of input data. It allows for efficient handling of large amounts of input data, reducing the overhead of frequent I/O operations and improving the performance of various analysis phases, such as lexical analysis. Input buffering is utilized in various applications, including compilers, interpreters, and text processing systems.
4. What are the advantages of using input buffering in lexical analysis?
Ans. The advantages of using input buffering in lexical analysis are as follows: - Reduced I/O overhead: Input buffering allows for the reading and storage of a chunk of input data, reducing the frequency of I/O operations and improving overall performance. - Faster analysis: By processing a chunk of input data at once, input buffering speeds up the lexical analysis phase. - Efficient memory usage: Input buffering helps optimize memory usage by storing a chunk of input data in a buffer, allowing for better utilization of available resources.
5. Are there any drawbacks or limitations of input buffering in lexical analysis?
Ans. While input buffering offers several advantages, there are also some drawbacks and limitations to consider. One limitation is the increased memory consumption due to the buffer size. If the buffer is too large, it may lead to unnecessary memory usage. Additionally, input buffering may introduce a slight delay in processing as the buffer needs to be filled before analysis can begin. However, these limitations can be mitigated by carefully selecting an appropriate buffer size and optimizing the buffering strategy.
Download as PDF
Explore Courses for Computer Science Engineering (CSE) exam

Top Courses for Computer Science Engineering (CSE)

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Computer Science and IT Engineering - Computer Science Engineering (CSE)

,

Sample Paper

,

mock tests for examination

,

Semester Notes

,

ppt

,

Previous Year Questions with Solutions

,

study material

,

shortcuts and tricks

,

Important questions

,

Input Buffering - Lexical Analysis

,

past year papers

,

Summary

,

Free

,

Input Buffering - Lexical Analysis

,

Exam

,

pdf

,

MCQs

,

Input Buffering - Lexical Analysis

,

video lectures

,

Extra Questions

,

Objective type Questions

,

Computer Science and IT Engineering - Computer Science Engineering (CSE)

,

Viva Questions

,

Computer Science and IT Engineering - Computer Science Engineering (CSE)

,

practice quizzes

;