-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMCL_lex.l
84 lines (63 loc) · 1.93 KB
/
MCL_lex.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
/* lex scanner for MCL language */
%{
#include <stdio.h>
#include <math.h>
%}
DIGIT [0-9]
ID [a-zA-Z][a-zA-Z0-9]*
IFC _row_|_colum_|_printm_
Type int|real|matrix
Condition if|else|return|def|for|Null
Space [ \t\n\r]+
Operators "+"|"-"|"*"|"/"|"="|"^"
matrixopt ".*"|"./"
boolopt "=="|"!="|"&&"|"||"|"<"|">"|"<="|">="
Punctuations "("|")"|"{"|"}"|"["|"]"|":"|","|";"
Comments \/\*([^*\"]*|\".*\"|\*+[^/]|\")*((\*\/)|(\*\*\/))|\/\/[^\n]*
%%
{DIGIT}+ {
printf( "An integer: %s (%d)\n", yytext, atoi( yytext ) );
fprintf(yyout,"int %d\n",atoi(yytext));
}/*integer*/
{DIGIT}+"."{DIGIT}* {
printf( "A real: %s (%g)\n", yytext, atof( yytext ) );
fprintf(yyout,"real %g\n",atof( yytext ));
}/*real number*/
{Condition}|{Type} {
printf( "A keyword: %s\n", yytext );
fprintf(yyout,"key %s\n",yytext);
}
{IFC} {
printf( "An inner function %s\n",yytext);
fprintf(yyout,"inner %s\n",yytext);
}
{ID} {
printf( "An identifier: %s\n", yytext );
fprintf(yyout,"ID %s\n",yytext);
}
{Comments} {
printf( "A comment: %s\n",yytext);
}
{Operators}|{matrixopt}|{boolopt} {
printf( "An operator: %s\n", yytext );
fprintf(yyout,"OPS %s\n",yytext);
}
{Punctuations} {
printf( "A punctuation: %s\n", yytext );
fprintf(yyout,"punctuation %s\n",yytext);
}
"{"[^}\n]*"}"
{Space} printf("Space\n");
. printf( "Unrecognized character: %s\n", yytext );
%%
int main( int argc, char **argv )
{
++argv, --argc; /* skip over program name */
if ( argc > 0 )
yyin = fopen( argv[0], "r" );
else
yyin = stdin;
FILE* fpo=fopen("lexout.txt","w");
yyout=fpo;
yylex();
}