Beruflich Dokumente
Kultur Dokumente
CONSTRUCTION
LECTURE 5
LEXICAL ANALYZER IMPLEMENTATION
Case Study
• Complete Lexical Specification
• C++ Code
Problem : Write Lexical specification and Develop Lexical Analyzer for MINI C++
Requirements :
1- Main Block
void main() { }
2- Simple Declaration Statement
( only int type)
int a;
3- Identifiers
- First is letter followed by more letters/digits
4- Operators only =
5- Punctuation only ; { } ( )
6- Delimiters ignored
7- Int-Literals
one or more digits
...........................................................................................................................................................................
Sample Program
void main() {
int a;
a=20;
}
...............................................................................................................................................................................
Lexical Specification
)
10
) Sp|new|tab Letter/Digit
( 9 *
letter other id/keyword
( 0 1 2
Digit Digit
}
} 8
{ ; =
other *
3
4
Int literal
7
{ 6 5
; =
C++ CODE
#include <iostream.h>
#include <fstream.h>
#include <string.h>
#include <cctype>
#include <ctype.h>
class LexicalAnalyzer{
private:
char fname[50];
ifstream sourceFile;
char buffer[500];
public :
LexicalAnalyzer(char *n){
strcpy(fname,n);
loadBuffer(); }
void loadBuffer() {
sourceFile.open(fname,ios::in);
char ch;
int i=0;
while(sourceFile.read((char*)&ch,sizeof(ch)))
buffer[i++]=ch;
buffer[i]='\0';
sourceFile.close();
}
void printBuffer(){
else if (ch=='{')
int i=0; state=7;
char ch; else if (ch=='}')
state=8;
while ((ch=buffer[i])!='\0'){ else if (ch==';')
state=6;
cout<<ch; else if (ch==')')
i++; state=10;
else if (ch=='(')
} state=9;
else if ((ch==32) ||(ch=='\t')||(ch=='\n'))
}
state=0;
void tokenize() { break;
case 1 : if ((isalpha(ch)) || (isdigit(ch)))
int state=0; state=1;
int i=0; else
state=2;
char ch; break;
char lexeme[50];
case 2 : lexeme[j]='\0';
int j=0;
cout<<"i="<<i<<" j = "<< j <<endl;
while ((ch=buffer[i])!='\0'){ --i; //unget
if (iskeyword(lexeme))
switch (state){
cout<<lexeme<<" keyword "<<endl;
case 0: if ( isalpha(ch)) else
cout<<lexeme<<" identifier "<<endl;
state=1; state=0;
else if (isdigit(ch))
j=0;
state=3;
}//end switch
else if (ch=='=')
state=5;
if ((ch!=32) &&(ch!='\t')&&(ch!='\n')){
lexeme[j]=ch;
j++;
} i++;
}//end while
}
bool iskeyword (char *lexeme){
char *keywords[]={"void","main","int"};
int i=0;
while ((i<3)&&(strcmp(lexeme,keywords[i])))
i++;
if (i<3)
return true;
else
return false;
}
};
void main(char *argv[], int argc){
char fname[60];
strcpy(fname,"c:\\temp\\a.cpp");
LexicalAnalyzer la(fname);
la.printBuffer();
la.tokenize();
}