Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
TreeFromStringParserCommon.h
Go to the documentation of this file.
1
6#pragma once
7
8#include <alib/vector>
9
10#include <core/stringApi.hpp>
11
13
16#include <alphabet/GapSymbol.h>
18
19namespace tree {
20
22public:
23 template < class SymbolType >
25 template < class SymbolType >
26 static ext::tree < SymbolType > parseUnrankedContent ( ext::istream & input, bool & isPattern, bool & isExtendedPattern, ext::set < SymbolType > & nonlinearVariables );
27};
28
29template < class SymbolType >
31 TreeFromStringLexer::Token token = TreeFromStringLexer::next ( input );
32
34 isPattern = true;
35 return ext::tree < common::ranked_symbol < SymbolType > > ( alphabet::WildcardSymbol::instance < common::ranked_symbol < SymbolType > > ( ), { } );
36 } else if ( token.type == TreeFromStringLexer::TokenType::NONLINEAR_VARIABLE ) {
37 isPattern = true;
39 nonlinearVariables.insert ( nonlinearVariable );
40 return ext::tree < common::ranked_symbol < SymbolType > > ( std::move ( nonlinearVariable ), { } );
41 } else {
42 bool isWildcard = false;
43 SymbolType symbol = [ & ] ( ) {
45 isWildcard = true;
46 return alphabet::NodeWildcardSymbol::instance < SymbolType > ( );
47 } else {
48 TreeFromStringLexer::putback ( input, token );
50 }
51 } ( );
52
53 token = TreeFromStringLexer::next ( input );
54 if ( token.type != TreeFromStringLexer::TokenType::RANK )
55 throw exception::CommonException ( "Missing rank" );
56
57 unsigned rank = ext::from_string < unsigned > ( token.value );
58 if ( isWildcard ) {
59 nodeWildcards.insert ( common::ranked_symbol < SymbolType > ( symbol, rank ) );
60 isPattern = true;
61 }
62
64 for ( unsigned i = 0; i < rank; i++ )
65 childs.emplace_back ( ext::tree < common::ranked_symbol < SymbolType > > ( parseRankedContent < SymbolType > ( input, isPattern, nonlinearVariables, nodeWildcards ) ) );
66
67 return ext::tree < common::ranked_symbol < SymbolType > > ( common::ranked_symbol < SymbolType > ( std::move ( symbol ), rank ), std::move ( childs ) );
68 }
69}
70
71template < class SymbolType >
72ext::tree < SymbolType > TreeFromStringParserCommon::parseUnrankedContent ( ext::istream & input, bool & isPattern, bool & isExtendedPattern, ext::set < SymbolType > & nonlinearVariables ) {
73 TreeFromStringLexer::Token token = TreeFromStringLexer::next ( input );
74
76 token = TreeFromStringLexer::next ( input );
77
78 if ( token.type != TreeFromStringLexer::TokenType::BAR )
79 throw exception::CommonException ( "Missing bar" );
80
81 isPattern = true;
82 return ext::tree < SymbolType > ( alphabet::WildcardSymbol::instance < SymbolType > ( ), { } );
83 } else if ( token.type == TreeFromStringLexer::TokenType::SUBTREE_GAP ) {
84 token = TreeFromStringLexer::next ( input );
85
86 if ( token.type != TreeFromStringLexer::TokenType::BAR )
87 throw exception::CommonException ( "Missing bar" );
88
89 isPattern = true;
90 return ext::tree < SymbolType > ( alphabet::GapSymbol::instance < SymbolType > ( ), { } );
91 } else if ( token.type == TreeFromStringLexer::TokenType::NONLINEAR_VARIABLE ) {
92 token = TreeFromStringLexer::next ( input );
93
94 if ( token.type != TreeFromStringLexer::TokenType::BAR )
95 throw exception::CommonException ( "Missing bar" );
96
97 isPattern = true;
98 SymbolType nonlinearVariable ( alphabet::NonlinearVariableSymbol < SymbolType > ( SymbolType ( token.value ) ) );
99 nonlinearVariables.insert ( nonlinearVariable );
100 return ext::tree < SymbolType > ( std::move ( nonlinearVariable ), { } );
101 } else {
102 SymbolType symbol = [ & ] ( ) {
104 isExtendedPattern = true;
105 isPattern = true;
106 return alphabet::NodeWildcardSymbol::instance < SymbolType > ( );
107 } else {
108 TreeFromStringLexer::putback ( input, token );
110 }
111 } ( );
112
114
115 token = TreeFromStringLexer::next ( input );
116
117 while ( token.type != TreeFromStringLexer::TokenType::BAR ) {
118 TreeFromStringLexer::putback ( input, token );
119 childs.emplace_back ( parseUnrankedContent < SymbolType > ( input, isPattern, isExtendedPattern, nonlinearVariables ) );
120 token = TreeFromStringLexer::next ( input );
121 }
122
123 if ( token.type != TreeFromStringLexer::TokenType::BAR )
124 throw exception::CommonException ( "Missing bar" );
125
126 return ext::tree < SymbolType > ( std::move ( symbol ), std::move ( childs ) );
127 }
128}
129
130} /* namespace tree */
131
Represents the nonlinear variable symbol used in a nonlinear tree pattern.
Definition: NonlinearVariableSymbol.h:36
Definition: ranked_symbol.hpp:20
Basic exception from which all other exceptions are derived.
Definition: CommonException.h:21
static void putback(ext::istream &input, const Token &token)
Definition: lexer.hpp:61
Definition: istream.h:32
Definition: set.hpp:44
Class introducing a tree with interface trying to be close to the interface of standard library conta...
Definition: tree.hpp:52
Class extending the vector class from the standard library. Original reason is to allow printing of t...
Definition: vector.hpp:45
static Token next(ext::istream &input)
Definition: TreeFromStringLexer.cpp:10
Definition: TreeFromStringParserCommon.h:21
static ext::tree< common::ranked_symbol< SymbolType > > parseRankedContent(ext::istream &input, bool &isPattern, ext::set< common::ranked_symbol< SymbolType > > &nonlinearVariables, ext::set< common::ranked_symbol< SymbolType > > &nodeWildcards)
Definition: TreeFromStringParserCommon.h:30
static ext::tree< SymbolType > parseUnrankedContent(ext::istream &input, bool &isPattern, bool &isExtendedPattern, ext::set< SymbolType > &nonlinearVariables)
Definition: TreeFromStringParserCommon.h:72
int i
Definition: AllEpsilonClosure.h:118
typename T::SymbolType SymbolType
Definition: ReachableStates.h:176
Definition: BackwardOccurrenceTest.h:17
Definition: stringApi.hpp:26