hobbes
a language, embedded compiler, and runtime for efficient dynamic expression evaluation, data storage and analysis
parser.H
Go to the documentation of this file.
1 /*
2  * parser : a module for deriving code to parse context-free grammars
3  */
4 
5 #ifndef HOBBES_PARSE_PARSER_HPP_INCLUDED
6 #define HOBBES_PARSE_PARSER_HPP_INCLUDED
7 
9 #include <hobbes/lang/expr.H>
11 #include <map>
12 #include <vector>
13 
14 namespace hobbes {
15 
16 // a parser rule looks like:
17 // S -> x0:C0 ... xn:Cn { R(x0..xn) }
18 // where:
19 // S is the name of a non-terminal symbol
20 // xi is a variable name fresh for this rule
21 // Ci is the name of a terminal or non-terminal symbol
22 // R is an arbitrary user expression that may involve values bound by the xi
23 // such that:
24 // the type of R matches the type of R for all other definitions of S
25 struct ParseRule {
26  typedef std::pair<std::string, terminal*> Binding;
27  typedef std::vector<Binding> Bindings;
28 
29  inline ParseRule(terminal* s, const Bindings& bs, const ExprPtr& r) : symbol(s), bindings(bs), reducer(r) { }
30 
32  Bindings bindings;
34 };
35 
36 // and a parser is a set of such rules
37 typedef std::vector<ParseRule> Parser;
38 
39 // consume a grammar to produce a parser for that grammar
40 class cc;
41 
42 ExprPtr makeParser(cc*, const Parser&, const precedence& prec, const LexicalAnnotation&);
43 ExprPtr makeParser(cc*, const Parser&, terminal* root, const precedence& prec, const LexicalAnnotation&);
44 
45 // show a parser (for convenient debugging)
46 void show(std::ostream&, const Parser&);
47 
48 }
49 
50 #endif
51 
Bindings bindings
Definition: parser.H:32
ExprPtr makeParser(cc *, const Grammar &, const LexicalAnnotation &)
Definition: grammar.C:49
std::pair< std::string, terminal * > Binding
Definition: parser.H:26
terminal * symbol
Definition: parser.H:31
Definition: terminal.H:19
Definition: boot.H:7
std::vector< Binding > Bindings
Definition: parser.H:27
Definition: terminal.H:74
std::string show(const Expr &e)
Definition: expr.C:19
std::shared_ptr< Expr > ExprPtr
Definition: expr.H:58
std::vector< ParseRule > Parser
Definition: parser.H:37
ParseRule(terminal *s, const Bindings &bs, const ExprPtr &r)
Definition: parser.H:29
size_t r(const reader::MetaData &md, size_t o, T *t)
Definition: storage.H:1730
Definition: cc.H:64
Definition: lannotation.H:22
ExprPtr reducer
Definition: parser.H:33
std::map< terminal *, prec > precedence
Definition: terminal.H:82
Definition: parser.H:25
Definition: terminal.H:46