/* $Id$ */
#include <time.h>
#include <string.h>
-#include <iostream.h>
-#include <iomanip.h>
+#include <iostream>
+#include <iomanip>
#include "globals.h"
#include "parser.h"
return new Symbol(str);
}
-void showIns(ostream &o, const Ins &i, const Ins &base){
+void showIns(std::ostream &o, const Ins &i, const Ins &base){
o.width(3);
o << &i - &base << ": ";
switch(i.i.tag){
;
}
-ostream& operator<<(ostream &o, const Range &r){
+std::ostream& operator<<(std::ostream &o, const Range &r){
if((r.ub - r.lb) == 1){
prtCh(o, r.lb);
} else {
char *MatchOp::type = "MatchOp";
-void MatchOp::display(ostream &o) const{
+void MatchOp::display(std::ostream &o) const{
o << match;
}
ctx->split(s);
}
-extern void printSpan(ostream&, uint, uint);
+extern void printSpan(std::ostream&, uint, uint);
void optimize(Ins *i){
while(!isMarked(i)){
}
}
-void genCode(ostream& o, RegExp *re){
+void genCode(std::ostream& o, RegExp *re){
CharSet cs;
uint j;
memset(&cs, 0, sizeof(cs));
/* $Id$ */
#include <stdlib.h>
#include <string.h>
-#include <iostream.h>
-#include <unistd.h>
+#include <iostream>
#include "scanner.h"
#include "parser.h"
#include "y.tab.h"
#define RETURN(i) {cur = cursor; return i;}
-Scanner::Scanner(int i) : in(i),
+Scanner::Scanner(std::istream& i) : in(i),
bot(NULL), tok(NULL), ptr(NULL), cur(NULL), pos(NULL), lim(NULL),
top(NULL), eof(NULL), tchar(0), tline(0), cline(1) {
;
delete [] bot;
bot = buf;
}
- if((cnt = read(in, (char*) lim, BSIZE)) != BSIZE){
+ if((cnt = in.rdbuf()->sgetn((char*) lim, BSIZE)) != BSIZE){
eof = &lim[cnt]; *eof++ = '\n';
}
lim += cnt;
#line 73 "scanner.re"
-int Scanner::echo(ostream &out){
+int Scanner::echo(std::ostream &out){
char *cursor = cur;
// Catch EOF
goto yy38;
yy38:
#line 160 "scanner.re"
-{ cerr << "unexpected character: " << *tok << endl;
+{ std::cerr << "unexpected character: " << *tok << std::endl;
goto scan;
}
#line 208 "re2c-output.c"
}
void Scanner::fatal(char *msg){
- cerr << "line " << tline << ", column " << (tchar + 1) << ": "
- << msg << endl;
+ std::cerr << "line " << tline << ", column " << (tchar + 1) << ": "
+ << msg << std::endl;
exit(1);
}
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
-#include <iomanip.h>
+#include <iomanip>
+#include <iostream>
#include "substr.h"
#include "globals.h"
#include "dfa.h"
}
}
-void prt(ostream& o, Go *g, State *s)
+void prt(std::ostream& o, Go *g, State *s)
{
Span *b = g->span, *e = &b[g->nSpans];
uint lb = 0;
public:
static BitMap *find(Go*, State*);
static BitMap *find(State*);
- static void gen(ostream&, uint, uint);
+ static void gen(std::ostream&, uint, uint);
static void stats();
BitMap(Go*, State*);
};
return NULL;
}
-void BitMap::gen(ostream &o, uint lb, uint ub)
+void BitMap::gen(std::ostream &o, uint lb, uint ub)
{
BitMap *b = first;
if(b)
{
o << "\n\t"; ++oline;
}
- o << setw(3) << (uint) bm[j] << ", ";
+ o << std::setw(3) << (uint) bm[j] << ", ";
}
}
o << "\n\t};\n";
uint n = 0;
for(BitMap *b = first; b; b = b->next)
{
- prt(cerr, b->go, b->on); cerr << endl;
+ prt(std::cerr, b->go, b->on); std::cerr << std::endl;
++n;
}
- cerr << n << " bitmaps\n";
+ std::cerr << n << " bitmaps\n";
first = NULL;
}
-void genGoTo(ostream &o, State *from, State *to, bool & readCh)
+void genGoTo(std::ostream &o, State *from, State *to, bool & readCh)
{
if (readCh && from->label + 1 != to->label)
{
++oline;
}
-void genIf(ostream &o, char *cmp, uint v, bool &readCh)
+void genIf(std::ostream &o, char *cmp, uint v, bool &readCh)
{
if (readCh)
{
o << "')";
}
-void indent(ostream &o, uint i)
+void indent(std::ostream &o, uint i)
{
while(i-- > 0)
{
}
}
-static void need(ostream &o, uint n, bool & readCh)
+static void need(std::ostream &o, uint n, bool & readCh)
{
if(n == 1)
{
++oline;
}
-void Match::emit(ostream &o, bool &readCh)
+void Match::emit(std::ostream &o, bool &readCh)
{
if (state->link)
{
}
}
-void Enter::emit(ostream &o, bool &readCh)
+void Enter::emit(std::ostream &o, bool &readCh)
{
if(state->link){
o << "\t++YYCURSOR;\n";
}
}
-void Save::emit(ostream &o, bool &readCh)
+void Save::emit(std::ostream &o, bool &readCh)
{
o << "\tyyaccept = " << selector << ";\n";
++oline;
;
}
-void Move::emit(ostream &o, bool &readCh){
+void Move::emit(std::ostream &o, bool &readCh){
;
}
;
}
-void Accept::emit(ostream &o, bool &readCh)
+void Accept::emit(std::ostream &o, bool &readCh)
{
bool first = true;
for(uint i = 0; i < nRules; ++i)
;
}
-void Rule::emit(ostream &o, bool &readCh)
+void Rule::emit(std::ostream &o, bool &readCh)
{
uint back = rule->ctx->fixedLength();
if(back != ~0u && back > 0u) {
// << "\n\t" << rule->code->text << "\n";
}
-void doLinear(ostream &o, uint i, Span *s, uint n, State *from, State *next, bool &readCh)
+void doLinear(std::ostream &o, uint i, Span *s, uint n, State *from, State *next, bool &readCh)
{
for(;;)
{
indent(o, i); genGoTo(o, from, next, readCh);
}
-void Go::genLinear(ostream &o, State *from, State *next, bool &readCh)
+void Go::genLinear(std::ostream &o, State *from, State *next, bool &readCh)
{
doLinear(o, 0, span, nSpans, from, next, readCh);
}
-void genCases(ostream &o, uint lb, Span *s)
+void genCases(std::ostream &o, uint lb, Span *s)
{
if(lb < s->ub)
{
}
}
-void Go::genSwitch(ostream &o, State *from, State *next, bool &readCh)
+void Go::genSwitch(std::ostream &o, State *from, State *next, bool &readCh)
{
if(nSpans <= 2){
genLinear(o, from, next, readCh);
}
}
-void doBinary(ostream &o, uint i, Span *s, uint n, State *from, State *next, bool &readCh)
+void doBinary(std::ostream &o, uint i, Span *s, uint n, State *from, State *next, bool &readCh)
{
if(n <= 4)
{
}
}
-void Go::genBinary(ostream &o, State *from, State *next, bool &readCh)
+void Go::genBinary(std::ostream &o, State *from, State *next, bool &readCh)
{
doBinary(o, 0, span, nSpans, from, next, readCh);
}
-void Go::genBase(ostream &o, State *from, State *next, bool &readCh)
+void Go::genBase(std::ostream &o, State *from, State *next, bool &readCh)
{
if(nSpans == 0)
{
}
}
-void Go::genGoto(ostream &o, State *from, State *next, bool &readCh)
+void Go::genGoto(std::ostream &o, State *from, State *next, bool &readCh)
{
if(bFlag)
{
genBase(o, from, next, readCh);
}
-void State::emit(ostream &o, bool &readCh){
+void State::emit(std::ostream &o, bool &readCh){
o << "yy" << label << ":";
/* o << "\nfprintf(stderr, \"<" << label << ">\");\n";*/
action->emit(o, readCh);
s->go.span[0].to = move;
}
-void DFA::emit(ostream &o)
+void DFA::emit(std::ostream &o)
{
static uint label = 0;
State *s;
return '0' + c%8;
}
-void prtCh(ostream &o, uchar c){
+void prtCh(std::ostream &o, uchar c){
uchar oc = talx[c];
switch(oc){
case '\'': o << "\\'"; break;
}
}
-void printSpan(ostream &o, uint lb, uint ub){
+void printSpan(std::ostream &o, uint lb, uint ub){
if(lb > ub)
o << "*";
o << "[";
o << "]";
}
-uint Span::show(ostream &o, uint lb){
+uint Span::show(std::ostream &o, uint lb){
if(to){
printSpan(o, lb, ub);
o << " " << to->label << "; ";
return ub;
}
-ostream& operator<<(ostream &o, const State &s){
+std::ostream& operator<<(std::ostream &o, const State &s){
o << "state " << s.label;
if(s.rule)
o << " accepts " << s.rule->accept;
return o;
}
-ostream& operator<<(ostream &o, const DFA &dfa){
+std::ostream& operator<<(std::ostream &o, const DFA &dfa){
for(State *s = dfa.head; s; s = s->next)
{
o << s << "\n\n";
#ifndef _dfa_h
#define _dfa_h
-#include <iostream.h>
+#include <iosfwd>
#include "re.h"
-extern void prtCh(ostream&, uchar);
-extern void printSpan(ostream&, uint, uint);
+extern void prtCh(std::ostream&, uchar);
+extern void printSpan(std::ostream&, uint, uint);
class DFA;
class State;
State *state;
public:
Action(State*);
- virtual void emit(ostream&, bool&) = 0;
+ virtual void emit(std::ostream&, bool&) = 0;
virtual bool isRule() const;
virtual bool isMatch() const;
virtual bool readAhead() const;
class Match: public Action {
public:
Match(State*);
- void emit(ostream&, bool&);
+ void emit(std::ostream&, bool&);
bool isMatch() const;
};
uint label;
public:
Enter(State*, uint);
- void emit(ostream&, bool&);
+ void emit(std::ostream&, bool&);
};
class Save: public Match {
uint selector;
public:
Save(State*, uint);
- void emit(ostream&, bool&);
+ void emit(std::ostream&, bool&);
bool isMatch() const;
};
class Move: public Action {
public:
Move(State*);
- void emit(ostream&, bool&);
+ void emit(std::ostream&, bool&);
};
class Accept: public Action {
State **rules;
public:
Accept(State*, uint, uint*, State**);
- void emit(ostream&, bool&);
+ void emit(std::ostream&, bool&);
};
class Rule: public Action {
RuleOp *rule;
public:
Rule(State*, RuleOp*);
- void emit(ostream&, bool&);
+ void emit(std::ostream&, bool&);
bool isRule() const;
};
uint ub;
State *to;
public:
- uint show(ostream&, uint);
+ uint show(std::ostream&, uint);
};
class Go {
uint nSpans;
Span *span;
public:
- void genGoto(ostream&, State *from, State*, bool &readCh);
- void genBase(ostream&, State *from, State*, bool &readCh);
- void genLinear(ostream&, State *from, State*, bool &readCh);
- void genBinary(ostream&, State *from, State*, bool &readCh);
- void genSwitch(ostream&, State *from, State*, bool &readCh);
+ void genGoto(std::ostream&, State *from, State*, bool &readCh);
+ void genBase(std::ostream&, State *from, State*, bool &readCh);
+ void genLinear(std::ostream&, State *from, State*, bool &readCh);
+ void genBinary(std::ostream&, State *from, State*, bool &readCh);
+ void genSwitch(std::ostream&, State *from, State*, bool &readCh);
void compact();
void unmap(Go*, State*);
};
public:
State();
~State();
- void emit(ostream&, bool&);
- friend ostream& operator<<(ostream&, const State&);
- friend ostream& operator<<(ostream&, const State*);
+ void emit(std::ostream&, bool&);
+ friend std::ostream& operator<<(std::ostream&, const State&);
+ friend std::ostream& operator<<(std::ostream&, const State*);
};
class DFA {
void split(State*);
void findSCCs();
- void emit(ostream&);
+ void emit(std::ostream&);
- friend ostream& operator<<(ostream&, const DFA&);
- friend ostream& operator<<(ostream&, const DFA*);
+ friend std::ostream& operator<<(std::ostream&, const DFA&);
+ friend std::ostream& operator<<(std::ostream&, const DFA*);
};
inline Action::Action(State *s) : state(s) {
inline bool Rule::isRule() const
{ return true; }
-inline ostream& operator<<(ostream &o, const State *s)
+inline std::ostream& operator<<(std::ostream &o, const State *s)
{ return o << *s; }
-inline ostream& operator<<(ostream &o, const DFA *dfa)
+inline std::ostream& operator<<(std::ostream &o, const DFA *dfa)
{ return o << *dfa; }
#endif
#ifndef _ins_h
#define _ins_h
-#include <iostream.h>
#include "basics.h"
const uint nChars = 256;
#endif
#include <fstream>
+#include <iostream>
#include <stdlib.h>
-#include <fcntl.h>
-#include <unistd.h>
#include <string.h>
#include "globals.h"
{'v', 0, "version"}
};
+static void usage()
+{
+ cerr << "usage: re2c [-esbvh] file\n"
+ "\n"
+ "-? -h --help Display this info.\n"
+ "\n"
+ "-b --bit-vectors Implies -s. Use bit vectors as well in the attempt to\n"
+ " coax better code out of the compiler. Most useful for\n"
+ " specifications with more than a few keywords (e.g. for\n"
+ " most programming languages).\n"
+ "\n"
+ "-e --ecb Cross-compile from an ASCII platform to\n"
+ " an EBCDIC one.\n"
+ "\n"
+ "-s --nested-ifs Generate nested ifs for some switches. Many compilers\n"
+ " need this assist to generate better code.\n"
+ "\n"
+ "-v --version Show version information.\n";
+}
+
int main(int argc, char *argv[])
{
int c;
- fileName = NULL;
+ fileName = NULL;
- if (argc == 1) {
- goto usage;
+ if (argc == 1) {
+ usage();
+ return 2;
}
while ((c = mbo_getopt(argc, argv, OPTIONS, &opt_arg, &opt_ind, 0))!=-1) {
switch (c) {
- case '?':
- goto usage;
- case 'b':
- sFlag = true;
- bFlag = true;
- break;
- case 'e':
- xlat = asc2ebc;
- talx = ebc2asc;
- break;
- case 'h':
- goto usage;
- case 's':
- sFlag = true;
- break;
- case 'v':
- goto version;
- default:
- goto usage;
+ case 'b':
+ sFlag = true;
+ bFlag = true;
+ break;
+ case 'e':
+ xlat = asc2ebc;
+ talx = ebc2asc;
+ break;
+ case 's':
+ sFlag = true;
+ break;
+ case 'v':
+ cerr << "re2c " << PACKAGE_VERSION << "\n";
+ return 2;
+ case 'h':
+ case '?':
+ default:
+ usage();
+ return 2;
}
}
}
else
{
- goto usage;
+ usage();
+ return 2;
}
- int fd;
- if (fileName[0] == '-' && fileName[1] == '\0')
- {
+ istream* input = 0;
+ ifstream inputFile;
+ if (fileName[0] == '-' && fileName[1] == '\0')
+ {
fileName = "<stdin>";
- fd = 0;
- }
- else
- {
- if ((fd = open(fileName, O_RDONLY)) < 0)
+ input = &cin;
+ }
+ else
+ {
+ inputFile.open(fileName);
+ if (!inputFile)
{
- cerr << "can't open " << fileName << "\n";
- return 1;
+ cerr << "can't open " << fileName << "\n";
+ return 1;
}
- }
- parse(fd, cout);
- return 0;
-
-usage:
- cerr << "usage: re2c [-esbvh] file\n"
- "\n"
- "-? -h --help Display this info.\n"
- "\n"
- "-b --bit-vectors Implies -s. Use bit vectors as well in the attempt to\n"
- " coax better code out of the compiler. Most useful for\n"
- " specifications with more than a few keywords (e.g. for\n"
- " most programming languages).\n"
- "\n"
- "-e --ecb Cross-compile from an ASCII platform to\n"
- " an EBCDIC one.\n"
- "\n"
- "-s --nested-ifs Generate nested ifs for some switches. Many compilers\n"
- " need this assist to generate better code.\n"
- "\n"
- "-v --version Show version information.\n";
- return 2;
+ input = &inputFile;
+ }
+ parse(*input, cout);
+ return 0;
-version:
- cerr << "re2c " << PACKAGE_VERSION << "\n";
- return 2;
}
#include "scanner.h"
#include "re.h"
+#include <iosfwd>
class Symbol {
public:
static Symbol *find(const SubStr&);
};
-void line_source(unsigned int, ostream&);
-void parse(int, ostream&);
+void line_source(unsigned int, std::ostream&);
+void parse(std::istream&, std::ostream&);
#endif
}
} // end extern "C"
-void line_source(unsigned int line, ostream& o)
+void line_source(unsigned int line, std::ostream& o)
{
char * fnamebuf;
char * token;
free( fnamebuf );
}
-void parse(int i, ostream &o){
+void parse(std::istream& i, std::ostream &o){
o << "/* Generated by re2c " PACKAGE_VERSION " on ";
time_t now = time(&now);
#ifndef _re_h
#define _re_h
-#include <iostream.h>
+#include <iostream>
#include "token.h"
#include "ins.h"
{ }
Range(Range &r) : next(NULL), lb(r.lb), ub(r.ub)
{ }
- friend ostream& operator<<(ostream&, const Range&);
- friend ostream& operator<<(ostream&, const Range*);
+ friend std::ostream& operator<<(std::ostream&, const Range&);
+ friend std::ostream& operator<<(std::ostream&, const Range*);
};
-inline ostream& operator<<(ostream &o, const Range *r){
+inline std::ostream& operator<<(std::ostream &o, const Range *r){
return r? o << *r : o;
}
virtual void calcSize(Char*) = 0;
virtual uint fixedLength();
virtual void compile(Char*, Ins*) = 0;
- virtual void display(ostream&) const = 0;
- friend ostream& operator<<(ostream&, const RegExp&);
- friend ostream& operator<<(ostream&, const RegExp*);
+ virtual void display(std::ostream&) const = 0;
+ friend std::ostream& operator<<(std::ostream&, const RegExp&);
+ friend std::ostream& operator<<(std::ostream&, const RegExp*);
};
-inline ostream& operator<<(ostream &o, const RegExp &re){
+inline std::ostream& operator<<(std::ostream &o, const RegExp &re){
re.display(o);
return o;
}
-inline ostream& operator<<(ostream &o, const RegExp *re){
+inline std::ostream& operator<<(std::ostream &o, const RegExp *re){
return o << *re;
}
void calcSize(Char*);
uint fixedLength();
void compile(Char*, Ins*);
- void display(ostream &o) const {
+ void display(std::ostream &o) const {
o << "_";
}
};
void calcSize(Char*);
uint fixedLength();
void compile(Char*, Ins*);
- void display(ostream&) const;
+ void display(std::ostream&) const;
};
class RuleOp: public RegExp {
void split(CharSet&);
void calcSize(Char*);
void compile(Char*, Ins*);
- void display(ostream &o) const {
+ void display(std::ostream &o) const {
o << exp << "/" << ctx << ";";
}
};
void calcSize(Char*);
uint fixedLength();
void compile(Char*, Ins*);
- void display(ostream &o) const {
+ void display(std::ostream &o) const {
o << exp1 << "|" << exp2;
}
friend RegExp *mkAlt(RegExp*, RegExp*);
void calcSize(Char*);
uint fixedLength();
void compile(Char*, Ins*);
- void display(ostream &o) const {
+ void display(std::ostream &o) const {
o << exp1 << exp2;
}
};
void split(CharSet&);
void calcSize(Char*);
void compile(Char*, Ins*);
- void display(ostream &o) const {
+ void display(std::ostream &o) const {
o << exp << "+";
}
};
void split(CharSet&);
void calcSize(Char*);
void compile(Char*, Ins*);
- void display(ostream &o) const {
+ void display(std::ostream &o) const {
o << exp << "+";
}
};
-extern void genCode(ostream&, RegExp*);
+extern void genCode(std::ostream&, RegExp*);
extern RegExp *mkDiff(RegExp*, RegExp*);
extern RegExp *strToRE(SubStr);
extern RegExp *ranToRE(SubStr);
#ifndef _scanner_h
#define _scanner_h
+#include <iosfwd>
#include "token.h"
class Scanner {
private:
- int in;
+ std::istream& in;
char *bot, *tok, *ptr, *cur, *pos, *lim, *top, *eof;
uint tchar, tline, cline;
private:
char *fill(char*);
+ Scanner(const Scanner&); //unimplemented
+ Scanner& operator=(const Scanner&); //unimplemented
public:
- Scanner(int);
- int echo(ostream&);
+ Scanner(std::istream&);
+ int echo(std::ostream&);
int scan();
void fatal(char*);
SubStr token();
/* $Id$ */
#include <stdlib.h>
#include <string.h>
-#include <iostream.h>
-#include <unistd.h>
+#include <iostream>
#include "scanner.h"
#include "parser.h"
#include "y.tab.h"
#define RETURN(i) {cur = cursor; return i;}
-Scanner::Scanner(int i) : in(i),
+Scanner::Scanner(std::istream& i) : in(i),
bot(NULL), tok(NULL), ptr(NULL), cur(NULL), pos(NULL), lim(NULL),
top(NULL), eof(NULL), tchar(0), tline(0), cline(1) {
;
delete [] bot;
bot = buf;
}
- if((cnt = read(in, (char*) lim, BSIZE)) != BSIZE){
+ if((cnt = in.rdbuf()->sgetn((char*) lim, BSIZE)) != BSIZE){
eof = &lim[cnt]; *eof++ = '\n';
}
lim += cnt;
digit = [0-9];
*/
-int Scanner::echo(ostream &out){
+int Scanner::echo(std::ostream &out){
char *cursor = cur;
// Catch EOF
goto scan;
}
- any { cerr << "unexpected character: " << *tok << endl;
+ any { std::cerr << "unexpected character: " << *tok << std::endl;
goto scan;
}
*/
}
void Scanner::fatal(char *msg){
- cerr << "line " << tline << ", column " << (tchar + 1) << ": "
- << msg << endl;
+ std::cerr << "line " << tline << ", column " << (tchar + 1) << ": "
+ << msg << std::endl;
exit(1);
}
#include "substr.h"
#include "globals.h"
-void SubStr::out(ostream& o) const {
+void SubStr::out(std::ostream& o) const {
o.write(str, len);
for (size_t i = 0; i < (size_t)len; ++i)
{
#ifndef _substr_h
#define _substr_h
-#include <iostream.h>
+#include <iostream>
#include "basics.h"
class SubStr {
SubStr(uchar*, uint);
SubStr(char*, uint);
SubStr(const SubStr&);
- void out(ostream&) const;
+ void out(std::ostream&) const;
};
class Str: public SubStr {
~Str();
};
-inline ostream& operator<<(ostream& o, const SubStr &s){
+inline std::ostream& operator<<(std::ostream& o, const SubStr &s){
s.out(o);
return o;
}
-inline ostream& operator<<(ostream& o, const SubStr* s){
+inline std::ostream& operator<<(std::ostream& o, const SubStr* s){
return o << *s;
}