• C

# lex

please explain me shortly how to use lex.
I'm writing a calculator so what I have in my strings are operators and digits and functions with alphanumeric name.

10x alot.
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
Entire books (or at least large portions of book) have been written on lex.  Let me suggest the O'Reilly "lex and yacc" book to help you with your homework problem.

http://www.oreilly.com/catalog/lex/
0
Author Commented:
I asked this question just because I don't want to read the whole book inorder to do one simple thing.
0
Commented:
Yes, it would be awful if you actually had to learn something in order to do the work.  Maybe some other expert will take pity on you and post a complete tutorial on using lex for 50 pts. but I doubt it.

0
Author Commented:
Many questions like the one I asked have short and clear answers and do not require reading the whole book.
If you save a programmer time by answering his question he can use this time to learn the things that he really needs to know thoroughly.
Do you want to help or to argue?
0
Commented:
%{
int i=0;char o='';
%}
%%
DIGIT [0-9]
%%
{DIGIT}+      { n[i++]=atoi( yytext );}
"+"|"-"|"*"|"/"      { o=yytext[0];}
\n      { if(o=='+')printf("%d\n",n[0]+n[1]);
if(o=='-')printf("%d\n",n[0]-n[1]);
if(o=='/')printf("%d\n",n[0]/n[1]);
if(o=='*')printf("%d\n",n[0]*n[1]);
i=0;o='';
}
..      { printf("error\n"); }

%%
void main (int a,char*v[]){yylex();}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.