The Fast Lexical Analyser Generator
Copyright c 1998–2009 by Gerwin Klein
JFlex User’s Manual
Version 1.4.3, January 31, 2009
翻譯了1-5章節 又需要可以在這裏下載
Contents
1 Introduction 3
1.1 Design goals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2 About this manual . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2 Installing and Running JFlex 4
2.1 Installing JFlex . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.1.1 Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.1.2 Unix with tar archive . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.1.3 Linux with RPM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.2 Running JFlex . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
3 A simple Example: How to work with JFlex 6
3.1 Code to include . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
3.2 Options and Macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
3.3 Rules and Actions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
3.4 How to get it going . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
4 Lexical Specifications 12
4.1 User code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
4.2 Options and declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
4.2.1 Class options and user class code . . . . . . . . . . . . . . . . . . . . . . 13
4.2.2 Scanning method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
4.2.3 The end of file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
4.2.4 Standalone scanners . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
4.2.5 CUP compatibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
4.2.6 BYacc/J compatibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
4.2.7 Code generation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
4.2.8 Character sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
4.2.9 Line, character and column counting . . . . . . . . . . . . . . . . . . . . 20
1
4.2.10 Obsolete JLex options . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
4.2.11 State declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
4.2.12 Macro definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
4.3 Lexical rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
4.3.1 Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
4.3.2 Semantics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
4.3.3 How the input is matched . . . . . . . . . . . . . . . . . . . . . . . . . . 27
4.3.4 The generated class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
4.3.5 Scanner methods and fields accessible in actions (API) . . . . . . . . . . 29
5 Encodings, Platforms, and Unicode 31
5.1 The Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
5.2 Scanning text files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
5.3 Scanning binaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
6 A few words on performance 33
6.1 Comparison of JLex and JFlex . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
6.2 How to write a faster specification . . . . . . . . . . . . . . . . . . . . . . . . . 35
7 Porting Issues 37
7.1 Porting from JLex . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
7.2 Porting from lex/flex . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
7.2.1 Basic structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
7.2.2 Macros and Regular Expression Syntax . . . . . . . . . . . . . . . . . . 38
7.2.3 Lexical Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
8 Working together 39
8.1 JFlex and CUP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
8.1.1 CUP version 0.10j and above . . . . . . . . . . . . . . . . . . . . . . . . 39
8.1.2 Custom symbol interface . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
8.1.3 Using existing JFlex/CUP specifications with CUP 0.10j . . . . . . . . . 40
8.1.4 Using older versions of CUP . . . . . . . . . . . . . . . . . . . . . . . . . 41
8.2 JFlex and BYacc/J . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
9 Bugs and Deficiencies 45
9.1 Deficiencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
9.2 Bugs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
10 Copying and License 46