top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Program for Lexical Analysis

+2 votes
548 views

Need a C program which reads a program written in any programming language and then
perform lexical analysis. The output of program should contain the tokens i.e. classification
as identifier, special symbol, delimiter, operator, keyword or string. It should also display the
number of identifiers, special symbol, delimiter, operator, keyword, strings and statements.

posted Feb 21, 2014 by Neeraj Pandey

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button
It is not possible to write a generic C program which can do lexical analysis of program written in any programming language since every language has different programming language constructs and correspondingly different leximes and different tokens................
Hmm one thing u can do..........
U can write different function for each programming language..........i mean to say different lexical analyser function for different program written in different language.(Hmmm some part may be sharable like identifying identifiers etc.)
U must have pre-awareness of the language of the program and accordingly u can invoke the function which does the lexical analysis of that program.

1 Answer

0 votes

Source : http://c-madeeasy.blogspot.in/2012/05/program-for-lexical-analyser-for-c.html

letter [a-zA-Z]  
 digit[0-9]  
 %%  
 {digit}+("E"("+"|"-")?{digit}+)? printf("\n%s\tis real number",yytext);  
 {digit}+"."{digit}+("E"("+"|"-")?{digit}+)? printf("\n%s\t is floating pt no ",yytext);  
 "if"|"else"|"int"|"char"|"scanf"|"printf"|"switch"|"return"|"struct"|"do"|"while"|"void"|"for"|"float" printf("\n%s\t is keywords",yytext);  
 "\a"|"\\n"|"\\b"|"\t"|"\\t"|"\b"|"\\a" printf("\n%s\tis Escape sequences",yytext);  
 {letter}({letter}|{digit})* printf("\n%s\tis identifier",yytext);  
 "&&"|"<"|">"|"<="|">="|"="|"+"|"-"|"?"|"*"|"/"|"%"|"&"|"||" printf("\n%s\toperator ",yytext);  
 "{"|"}"|"["|"]"|"("|")"|"#"|"'"|"."|"\""|"\\"|";"|"," printf("\n%s\t is a special character",yytext);  
 "%d"|"%s"|"%c"|"%f"|"%e" printf("\n%s\tis a format specifier",yytext);  
 %%  
 int yywrap()  
 {  
 return 1;  
 }  
 int main(int argc,char *argv[])  
 {  
 yyin=fopen(argv[1],"r");  
 yylex();  
 fclose(yyin);  
 return 0;  
 }  
 /*INPUT PROGRAM  
 #include<stdio.h>  
 void main()  
 {  
  printf("\nhai\n");  
 }  
/*
OUTPUT

#  is a special character
include is identifier
< operator 
stdio is identifier
.  is a special character
h is identifier
> operator 

void  is keywords 
main is identifier
(  is a special character
)  is a special character

{  is a special character

printf  is keywords
(  is a special character
"  is a special character
\n is Escape sequences
hai is identifier
\n is Escape sequences
"  is a special character
)  is a special character
;  is a special character

}  is a special character
*/
answer Mar 18, 2014 by Pavan P Naik
Similar Questions
+1 vote

Here my goal is to detect only real errors in the code (i.e. have zero false positives).

+3 votes

Please let me know any open source c/c++ static code analysis tool, I need to use that tool for stataic analysis

+1 vote

How to write a c program for data signaling rate for four signals? Data signaling rate formula should be written in c language as formula cannot be inserted?

...