Skip to content

Commit f94baa8

Browse files
committed
README and docs update
1 parent bd82c00 commit f94baa8

File tree

7 files changed

+807
-54
lines changed

7 files changed

+807
-54
lines changed

README.md

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ Click on the type name in the following list to see more detailed documentation
8989
- [`forest::tree`](docs/forest_tree.md) - a tree extracted from a graph
9090
- [`tgf`](docs/tgf.md) - Tau Grammar Form parser - reads a grammar in TGF format from a string or a file. An alternative way to describe a grammar instead of creating it programatically.
9191
- [`traverser`](docs/traverser.md) - struct for traversing and accessing rewriter trees
92-
- rewriting - API for rewriting a resulting parse tree
93-
- measure - simple struct for measuring time
92+
- [`rewriting`](docs/rewriting.md) - API for rewriting a resulting parse tree
93+
- [`measure`](docs/measure.md) - simple struct for measuring time
9494

9595
### Classes and structs usable for building command line interfaces
9696

@@ -108,21 +108,18 @@ There are several areas covered by functions provided by this library
108108
- [devhelpers](docs/devhelpers.md) - helper forest transformations to various formats (TML facts, TML rules, DOT) useful when developing a parser
109109

110110

111-
<a name="tau-grammar-form"></a>
112-
113111
## TGF - Tau Grammar Form
114112

115113
TGF is an EBNF-based form to describe grammars. Specification of the form can be found on page [`Tau Grammar Form`](docs/tau_grammar_form.md)
116114

117115

118-
<a name="tgf-tool"></a>
119-
120116
## TGF tool
121117

122118
This library comes with a CLI executable `tgf` which features viewing, testing and debugging grammars written in TGF and it can also generate a C++ code from TGF grammar which is then usable in a C++ project.
123119

124120
More detailed information about this CLI can be found on page [`TGF tool`](docs/tgf_tool.md)
125121

122+
126123
## Tutorials
127124

128125
### CSV parser

docs/forest.md

Lines changed: 43 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[back to index](../README.md#overview-of-types)
1+
[back to index](../README.md#classes-and-structs)
22

33
# forest
44

@@ -8,18 +8,23 @@ template <typename NodeT> struct forest;
88

99
`forest` is a structure which can contain multiple trees. Nodes can have multiple sets of children where more than one set represents splitting of trees meaning the part from root to the node is shared among all trees splitted.
1010

11-
It offers methods for traversal or tree or graph extractions.
11+
It offers methods counting trees, traversal or tree or graph extractions.
1212

13-
`forest` is a generic structure since the type of nodes is templated.
13+
`forest` is a generic structure since the type of nodes `NodeT` is templated.
1414

1515
### parse forest
1616

17-
This library uses `forest` to represent all parse trees parsed from a given input. Each `parser<C, T>::parse(...)` call produces a `forest` with a type `std::pair<lit<C, T>, std::array<size_t, 2>>` where `lit<C, T>` is a parsed literal and the array contains starting and ending position of a literal in an input string.
17+
This library uses `forest` to represent all parse trees parsed from a given input. Each `parser<C, T>::parse(...)` call produces a `parser<C, T>::result` which contins a parsed forest (accessible with `get_forest()`). Parse nodes uses type (`NodeT`) `std::pair<lit<C, T>, std::array<size_t, 2>>` where `lit<C, T>` is a parsed literal and the array contains position span of a literal in an input string.
18+
19+
`parser` provides type [`pnode`](parser_pnode.md) for used as `NodeT` templated type and type `pforest` for a parse forest:
1820

19-
`parser` declares types `pnode` for a parse node and `pforest` for a parse forest:
2021
```
21-
typedef typename std::pair<lit<C, T>, std::array<size_t, 2>> pnode;
22-
typedef forest<pnode> pforest;
22+
class parser {
23+
// ...
24+
public:
25+
using pforest forest<pnode>;
26+
// ...
27+
}
2328
```
2429

2530
## constructor
@@ -64,11 +69,6 @@ For a given node `p` sets the new set of subforests with children nodes and retu
6469
For a given node `p` returns set of subforests with children nodes.
6570

6671

67-
### bool is_binarized() const;
68-
69-
Returns true if the forest is binarized, ie. each node has up to 2 children.
70-
71-
7272
### size_t count_trees(const NodeT& p) const;
7373

7474
Counts and returns a number of trees under a `p` node.
@@ -79,6 +79,22 @@ Counts and returns a number of trees under a `p` node.
7979
Counts and returns a number of trees in a whole forest.
8080

8181

82+
### bool is_binarized() const;
83+
84+
Returns true if the forest is binarized, ie. each node has up to 2 children.
85+
86+
87+
### template<typename TraversableT> bool detect_cycle(TraversableT& g) const;
88+
89+
Returns true if there exist a cycle in a `g`.
90+
91+
```
92+
// having a forest f and a graph fg
93+
if (f->detect_cycle(fg)) cout << "cycle detected\n";
94+
else cout << "no cycles\n";
95+
```
96+
97+
8298
### std::vector<graph> extract_graphs(const NodeT& root, cb_next_graph_t cb_next_graph, bool unique_edge = true) const;
8399

84100
Extracts graphs from the forest starting at a `root` node. For every extracted graph `cb_next_graph` callback is called.
@@ -93,15 +109,6 @@ auto next_g = [](parser<C, T>::pforest::graph& fg) {
93109
f->extract_graphs(f->root(), next_g);
94110
```
95111

96-
### template<typename TraversableT> bool detect_cycle(TraversableT& g) const;
97-
98-
Returns true if there exist a cycle in a `g`.
99-
100-
```
101-
// having a forest f and a graph fg
102-
if (f->detect_cycle(fg)) cout << "cycle detected\n";
103-
else cout << "no cycles\n";
104-
```
105112

106113
### bool traverse(...)
107114

@@ -146,21 +153,21 @@ Traverses the whole forest.
146153

147154
`cb_ambig` is called after entering a node which splits into more than one tree. The node is passed as a first argument, a set of vectors of nodes is passed as a second argument representing sets of children nodes where each set member represents a different tree.
148155

156+
Example of a traversal printing nodes and ambiguities of a parsed forest
149157
```
150-
151158
function print_node(ostream& os, parser<>::pnode& n) {
152159
return os << n.first << " [" << n.second[0] << ", " << n.second[1] << "]";
153160
};
154161
155162
void main() {
156163
grammar g(tgf<>::from_file("arithmetic.tgf"));
157164
parser p(g);
158-
auto f = p.parse("1+2*3");
159-
if (!p.found()) {
160-
cerr << p.get_error().to_str() << endl;
165+
auto r = p.parse("1+2*3");
166+
if (r.found) {
167+
cerr << r.parse_error.to_str() << endl;
161168
return;
162169
}
163-
f->traverse(
170+
r.get_forest()->traverse(
164171
[](const parser<>::pnode& n) {
165172
print_node(cout << "entering node: ", n) << "\n";
166173
},
@@ -183,3 +190,13 @@ void main() {
183190
);
184191
}
185192
```
193+
194+
195+
### bool replace_nodes(graph& g, nodes& s);
196+
197+
Replace each node with its immediate children, assuming its only one pack (unambigous) the caller to ensure the right order to avoid cyclic dependency if any. deletes from graph g as well. return true if any one of the nodes' replacement succeeds
198+
199+
200+
### bool replace_node(graph& g, const node& torep,const nodes& replacement);
201+
202+
Replaces node 'torep' in one pass with the given nodes 'replacement' everywhere in the forest and returns true if changed. Does not care if its recursive or cyclic, its caller's responsibility to ensure

docs/measure.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
[back to index](../README.md#classes-and-structs)
2+
3+
# measure
4+
5+
`measure` is a structure capable of measuring elapsed time. Values printed and returned are in milliseconds (ms).
6+
7+
## constructor
8+
9+
```
10+
measure(const std::string& label, bool start_measure = false, bool silent = false);
11+
```
12+
13+
Creates a measuring object labeled with a `label`.
14+
15+
If `start_measure` is true it starts measuring when instantiated (constructor calls `start()`).
16+
17+
If `silent` is true it prints no measurement info but `stop()` and `pause()` still returns elapsed time in ms.
18+
19+
20+
## methods
21+
22+
### void start();
23+
24+
Starts measuring.
25+
26+
### double pause();
27+
28+
Pauses measuring and returns elapsed time from start in ms.
29+
30+
### double unpause();
31+
32+
Unpause measuring.
33+
34+
### double stop();
35+
36+
Stops measuring, if `silent` is not `true` it prints measured info and returns elapsed time in ms.
37+
38+
`stop()` is called when measur struct is destructed.

docs/parser_pnode.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
[back to index](../README.md#overview-of-types)
1+
[back to index](../README.md#classes-and-structs)
22

33
# parser::pnode
44

5-
`pnode` is a structure used as a forest node. It maps nodes into pointers to make sure the forest does not contain duplicities.
5+
`pnode` is a structure used as a forest node. It maps nodes to pointers to make sure the forest does not contain duplicities.
66

77
It has the same api as `std::pair<lit<C, T>, std::array<size_t, 2>>` containing the literal and it's position span in the input.

0 commit comments

Comments
 (0)