![]() |
Spicy
|
#include <grammar.h>
Public Member Functions | |
Grammar (std::string name, Location l=location::None) | |
Grammar (const Grammar &)=default | |
Grammar (Grammar &&)=default | |
Grammar & | operator= (Grammar &&)=default |
Grammar & | operator= (const Grammar &)=default |
const std::string & | name () const |
const Location & | location () const |
void | resolve (production::Unresolved *r, Production p) |
const Production & | resolved (const production::Resolved &r) const |
Result< Nothing > | setRoot (const Production &p) |
Result< Nothing > | finalize () |
std::optional< Production > | root () const |
const std::map< std::string, Production > & | productions () const |
hilti::Result< std::set< Production > > | lookAheadsForProduction (Production p, std::optional< Production > parent={}) const |
bool | needsLookAhead () const |
void | printTables (std::ostream &out, bool verbose=false) |
A Spicy grammar. Each unit is translated into a grammar for parsing.
|
inline |
Instantiates a new grammar that's initially empty. setRoot
then initializes the grammar with its root production.
name | name associated with the grammar; must be unique, and is used both for debugging and for generating labels during code generation |
root | top-level root production |
l | associated location |
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.
|
inline |
Returns the location associated with the production.
hilti::Result< std::set< Production > > Grammar::lookAheadsForProduction | ( | Production | p, |
std::optional< Production > | parent = {} |
||
) | const |
Returns the set of look-ahead terminals for a given production.
p | production to examome |
parent | if given and p is nullable, then include any look-aheads of the parent as well |
|
inline |
Returns the name of the grammar. The name uniquely identifies the grammar.
|
inline |
Returns true if the grammar needs look-ahead for parsing.
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.
|
inline |
Returns a closure of all the grammar's productions starting with the root. The result maps each production's symbol to the production itself. Productions without symbols are not included.
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.
const Production & Grammar::resolved | ( | const production::Resolved & | r | ) | const |
Returns a the actual production a resolved production refers to.
|
inline |
Returns the root production, if set already.
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.