To generate tokens
%%
{letter}({letter}|{digit})* printf(“id:
%s\n”, yytext);
%%
--------------------
1.
%{
#include<string.h>
%}
%%
(int|float|double|long|short|return|include|char) {
printf("\n< Keyword,%s
>",yytext);func(yytext,"Keyword");}
[\"] { printf("\n< Operator,%s
>",yytext);func(yytext,"Operator"); }
['"'][\\a-zA-Z0-9|' ']+['"'] { printf("\n<
id,%s >",yytext); func(yytext,"String");}
\+\+ { printf("\n< %s
>",yytext);func(yytext,"Operator"); }
\-\- { printf("\n< %s >",yytext);
func(yytext,"Operator");}
\=\= { printf("\n< %s >",yytext);
func(yytext,"Operator");}
\!\= { printf("\n< %s >",yytext);
func(yytext,"Operator");}
\+\= { printf("\n< %s >",yytext);
func(yytext,"Operator");}
\-\= { printf("\n< %s >",yytext);
func(yytext,"Operator");}
[/|'.']+ { printf("\n< %s >",yytext);
func(yytext,"Operator");}
(printf|scanf) { printf("\n< function,%s
>",yytext);func(yytext,"Function");}
[0-99]+ { printf("\n<Number,%s
>",yytext);func(yytext,"Number"); }
[a-zA-Z|_][0-99|a-zA-Z|_]*
{ printf("\n< variable,%s>",yytext);
func(yytext,"variable");}
['+'|\-|'='|':'|'%'|'&'|'('|')'|'{'|'}'|','|'#'|'!'|'@'|'$'|'*'|'<'|'>'|'?']
{ printf("\n< %s >",yytext);func(yytext,"Operator");}
[';']+ {
printf("\n< %s >",yytext);func(yytext,"Operator");}
\n func2();
%%
struct entry
{
char name[10];
char token[10];
}st[10];
int i=0;
func(char
n[10],char t[10])
{
int
j,flag=0;
for(j=0;j<i;j++)
{
if(strcmp(n,st[j].name)==0)
flag=1;
}
if(flag==0)
{
strcpy(st[i].name,n);
strcpy(st[i].token,t);
i++;
}
}
func2()
{
int k;
printf("\n===========Symbol
Table==================\n");
for(k=0;k<=i;k++)
printf("%s
%s\n",st[k].name,st[k].token);
}
No comments:
Post a Comment