dfa.h

来自「开放源码的编译器open watcom 1.6.0版的源代码」· C头文件 代码 · 共 183 行

H
183
字号
/****************************************************************************
*
*                            Open Watcom Project
*
*    Portions Copyright (c) 1983-2002 Sybase, Inc. All Rights Reserved.
*
*  ========================================================================
*
*    This file contains Original Code and/or Modifications of Original
*    Code as defined in and that are subject to the Sybase Open Watcom
*    Public License version 1.0 (the 'License'). You may not use this file
*    except in compliance with the License. BY USING THIS FILE YOU AGREE TO
*    ALL TERMS AND CONDITIONS OF THE LICENSE. A copy of the License is
*    provided with the Original Code and Modifications, and is also
*    available at www.sybase.com/developer/opensource.
*
*    The Original Code and all software distributed under the License are
*    distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
*    EXPRESS OR IMPLIED, AND SYBASE AND ALL CONTRIBUTORS HEREBY DISCLAIM
*    ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF
*    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR
*    NON-INFRINGEMENT. Please see the License for the specific language
*    governing rights and limitations under the License.
*
*  ========================================================================
*
* Description:  WHEN YOU FIGURE OUT WHAT THIS FILE DOES, PLEASE
*               DESCRIBE IT HERE!
*
****************************************************************************/


#ifndef _dfa_h
#define _dfa_h

#include <iostream.h>
#include "re.h"

extern void prtCh(ostream&, uchar);
extern void printSpan(ostream&, uint, uint);

class DFA;
class State;

class Action {
public:
    State               *state;
public:
    Action(State*);
    virtual void emit(ostream&) = 0;
};

class Match: public Action {
public:
    Match(State*);
    void emit(ostream&);
};

class Enter: public Action {
public:
    uint                label;
public:
    Enter(State*, uint);
    void emit(ostream&);
};

class Save: public Match {
public:
    uint                selector;
public:
    Save(State*, uint);
    void emit(ostream&);
};

class Move: public Action {
public:
    Move(State*);
    void emit(ostream&);
};

class Accept: public Action {
public:
    uint                nRules;
    uint                *saves;
    State               **rules;
public:
    Accept(State*, uint, uint*, State**);
    void emit(ostream&);
};

class Rule: public Action {
public:
    RuleOp              *rule;
public:
    Rule(State*, RuleOp*);
    void emit(ostream&);
};

class Span {
public:
    uint                ub;
    State               *to;
public:
    uint show(ostream&, uint);
};

class Go {
public:
    uint                nSpans;
    Span                *span;
public:
    void genGoto(ostream&, State*);
    void genBase(ostream&, State*);
    void genLinear(ostream&, State*);
    void genBinary(ostream&, State*);
    void genSwitch(ostream&, State*);
    void compact();
    void unmap(Go*, State*);
};

class State {
public:
    uint                label;
    RuleOp              *rule;
    State               *next;
    State               *link;
    uint                depth;          // for finding SCCs
    uint                kCount;
    Ins                 **kernel;
    bool                isBase:1;
    Go                  go;
    Action              *action;
public:
    State();
    ~State();
    void emit(ostream&);
    friend ostream& operator<<(ostream&, const State&);
    friend ostream& operator<<(ostream&, const State*);
};

class DFA {
public:
    uint                lbChar;
    uint                ubChar;
    uint                nStates;
    State               *head;
    State               **tail;
    State               *toDo;
public:
    DFA(Ins*, uint, uint, uint, Char*);
    ~DFA();
    void addState(State**, State*);
    State *findState(Ins**, uint);
    void split(State*);

    void findSCCs();
    void emit(ostream&);

    friend ostream& operator<<(ostream&, const DFA&);
    friend ostream& operator<<(ostream&, const DFA*);
};

inline Action::Action(State *s) : state(s) {
    s->action = this;
}

inline Match::Match(State *s) : Action(s)
    { }

inline Enter::Enter(State *s, uint l) : Action(s), label(l)
    { }

inline Save::Save(State *s, uint i) : Match(s), selector(i)
    { }

inline ostream& operator<<(ostream &o, const State *s)
    { return o << *s; }

inline ostream& operator<<(ostream &o, const DFA *dfa)
    { return o << *dfa; }

#endif

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?