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 name [10];
- }STAB[20];
- int m=0,k=0;
- %}
- %%
- "#include"[ \t]*"<"|\"[ \t]*[^\n"][ \t]*">"|\" { printf("\n #include found: %s",yytext);insert(yytext); }
- "(" { printf("\n Parenthesis: %s",yytext);}
- ")" { printf("\n Parenthesis: %s",yytext);}
- [0-9] { printf("\n Number: %s",yytext);}
- "int"|"float"|"char"|"double"|"main" { printf("\n Keyword: %s",yytext);insert(yytext);}
- "," { printf("\n Comma: %s",yytext);}
- ";" { printf("\n Semicolon: %s",yytext);}
- "::" { printf("\n Scope resolution: %s",yytext);}
- "*"|"+"|"-"|"/" { printf("\n Operator: %s",yytext);}
- [
- "//"[^\n] { printf("\n Single line Comment: %s",yytext);}
- "/*" { printf("\n Multi line Comment Starts: %s",yytext);}
- "*/" { printf("\n Single line Comment Ends: %s",yytext);}
- %%
- int insert(char *yytext)
- {
- int i=0;
- int l,k;
- for(l=0; l<=m;l++)
- {
- if(strcmp(STAB[l].name,yytext)==0)
- {
- i=1;
- break;
- }
- }
- if(i==0)
- {
- printf("\nIdentifier not found. Inserting\n");
- strcpy(STAB[m].name,yytext);
- m++;
- }
- else
- printf("\nIdentifier found\n");
- }
- main()
- {
- FILE *file;
- file=fopen("code.c","r");
- yyin=file;
- yylex();
- printf("Symbol table is:\n");
- for(k=0;k<m;k++)
- printf("%s\n",STAB[k].name);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement