Spicy
Public Member Functions | List of all members
spicy::detail::codegen::Grammar Class Reference

#include <grammar.h>

Public Member Functions

 Grammar (std::string name, Location l=location::None)
 
 Grammar (const Grammar &)=default
 
 Grammar (Grammar &&)=default
 
Grammaroperator= (Grammar &&)=default
 
Grammaroperator= (const Grammar &)=default
 
const std::string & name () const
 
const Locationlocation () const
 
void resolve (production::Unresolved *r, Production p)
 
const Productionresolved (const production::Resolved &r) const
 
Result< NothingsetRoot (const Production &p)
 
Result< Nothingfinalize ()
 
std::optional< Productionroot () const
 
const std::map< std::string, Production > & productions () const
 
bool needsLookAhead () const
 
void printTables (std::ostream &out, bool verbose=false)
 

Detailed Description

A Spicy grammar. Each unit is translated into a grammar for parsing.

Constructor & Destructor Documentation

◆ Grammar()

spicy::detail::codegen::Grammar::Grammar ( std::string  name,
Location  l = location::None 
)
inline

Instantiates a new grammar that's initially empty. setRoot then initializes the grammar with its root production.

Parameters
namename associated with the grammar; must be unique, and is used both for debugging and for generating labels during code generation
roottop-level root production
lassociated location

Member Function Documentation

◆ finalize()

Result< Nothing > Grammar::finalize ( )

Freezes the grammar, computes the parsing tables for all previously added productions, and then registers the look-ahead sets with all LookAhead productions. If this method fails, grammar and production will be left in an undefined state.

Returns
error if the parsing tables couldn't be computed (e.g., due to ambiguties); the error description will then be describing the issue.

◆ location()

const Location& spicy::detail::codegen::Grammar::location ( ) const
inline

Returns the location associated with the production.

◆ name()

const std::string& spicy::detail::codegen::Grammar::name ( ) const
inline

Returns the name of the grammar. The name uniquely identifies the grammar.

◆ needsLookAhead()

bool spicy::detail::codegen::Grammar::needsLookAhead ( ) const
inline

Returns true if the grammar needs look-ahead for parsing.

Note
will always return false until the root production gets set.

◆ printTables()

void Grammar::printTables ( std::ostream &  out,
bool  verbose = false 
)

Prints the grammar in a (somewhat) human readable form. This is for debugging. In verbose mode, the grammar and all the internal nullable/first/follow tables are printed.

◆ productions()

const std::map<std::string, Production>& spicy::detail::codegen::Grammar::productions ( ) const
inline

Returns a closure of all the grammar's productions startin with the root. The result maps each production's symbol to the production itself. Productions without symbols are not included.

Note
will return an empty map until the root production gets set.

◆ resolve()

void Grammar::resolve ( production::Unresolved r,
Production  p 
)

Resolves an previous place-holder production with an actual production. Once resolved, parser table construction will use the actual production everywhere where the place-holder is referenced.

◆ resolved()

const Production & Grammar::resolved ( const production::Resolved r) const

Returns a the actual production a resolved production refers to.

◆ root()

std::optional<Production> spicy::detail::codegen::Grammar::root ( ) const
inline

Returns the root production, if set already.

◆ setRoot()

Result< Nothing > Grammar::setRoot ( const Production p)

Sets the root produnction for the grammar. This recursively adds all childrens of the root to the grammar, too. The root production cannot be changeda anymore once set.


The documentation for this class was generated from the following files: