-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtokens.l
36 lines (35 loc) · 888 Bytes
/
tokens.l
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
%{
#include<stdio.h>
%}
digit([0-9])
letter([A-Za-z])
key void|main|int|float|printf|String
id({letter})({letter}|{digit})*
sep "{"|"}"|"("|")"|";"|","
op "+"|"-"|"="|"*"|"/"
int({digit})({digit})*
float({digit}+)"."({digit})*
oct [0]({digit}+)
litrl(\".*\")
%%
{key} {fprintf(yyout,"\n %s is a keyword",yytext);}
{id} {fprintf(yyout,"\n %s is an identifier",yytext);}
{oct} {fprintf(yyout,"\n %s is an octal",yytext);}
{int} {fprintf(yyout,"\n %s is an integer ",yytext);}
{float} {fprintf(yyout,"\n %s is a float",yytext);}
{litrl} {fprintf(yyout,"\n %s is a literal",yytext);}
{sep} {fprintf(yyout,"\n %s is a seperator ",yytext);}
{op} {fprintf(yyout,"\n %s is an operator",yytext);}
. {}
%%
void main()
{
extern FILE *yyin,*yyout;
yyin=fopen("sample1.txt","r");
yyout=fopen("sample2.txt","w");
yylex();
}
int yywrap()
{
return 1;
}