The lexical analyzer scans the input from left to right one character at a time . it uses two pointers begin ptr(bp) and forward to keep track of the pointer of the input scanned.
Initially both the pointers point to the first character of the input string as shown below,
The forward ptr moves ahead to search for end of lexeme. As soon as the blank space is encountered, it indicates end of lexeme. In above example as soon as ptr (fp) encounters a blank space the lexeme “int” is identified.
The fp will be moved ahead at white space, when fp encounters white space, it ignore and moves ahead. then both the begin ptr(bp) and forward ptr(fp) are set at next token.
The input character is thus read from secondary storage, but reading in this way from secondary storage is costly. hence buffering technique is used.A block of data is first read into a buffer, and then second by lexical analyzer. there are two methods used in this context: One Buffer Scheme, and Two Buffer Scheme. These are explained as following below.
- One Buffer Scheme:
In this scheme, only one buffer is used to store the input string.but the problem with this scheme is that if lexeme is very long then it crosses the buffer boundary, to scan rest of the lexeme the buffer has to be refilled, that makes overwriting the first of lexeme.
- Two Buffer Scheme:
To overcome the problem of one buffer scheme, in this method two buffers are used to store the the input string. the first buffer and second buffer are scanned alternately. when end of current buffer is reached the other buffer is filled. the only problem with this method is that if length of the lexeme is longer than length of the buffer then scanning input cannot be scanned completely.
Initially both the bp and fp are pointing to the first character of first buffer. Then the fp moves towards right in search of end of lexeme. as soon as blank character is recognized, the string between bp and fp is identified as corresponding token. to identify, the boundary of first buffer end of buffer character should be placed at the end first buffer.
Similarly end of second buffer is also recognized by the end of buffer mark present at the end of second buffer. when fp encounters first eof, then one can recognize end of first buffer and hence filling up second buffer is started. in the same way when second eof is obtained then it indicates of second buffer. alternatively both the buffers can be filled up until end of the input program and stream of tokens is identified. This eof character introduced at the end is calling Sentinel which is used to identify the end of buffer.
- Compiler Theory | Set 1
- Compiler Theory | Set 2
- Compiler Design | Lexical Analysis
- Compiler Design | Introduction to Syntax Analysis
- Compiler Design | Why FIRST and FOLLOW?
- Compiler Design | FIRST Set in Syntax Analysis
- Compiler Design | FOLLOW Set in Syntax Analysis
- Compiler Design | Ambiguous Grammar
- Compiler Design | Runtime Environments
- Compiler Design | Syntax Directed Translation
- Compiler Design | Intermediate Code Generation
- Compiler Design | Peephole Optimization
- Language Processors: Assembler, Compiler and Interpreter
- Compiler Design | Code Optimization
- Compiler Design | Introduction of Object Code
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.