-
Notifications
You must be signed in to change notification settings - Fork 1
/
cezinho-scanner.l
103 lines (82 loc) · 2.44 KB
/
cezinho-scanner.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
%{
/*
*
* Created by Murilo Adriano Vasconcelos || Matrícula : 094816
* & Paulo Cezar Pereira Costa || Matrícula : 080153
*
* Copyright 2011 Universidade Federal de Goias. All rights reserved.
*
*/
#include <cstdio>
#include <cstdlib>
#include <string>
#include <cstring>
#include "ast/ast.h"
#include "parser.hpp"
void yyerror(const char *);
char invalidchar[30] = "CARACTER INVALIDO: ";
std::string buf;
#define TOKEN(tk) tk
%}
%x STRING COMMENT RCHAR ESCP ENDCHAR
DELIM [ \t\r]
WS {DELIM}+
DIGIT [0-9]
LETTER [A-Za-z_]
%%
{WS} {;}
\n {yylineno++;}
"main" { yylval.lxval = new std::string(yytext); return MAIN; }
"int" { return INT; }
"char" { return CHAR; }
"return" { return RETURN; }
"read" { return READ; }
"write" { return WRITE; }
"writeln" { return WRITELN; }
"break" { return BREAK; }
"if" { return IF; }
"else" { return ELSE; }
"while" { return WHILE; }
[-!(){}<>+*/=;,] { return *yytext; }
"[" { return *yytext; }
"]" { return *yytext; }
"==" { return EQUAL; }
"!=" { return NEQUAL; }
"<=" { return LESSEQ; }
">=" { return GREATEREQ; }
"||" { return OR; }
"&&" { return AND; }
{LETTER}({LETTER}|{DIGIT})* { yylval.lxval = new std::string(yytext); return ID; }
{DIGIT}+ { yylval.lxval = new std::string(yytext); return LITINT; }
' { BEGIN RCHAR; }
<RCHAR>\\ { BEGIN ESCP; }
<RCHAR>. { buf = ""; buf += *yytext; BEGIN ENDCHAR; }
<ESCP>{
[0ntarbf\\'] { buf = "\\"; buf += *yytext; BEGIN ENDCHAR; }
}
<ESCP>. { yyerror( "FALTANDO DELIMITADOR ' EM CONSTANTE TIPO char." ); }
<ENDCHAR>' { BEGIN INITIAL; yylval.lxval = new std::string( buf ); return LITCHAR; }
<ENDCHAR>. { yyerror( "FALTANDO DELIMITADOR ' EM CONSTANTE TIPO char." ); };
\" { BEGIN STRING; buf = ""; }
<STRING>\" { BEGIN INITIAL; yylval.lxval = new std::string( buf ); return LITSTRING; }
<STRING>\n { yylineno++; yyerror("CADEIA DE CARACTERES OCUPA MAIS DE UMA LINHA"); }
<STRING>{
"\\n" { buf += '\n'; }
"\\t" { buf += '\t'; }
"\\a" { buf += '\a'; }
"\\r" { buf += '\r'; }
"\\b" { buf += '\b'; }
"\\f" { buf += '\f'; }
"\\\"" { buf += '\\'; }
}
<STRING><<EOF>> { yyerror("STRING NAO TERMINA."); }
<STRING>. { buf += *yytext; }
"/*" { BEGIN COMMENT; }
<COMMENT>"*"+"/" { BEGIN INITIAL; }
<COMMENT>"*"+[^*/\n] {;}
<COMMENT>\n { yylineno++; }
<COMMENT>[^*\n] {;}
<COMMENT><<EOF>> { yyerror("COMENTARIO NAO TERMINA."); }
. { yyerror( strcat( invalidchar, yytext ) ); }
%%
int yywrap(){ return 1; }