how does a compiler scan a program and find/recognise keywords and functions? does anyone have any sample code please? thanks
how does a compiler scan a program and find/recognise keywords and functions? does anyone have any sample code please? thanks
a compiler reads in the input a line at a time, and breaks the input into tokens depending upon whitespace and parsing rules. for instance, ()+- will break into '(', ')', '+', '-' while int x; will break into 'int', 'x', ';'. in the case of c (and probably others), keywords need whitespace about them (or valid tokens) ie intlol will break into 'intlol'. each token is tied to a specific type, such as ADD for '+', and INT for 'int', and the unknown tokens are treated as identifiers. as for recognizing functions, a c function could be defined as a return type keyword followed by an identifier followed by an argument list. the compiler has to take all of the rules into context. while i don't have a nice and simple sample program ready at hand, you might be able to use these:
www.google.com/search?q=lcc+source
www.google.com/search?q=gcc+source
etc...
.sect signature
hi
i have to make a symbol table containing c keywords. I need to then scan thru a program and list all keywords, identifiers etc within the program....does anyone have any idea as to what kinda info i need to look for and also if u have any examples that would be gr8 thank u
Ah, you do not need anything as complex as a a C compiler. Just read in a line at a time from the file. Then use something like strchr to find all of the words in the line you read in. Then compare each word with your array of keywords and if it matches, do whatever you need to do. Your program does not need to understand C at all, just needs to do some simple string comparison.
If you do not know how to use strchr or strcmp, go read the man pages. They can be found on the internet if you do not have them installed. www.freebsd.org has them under documentation section.
Additionally, assuming that you're trying to validate a C program or something similar, you'll want to make your program ignore keywords and symbols which are enclosed in a string.
int x;
char s[] = "int x;" /* this 'int x;' is not counted */
Also, you'll likely want to ignore stuff in comments. For extra credit you could also omit things like:
Quzah.Code:#if 0 /*this line would count */ /* everything here is ignored */ #endif /* this line would count */
Hope is the first step on the road to disappointment.