oolex -t file
The program oolex is a simple extension of lex to allow for embedding a scanner in a class. It generates a scanner class definition from an ordinary lex specification.
%{
#include <grammar/lex.h>
class item {
static int n;
char* s;
public:
item(char* p) { n = n + 1; s = new char[strlen(p)+1]; strcpy(s,p); }
char* str() { return s; }
int sum() { return n; }
};
inline ostream& operator<<(ostream& c, item& a)
{
c << " (" << a.sum() << ") " << a.str();
return c;
}
%}
\ %class lex
%%
[a-z]+ { item x(yytext); cout << x; }
%%
main() { lex l; l(); }
The scanner class lex makes use of a (pre-defined) class declaration given in <grammar/lex.h> displayed below. Similar declarations may be given by the user.
// file: <grammar/lex.h>
#include <grammar/scanner.h>
class lex : public scanner {
public:
lex(istream& in = cin, ostream& out = cout ) : scanner(in,out) { }
token operator()();
};
For further examples, look in /usr/prac/se/hush/examples/grammar.
|
Hush Online Technology
hush@cs.vu.nl
09/24/99 |
|
|