Important alert: (current site time 7/15/2013 6:11:18 PM EDT)
 

winzip icon

Lexical Analyzer for C Lang.

Email
Submitted on: 1/20/2008 11:28:01 PM
By: Samir Solanki 
Level: Advanced
User Rating: By 4 Users
Compatibility: C++ (general), Microsoft Visual C++, Borland C++
Views: 29784
author picture
(About the author)
 
     Hello Programmers!! I have made Lexical Analyzer for C in Turbo C 3.0 Rate me, if you like my code. Contact me: solanki.samir@gmail.com Happy Programming !!!

 
winzip iconDownload code

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. Afterdownloading it, you will need a program like Winzip to decompress it.Virus note:All files are scanned once-a-day by Planet Source Code for viruses, but new viruses come out every day, so no prevention program can catch 100% of them. For your own safety, please:
  1. Re-scan downloaded files using your personal virus checker before using it.
  2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

If you don't have a virus scanner, you can get one at many places on the net including:McAfee.com

 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.


Other 7 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Advanced category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments
1/21/2008 12:36:07 AMBrenton A. Saunders

Nice!
(If this comment was disrespectful, please report it.)

 
5/29/2008 5:04:57 PMCrowded Flowers

Um this is very poorly programmed. Your formatting is terrible, and its not even really a lexical analyzer. I suggest reading the book "Writing Compilers and interpreters" by Ronald Mak. This sites code makes me laugh..


(If this comment was disrespectful, please report it.)

 
11/12/2008 5:20:13 AMhribek

i'd suggest "red dragon" book Compilers: Principles, Techniques, and Tools from Aho, Sethi, Ullman
(If this comment was disrespectful, please report it.)

 
2/25/2009 5:55:47 AMmilos chmel

instead of writin in the middle ->
strcpy(defKey[0],"int"); strcpy(defKey[1],"float");
-> one writes in the initialization ->
char *defKey[] = {"int","float",""}; // "" to always recognize the end

better to avoid globals -> one can use scoping and static
if (1) {
int scoped = 2;
printf("scoped=%d", scoped);
if (2) {
int scoped = 3;
static int incremented = 2;

incremented++;
printf("local=%d scoped=%d", incremented, scoped);
}
// will fail - so delete it
printf("%d ", incremented);
//
printf("scoped=%d", scoped);
}
(If this comment was disrespectful, please report it.)

 
2/25/2009 6:26:31 AMmilos chmel

example of use of stdlib to parse text ->
char alphabet[256+1];
char digits[10+1];

void
init(char *alphabet, char *digits) {
char *pa = alphabet, *pd = digits;
for (i =0 ;i < 256; ++i) {
if (isalpha(i)) {
*pa++ = i;
*pa = 0;
} else if (isdigit(i)) {
*pd++ = i;
*pd = 0;
}
}
}
enum {
INTEGER,
REAL,
OPERAND,
ERROR,
};
int
lex(char *y, int maxsiz, char *x) {
char sequence[256+1];
int length;

strcpy(sequence,".+-");
strcat(sequence,"eE");
strcat(sequence,d igits);
length
= strspn(x, sequence);
if (0 < length) {
strncpy(y,x,maxsiz);
return strchr(x, ".eE")? REAL: INTEGER;
}

strcpy(sequence,alphabet);
strcat(sequence,digits);
strca t(sequence,"_");

length = strspn(x, sequence);
if (0 < length) {
strncpy(y,x,maxsiz);
return OPERAND;
}
return ERROR;
}
(If this comment was disrespectful, please report it.)

 
2/28/2009 12:02:13 PMmilos chmel

strcpy(sequence,".+-");
strcat(sequence,"eE");
strcat(sequence,digits);
// can be written also as
strcat(strcat(strcpy(sequence,".+-"),"eE),digits);
// if You care about speed You can cache/store it somewhere in initialization
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.