-
Notifications
You must be signed in to change notification settings - Fork 0
/
trabalho123.lex
76 lines (53 loc) · 2.3 KB
/
trabalho123.lex
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
%{
char* troca_aspas( char* lexema );
%}
DELIM [\t ]
LINHA [\n]
NUMERO [0-9]
LETRA [A-Za-z_]
INT {NUMERO}+
DOUBLE {NUMERO}+("."{NUMERO}+)?
ID {LETRA}({LETRA}|{NUMERO})*
CSTRING """([^\n']|"''")*"""
COMMENT "(*"([^*]|"*"[^)])*"*)"
%%
{LINHA} { nlinha++; }
{DELIM} {}
{COMMENT} {}
"int" { yylval = Atributos( yytext ); return TK_INT; }
"double" { yylval = Atributos( yytext ); return TK_DOUBLE; }
"char" { yylval = Atributos( yytext ); return TK_CHAR; }
"boolean" { yylval = Atributos( yytext ); return TK_BOOL; }
"string" { yylval = Atributos( yytext ); return TK_STRING; }
"imprimi" { yylval = Atributos( yytext ); return TK_PRINT; }
"si" { yylval = Atributos( yytext ); return TK_IF; }
"sinao" { yylval = Atributos( yytext ); return TK_ELSE; }
"para" { yylval = Atributos( yytext ); return TK_FOR; }
"facanto" { yylval = Atributos( yytext ); return TK_DO;}
"interruptor" { yylval = Atributos( yytext ); return TK_SWITCH; }
"enquanto" { yylval = Atributos( yytext ); return TK_WHILE; }
"funcao" { yylval = Atributos( yytext ); return TK_FUNCTION; }
"le" { yylval = Atributos( yytext ); return TK_INPUT; }
"fim" { yylval = Atributos( yytext ); return TK_END; }
"principal" { yylval = Atributos( yytext ); return TK_MAIN; }
"<=" { yylval = Atributos( yytext ); return TK_MEIG; }
"==" { yylval = Atributos( yytext ); return TK_IG; }
">=" { yylval = Atributos( yytext ); return TK_MAIG; }
"!=" { yylval = Atributos( yytext ); return TK_DIF; }
"&&" { yylval = Atributos( yytext ); return TK_AND; }
"++" { yylval = Atributos( yytext ); return TK_PLUSPLUS; }
"--" { yylval = Atributos( yytext ); return TK_MINUSMINUS; }
"||" { yylval = Atributos( yytext ); return TK_OR; }
{CSTRING} { yylval = Atributos( troca_aspas( yytext ), Tipo( "string" ) );
return TK_CSTRING; }
{ID} { yylval = Atributos( yytext ); return TK_ID; }
{INT} { yylval = Atributos( yytext, Tipo( "int" ) ); return TK_CINT; }
{DOUBLE} { yylval = Atributos( yytext, Tipo( "double" ) ); return TK_CDOUBLE; }
. { yylval = Atributos( yytext ); return *yytext; }
%%
char* troca_aspas( char* lexema ) {
int n = strlen( lexema );
lexema[0] = '"';
lexema[n-1] = '"';
return lexema;
}