-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcf.l
79 lines (75 loc) · 3.14 KB
/
cf.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
%{
#include<stdio.h>
#include<stdlib.h>
int yychars = 1;
%}
%option yylineno
INT ([1-9][0-9]*)|0
FLOAT ((0|[1-9][0-9]*)[.][0-9]*)|([.][0-9][0-9]*)
ID [a-zA-Z|_]([a-zA-Z_]|[0-9])*
SEMI ;
COMMA ,
RELOP >|<|>=|<=|==|!=
MINUS \-
PLUS \+
DIV \/
MUL \*
AND &&
OR \|\|
DOT \.
NOT \!
TYPE int|float
LP \(
RP \)
LB \[
RB \]
LC \{
RC \}
STRUCT struct
RETURN return
IF if
ELSE else
WHILE while
ASSIGNOP =
%%
[\t\r]+ {yychars += yyleng;}
[\n] {yychars = 1;}
{FLOAT} {printf("FLOAT at line %d,char %d: %s\n", yylineno, yychars, yytext); yychars += yyleng;}
{INT} {printf("INT at line %d, char %d: %s\n", yylineno, yychars, yytext); yychars += yyleng;}
{SEMI} {printf("SEMI at line %d, char %d: %s\n", yylineno, yychars, yytext); yychars += yyleng;}
{DIV} {printf("DIV at line %d, char %d: %s\n", yylineno, yychars, yytext); yychars += yyleng;}
{MUL} {printf("MUL at line %d, char %d: %s\n", yylineno, yychars, yytext); yychars += yyleng;}
{COMMA} {printf("COMMA at line %d, char %d: %s\n", yylineno, yychars, yytext); yychars += yyleng;}
{RELOP} {printf("RELOP at line %d, char %d: %s\n", yylineno, yychars, yytext); yychars += yyleng;}
{MINUS} {printf("MINUS at line %d, char %d: %s\n", yylineno, yychars, yytext); yychars += yyleng;}
{AND} {printf("AND at line %d, char %d: %s\n", yylineno, yychars, yytext); yychars += yyleng;}
{OR} {printf("OR at line %d, char %d: %s\n", yylineno, yychars, yytext); yychars += yyleng;}
{DOT} {printf("DOT at line %d, char %d: %s\n", yylineno, yychars, yytext); yychars += yyleng;}
{NOT} {printf("NOT at line %d, char %d: %s\n", yylineno, yychars, yytext); yychars += yyleng;}
{TYPE} {printf("TYPE at line %d, char %d: %s\n", yylineno, yychars, yytext); yychars += yyleng;}
{LP} {printf("LP at line %d, char %d: %s\n", yylineno, yychars, yytext); yychars += yyleng;}
{RP} {printf("RP at line %d, char %d: %s\n", yylineno, yychars, yytext); yychars += yyleng;}
{LB} {printf("LB at line %d, char %d: %s\n", yylineno, yychars, yytext); yychars += yyleng;}
{RB} {printf("RB at line %d, chars %d: %s\n", yylineno, yychars, yytext); yychars += yyleng;}
{LC} {printf("LC at line %d, char %d: %s\n", yylineno, yychars, yytext); yychars += yyleng;}
{RC} {printf("RC at line %d, char %d: %s\n", yylineno, yychars, yytext); yychars += yyleng;}
{STRUCT} {printf("STRUCT at line %d, char %d: %s\n", yylineno, yychars, yytext); yychars += yyleng;}
{IF} {printf("IF at line %d, char %d: %s\n", yylineno, yychars, yytext); yychars += yyleng;}
{ELSE} {printf("ELSE at line %d, char %d: %s\n", yylineno, yychars, yytext); yychars += yyleng;}
{RETURN} {printf("RETURN at line %d, char %d: %s\n", yylineno, yychars, yytext); yychars += yyleng;}
{WHILE} {printf("WHILE at line %d, char %d: %s\n", yylineno, yychars, yytext); yychars += yyleng;}
{ASSIGNOP} {printf("ASSIGNOP at line %d, char %d: %s\n", yylineno, yychars, yytext); yychars += yyleng;}
{ID} {printf("ID at line %d, char %d: %s\n", yylineno, yychars, yytext); yychars += yyleng;}
. {printf("UNDEFINED at line %d, char %d: %s\n", yylineno, yychars, yytext); yychars += yyleng;}
%%
int main(int argc, char* argv[])
{
if(argc > 1){
if(!(yyin = fopen(argv[1], "r"))){
perror(argv[1]);
return 1;
}
while (yylex() != 0);
}
return 0;
}