gcc config
[prop.git] / prop-src / parsegen.ph
blob6b1789da98deefab8019f5ea7eaa288f9b7bcd5e
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 //  This file contains the definitions used in the parser generator
4 //  and meta syntax compiler.   
5 //
6 ///////////////////////////////////////////////////////////////////////////////
7 #ifndef parser_generator_h
8 #define parser_generator_h
10 #include <AD/automata/grammar.h>
11 #include "basics.ph"
12 #include "ir.ph"
13 #include "codegen.h"
14 #include "classdef.h"
16 ///////////////////////////////////////////////////////////////////////////////
18 //  Forward declarations
20 ///////////////////////////////////////////////////////////////////////////////
21 class Grammar;      // grammar class
22 class LALR1Gen;     // lalr1 parser class
23 class OpPrecedence; // operator precedence class
25 datatype Exp and Decl
27 ///////////////////////////////////////////////////////////////////////////////
29 //  Parametric grammar expression.
31 ///////////////////////////////////////////////////////////////////////////////
32 and      GramExp : Loc
33                  = EXPgram (List<PrecRule>, ShiftReduceErrors, List<BNF>) 
34                                                    // a grammar expresson
35                  | POLYgram (int, Id [], GramExp)  // polymorphic scheme
36                  | UNIONgram (GramExp, GramExp)    // union
37                  | RESTRICTgram (GramExp)          // restriction
38                  | APPgram (GramExp, GramExp)      // application
40 ///////////////////////////////////////////////////////////////////////////////
42 //  BNF rules
44 ///////////////////////////////////////////////////////////////////////////////
45 and  BNF : Loc = BNFrule (Id, Ty, List<ProductionSymbols>)
47 ///////////////////////////////////////////////////////////////////////////////
49 //  Precedence rules
51 ///////////////////////////////////////////////////////////////////////////////
52 and PrecRule : Loc = PRECrule (PrecMode, int, ProductionSymbols)
54 ///////////////////////////////////////////////////////////////////////////////
56 //  Operator association 
58 ///////////////////////////////////////////////////////////////////////////////
59 and PrecMode = LEFTassoc | RIGHTassoc | NONassoc
61 ///////////////////////////////////////////////////////////////////////////////
63 //  Type abbreviations
65 ///////////////////////////////////////////////////////////////////////////////
66 where type ProductionSymbols = List<ProductionSymbol>
67 and        PrecRules         = List<PrecRule>
68 and        BNFs              = List<BNF>
69 and        GramExps          = List<GramExp>
70 and        ShiftReduceErrors = int
73 ///////////////////////////////////////////////////////////////////////////////
75 //  Pretty printing methods.
77 ///////////////////////////////////////////////////////////////////////////////
78 extern std::ostream& operator << (std::ostream&, ProductionSymbol);
79 extern std::ostream& operator << (std::ostream&, ProductionSymbols);
80 extern std::ostream& operator << (std::ostream&, PrecRule);
81 extern std::ostream& operator << (std::ostream&, PrecRules);
82 extern std::ostream& operator << (std::ostream&, BNF);
83 extern std::ostream& operator << (std::ostream&, BNFs);
84 extern std::ostream& operator << (std::ostream&, GramExp);
85 extern std::ostream& operator << (std::ostream&, GramExps);
87 ///////////////////////////////////////////////////////////////////////////////
89 //  Class to represent a syntax class
91 ///////////////////////////////////////////////////////////////////////////////
92 class SyntaxClass : public ClassDefinition
93 {  SyntaxClass(const SyntaxClass&);
94    void operator = (const SyntaxClass&);
96 public:
98    typedef Grammar::Terminal    Terminal;
99    typedef Grammar::NonTerminal NonTerminal;
100    typedef Grammar::Action      Action;
101    typedef Grammar::Production  Production;
103 protected:
105    ////////////////////////////////////////////////////////////////////////////
106    //
107    //  Internal data for generating a parser
108    //
109    ////////////////////////////////////////////////////////////////////////////
110    BNFs                 production_rules;       // production rules
111    PrecRules            precedence_rules;       // precedence rules
112    Production *         productions;            // products array
113    Grammar *            G;                      // Grammar object
114    LALR1Gen *           parserGen;              // Parser generator object
115    OpPrecedence *       prec;                   // operator precedence object
116    int                  number_of_productions;  // Number of productions
117    HashTable            nonterm_map;            // map nonterm -> Nonterminal
118    HashTable            action_map;             // map Action -> Decls
119    HashTable            inner_action_map;       //
120    HashTable            line_map;               // map Action -> line#
121    HashTable            predicate_map;          // ??
122    Terminal             min_term;               // min Terminal #
123    Terminal             max_term;               // max Terminal #
124    Terminal             error_term;             // error Terminal #
125    NonTerminal          max_nonterm;            // max NonTerminal #
126    NonTerminal          start_symbol;           // start symbol of grammar
127    Action               min_action;             // min Action #
128    Id *                 symbol_names;           // map NonTerminal -> Id
129    Ty *                 ty_map;                 // map NonTerminal -> Ty
132 public:
133    SyntaxClass(CLASS_TYPE, Id, Inherits, TyQual, Decls);
134   ~SyntaxClass();
136    ////////////////////////////////////////////////////////////////////////////
137    //
138    //  Methods to generate a syntax class
139    //
140    ////////////////////////////////////////////////////////////////////////////
141    virtual void gen_parser (CodeGen&, GramExp);
142    virtual void gen_class_interface(CodeGen&);
143    virtual void gen_class_implementation(CodeGen&, Tys, DefKind);
145 private:
146     // compile a set of rules
147    virtual void compile_rules   (CodeGen&, PrecRules, ShiftReduceErrors, BNFs); 
148     // compile a grammar expression
149    virtual void compile_grammar (CodeGen&, GramExp);         
151    // Various phases of the compilation process
152    void initialize();
153    void cleanup();
154    void preprocess_grammar();
155    void translate_into_grammar();
156    void define_operator_precedence();
157    void process_parser_errors(ShiftReduceErrors);
159    // Various phases of the code generation process
160    void generate_semantic_stack_definition(CodeGen&);
161    void generate_semantic_stack_growth(CodeGen&);
162    void generate_semantic_stack_adjustment(CodeGen&);
163    void generate_debugging_tables(CodeGen&);
164    void generate_parse_method(CodeGen&);
165    void generate_parser_tables(CodeGen&);
166    void generate_action_driver(CodeGen&);
167    virtual void generate_semantic_actions(CodeGen&);
169    virtual void gen_class_constructor_initializers(CodeGen&, Tys, DefKind);
172 ///////////////////////////////////////////////////////////////////////////////
174 //  Definition of the parser generator
176 ///////////////////////////////////////////////////////////////////////////////
177 class ParserCompiler : virtual public CodeGen {
179    ParserCompiler(const ParserCompiler&);  // no copy constructor
180    void operator = (const ParserCompiler&); // no assignment
182 public:
183    ParserCompiler();
184   ~ParserCompiler();
186    ////////////////////////////////////////////////////////////////////////////
187    //  Method to generate a syntax class
188    ////////////////////////////////////////////////////////////////////////////
189    virtual void  gen_parser (Id, GramExp);
192 #endif