-
Notifications
You must be signed in to change notification settings - Fork 0
/
token.l
121 lines (101 loc) · 2.23 KB
/
token.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*TITLE: Token Table Generator*/
%{
#include<stdio.h>
int lineno = 1;
int tokencode=255;
%}
letter [a-zA-Z]
digit [0-9]
under [_]
%%
[\n]* {lineno++;}
void |
int |
char |
include |
main |
printf |
if |
for |
break |
while {printf("\n%d \t %s \t\t Keyword\t\t%d", lineno,yytext, tokencode);}
"+" |
"-" |
"*" |
"/" {printf("\n%d \t %s \t\t Binary Operator\t%d", lineno,yytext, tokencode+1);}
"=" {printf("\n%d \t %s \t\t Assignment Operator\t%d", lineno,yytext, tokencode+2);}
"==" |
"<=" |
">=" |
"<" |
">" |
"!=" {printf("\n%d \t %s \t\t Relational Operator\t%d", lineno,yytext, tokencode+3);}
";" |
"(" |
")" |
"{" |
"," |
"}" {printf("\n%d \t %s \t\t Punctuation\t\t%d", lineno,yytext, tokencode+4);}
{digit}+ {printf("\n%d \t %s \t\t Constant\t\t%d", lineno,yytext, tokencode+5);}
{letter}({letter}|{digit}|{under})* {printf("\n%d \t %s \t\t Identifier\t\t%d", lineno,yytext, tokencode+6);}
{digit}({letter}|{digit}|{under})* {printf("\n%d \t %s \t\t Error ", lineno, yytext);}
(\n)*"/*"(.|\n)*"*/" {}
(\n)*"//"(.)* {}
%%
int main(int argc, char **argv) {
FILE *fp;
fp=fopen(argv[1],"r");
yyin=fp;
printf("\nLineNo. Lexeme \t Token\t\tToken Code\n");
yylex();
return 1;
}
yywrap()
{}
INPUT FILE:
main()
{
int a,b;
int 9c=20;
a= 10 ;
b= 20 ;
a=b+a* 2 ;
//trial
/*multiline comment
zjhfg*/
}
OUTPUT:
[ccoew@localhost ~]$ lex token.l
[ccoew@localhost ~]$ gcc lex.yy.c
[ccoew@localhost ~]$ ./a.out main.c
LineNo. Lexeme Token Token Code
2 main Keyword 255
2 ( Punctuation 259
2 ) Punctuation 259
3 { Punctuation 259
4 int Keyword 255
4 a Identifier 261
4 , Punctuation 259
4 b Identifier 261
4 ; Punctuation 259
5 int Keyword 255
5 9c Error
5 = Assignment Operator 257
5 20 Constant 260
5 ; Punctuation 259
6 a Identifier 261
6 = Assignment Operator 257
6 10 Constant 260
6 ; Punctuation 259
7 b Identifier 261
7 = Assignment Operator 257
7 20 Constant 260
7 ; Punctuation 259
8 a Identifier 261
8 = Assignment Operator 257
8 b Identifier 261
8 + Binary Operator 256
8 a Identifier 261
8 * Binary Operator 256
8 2 Constant 260
8 ; Punctuation 259