LL1Checker 5.0
“Tool for verifying LL(1) grammars and validating input strings.”
Loading...
Searching...
No Matches
Grammar Struct Reference

Public Member Functions

 Grammar (std::string filename)
 Constructs a grammar by reading from the specified file.
void ReadFromFile ()
 Reads and loads the grammar from a file.
void AddRule (symbol_table::TokenID antecedent, const production &consequent)
 Adds a rule to the grammar.
void SetAxiom (symbol_table::TokenID axiom)
 Sets the axiom (entry point) of the grammar.
std::vector< std::pair< symbol_table::TokenID, production > > FilterRulesByConsequent (symbol_table::TokenID arg)
 Filters grammar rules that contain a specific token in their consequent.
void Debug ()
 Prints the current grammar structure to standard output.
std::string GenerateNewNonTerminal (const std::string &base)
 Generate a new non terminal symbol by appending ' to base until it is not in symbol table.

Static Public Member Functions

static std::vector< symbol_table::TokenID > Split (const std::string &s)
 Splits a production string into individual tokens.

Public Attributes

std::unordered_map< symbol_table::TokenID, std::vector< production > > g_
 Stores the grammar rules with each antecedent mapped to a list of productions.
std::vector< symbol_table::TokenID > nt_order_ {}
 Keeps the insertion order of non-terminal symbols.
symbol_table::TokenID axiom_ {}
 The axiom or entry point of the grammar.
const std::string kFilename
 The filename from which the grammar is read.

Constructor & Destructor Documentation

◆ Grammar()

Grammar::Grammar ( std::string filename)
explicit

Constructs a grammar by reading from the specified file.

Parameters
filenamePath to the file containing the grammar definitions.

Initializes the grammar by setting the filename for input. The actual grammar data is read and processed through the read_from_file function.

Member Function Documentation

◆ AddRule()

void Grammar::AddRule ( symbol_table::TokenID antecedent,
const production & consequent )

Adds a rule to the grammar.

Parameters
antecedentThe left-hand side (LHS) symbol of the rule.
consequentThe right-hand side (RHS) of the rule as a string.

Adds a rule to the grammar by specifying the antecedent symbol and the consequent production. This function processes and adds each rule for parsing.

◆ Debug()

void Grammar::Debug ( )

Prints the current grammar structure to standard output.

This function provides a debug view of the grammar by printing out all rules, the axiom, and other relevant details.

◆ FilterRulesByConsequent()

std::vector< std::pair< symbol_table::TokenID, production > > Grammar::FilterRulesByConsequent ( symbol_table::TokenID arg)

Filters grammar rules that contain a specific token in their consequent.

Parameters
argThe token to search for within the consequent of the rules.
Returns
std::vector of pairs where each pair contains an antecedent and its respective production that includes the specified token.

Searches for rules in which the specified token is part of the consequent and returns those rules.

◆ GenerateNewNonTerminal()

std::string Grammar::GenerateNewNonTerminal ( const std::string & base)

Generate a new non terminal symbol by appending ' to base until it is not in symbol table.

Parameters
baseBase non terminal used to generate the new one
Returns
New non terminal

◆ ReadFromFile()

void Grammar::ReadFromFile ( )

Reads and loads the grammar from a file.

This function reads the grammar rules from the specified file, parsing each rule and storing it in the grammar structure. The file format requirements are outlined in the README.md.

Exceptions
GrammarErrorif there are errors reading symbols, parsing the grammar, or splitting the rules as specified in the input file.

◆ SetAxiom()

void Grammar::SetAxiom ( symbol_table::TokenID axiom)

Sets the axiom (entry point) of the grammar.

Parameters
axiomThe entry point or start symbol of the grammar.

Defines the starting point for the grammar, which is used in parsing algorithms and must be a non-terminal symbol present in the grammar.

◆ Split()

std::vector< symbol_table::TokenID > Grammar::Split ( const std::string & s)
static

Splits a production string into individual tokens.

Parameters
sThe production string to split.
Returns
std::vector containing tokens extracted from the production string.

The function decomposes a production string into individual symbols based on the symbol table, allowing terminals and non-terminals to be identified.


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