-
Notifications
You must be signed in to change notification settings - Fork 1
/
grammar.h
81 lines (58 loc) · 1.53 KB
/
grammar.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
#ifndef GRAMMAR_H
#define GRAMMAR_H
#include <cstdio>
#include <set>
#include "hash.h"
#define MAX_SYMBOL 200
typedef struct Symbol
{
int id;
char *name;
int terminal;
int nullable;
std::set<int> first;
std::set<int> follow;
} Symbol;
typedef struct Symbol_List
{
struct Symbol *sym;
struct Symbol_List *next;
} Symbol_List;
struct Product
{
int id;
struct Product *next;
};
struct Product_List
{
struct Product *product;
struct Product_List *next;
};
struct Rule
{
int lhs_id;
struct Product_List *pl;
struct Rule *next;
};
struct Grammar
{
struct Rule *rule;
Symbol *symbol_table[MAX_SYMBOL];
Symbol_List *hash_table[MAX_HASH_BUCKET];
};
Symbol *insert_symbol(Symbol_List *hash_table[], char *str, int sym_id);
int symbol_id(Symbol_List *hash_table[], char *str);
Symbol *create_symbol(int id, char *str);
void delete_symbol(Symbol *sym);
void push_product(struct Product_List *pl, int sym_id);
struct Product_List *create_productlist();
void add_productlist(struct Rule *rule, struct Product_List *pl);
struct Rule *create_rule(int lhs_id);
struct Rule *find_rule(struct Grammar *grammar, int lhs_id);
void grammar_init(struct Grammar *grammar);
void grammar_addrule(struct Grammar *grammar, struct Rule *rule);
int set_union(std::set<int> &S1, std::set<int> &S2);
void nullable(struct Grammar *grammar, Symbol *symbol_table[]);
void first(struct Grammar *grammar, Symbol *symbol_table[], int sym_id);
void follow(struct Grammar *grammar, Symbol *symbol_table[]);
#endif