Professional Documents
Culture Documents
Assignment by GROUP 9
Name:
Aitdaal Anwar
Roll no:
2201
Semester:
7th (Morning)
Topic:
Patterns & Lexical Errors
[1]
GOVT. Postgraduate College of Science
Patterns
Patterns:
There is a group of strings in
the input for which same token can be
produced
This group is described by a rule called
pattern
Lexemes:
A lexeme is a sequence of
characters in the source program that is matched
by the pattern for a token.
For example:
const float pi=3.1416;
pi is lexeme for the token “identifier”.
[2]
GOVT. Postgraduate College of Science
Lexical Errors
Lexical Errors:
Only few errors can be
detected at lexical analysis phase. For e.g.,
Suppose a string encountered:
fi (a==5)
Lexical analyzer cannot tell that “fi” is a misspelled
keyword or undeclared identifier.
Lexical analyzer must return the token for identifier.
This happens where variable declaration is not
mandatory.
Sometimes lexical analyzer is unable to proceed then
the Simplest technique is “panic mode” recovery. It
suggests the solution for the error.
[3]
GOVT. Postgraduate College of Science
Introduction
Name:
Zain Wajid
Roll no:
2220
Semester:
7th (Morning)
Topic:
Lexical Analyzer Working
[4]
GOVT. Postgraduate College of Science
How it Works
[6]
GOVT. Postgraduate College of Science
How it Works
(Source code)
(Token Stream)
‘if’ and other keywords are
are written in separate blocks
Ad-hoc Lexer:
It is a Handwritten code to generate token.
It partitions the input string by reading left to right and
recognize one token at time.A “Lookahead” variable is
require to decide where one token end and next token
start.
[6]
GOVT. Postgraduate College of Science
Introduction
Name:
Muhammad Maaz
Roll no:
2240
Semester:
7th (Morning)
Topic:
Ad-Hoc Lexer Program
[7]
GOVT. Postgraduate College of Science
Ad-Hoc Lexer Program
Ad-hoc Lexer:
class Lexer
{
Inputstream s;
char next; //look ahead
Lexer(Inputstream _s){
s=_s;
next=s.read( );
}
Token nextToken(){
if(idChar(next))
return readId();
if(number(next))
return readNumber();
if(next==‘ ” ’)
return readString();
…..
……
[9]
GOVT. Postgraduate College of Science
Ad-Hoc Lexer Program
Token readId( ){
string id=“ “;
while(true){
char c=input.read( );
if(idChar ( c ) ==false)
return
new Token{TID,id};
id=id + string( c );
}
}
boolean idChar(char c){
if( isAlpha ( c) )
return true;
if( isDigit( c ) )
return true;
if( c== ‘ _ ’)
return true;
else
return false; }
[9]
GOVT. Postgraduate College of Science
Introduction
Name:
Muhammad Raza Ijaz
Roll no:
2232
Semester:
7th (Morning)
Topic:
Ad-Hoc Lexer Problems
[10]
GOVT. Postgraduate College of Science
Ad-Hoc Lexer Problems
Problems:
1. We do not know what kind of token we are going to read
from seeing the first character
[11]
GOVT. Postgraduate College of Science
Ad-Hoc Lexer Problems
[12]
GOVT. Postgraduate College of Science