Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* This is a lex program to identify and count identifiers, keywords, numbers and operators in a c program */
- %{
- int numcount=0,idcount=0, keycount=0, opcount=0;
- %}
- id [a-zA-Z][A-Za-z0-9]*
- num [0-9]+
- keywords "auto"|"break"|"case"|"char"|"const"|"continue"|"default"|"do"|"double"|"else"|"enum"|"extern"|"float"|"for"|"goto"|"if"|"int"|"long"|"register"|"return"|"short"|"signed"|"sizeof"|"static"|"struct"|"switch"|"typedef"|"union"|"unsigned"|"void"|"volatile"|"while"
- test "eureka"
- preprocessor [#][a-z A-Z0-9\.<>]*
- operators [+\-=/*]
- %%
- /*"quit" {
- printf("There are %d numbers.\n",numcount);
- printf("There are %d identifiers.\n",idcount);
- yyterminate();
- }*/
- {preprocessor} {
- printf("Skipping preprocessor directives %s\n",yytext);
- }
- {keywords} {
- printf("%s is a keyword.\n",yytext);
- keycount++;
- }
- {test} {
- printf("This is a test.\n");
- }
- {id} {
- printf("%s is an Identifier\n",yytext);
- idcount++;
- }
- {num} {
- printf("%s is a Number\n",yytext);
- numcount++;
- }
- {operators} {
- printf("%s is an operator.\n",yytext);
- opcount++;
- }
- . {;}
- %%
- int
- main(int argc, char **argv){
- if(argc==2){
- yyin=fopen(argv[1],"r");
- }
- yylex();
- printf("There are %d numbers.\n",numcount);
- printf("There are %d identifiers.\n",idcount);
- printf("There are %d keywords.\n",keycount);
- printf("There are %d operators.\n",opcount);
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement