#ifndef LR_TABLE_H #define LR_TABLE_H #include "md5.h" class PGF_INTERNAL_DECL PgfLRTableMaker { struct CCat; struct Production; struct Item; struct State; struct CompareItem; static const CompareItem compare_item; typedef std::pair,size_t> Key0; struct PGF_INTERNAL_DECL CompareKey0 : std::less { bool operator() (const Key0& k1, const Key0& k2) const { int cmp = textcmp(k1.first,k2.first); if (cmp < 0) return true; else if (cmp > 0) return false; return (k1.second < k2.second); } }; typedef std::pair,size_t> Key1; struct PGF_INTERNAL_DECL CompareKey1 : std::less { bool operator() (const Key1& k1, const Key1& k2) const { if (k1.first < k2.first) return true; else if (k1.first > k2.first) return false; return (k1.second < k2.second); } }; typedef std::pair Key2; struct PGF_INTERNAL_DECL CompareKey2 : std::less { bool operator() (const Key2& k1, const Key2& k2) const { if (k1.first < k2.first) return true; else if (k1.first > k2.first) return false; return (k1.second < k2.second); } }; ref abstr; ref concr; size_t ccat_id; std::queue todo; std::map states; std::map ccats1; std::map ccats2; // The Threefold Way of building an automaton typedef enum { INIT, PROBE, REPEAT } Fold; void process(State *state, Fold fold, Item *item); void symbol(State *state, Fold fold, Item *item, PgfSymbol sym); template void predict(State *state, Fold fold, Item *item, T cat, bool exact, ref> vars, PgfLParam *r); void predict(State *state, Fold fold, Item *item, ref cat, bool exact, size_t lin_idx); void predict(State *state, Fold fold, Item *item, CCat *ccat, bool exact, size_t lin_idx); void predict(ref absfun, CCat *ccat); void complete(State *state, Fold fold, Item *item); void print_production(CCat *ccat, Production *prod); void print_item(Item *item); public: PgfLRTableMaker(ref abstr, ref concr); ref make(); ~PgfLRTableMaker(); }; class PgfPrinter; class PGF_INTERNAL_DECL PgfParser : public PgfPhraseScanner, public PgfExprEnum { ref concr; PgfText *sentence; PgfMarshaller *m; PgfUnmarshaller *u; struct Choice; struct Production; struct StackNode; struct Stage; struct ExprState; struct ExprInstance; struct CompareExprState : std::less { bool operator() (const ExprState *state1, const ExprState *state2) const; }; Stage *before, *after, *ahead; std::priority_queue, CompareExprState> queue; int last_fid; Choice *top_choice; size_t top_choice_index; void shift(StackNode *parent, ref lincat, size_t r, Production *prod, Stage *before, Stage *after); void reduce(StackNode *parent, ref lin, ref red, size_t n, std::vector &args, Stage *before, Stage *after); void complete(StackNode *parent, ref lincat, size_t seq_index, size_t n, std::vector &args); void reduce_all(StackNode *state); void print_prod(Choice *choice, Production *prod); void print_transition(StackNode *source, StackNode *target, Stage *stage); typedef std::map,Choice*> intersection_map; Choice *intersect_choice(Choice *choice1, Choice *choice2, intersection_map &im); void print_expr_state_before(PgfPrinter *printer, ExprState *state); void print_expr_state_after(PgfPrinter *printer, ExprState *state); void print_expr_state(ExprState *state); void predict_expr_states(Choice *choice, prob_t outside_prob); bool process_expr_state(ExprState *state); void complete_expr_state(ExprState *state); void combine_expr_state(ExprState *state, ExprInstance &inst); void release_expr_state(ExprState *state); public: PgfParser(ref concr, ref start, PgfText *sentence, PgfMarshaller *m, PgfUnmarshaller *u); virtual void space(PgfTextSpot *start, PgfTextSpot *end, PgfExn* err); virtual void start_matches(PgfTextSpot *end, PgfExn* err); virtual void match(ref lin, size_t seq_index, PgfExn* err); virtual void end_matches(PgfTextSpot *end, PgfExn* err); void prepare(); PgfExpr fetch(PgfDB *db, prob_t *prob); }; #endif