Elementa v8.0.0
Minimalistic library for any C++ application (C++11 and up)
Loading...
Searching...
No Matches

Description


  Classes for parsing LL(1) context-free grammars.
See also
General parsing of context-free grammars, Context-Free Grammars
Author
Juan-Antonio Fernandez-Madrigal. http://jafma.net
Date
2018
Collaboration diagram for Parsing of LL(1) grammars:

Classes

class  elementa::parsing::LL1Parser
 A parser for an LL(1) grammar. More...
 

Class Documentation

◆ elementa::parsing::LL1Parser

class elementa::parsing::LL1Parser

A parser for an LL(1) grammar.

If constructing an AST, the resulting tree will produce the original input if going through leaf vertices in depth-first fashion. Observers used in this class may contain the syntax-guided code equivalent to the one written in a Bison-like parser inside each rule: it executes in the same order as the rule is being built.

Definition at line 51 of file ll1parsing.h.

Inheritance diagram for elementa::parsing::LL1Parser:
Collaboration diagram for elementa::parsing::LL1Parser:

Public Member Functions

Constructors
 LL1Parser (const CFGrammar &gr, const CFGrammar::LL1Table &table, Lexer &lexer, const TransfTokenId &transftid=[](Token &) ->void{})
 Constructor for a grammar. More...
 
 ~LL1Parser (void)=default
 Destructor.
 
Pure virtual methods in Parser class
Parser::Result parseStep (Parser::Observer &obs)
 Do a parsing step. More...
 
void reset (void)
 Must prepare to begin a new parsing (not needed after construction). More...
 
void printStatus (elementa::base::OutSerCh &chout) const
 Must serialize current status in text format through CHOUT, for debug. More...
 
Methods
const CFGrammargrammar (void) const noexcept
 Return a reference to the internal grammar. More...
 
Lexerlexer (void) const noexcept
 Return a reference to the internal lexer. More...
 
void parse (Observer &obs)
 Parse the input until a complete sentence of the language is read. More...
 

Types, consts, etc.

 ELE_CLASS_ENUM (Result, kFinished, kMoreSteps, kNotInTable, kMismatch)
 Result codes after a parsing step.
 
using TransfTokenId = std::function< void(Token &) >
 Transformation of TokenId into CFGrammar::CodeElement. More...
 

Member Typedef Documentation

◆ TransfTokenId

using elementa::parsing::Parser::TransfTokenId = std::function< void(Token &) >
inherited

Transformation of TokenId into CFGrammar::CodeElement.

This comes handy to change the lexer token ids into grammar code elements, in case that the lexers do not work with the latter but with the former.

Definition at line 423 of file parsing.h.

Constructor & Destructor Documentation

◆ LL1Parser()

elementa::parsing::LL1Parser::LL1Parser ( const CFGrammar gr,
const CFGrammar::LL1Table table,
Lexer lexer,
const TransfTokenId transftid = [](Token &)->void{} 
)
inline

Constructor for a grammar.

This takes references to GR and to TABLE and store them internally, thus they must exist beyond this parser, since the parser does not take their ownership. The integrity of GR and its correspondence to TABLE are not checked.

Definition at line 63 of file ll1parsing.h.

Member Function Documentation

◆ parseStep()

Parser::Result elementa::parsing::LL1Parser::parseStep ( Parser::Observer obs)
virtual

Do a parsing step.

This method calls getToken() from the lexer when needed. If the lexer produces a null token, do as if it would produced kCodeEnd; otherwise, call transftid() for transforming the code of the token into the grammar code needed.

Note
- In the case of a match with a non-end code, the observer will receive a StepInfo with the terminal info plus the rule iterator of the rule that is currently being parsed.
See also
Parser::Observer, StepInfo.

Implements elementa::parsing::Parser.

◆ reset()

void elementa::parsing::LL1Parser::reset ( void  )
virtual

Must prepare to begin a new parsing (not needed after construction).

Implements elementa::parsing::Parser.

◆ printStatus()

void elementa::parsing::LL1Parser::printStatus ( elementa::base::OutSerCh chout) const
virtual

Must serialize current status in text format through CHOUT, for debug.

Implements elementa::parsing::Parser.

◆ grammar()

const CFGrammar & elementa::parsing::Parser::grammar ( void  ) const
inlinenoexceptinherited

Return a reference to the internal grammar.

Definition at line 458 of file parsing.h.

◆ lexer()

Lexer & elementa::parsing::Parser::lexer ( void  ) const
inlinenoexceptinherited

Return a reference to the internal lexer.

Definition at line 461 of file parsing.h.

◆ parse()

void elementa::parsing::Parser::parse ( Observer obs)
inherited

Parse the input until a complete sentence of the language is read.

The resulting top-down sequence of rules is passed sequentially to the observer methods in OBS.