Elementa v8.0.0
Minimalistic library for any C++ application (C++11 and up)
Loading...
Searching...
No Matches
ll1parsing.h
Go to the documentation of this file.
1
3#include "elementa/license.inc"
4#include "elementa/checks.inc"
5
6#ifndef ELEMENTA_PARSING_LL1PARSING_H
7#define ELEMENTA_PARSING_LL1PARSING_H
8
9
10#include <deque>
12
13namespace elementa
14{
15
16namespace parsing
17{
18
39/* **********************************************************************
40
41 Class: LL1Parser
42
43*************************************************************************/
44
46
51class LL1Parser final: public Parser
52{
53 public:
54
59
63 LL1Parser(const CFGrammar & gr,
64 const CFGrammar::LL1Table & table,
65 Lexer & lexer,
66 const TransfTokenId & transftid = [](Token &)->void{}):
67 Parser{gr,lexer,transftid},
68 table_{table}
69 { reset(); }
70
71
73 ~LL1Parser(void) = default;
74
82
93 Parser::Result parseStep(Parser::Observer & obs);
94
95 void reset(void);
97
101 private:
102
103 class StackItem
104 {
105 public:
106
107 using Deque = std::deque<StackItem>;
108
109 bool endofrule_or_elem;
110 union Data
111 {
112 CFGrammar::RuleIterator endofrule;
114
115 Data(const CFGrammar::RuleIterator & ri): endofrule{ri} {}
116 Data(const CFGrammar::CodeElement ce): elem{ce} {}
117 ~Data(void) {}
118 // copies and moves deleted
119 } data;
120
121 StackItem(const CFGrammar::RuleIterator & ri):
122 endofrule_or_elem{true},
123 data{ri} {}
124 StackItem(const CFGrammar::CodeElement ce):
125 endofrule_or_elem{false},
126 data{ce} {}
127
128 ~StackItem(void) {}
129 // copies and moves deleted - use emplace_back to push values into deque
130
131 std::string to_string(const CFGrammar & gr) const
132 { return(std::string{"StackItem "} +
133 (endofrule_or_elem ? "rule marker" : "grammar element") +
134 (endofrule_or_elem ? (": [" + gr.to_string(*data.endofrule) +
135 "]") :
136 (": code " + std::to_string(data.elem) +
137 " '" + (gr.isTerminal(data.elem,true) ?
138 gr.nameOfTerminal(data.elem) :
139 gr.nameOfNonTerminal(data.elem))
140 + "'"))); }
141 };
142
143 const CFGrammar::LL1Table & table_;
144 StackItem::Deque stack_;
145 bool thereislookahead_;
146 Token::Ptr lookahead_;
147 elementa::base::SerChLoc::Ptr lookaheadloc_;
148
149 const CFGrammar::RuleIterator & lastRuleInStack(void) const;
150 void updateLookahead(void);
151 Parser::Result unrollStack(Parser::Observer & obs);
152
153};
154
155 // LL1 Parsing
157
158} // End parsing namespace
159
160} // End elementa namespace
161
162
163#endif
An index of a rule in the grammar, i.e., an iterator on the rules.
Definition: cfgrammars.h:125
Abstract class for observing the steps of the parser.
Definition: parsing.h:407
std::map< LL1TableIndex, RuleIterator > LL1Table
An LL(1) parsing table (sparse).
Definition: cfgrammars.h:287
TokenId CodeElement
Unique code of an element of the grammar, either terminal or non-term.
Definition: cfgrammars.h:97
A context-free grammar.
Definition: cfgrammars.h:71
void printStatus(elementa::base::OutSerCh &chout) const
Must serialize current status in text format through CHOUT, for debug.
LL1Parser(const CFGrammar &gr, const CFGrammar::LL1Table &table, Lexer &lexer, const TransfTokenId &transftid=[](Token &) ->void{})
Constructor for a grammar.
Definition: ll1parsing.h:63
void reset(void)
Must prepare to begin a new parsing (not needed after construction).
Parser::Result parseStep(Parser::Observer &obs)
Do a parsing step.
~LL1Parser(void)=default
Destructor.
A parser for an LL(1) grammar.
Definition: ll1parsing.h:52
std::shared_ptr< Token > Ptr
Safe pointer to a token.
Definition: lexer.h:149
A lexer that produces terminals.
Definition: lexer.h:236
Base class for any token (i.e., lexical element) produced by a lexer.
Definition: lexer.h:146
Lexer & lexer(void) const noexcept
Return a reference to the internal lexer.
Definition: parsing.h:461
std::function< void(Token &) > TransfTokenId
Transformation of TokenId into CFGrammar::CodeElement.
Definition: parsing.h:423
Abtract base class for any parser of context-free grammars.
Definition: parsing.h:390
std::shared_ptr< SerChLoc > Ptr
Safe pointer for polymorphic behaviours.
Definition: basics.h:776
std::ostream OutSerCh
"Base class" that represents any output serial channel in Elementa.
Definition: basics.h:247