-
Notifications
You must be signed in to change notification settings - Fork 0
/
cfg.h
152 lines (102 loc) · 4.2 KB
/
cfg.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
//
// Created by hsawaf on 4/20/18.
//
#ifndef KOMPILER_CFG_H
#define KOMPILER_CFG_H
#include <unordered_map>
#include <unordered_set>
#include <string>
#include <vector>
#include <iostream>
#define EPS std::string(1, 0x01)
#define EOI std::string("$")
class cfg {
public:
class symbol {
public:
class production {
/* A production is represented by an ordered
* list of symbols */
std::vector<symbol *> symbols;
/* Each production has a first set, which is a set
* of terminals that can be matched from the non-terminal
* at a given input */
std::unordered_set<std::string> first;
/* Key of the nonterminal lhs of this production */
std::string lhs;
/* Overloading the ostream operator */
friend std::ostream &operator<<(std::ostream& stream, const production &prod);
public:
/* Default Constructor */
production();
/* Construct production from list of symbols and rhs */
production(std::string _lhs, std::vector<symbol *> symbols);
/* Appends symbol to production */
void add_symbol(symbol *sym);
/* Return list representation of production. */
std::vector<symbol *> get_symbols(void);
std::unordered_set<std::string> get_first() const;
/* Add a symbol to the first set of this production */
/* TODO: add_first can be implemented internally, hiding
* implementaiton details, to disallow inconsistency
* caused by manipulation outside cfg class */
bool add_first(std::string _key);
bool contains_first(std::string _key) const;
};
symbol();
symbol(std::string _key, bool _terminal = false);
bool is_terminal(void) const;
bool is_eps(void) const;
std::string get_key(void) const;
void add_production(production _production);
void add_production(std::vector<symbol *> &rhs);
void clear_productions();
bool add_follow(std::string _key);
bool contains_first(std::string _key) const;
bool contains_follow(std::string _key) const;
std::unordered_set<std::string> get_first() const;
std::unordered_set<std::string> get_follow() const;
std::vector<production> get_productions();
production &operator[](std::size_t idx);
std::size_t get_production_count() const;
private:
bool terminal;
std::string key;
std::vector<production> productions;
std::unordered_set<std::string> follow;
};
cfg(std::string _grammar);
/* Adds and returns the symbol c by _key if it does not exist.
* if it does, the identified symbol is simply returned.
*/
symbol *add_symbol(std::string _key, bool _terminal = false);
/* Adds a new production identified as lhs -> rhs;
* rhs is a list of all symbol names in the production.
* all the symbols will be added to the cfg symbols automatically if they do not exist.
* */
bool add_production(std::string lhs, std::vector<std::string> rhs);
/* Adds a new production identified as lhs -> rhs;
* rhs is a list of all symbols in the production.
* all the symbols will be added to the cfg symbols automatically if they do not exist.
* */
bool add_production(std::string lhs, std::vector<symbol *> rhs);
symbol *get_symbol(std::string _key);
symbol *get_starting_symbol(void);
void set_starting_symbol(symbol *s);
std::vector<std::string> get_symbols();
/* Tries to convert the cfg to LL(1)
* returns true if the cfg was converted successfully
* returns false if the cfg was already LL(1) */
bool to_ll1();
/* Builds CFG by computing first and follow sets for
* each non-terminal, which are later used to build
* parsing table */
void build();
private:
std::unordered_map<std::string, symbol> symbols;
symbol *starting_symbol;
std::string grammar;
/* Overloading the ostream operator */
friend std::ostream &operator<<(std::ostream& stream, cfg &grmr);
};
#endif //KOMPILER_CFG_H