You are on page 1of 14

Ad - Hoc Lexer

Assalam o Alaikum!

I am Mohammad Aakash
(2223)

2
What is Ad hoc?

▸ In English, The Meaning of Ad hoc is


“For this”
▸ Ad hoc refers to something that has
been created for a specific purpose or
goal ,often existing only until that goal
has been achieved.

3
What is Lexer?

▸ A program that perform lexical analysis


is known as Lexer.

4
What is Lexical Analysis?
“ “The process of converting
sequence of characters
into sequence of tokens ”
https://en.wikipedia.org/w
iki/Lexical

6
Ad - hoc Lexer

- Source Code to - Partition the - Lookahead is


generate input string by required to
tokens. string by decide where
reading left - to one token ends
- right , and next token
recognizing one begins.
token at time .
7
What is InputStream?

- Inputstream class is the Superclass(Base


Class) of all the io(input output) classes
- There are the many methods of the Inputstream
like (read , close , skip , etc.)
- read() reads the characters

8
class Lexer
{
Inputstream s;
char next; //Lookahead
Lexer(Inputstream _s){
s=_s;
next=s.read( );
} 9
Token nextToken(){
if(idChar(next))
return readId();
if(number(next))
return readNumber();
if(next==‘ ” ’)
return readString();
…...
10
Token readId( ){
string id=“ “;
while(true){
char c=input.read( );
if(idChar ( c ) ==false)
return
new Token{TID,id};
id=id + string( c );
}
}

11
boolean idChar(char c){
if( isAlpha ( c) )
return true;
if( isDigit( c ) )
return true;
if( c== ‘ _ ’)
return true;

return false;
}
12
Token readNumber(){
string num = “”;
while(true){
next = input.read();
if( !isNumber(next))
return
new Token(TNUM,num);
num = num+string(next);
}
}
13
THANKS!
Any questions?

14

You might also like