#include <ll1_parser.hpp>
Public Member Functions | |
LL1Parser ()=default | |
LL1Parser (Grammar gr) | |
Constructs an LL1Parser with a grammar object and an input file. | |
bool | CreateLL1Table () |
Creates the LL(1) parsing table for the grammar. | |
void | First (std::span< const std::string > rule, std::unordered_set< std::string > &result) |
Calculates the FIRST set for a given production rule in a grammar. | |
void | ComputeFirstSets () |
Computes the FIRST sets for all non-terminal symbols in the grammar. | |
void | ComputeFollowSets () |
Computes the FOLLOW sets for all non-terminal symbols in the grammar. | |
std::unordered_set< std::string > | Follow (const std::string &arg) |
Computes the FOLLOW set for a given non-terminal symbol in the grammar. | |
std::unordered_set< std::string > | PredictionSymbols (const std::string &antecedent, const std::vector< std::string > &consequent) |
Computes the prediction symbols for a given production rule. | |
Public Attributes | |
ll1_table | ll1_t_ |
The LL(1) parsing table, mapping non-terminals and terminals to productions. | |
Grammar | gr_ |
Grammar object associated with this parser. | |
std::unordered_map< std::string, std::unordered_set< std::string > > | first_sets_ |
FIRST sets for each non-terminal in the grammar. | |
std::unordered_map< std::string, std::unordered_set< std::string > > | follow_sets_ |
FOLLOW sets for each non-terminal in the grammar. | |
|
default |
|
explicit |
void LL1Parser::ComputeFirstSets | ( | ) |
Computes the FIRST sets for all non-terminal symbols in the grammar.
This function calculates the FIRST set for each non-terminal symbol in the grammar by iteratively applying a least fixed-point algorithm. This approach ensures that the FIRST sets are fully populated by repeatedly expanding and updating the sets until no further changes occur (i.e., a fixed-point is reached).
void LL1Parser::ComputeFollowSets | ( | ) |
Computes the FOLLOW sets for all non-terminal symbols in the grammar.
The FOLLOW set of a non-terminal symbol A contains all terminal symbols that can appear immediately after A in any sentential form derived from the grammar's start symbol. Additionally, if A can be the last symbol in a derivation, the end-of-input marker ($
) is included in its FOLLOW set.
This function computes the FOLLOW sets using the following rules:
The computed FOLLOW sets are cached in the follow_sets_
member variable for later use by the parser.
first_sets_
member variable.bool LL1Parser::CreateLL1Table | ( | ) |
Creates the LL(1) parsing table for the grammar.
This function constructs the LL(1) parsing table by iterating over each production in the grammar and determining the appropriate cells for each non-terminal and director symbol (prediction symbol) combination. If the grammar is LL(1) compatible, each cell will contain at most one production, indicating no conflicts. If conflicts are found, the function will return false
, signaling that the grammar is not LL(1).
A -> α
, the function calculates the director symbols using the director_symbols
function.A
and each director symbol in the set.true
if the table is created successfully, indicating the grammar is LL(1) compatible; false
if any conflicts are detected, showing that the grammar does not meet LL(1) requirements. void LL1Parser::First | ( | std::span< const std::string > | rule, |
std::unordered_set< std::string > & | result ) |
Calculates the FIRST set for a given production rule in a grammar.
The FIRST set of a production rule contains all terminal symbols that can appear at the beginning of any string derived from that rule. If the rule can derive the empty string (epsilon), epsilon is included in the FIRST set.
This function computes the FIRST set by examining each symbol in the production rule:
rule | A span of strings representing the production rule for which to compute the FIRST set. Each string in the span is a symbol (either terminal or non-terminal). |
result | A reference to an unordered set of strings where the computed FIRST set will be stored. The set will contain all terminal symbols that can start derivations of the rule, and possibly epsilon if the rule can derive an empty string. |
std::unordered_set< std::string > LL1Parser::Follow | ( | const std::string & | arg | ) |
Computes the FOLLOW set for a given non-terminal symbol in the grammar.
The FOLLOW set for a non-terminal symbol includes all symbols that can appear immediately to the right of that symbol in any derivation, as well as any end-of-input markers if the symbol can appear at the end of derivations. FOLLOW sets are used in LL(1) parsing table construction to determine possible continuations after a non-terminal.
arg | Non-terminal symbol for which to compute the FOLLOW set. |
arg
. std::unordered_set< std::string > LL1Parser::PredictionSymbols | ( | const std::string & | antecedent, |
const std::vector< std::string > & | consequent ) |
Computes the prediction symbols for a given production rule.
The prediction symbols for a rule, determine the set of input symbols that can trigger this rule in the parsing table. This function calculates the prediction symbols based on the FIRST set of the consequent and, if epsilon (the empty symbol) is in the FIRST set, also includes the FOLLOW set of the antecedent.
antecedent | The left-hand side non-terminal symbol of the rule. |
consequent | A vector of symbols on the right-hand side of the rule (production body). |
std::unordered_map<std::string, std::unordered_set<std::string> > LL1Parser::first_sets_ |
FIRST sets for each non-terminal in the grammar.
std::unordered_map<std::string, std::unordered_set<std::string> > LL1Parser::follow_sets_ |
FOLLOW sets for each non-terminal in the grammar.
ll1_table LL1Parser::ll1_t_ |
The LL(1) parsing table, mapping non-terminals and terminals to productions.