-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscanner.flex
99 lines (81 loc) · 2.97 KB
/
scanner.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
/* JFlex example: part of Java language lexer specification */
import java_cup.runtime.*;
/**
%%
/* -----------------Options and Declarations Section----------------- */
/*
The name of the class JFlex will create will be Lexer.
Will write the code to the file Lexer.java.
*/
%class Scanner
/*
The current line number can be accessed with the variable yyline
and the current column number with the variable yycolumn.
*/
%line
%column
/*
Will switch to a CUP compatibility mode to interface with a CUP
generated parser.
*/
%cup
/*
Declarations
Code between %{ and %}, both of which must be at the beginning of a
line, will be copied letter to letter into the lexer class source.
Here you declare member variables and functions that are used inside
scanner actions.
*/
%{
StringBuffer stringBuffer = new StringBuffer();
private Symbol symbol(int type) {
return new Symbol(type, yyline, yycolumn);
}
private Symbol symbol(int type, Object value) {
return new Symbol(type, yyline, yycolumn, value);
}
%}
/*
Macro Declarations
These declarations are regular expressions that will be used latter
in the Lexical Rules Section.
*/
Ident = [a-zA-Z$_] [a-zA-Z0-9$_]*
/* A line terminator is a \r (carriage return), \n (line feed), or
\r\n. */
LineTerminator = \r|\n|\r\n
/* White space is a line terminator, space, tab, or line feed. */
WhiteSpace = {LineTerminator} | [ \t\f]
%state STRING
%%
/* ------------------------Lexical Rules Section---------------------- */
<YYINITIAL> {
/* keywords */
"if" { return symbol(sym.IF); }
"else" { return symbol(sym.ELSE); }
"reverse" { return symbol(sym.REVERSE); }
"prefix" { return symbol(sym.PREFIX); }
/* operators */
"(" { return symbol(sym.LPAREN); }
")" { return symbol(sym.RPAREN); }
"{" { return symbol(sym.LCURLBRACKET); }
"}" { return symbol(sym.RCURLBRACKET); }
"," { return symbol(sym.COMMA); }
"+" { return symbol(sym.PLUS); }
\" { stringBuffer.setLength(0); yybegin(STRING); }
{Ident} { return symbol(sym.IDENT, yytext()); }
{WhiteSpace} { /* just skip what was found, do nothing */ }
}
<STRING> {
\" { yybegin(YYINITIAL);
return symbol(sym.STRING_LITERAL, stringBuffer.toString()); }
[^\n\r\"\\]+ { stringBuffer.append( yytext() ); }
\\t { stringBuffer.append('\t'); }
\\n { stringBuffer.append('\n'); }
\\r { stringBuffer.append('\r'); }
\\\" { stringBuffer.append('\"'); }
\\ { stringBuffer.append('\\'); }
}
/* No token was found for the input so through an error. Print out an
Illegal character message with the illegal character that was found. */
[^] { throw new Error("Illegal character <"+yytext()+">"); }