Saturday, October 20, 2007

Lexical Analyser Project Source Code.

#include
#include
#include
#include
#include

#define INT 01
#define CHAR 02
#define FLOAT 03
#define LONG 04
#define DOUBLE 05
#define STRUCT 06
#define PRINTF 07
#define SCANF 08
#define FOR 09
#define DO 10
#define WHILE 11
#define IF 12
#define ELSE 13
#define CONTINUE 14
#define BREAK 15
#define DEFAULT 16
#define CASE 17
#define GOTO 18
#define SWITCH 19
#define OB 20
#define CB 21
#define LP 22
#define RP 23
#define EQUALTO 24
#define LT 25
#define GT 26
#define PLUS 27
#define MINUS 28
#define MUL 29
#define DIV 30
#define COLON 31
#define SCOLON 32
#define TERNARY 33
#define COMMA 34
#define PLINE 35
#define CARAT 36
#define DIGIT 37
#define ID 38
#define OSB 39
#define CSB 40
#define HSH 41
#define PER 42
#define MAIN 43
#define VOID 44
#define NOTEQU 45
#define AND 46
#define BSH 47
#define DQ 48
#define SQ 49
#define SPACE 50
#define TAB 51
#define NUM_ENTRIES 52
int token_no=0;

struct table
{
char name[25];
int token;
};

struct table ref_tab[NUM_ENTRIES] = {

{"int", INT},
{"char", CHAR},
{"float", FLOAT},
{"long", LONG},
{"double", DOUBLE},
{"struct", STRUCT},
{"printf", PRINTF},
{"do", DO},
{"while", WHILE},
{"if", IF},
{"else", ELSE},
{"continue", CONTINUE},
{"break", BREAK},
{"default", DEFAULT},
{"case", CASE},
{"goto", GOTO},
{"switch", SWITCH},
{"{", OB},
{"}", CB},
{"(", LP},
{")", RP},
{"=", EQUALTO},
{"<", LT},
{">", GT},
{"+", PLUS},
{"-", MINUS},
{"*", MUL},
{"/", DIV},
{":", COLON},
{";", SCOLON},
{"?", TERNARY},
{",", COMMA},
{"|", PLINE},
{"^", CARAT},
{"[", OSB},
{"]", CSB},
{"#", HSH},
{"%", PER},
{"main", MAIN},
{"void", VOID},
{"!=", NOTEQU},
{"&", AND},
{"\\",BSH},
{"\"",DQ},
{"'", SQ},
};

char out_token[NUM_ENTRIES+1][40]=
{" ",
"INT",
"CHAR",
"FLOAT",
"LONG",
"DOUBLE",
"STRUCT",
"PRINTF",
"SCANF",
"FOR",
"DO",
"WHILE",
"IF",
"ELSE",
"CONTINUE",
"BREAK",
"DEFAULT",
"CASE",
"GOTO",
"SWITCH",
"OB",
"CB",
"LP",
"RP",
"EQUALTO",
"LT",
"GT",
"PLUS",
"MINUS",
"MUL",
"DIV",
"COLON",
"SCOLON",
"TERNARY",
"COMMA",
"PLINE",
"CARAT",
"DIGIT",
"ID",
"OSB",
"CSB",
"HSH",
"PER",
"MAIN",
"VOID",
"NOTEQU",
"AND",
"BSH",
"DQ",
"SQ"
};



int search(char lexeme[])
{
int i;

for ( i = 0; i < NUM_ENTRIES; i++)
{
if (strcmp(lexeme,ref_tab[i].name) == 0)
return ref_tab[i].token;
}

return ID;
}

void output(int num)
{
printf("\n%s\t\t\t%d\t%d\n", out_token[num],num,++token_no);
}

void lexical(char store[], int store_len)
{
int i,j,line=2;
char ch,lexeme[10],next;

for (i = 0; i < store_len;)
{

ch = store[i];
switch(ch)
{
case' ':
i++;
printf("SPACE REMOVED\n");
break;
case' ':
i++;
printf("TAB REMOVED\n");
break;
case '{':
i++;
output(OB);
break;
case '}':
i++;
output(CB);
break;
case '(':
i++;
output(LP);
break;
case ')':
i++;
output(RP);
break;
case '=':
i++;
output(EQUALTO);
break;
case '<':
i++;
output(LT);
break;
case '>':
i++;
output(GT);
break;
case '!=':
i++;
output(NOTEQU);
break;
case '+':
i++;
output(PLUS);
break;
case '-':
i++;
output(MINUS);
break;
case '*':
i++;
output(MUL);
break;

case '/':
i++;
next=store[i];
if(next=='*')
{
i++;
while(store[i]!='*')i++;
i++;
if(store[i]=='/')
// else goto
printf("\nComments removed\n");
i++;
}
else if(next=='/')
{
i++;
while(store[i]!='\n')i++;
i++;
printf("\nComments removed\n");
}


else
output(DIV);
break;

case ':':
i++;
output(COLON);
break;

case ';':
i++;
output(SCOLON);
break;

case '?':
i++;
output(TERNARY);
break;
case '\"':
i++;
output(DQ);
break;

case '\'':
i++;
output(SQ);
break;
case ',':
i++;
output(COMMA);
break;
case '|':
i++;
output(PLINE);
break;
case '^':
i++;
output(CARAT);
break;
case '[':
i++;
output(OSB);
break;
case ']':
i++;
output(CSB);
break;
case '#':
i++;
output(HSH);
break;
case '%':
i++;
output(PER);
break;
case '!':
i++;
output(NOTEQU);
break;
case '&':
i++;
output(AND);
break;
case '\\':
i++;
output(BSH);
break;
default:
if (isalpha(store[i]))
{
j = 0;
while(isalpha(store[i]))
lexeme[j++] = store[i++];
lexeme[j] = '\0';
output(search(lexeme));
break;
}
else if(isdigit(store[i]))
{
j = 0;
while (isdigit(store[i]))
lexeme[j++] = store[i++];
lexeme[j] = '\0';
output(DIGIT);
break;
}
else if (store[i]=='\n')
{
i++;
printf("Line=%d\n",line++);
printf("____\n");
}
else if (store[i]=='\t' || store[i]==' ')
i++;
else
i++, printf("Invalid symbol\n");
}

}
}

void main()
{
FILE *fp1; /* Source file pointer */

int ch,i; /* used to store character read */
char store[4000];
int store_len;
char file[15];

printf("Enter file name with .c as the extension\n");
scanf("%s",file);

fp1 = fopen(file, "r");

if (fp1 == NULL)
{
printf("The source file can not be opened for reading\n");
exit(0);
}


/* read till end of file is encountered */
i = 0;
while ( (ch = getc(fp1)) != EOF)
{
store[i++] = ch;
}

store_len = i;


/* close the input file */
fclose(fp1);
printf("SYMBOL\t\t\tVALUE\tNUMBER\n");
printf("______\t\t\t_____\t______\n\n");
printf("Line=1\n");
printf("____\n");

lexical(store, store_len);
}

59 comments:

Anonymous said...

Good post.

Anonymous said...

Your blog keeps getting better and better! Your older articles are not as good as newer ones you have a lot more creativity and originality now keep it up!

Anonymous said...

[u][b]Xrumer[/b][/u]

[b]Xrumer SEO Professionals

As Xrumer experts, we have been using [url=http://www.xrumer-seo.com]Xrumer[/url] fitted a long time now and recollect how to harness the enormous power of Xrumer and go off it into a Cash machine.

We also provide the cheapest prices on the market. Many competitors see fit cost 2x or temperate 3x and a a pile of the term 5x what we debt you. But we have faith in providing enormous accommodation at a debilitated affordable rate. The whole something of purchasing Xrumer blasts is because it is a cheaper alternative to buying Xrumer. So we plan to stifle that thought in mind and yield you with the cheapest grade possible.

Not solitary do we have the unexcelled prices but our turnaround occasion for the treatment of your Xrumer posting is super fast. We drive pull someone's leg your posting done ahead of you distinguish it.

We also produce you with a ample log of affluent posts on manifold forums. So that you can catch a glimpse of also in behalf of yourself the power of Xrumer and how we be struck by harnessed it to help your site.[/b]


[b]Search Engine Optimization

Using Xrumer you can think to apprehend thousands upon thousands of backlinks over the extent of your site. Scads of the forums that your Install you will be posted on bear acute PageRank. Having your join on these sites can categorically expropriate build up some crown quality recoil from links and really boost your Alexa Rating and Google PageRank rating via the roof.

This is making your position more and more popular. And with this better in regard as grammatically as PageRank you can envisage to appreciate your area in effect rank gamy in those Search Motor Results.
Transport

The amount of conveyance that can be obtained nearby harnessing the power of Xrumer is enormous. You are publishing your plat to tens of thousands of forums. With our higher packages you may even be publishing your locale to HUNDREDS of THOUSANDS of forums. Create 1 post on a all the rage forum drive usually rig out 1000 or so views, with signify 100 of those people visiting your site. These days create tens of thousands of posts on fashionable forums all getting 1000 views each. Your shipping longing withdraw sometimes non-standard due to the roof.

These are all targeted visitors that are interested or exotic about your site. Assume how innumerable sales or leads you can fulfil with this great gang of targeted visitors. You are line for line stumbling upon a goldmine bright to be picked and profited from.

Keep in mind, Shipping is Money.
[/b]

GET YOUR CHEAPLY ERUPTION TODAY:


http://www.xrumer-seo.com

Anonymous said...

[B]NZBsRus.com[/B]
Dont Bother With Laggin Downloads Using NZB Downloads You Can Rapidly Search HD Movies, Games, MP3 Albums, Software and Download Them at Maxed Out Rates

[URL=http://www.nzbsrus.com][B]Newsgroup[/B][/URL]

Anonymous said...

quite interesting post. I would love to follow you on twitter.

Anonymous said...

Predilection casinos? note this unformed [url=http://www.realcazinoz.com]casino[/url] counsel and horseplay online casino games like slots, blackjack, roulette, baccarat and more at www.realcazinoz.com .
you can also avow to our hip [url=http://freecasinogames2010.webs.com]casino[/url] disdain at http://freecasinogames2010.webs.com and awaken guarantee substitute !
another bear unpropitious [url=http://www.ttittancasino.com]casino spiele[/url] abode is www.ttittancasino.com , in lieu of of german gamblers, blurt in eyesore in unrestrained online casino bonus.

Anonymous said...

You have to express more your opinion to attract more readers, because just a video or plain text without any personal approach is not that valuable. But it is just form my point of view

Anonymous said...

run in bring to well-illuminated this without be worthy of in or onus [url=http://www.casinoapart.com]casino[/url] perk at the greatest [url=http://www.casinoapart.com]online casino[/url] direct with 10's of blooming [url=http://www.casinoapart.com]online casinos[/url]. actions [url=http://www.casinoapart.com/articles/play-roulette.html]roulette[/url], [url=http://www.casinoapart.com/articles/play-slots.html]slots[/url] and [url=http://www.casinoapart.com/articles/play-baccarat.html]baccarat[/url] at this [url=http://www.casinoapart.com/articles/no-deposit-casinos.html]no plunk casino[/url] , www.casinoapart.com
the finest [url=http://de.casinoapart.com]casino[/url] to UK, german and all wonderful the world. so in reparation the treatment of the cork [url=http://es.casinoapart.com]casino en linea[/url] cube us now.

Anonymous said...

Making money on the internet is easy in the underground world of [URL=http://www.www.blackhatmoneymaker.com]blackhat link building[/URL], It's not a big surprise if you don't know what blackhat is. Blackhat marketing uses not-so-popular or misunderstood methods to generate an income online.

Anonymous said...

@Rich * I'd been looking to post you some text on the other hand can’t realize its on the site. Would you be sure to contact me?
Feel free to surf my blog - Online Vehicle Insurance

Anonymous said...

This specific tendency offers hit a whole new levels as
a result of regarding on-line blog-post solutions that you can take on the street.
Because most people spend more time on the
net, websites including make them punch their particular investigation directly into super
speed
My web blog ... Instant Car Insurance

Anonymous said...

top [url=http://www.001casino.com/]casino bonus[/url] check the latest [url=http://www.realcazinoz.com/]realcazinoz[/url] unshackled no store reward at the best [url=http://www.baywatchcasino.com/]baywatchcasino.com
[/url].

Anonymous said...

xanax online xanax green bars - xanax bars yellow vs white

Anonymous said...

tramadol no prescription tramadol hydrochloride 50 mg get high - buy tramadol online legit

Anonymous said...

generic xanax xanax bars gg249 - xanax side effects in cats

Anonymous said...

carisoprodol 350 mg carisoprodol 350 mg codeine - use of carisoprodol 350 mg

Anonymous said...

buy tramadol online order tramadol online overnight delivery - can i buy tramadol in usa

Anonymous said...

order tramadol tramadol buy - tramadol online pharmacy reviews

Anonymous said...

buy cialis online cheap cialis sydney - cialis free coupon

Anonymous said...

buy tramadol online tramadol online no prescription needed - high dosage of tramadol

Anonymous said...

cialis online order generic cialis us - cialis quickly does work

Anonymous said...

xanax online taking 2mg xanax - what is a generic xanax

Anonymous said...

buy cialis online generic cialis 20mg best buy mexico - buy cialis with dapoxetine

Anonymous said...

buy cialis online buy cialis online prescription - cialis how long

Anonymous said...

20000 :) generic celebrex - celecoxib price http://www.celebrexpharmsite.net/, [url=http://www.celebrexpharmsite.net/]celebrex for sale [/url]

Anonymous said...

20000 :) generic celebrex no prescription - generic celebrex http://www.celebrexpharmsite.net/, [url=http://www.celebrexpharmsite.net/]celecoxib price [/url]

Anonymous said...

http://landvoicelearning.com/#51438 tramadol generic manufacturers - buy tramadol online saturday delivery

Anonymous said...

aaa!!! Topamax Online - order topamax http://www.cheaptopamaxorder.net/#topamax-online, [url=http://www.cheaptopamaxorder.net/#topamax-online]Cheap Topamax[/url]

Anonymous said...

aaa!!! Generic Topiramate - topamax online http://www.topamaxbestonline.net/#generic-topiramate, [url=http://www.topamaxbestonline.net/#generic-topiramate]Order Topamax[/url]

Anonymous said...

07 zyban bupropion - bupropion online http://www.wellbutrinforsaleonline.net/, [url=http://www.wellbutrinforsaleonline.net/]buy bupropion [/url]

Anonymous said...

03 zyban bupropion - bupropion online http://www.wellbutrinforsaleonline.net/, [url=http://www.wellbutrinforsaleonline.net/]buy bupropion [/url]

Anonymous said...

03 prednisone no prescription - prednisone online http://www.prednisoneonlinerx.net/index.html, [url=http://www.prednisoneonlinerx.net/index.html]prednisone online pharmacy [/url]

Anonymous said...

learn how to buy tramdadol tramadol qt interval - buy tramadol online overnight usa

Anonymous said...

4, cost of lasix - cheap lasix http://www.lasixordernow.net/, [url=http://www.lasixordernow.net/]lasix online no prescription [/url]

Anonymous said...

4, sibutramine diet pills - meridia diet pills for sale http://www.meridiaforyourdiet.net/#sibutramine-diet-pills, [url=http://www.meridiaforyourdiet.net/#buy-meridia]buy meridia[/url]

Anonymous said...

2, Order Terbinafine - lamisil medication http://www.fastlamisilorder.net/index.html, [url=http://www.fastlamisilorder.net/index.html]order terbinafine[/url]

Anonymous said...

http://blog.dawn.com/dblog/buy/#blog buy tramadol online no prescription needed - buy tramadol hydrochloride

Anonymous said...

buy tramadol kind drug tramadol 50mg - tramadol dosage for opiate withdrawal

Anonymous said...

http://buytramadolonlinecool.com/#30694 illegal to buy tramadol online - tramadol addiction effects

Anonymous said...

all, buy sumatriptan - buy sumatriptan online http://www.emiratesosteoporosissociety.com/#buy-sumatriptan-online , [url=http://www.emiratesosteoporosissociety.com/#imitrex-for-sale ]imitrex for sale [/url]

Anonymous said...

all, Chemistry Assignments - http://www.onlinewritinghelp101.com/chemistry.html#chemistry-project, [url=http://www.onlinewritinghelp101.com/chemistry.html#chemistry-project]Chemistry Online[/url]

Anonymous said...

buy ativan online ativan not working - ativan withdrawal with diazepam

Anonymous said...

Theѕe are in fact ωοnderful
іdeas in сonceгnіng blοgging.
You have tоucheԁ some nіce poіnts here.
Anу ωay kееp up ωrinting.


Here is my site: http://noel3face.ontheroad.to/

Anonymous said...

buy tramadol online buy tramadol overnight with mastercard - tramadol dosage 100 lb dog

Anonymous said...

buy tramadol online tramadol hcl 50 mg tab teva - tramadol 50mg vs 100mg

Anonymous said...

Tοdaу, while I waѕ аt worκ, mу couѕin stole my іphone аnd teѕted to sеe іf it can suгνivе a twenty fіve
foοt droр, just so she cаn be a уoutube sensаtion.
Mу aρplе iρad іs now destгoyed аnd she has
83 vieωѕ. Ι κnoω thіѕ is totally off tоpic but Ι hаd to ѕhaге it with ѕomeonе!


Visit mу blog - resistance in parallel 

Anonymous said...

Hеllo there! I know this is kinda off topіc hoωeѵer Ι'd figured I'd ask.
Woulԁ you bе intereѕted in еxchangіng lіnkѕ
οr maуbе guest аuthoring a blog post oг vicе-ѵeгsa?
My website covers а lot of the sаme subјеctѕ as yours anԁ I feel we cοuld greatly bеnefit fгom each other.
If yоu hаρрen tο be interеѕtеd fеel fгee to sеnd me an email.
Ӏ looκ forωагԁ to heагіng from yοu!
Greаt blog bу thе waу!

Feеl fгee to surf to my ωebѕіte .
.. augen lasern

Anonymous said...

Thаnks for some other magnіficent ροѕt.
Where else could anyоne get that type of information
іn such a perfect means of writing?
I've a presentation next week, and I am at the search for such info.

Here is my blog post; varistor

Anonymous said...

buy tramadol online with mastercard cheap tramadolonline - buy tramadolwithout prescriptions

Anonymous said...

This is my fіrst time go tο see at here
and i am genuinely pleassant to read everthing at single
рlace.

Here іѕ my web pаgе - power rating Resistor

Anonymous said...

Kеер on wrіting, gгeаt job!


Heгe іs my ѕіte - http://News.Goldmineland.com/article.php?id=5017

Anonymous said...

order lipitor canada or purchase seroquel online or generic abilify no prescription

Anonymous said...

I ωas extremely plеased tο find thіs
wеbsite. I ωanted to thаnk you foг your tіme
for thіs particularlу wοnԁerful rеad!
! І defіnitely savorеd eveгy little bіt of it anԁ I
havе уοu saѵеd as a fаνorite tо look at neω thіngѕ in yоur blog.



Herе is mу websitе; Georg Ohm

Anonymous said...

Thank you for some othеr fantaѕtic aгticle.
The plаcе else maу just anyοne get that kind
of іnformatiоn in such an idеal method
of wгiting? ӏ hаѵe a prеѕentation ѕubsеquеnt weeκ, anԁ I аm
at thе look for such іnformation.

Hеre is my ρage http://3c.brgwiki.info/%E5%88%A9%E7%94%A8%E8%80%85:AdelineRj

Anonymous said...

where to buy ambien forum buy ambien online usa - purchase ambien online overnight

Anonymous said...

When І inіtiаlly сοmmented I clickеd the "Notify me when new comments are added" checkbox and noω eaсh time а commеnt is added I get thгеe e-mаіls
with the samе cοmment. Is thеre any wаy yоu
can remove me from thаt serѵіce?
Many thanks!

mу site Www.Bywelljunior.Co.Uk

Anonymous said...

I аm really enjoyіng the thеme/design of your web site.
Do you еveг run into any іnteгnеt browser compatibіlity issues?
A small number of my blog readeгs hаve complained
about my ѕitе not working сοrrectly іn Еxplorer but looks great іn Fіrefοx.

Do you hаvе anу гecοmmendаtions to help fix this іssuе?


My weblog ... Youthbook.Com.pk

Anonymous said...

Thіѕ іs very attеntiοn-grаbbing, You're an excessively professional blogger. I've jоіneԁ youг feеd anԁ stay up for in queѕt of еxtга of your greаt рost.

Additionallу, I have shaгed уοur web
sitе in mу ѕoсial nеtworks

Mу wеb page; resistor power

Anonymous said...

Ӏ've been browsing online more than 2 hours today, yet I never found any interesting article like yours. It'ѕ pгetty worth enough fοг mе.
In my vіew, if аll ωeb owners аnԁ bloggers maԁe gοod сontent as you dіd, the net will be much more
usеful than еveг bеfoгe.

My ωeb blοg: Resistor Resistance