1 //////////////////////////////////////////////////////////////////////////////
4 // ADLib, Prop and their related set of tools and documentation are in the
5 // public domain. The author(s) of this software reserve no copyrights on
6 // the source code and any code generated using the tools. You are encouraged
7 // to use ADLib and Prop to develop software, in both academic and commercial
8 // settings, and are free to incorporate any part of ADLib and Prop into
11 // Although you are under no obligation to do so, we strongly recommend that
12 // you give away all software developed using our tools.
14 // We also ask that credit be given to us when ADLib and/or Prop are used in
15 // your programs, and that this notice be preserved intact in all the source
18 // This software is still under development and we welcome any suggestions
19 // and help from the users.
23 //////////////////////////////////////////////////////////////////////////////
25 #ifndef lexical_scanner_h
26 #define lexical_scanner_h
28 //////////////////////////////////////////////////////////////////////////////
29 // A standalone, bareboned lexical scanner\cite{Dragon-book}.
30 //////////////////////////////////////////////////////////////////////////////
32 #include <AD/automata/dfatable.h> // dfa tables
34 class Lexer
: public DFATables
{
36 Lexer(const Lexer
&); // no copy constructor
37 void operator = (const Lexer
&); // no assignment
41 ///////////////////////////////////////////////////////////////////////////
43 ///////////////////////////////////////////////////////////////////////////
44 typedef DFATables Super
;
45 typedef Super::Symbol Symbol
; // An input symbol
46 typedef Super::State State
; // A state in the automaton
47 typedef Super::Offset Offset
; // A state in the automaton
48 typedef Super::Rule Rule
; // Rule number of regular expressions
50 ///////////////////////////////////////////////////////////////////////////
51 // The default start state
52 ///////////////////////////////////////////////////////////////////////////
53 enum Lexer_constants
{
54 start_state
= 1 // the start state
59 ///////////////////////////////////////////////////////////////////////////
60 // The lexical scanner tables, compressed. See Aho et al. for details
61 ///////////////////////////////////////////////////////////////////////////
62 const Offset
* const base
;
63 const State
* const check
;
64 const State
* const def
;
65 const State
* const next
;
66 const Rule
* const rule
;
67 const unsigned char * equiv_classes
;
71 ///////////////////////////////////////////////////////////////////////////
73 ///////////////////////////////////////////////////////////////////////////
74 Lexer( const Offset base_table
[],
75 const State check_table
[],
76 const State def_table
[],
77 const State next_table
[],
78 const Rule rule_table
[],
79 const unsigned char equiv_table
[]
81 : base(base_table
), check(check_table
), def(def_table
), next(next_table
),
82 rule(rule_table
), equiv_classes(equiv_table
) {}
84 ///////////////////////////////////////////////////////////////////////////
85 // Advance thru the automaton:
86 // method accept(...) --- see if the state is an accept state.
87 // method go(...) --- advance to a new state.
88 ///////////////////////////////////////////////////////////////////////////
89 inline Rule
accept(State s
) const { return rule
[s
]; } // accept rule of state
90 inline State
go(State s
, Symbol c
) const // advance to the next state
91 { register Offset offset
;
92 register int disp
= equiv_classes
[c
];
93 while (check
[ offset
= base
[s
] + disp
] != s
) {
94 if ((s
= def
[ s
]) == 0) return 0;
96 return next
[ offset
];