-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmjlexer.flex
133 lines (111 loc) · 4.94 KB
/
mjlexer.flex
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
122
123
124
125
126
127
128
129
130
131
132
133
/*
* Copyright (C) 2018 Danijel Askov
*
* This file is part of MicroJava Compiler.
*
* MicroJava Compiler is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* MicroJava Compiler is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package askov.schoolprojects.compilerconstruction.mjcompiler;
import java_cup.runtime.Symbol;
import askov.schoolprojects.compilerconstruction.mjcompiler.loggers.LexicalErrorMJLogger;
%%
%class MJLexer
%cup
%line
%column
%{
LexicalErrorMJLogger lexicalErrorMJLogger = new LexicalErrorMJLogger();
private Symbol newSymbol(int type) {
return new Symbol(type, yyline + 1, yycolumn + 1);
}
private Symbol newSymbol(int type, Object value) {
return new Symbol(type, yyline + 1, yycolumn + 1, value);
}
%}
%eofval{
return newSymbol(sym.EOF);
%eofval}
LineTerminator = \r|\n|\r\n
InputCharacter = [^\r\n]
Whitespace = {LineTerminator}|[\t\f\b ]
SingleLineComment = "//"{InputCharacter}*{LineTerminator}?
Comment = {SingleLineComment}
DecIntegerLiteral = [0-9]+
PrintableCharLiteral = "'"[ -~]"'"
BooleanLiteral = "true"|"false"
Identifier = [a-zA-Z][a-zA-Z0-9_]*
%%
<YYINITIAL> {
// Whitespace characters (Beline)
{Whitespace} { /* ignore */ }
// Keywords (Ključne reci)
"program" { return newSymbol(sym.PROGRAM); }
"break" { return newSymbol(sym.BREAK); }
"class" { return newSymbol(sym.CLASS); }
"else" { return newSymbol(sym.ELSE); }
"if" { return newSymbol(sym.IF); }
"new" { return newSymbol(sym.NEW); }
"print" { return newSymbol(sym.PRINT); }
"read" { return newSymbol(sym.READ); }
"return" { return newSymbol(sym.RETURN); }
"void" { return newSymbol(sym.VOID); }
"do" { return newSymbol(sym.DO); }
"while" { return newSymbol(sym.WHILE); }
"extends" { return newSymbol(sym.EXTENDS);}
"continue" { return newSymbol(sym.CONTINUE); }
"const" { return newSymbol(sym.CONST); }
// Operators (Operatori)
// Arithmetic (Aritmetički)
"+" { return newSymbol(sym.PLUS); }
"-" { return newSymbol(sym.MINUS); }
"*" { return newSymbol(sym.TIMES); }
"/" { return newSymbol(sym.DIV); }
"%" { return newSymbol(sym.MOD); }
"++" { return newSymbol(sym.INCR); }
"--" { return newSymbol(sym.DECR); }
// Relational (Relacioni)
"==" { return newSymbol(sym.EQ); }
"!=" { return newSymbol(sym.NEQ); }
"<" { return newSymbol(sym.LT); }
"<=" { return newSymbol(sym.LEQ); }
">" { return newSymbol(sym.GT); }
">=" { return newSymbol(sym.GEQ); }
// Logical (Logički)
"&&" { return newSymbol(sym.AND); }
"||" { return newSymbol(sym.OR); }
// Assignment (Dodela vrednosti)
"=" { return newSymbol(sym.ASSIGN); }
// Separators (Separatori)
";" { return newSymbol(sym.SEMI); }
"," { return newSymbol(sym.COMMA); }
"." { return newSymbol(sym.DOT); }
// Delimiters (Delimiteri)
"(" { return newSymbol(sym.LPAREN); } // Parentheses (Oble (male) zagrade).
")" { return newSymbol(sym.RPAREN); }
"[" { return newSymbol(sym.LBRACKET); } // (Square) Brackets (Uglaste (srednje) zagrade).
"]" { return newSymbol(sym.RBRACKET); }
"{" { return newSymbol(sym.LBRACE); } // (Curly) Braces (Vitičaste (velike) zagrade).
"}" { return newSymbol(sym.RBRACE); }
// Comments (Komentari)
{Comment} { /* ignore */ }
// Literals (Literali)
{DecIntegerLiteral} { return newSymbol(sym.INT, new Integer(yytext())); }
{BooleanLiteral} { return newSymbol(sym.BOOL, Boolean.valueOf(yytext())); }
{PrintableCharLiteral} { return newSymbol(sym.CHAR, new Character(yytext().charAt(1))); }
// Identifiers (Identifikatori)
{Identifier} { return newSymbol(sym.IDENT, yytext()); }
// Lexical error (Leksička greška)
// Svi tekst-editori numerisu linije počev od broja 1. Stoga je na yyline dodat broj 1.
[^] { lexicalErrorMJLogger.log(yytext(), yyline + 1, yycolumn + 1); return newSymbol(sym.ERROR); }
}