Add to Favorites    Make Home Page 9946 Online  
 Language Categories  
 Our Services  

Home » C Home » Device Driver & Compiler Design Home » Lexical Analysis

A D V E R T I S E M E N T

Search Projects & Source Codes:

Title Lexical Analysis
Author Darshana Kapadia
Author Email darshana_kapadia [at] yahoo.com
Description Using case switches its more easier to perform Lexical Analysis Program.
Category C » Device Driver & Compiler Design
Hits 48023
Code Select and Copy the Code
/* Program on lexical analysis */ #include<stdio.h> #include<conio.h> #define MAX 30 void main() { char str[MAX]; int state=0; int i=0, j, startid=0, endid, startcon, endcon; clrscr(); for(j=0; j<MAX; j++) str[j]=NULL; //Initialise NULL printf("*** Program on Lexical Analysis ***"); printf(" Enter the string: "); gets(str); //Accept input string str[strlen(str)]=' '; printf(" Analysis: "); while(str[i]!=NULL) { while(str[i]==' ') //To eliminate spaces i++; switch(state) { case 0: if(str[i]=='i') state=1; //if else if(str[i]=='w') state=3; //while else if(str[i]=='d') state=8; //do else if(str[i]=='e') state=10; //else else if(str[i]=='f') state=14; //for else if(isalpha(str[i]) || str[i]=='_') { state=17; startid=i; } //identifiers else if(str[i]=='<') state=19; //relational '<' or '<=' else if(str[i]=='>') state=21; //relational '>' or '>=' else if(str[i]=='=') state=23; //relational '==' or assignment '=' else if(isdigit(str[i])) { state=25; startcon=i; } //constant else if(str[i]=='(') state=26; //special characters '(' else if(str[i]==')') state=27; //special characters ')' else if(str[i]==';') state=28; //special characters ';' else if(str[i]=='+') state=29; //operator '+' else if(str[i]=='-') state=30; //operator '-' break; //States for 'if' case 1: if(str[i]=='f') state=2; else { state=17; startid=i-1; i--; } break; case 2: if(str[i]=='(' || str[i]==NULL) { printf(" if : Keyword"); state=0; i--; } else { state=17; startid=i-2; i--; } break; //States for 'while' case 3: if(str[i]=='h') state=4; else { state=17; startid=i-1; i--; } break; case 4: if(str[i]=='i') state=5; else { state=17; startid=i-2; i--; } break; case 5: if(str[i]=='l') state=6; else { state=17; startid=i-3; i--; } break; case 6: if(str[i]=='e') state=7; else { state=17; startid=i-4; i--; } break; case 7: if(str[i]=='(' || str[i]==NULL) { printf(" while : Keyword"); state=0; i--; } else { state=17; startid=i-5; i--; } break; //States for 'do' case 8: if(str[i]=='o') state=9; else { state=17; startid=i-1; i--; } break; case 9: if(str[i]=='{' || str[i]==' ' || str[i]==NULL || str[i]=='(') { printf(" do : Keyword"); state=0; i--; } break; //States for 'else' case 10: if(str[i]=='l') state=11; else { state=17; startid=i-1; i--; } break; case 11: if(str[i]=='s') state=12; else { state=17; startid=i-2; i--; } break; case 12: if(str[i]=='e') state=13; else { state=17; startid=i-3; i--; } break; case 13: if(str[i]=='{' || str[i]==NULL) { printf(" else : Keyword"); state=0; i--; } else { state=17; startid=i-4; i--; } break; //States for 'for' case 14: if(str[i]=='o') state=15; else { state=17; startid=i-1; i--; } break; case 15: if(str[i]=='r') state=16; else { state=17; startid=i-2; i--; } break; case 16: if(str[i]=='(' || str[i]==NULL) { printf(" for : Keyword"); state=0; i--; } else { state=17; startid=i-3; i--; } break; //States for identifiers case 17: if(isalnum(str[i]) || str[i]=='_') { state=18; i++; } else if(str[i]==NULL||str[i]=='<'||str[i]=='>'||str[i]=='('||str[i]==')'||str[i]==';'||str[i]=='='||str[i]=='+'||str[i]=='-') state=18; i--; break; case 18: if(str[i]==NULL || str[i]=='<' || str[i]=='>' || str[i]=='(' || str[i]==')' || str[i]==';' || str[i]=='=' || str[i]=='+' ||str[i]=='-') { endid=i-1; printf(" "); for(j=startid; j<=endid; j++) printf("%c", str[j]); printf(" : Identifier"); state=0; i--; } break; //States for relational operator '<' & '<=' case 19: if(str[i]=='=') state=20; else if(isalnum(str[i]) || str[i]=='_') { printf(" < : Relational operator"); i--; state=0; } break; case 20: if(isalnum(str[i]) || str[i]=='_') { printf(" <= : Relational operator"); i--; state=0; } break; //States for relational operator '>' & '>=' case 21: if(str[i]=='=') state=22; else if(isalnum(str[i]) || str[i]=='_') { printf(" > : Relational operator"); i--; state=0; } break; case 22: if(isalnum(str[i]) || str[i]=='_') { printf(" >= : Relational operator"); i--; state=0; } break; //States for relational operator '==' & assignment operator '=' case 23: if(str[i]=='=') state=24; else { printf(" = : Assignment operator"); i--; state=0; } break; case 24: if(isalnum(str[i])) { printf(" == : Relational operator"); state=0; i--; } break; //States for constants case 25: if(isalpha(str[i])) { printf(" *** ERROR *** "); puts(str); for(j=0; j<i; j++) printf(" "); printf("^"); printf(" Error at position %d Alphabet cannot follow digit", i); state=99; } else if(str[i]=='(' || str[i]==')' || str[i]=='<' || str[i]=='>' || str[i]==NULL || str[i]==';' || str[i]=='=') { endcon=i-1; printf(" "); for(j=startcon; j<=endcon; j++) printf("%c", str[j]); printf(" : Constant"); state=0; i--; } break; //State for special character '(' case 26: printf(" ( : Special character"); startid=i; state=0; i--; break; //State for special character ')' case 27: printf(" ) : Special character"); state=0; i--; break; //State for special character ';' case 28: printf(" ; : Special character"); state=0; i--; break; //State for operator '+' case 29: printf(" + : Operator"); state=0; i--; break; //State for operator '-' case 30: printf(" + : Operator"); state=0; i--; break; //Error State case 99: goto END; } i++; } printf(" End of program"); END: getch(); } /* Output Correct input ------------- *** Program on Lexical Analysis *** Enter the string: for(x1=0; x1<=10; x1++); Analysis: for : Keyword ( : Special character x1 : Identifier = : Assignment operator 0 : Constant ; : Special character x1 : Identifier <= : Relational operator 10 : Constant ; : Special character x1 : Identifier + : Operator + : Operator ) : Special character ; : Special character End of program Wrong input ----------- *** Program on Lexical Analysis *** Enter the string: for(x1=0; x1<=19x; x++); Analysis: for : Keyword ( : Special character x1 : Identifier = : Assignment operator 0 : Constant ; : Special character x1 : Identifier <= : Relational operator Token cannot be generated */

Related Source Codes

Script Name Author
The Game Opposite as seen on Nokia 2300 Mobile Manikanta
RECURSIVE BALANCED QUICK SORT ashish
Radix Sort ashish
Change your mouse pointer Ashim
The blinking star Shashank
Data Validation Crylittlebaby
To search a file by giving file type like mp3 or mpeg or doc Prashanth SR
Menus Demonstration B.Chidhambaram
Employee Database Project Using C. Reenku Raman Nayak
Creating a Lexical Analyzer in c fahad bader al-buhairi դ ? ??
Calendar Program Omkar & Devendra
Stop double Process for start in C Cedrik Jurak
Stop double Process for start in C Cedrik Jurak
Time Scheduler Atiq Anwar
A timepass game between atmost two players Rahul Roy

A D V E R T I S E M E N T




Google Groups Subscribe to SourceCodesWorld - Techies Talk
Email:

Free eBook - Interview Questions: Get over 1,000 Interview Questions in an eBook for free when you join JobsAssist. Just click on the button below to join JobsAssist and you will immediately receive the Free eBook with thousands of Interview Questions in an ebook when you join.

New! Click here to Add your Code!


ASP Home | C Home | C++ Home | COBOL Home | Java Home | Pascal Home
Source Codes Home Page

 Advertisements  

Google Search

Google

Source Codes World.com is a part of Vyom Network.

Vyom Network : Web Hosting | Dedicated Server | Free SMS, GRE, GMAT, MBA | Online Exams | Freshers Jobs | Software Downloads | Interview Questions | Jobs, Discussions | Placement Papers | Free eBooks | Free eBooks | Free Business Info | Interview Questions | Free Tutorials | Arabic, French, German | IAS Preparation | Jokes, Songs, Fun | Free Classifieds | Free Recipes | Free Downloads | Bangalore Info | Tech Solutions | Project Outsourcing, Web Hosting | GATE Preparation | MBA Preparation | SAP Info | Software Testing | Google Logo Maker | Freshers Jobs

Sitemap | Privacy Policy | Terms and Conditions
Copyright ©2003-2017 SourceCodesWorld.com, All Rights Reserved.
Page URL: http://www.sourcecodesworld.com/source/show.asp?ScriptID=804


Download Yahoo Messenger | Placement Papers | Free SMS | C Interview Questions | C++ Interview Questions | Quick2Host Review