File: Symbol.hh

package info (click to toggle)
eclipse-titan 6.1.0-1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 79,084 kB
  • ctags: 29,092
  • sloc: cpp: 210,764; ansic: 44,862; yacc: 21,034; sh: 12,594; makefile: 12,225; lex: 8,972; xml: 5,348; java: 4,849; perl: 3,780; python: 2,834; php: 175
file content (137 lines) | stat: -rw-r--r-- 4,597 bytes parent folder | download
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
/******************************************************************************
 * Copyright (c) 2000-2016 Ericsson Telecom AB
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   Balasko, Jeno
 *   Forstner, Matyas
 *
 ******************************************************************************/
#ifndef _langviz_Symbol_HH
#define _langviz_Symbol_HH

#include "Node.hh"
#include "../compiler2/string.hh"
#include "../compiler2/vector.hh"
#include "../compiler2/map.hh"

class Symbol;
class Grammar;

/**
 * Unique set of symbols.
 */
class SymbolSet : public Node {
protected:
  map<Symbol*, void> ss;

  SymbolSet(const SymbolSet& p);
public:
  SymbolSet() {}
  virtual ~SymbolSet();
  virtual SymbolSet* clone() const {return new SymbolSet(*this);}
  void add_s(Symbol *p_s);
  void add_ss(const SymbolSet& p_ss);
  void remove_s(Symbol *p_s);
  void remove_ss(const SymbolSet& p_ss);
  void remove_all();
  size_t get_nof_ss() const {return ss.size();}
  Symbol* get_s_byIndex(size_t p_i) const {return ss.get_nth_key(p_i);}
};

/**
 * Terminal symbols (tokens) and nonterminal symbols.
 */
class Symbol : public Node {
protected:
  string id;
  string id_dot; /**< this id is used in dot file */
  SymbolSet refd_by; /**< this symbol is referenced directly by these
                          symbols */
  SymbolSet refs; /**< these symbols are referenced directly by this
                       symbol */
  SymbolSet refd_from; /**< this symbol is referenced directly or
                            indirectly from these symbols */
  SymbolSet refs_weight; /**< these NONTERMINAL symbols are referenced
                              directly or indirectly by this symbol;
                              the dist of these symbols are greater
                              than dist of this */
  bool is_terminal; /**< true if is terminal symbol */
  bool is_recursive;
  bool can_be_empty;
  int dist; /**< shortest distance from the start symbol; -1:
                 unreachable */
  int weight; /**< 1 + number of nonterminal symbols that can be
                   reached from this one, and have greater dist than
                   this */

  Symbol(const Symbol& p);
public:
  Symbol(const string& p_id);
  virtual ~Symbol() {}
  virtual Symbol* clone() const {return new Symbol(*this);}
  bool get_is_terminal() {return is_terminal;}
  void set_is_terminal() {is_terminal=true;}
  const string& get_id() const {return id;}
  const string& get_id_dot();
  void add_refd_by(Symbol* p_symbol) {refd_by.add_s(p_symbol);}
  void add_refs(Symbol* p_symbol) {refs.add_s(p_symbol);}
  const SymbolSet& get_refd_by() {return refd_by;}
  const SymbolSet& get_refs() {return refs;}
  void set_is_recursive() {is_recursive=true;}
  void set_can_be_empty() {can_be_empty=true;}
  bool get_can_be_empty() {return can_be_empty;}
  void set_dist(int p_i) {if(dist==-1) dist=p_i;}
  int get_dist() {return dist;}
  int get_weight();
};

/**
 * Ordered list of symbols.
 */
class Symbols : public Node {
protected:
  vector<Symbol> ss; /**< symbols */

  Symbols(const Symbols& p);
public:
  Symbols() {}
  virtual ~Symbols();
  virtual Symbols* clone() const {return new Symbols(*this);}
  void add_s(Symbol *p_s);
  size_t get_nof_ss() const {return ss.size();}
  const Symbol* get_s_byIndex(size_t p_i) const {return ss[p_i];}
  Symbol*& get_s_byIndex(size_t p_i) {return ss[p_i];}
  void replace_aliases(Grammar *grammar);
};

/**
 * Unique map of symbols.
 */
class SymbolMap : public Node {
protected:
  map<string, Symbol> ss;

  SymbolMap(const SymbolMap& p);
public:
  SymbolMap() {}
  virtual ~SymbolMap();
  virtual SymbolMap* clone() const {return new SymbolMap(*this);}
  void add_s(Symbol *p_s);
  size_t get_nof_ss() const {return ss.size();}
  const Symbol* get_s_byIndex(size_t p_i) const {return ss.get_nth_elem(p_i);}
  Symbol*& get_s_byIndex(size_t p_i) {return ss.get_nth_elem(p_i);}
  bool has_s_withId(const string& p_id) const {return ss.has_key(p_id);}
  const Symbol* get_s_byId(const string& p_id) const {return ss[p_id];}
  Symbol*& get_s_byId(const string& p_id) {return ss[p_id];}
  /** Each Symbol instance is owned by the Grammar's SymbolMap. The
   *  only place from where you should destruct them. */
  void destruct_ss();
  /** Used by Grammar after replacing aliases */
  void destruct_symbol(const string& p_id);
};

#endif // _langviz_Symbol_HH