SyntaxTutor
Educational app designed to help compiler students understand LL(1) and SLR(1) parsing algorithms.
 
Loading...
Searching...
No Matches
Grammar Struct Reference

Represents a context-free grammar, including its rules, symbol table, and starting symbol. More...

#include <grammar.hpp>

Collaboration diagram for Grammar:

Public Member Functions

 Grammar ()
 
 Grammar (const std::unordered_map< std::string, std::vector< production > > &grammar)
 
void SetAxiom (const std::string &axiom)
 Sets the axiom (entry point) of the grammar.
 
bool HasEmptyProduction (const std::string &antecedent) const
 Checks if a given antecedent has an empty production.
 
std::vector< std::pair< const std::string, production > > FilterRulesByConsequent (const std::string &arg) const
 Filters grammar rules that contain a specific token in their consequent.
 
void Debug () const
 Prints the current grammar structure to standard output.
 
void AddProduction (const std::string &antecedent, const std::vector< std::string > &consequent)
 Adds a production rule to the grammar and updates the symbol table.
 
std::vector< std::string > Split (const std::string &s)
 Splits a string into grammar symbols using the current symbol table.
 

Public Attributes

std::unordered_map< std::string, std::vector< production > > g_
 Stores the grammar rules with each antecedent mapped to a list of productions.
 
std::string axiom_
 The axiom or entry point of the grammar.
 
SymbolTable st_
 Symbol table of the grammar.
 

Detailed Description

Represents a context-free grammar, including its rules, symbol table, and starting symbol.

This structure encapsulates all components required to define and manipulate a grammar, including production rules, the associated symbol table, and metadata such as the start symbol. It supports construction, transformation, and analysis of grammars.

Constructor & Destructor Documentation

◆ Grammar() [1/2]

Grammar::Grammar ( )
default

◆ Grammar() [2/2]

Grammar::Grammar ( const std::unordered_map< std::string, std::vector< production > > & grammar)
explicit

Member Function Documentation

◆ AddProduction()

void Grammar::AddProduction ( const std::string & antecedent,
const std::vector< std::string > & consequent )

Adds a production rule to the grammar and updates the symbol table.

This function inserts a new production of the form A → α into the grammar, where antecedent is the non-terminal A and consequent is the sequence α. It also updates the internal symbol table to reflect any new symbols introduced.

Parameters
antecedentThe left-hand side non-terminal of the production.
consequentThe right-hand side sequence of grammar symbols.

◆ Debug()

void Grammar::Debug ( ) const

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< const std::string, production > > Grammar::FilterRulesByConsequent ( const std::string & arg) const

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

Parameters
argThe token to search for within the consequents 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.

◆ HasEmptyProduction()

bool Grammar::HasEmptyProduction ( const std::string & antecedent) const

Checks if a given antecedent has an empty production.

Parameters
antecedentThe left-hand side (LHS) symbol to check.
Returns
true if there exists an empty production for the antecedent, otherwise false.

An empty production is represented as <antecedent> -> ;, indicating that the antecedent can produce an empty string.

◆ SetAxiom()

void Grammar::SetAxiom ( const std::string & 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< std::string > Grammar::Split ( const std::string & s)

Splits a string into grammar symbols using the current symbol table.

This function tokenizes the input string s into a sequence of grammar symbols based on the known entries in the symbol table. It uses a greedy approach, matching the longest valid symbol at each step.

Parameters
sThe input string to split.
Returns
A vector of grammar symbols extracted from the string.

Member Data Documentation

◆ axiom_

std::string Grammar::axiom_

The axiom or entry point of the grammar.

◆ g_

std::unordered_map<std::string, std::vector<production> > Grammar::g_

Stores the grammar rules with each antecedent mapped to a list of productions.

◆ st_

SymbolTable Grammar::st_

Symbol table of the grammar.


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