Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %{
- #include<stdlib.h>
- #include<stdio.h>
- #include<string.h>
- struct sym
- {
- char sname [15];
- }s[20];
- int refer = 0;
- int insert(char *yytext)
- { int i = 0 ;
- for(i = 0;i < 20; i++)
- if(strcmp(s[i].sname,yytext) == 0)
- { printf("\n%s keyword already there",yytext);
- return 0;
- }
- strcpy(s[refer++].sname,yytext);
- return 1;
- }
- void display()
- { int i = 0;
- for(i = 0 ; i<20; i++)
- printf("%s\n",s[i].sname);
- }
- %}
- %%
- "#include"[ \t]*"<"|\"[ \t]*[^\n"][ \t]*">"|\" { printf("\n #include found: %s",yytext); insert(yytext);}
- "main" { printf("\n Main starts: %s",yytext);insert(yytext);}
- "(" { printf("\n Parenthesis: %s",yytext);insert(yytext);}
- ")" { printf("\n Parenthesis: %s",yytext);insert(yytext);}
- [0-9] { printf("\n Number: %s",yytext);insert(yytext);}
- "int"|"float"|"char"|"double" { printf("\n Keyword: %s",yytext);insert(yytext);}
- "," { printf("\n Comma: %s",yytext);insert(yytext);}
- ";" { printf("\n Semicolon: %s",yytext);insert(yytext);}
- "::" { printf("\n Scope resolution: %s",yytext);insert(yytext);}
- "*"|"+"|"-"|"/" { printf("\n Operator: %s",yytext);insert(yytext);}
- "}" { display();}
- "//"[^\n] { printf("\n Single line Comment: %s",yytext);insert(yytext);}
- "/*" { printf("\n Multi line Comment Starts: %s",yytext);insert(yytext);}
- "*/" { printf("\n Single line Comment Ends: %s",yytext);insert(yytext);}
- %%
- main()
- {
- FILE *file;
- file=fopen("code.c","r");
- yyin=file;
- yylex();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement