3#include "elementa/license.inc"
4#include "elementa/checks.inc"
6#ifndef ELEMENTA_PARSING_LL1PARSING_H
7#define ELEMENTA_PARSING_LL1PARSING_H
107 using Deque = std::deque<StackItem>;
109 bool endofrule_or_elem;
122 endofrule_or_elem{true},
125 endofrule_or_elem{false},
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) +
136 (
": code " + std::to_string(data.elem) +
137 " '" + (gr.isTerminal(data.elem,
true) ?
138 gr.nameOfTerminal(data.elem) :
139 gr.nameOfNonTerminal(data.elem))
144 StackItem::Deque stack_;
145 bool thereislookahead_;
149 const CFGrammar::RuleIterator & lastRuleInStack(
void)
const;
150 void updateLookahead(
void);
151 Parser::Result unrollStack(Parser::Observer & obs);
An index of a rule in the grammar, i.e., an iterator on the rules.
Abstract class for observing the steps of the parser.
std::map< LL1TableIndex, RuleIterator > LL1Table
An LL(1) parsing table (sparse).
TokenId CodeElement
Unique code of an element of the grammar, either terminal or non-term.
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.
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.
std::shared_ptr< Token > Ptr
Safe pointer to a token.
A lexer that produces terminals.
Base class for any token (i.e., lexical element) produced by a lexer.
Lexer & lexer(void) const noexcept
Return a reference to the internal lexer.
std::function< void(Token &) > TransfTokenId
Transformation of TokenId into CFGrammar::CodeElement.
Abtract base class for any parser of context-free grammars.
std::shared_ptr< SerChLoc > Ptr
Safe pointer for polymorphic behaviours.
std::ostream OutSerCh
"Base class" that represents any output serial channel in Elementa.