for (line_no, line) in enbf.lines().enumerate() {
let line = line.trim();
if line.is_empty() || line.starts_with("#") {
continue;
}
let parts: Vec<&str> = line.split("::=").collect();
if parts.len() != 2 {
return Err(format!("Invalid rule at line {}", line_no + 1));
}
let head = parts[0].trim().to_string();
non_terminals.insert(head.clone());
let bodies_str = parts[1];
let bodies: Vec<Vec<String>> = bodies_str.split('|').map(|body| {
body.trim().split_whitespace().map(|s| s.to_string()).collect()
}).collect();
rules.push(Rule { head, bodies });
}
// Identify terminals
for rule in &rules {
for body in &rule.bodies {
for symbol in body {
if !symbol.starts_with("<") && !symbol.ends_with(">")
&& !symbol.starts_with("\"") && !symbol.starts_with("'")
{
terminals.insert(symbol.clone());
}
}
}
}
// Remove non-terminals from terminals set
for nt in &non_terminals {
terminals.remove(nt);
}
let mut grammar = Grammar {
rules,
terminals,
non_terminals,
first_sets: HashMap::new(),
follow_sets: HashMap::new(),
};
grammar.compute_first_sets()?;
grammar.compute_follow_sets()?;
Ok(grammar)
}
/// Computes FIRST sets for all non-terminals
fn compute_first_sets(&mut self) -> Result<(), String> {
// Initialize FIRST sets
for nt in &self.non_terminals {
self.first_sets.insert(nt.clone(), HashSet::new());
}
let mut changed = true;
while changed {
changed = false;
for rule in &self.rules {
let head = &rule.head;
let head_first = self.first_sets.get_mut(head).unwrap();
for body in &rule.bodies {
let mut can_produce_epsilon = true;
for symbol in body {
if self.non_terminals.contains(symbol) {
let symbol_first = self.first_sets.get(symbol).unwrap();
let before = head_first.len();
head_first.extend(symbol_first.iter().filter(|&&s| s != "ε").cloned());
let after = head_first.len();
if after > before {
changed = true;
}
if !symbol_first.contains("ε") {
can_produce_epsilon = false;
break;
}
} else {
let before = head_first.len();
head_first.insert(symbol.clone());
let after = head_first.len();
if after > before {
changed = true;
}
can_produce_epsilon = false;
break;
}
}
if can_produce_epsilon {
let before = head_first.len();
head_first.insert("ε".to_string());
let after = head_first.len();
if after > before {
changed = true;
}
}
}
}
}
Ok(())
}
/// Computes FOLLOW sets for all non-terminals
fn compute_follow_sets(&mut self) -> Result<(), String> {
// Initialize FOLLOW sets
for nt in &self.non_terminals {
self.follow_sets.insert(nt.clone(), HashSet::new());
}
// Assume the first rule's head is the start symbol
if let Some(start_rule) = self.rules.first() {
self.follow_sets.get_mut(&start_rule.head).unwrap().insert("$".to_string()); // End marker
} else {
return Err("No rules found in the grammar.".to_string());
}
let mut changed = true;
while changed {
changed = false;
for rule in &self.rules {
let head = &rule.head;
for body in &rule.bodies {
for i in 0..body.len() {
let symbol = &body[i];
if self.non_terminals.contains(symbol) {
let follow_set = self.follow_sets.get_mut(symbol).unwrap();
let before = follow_set.len();
// Compute FIRST of the rest of the body
let mut first_of_rest = HashSet::new();
let mut can_add_follow = true;
for sym in body.iter().skip(i + 1) {
if self.non_terminals.contains(sym) {
let sym_first = self.first_sets.get(sym).unwrap();
for f in sym_first {
if f != "ε" {
first_of_rest.insert(f.clone());
}
}
if !sym_first.contains("ε") {
can_add_follow = false;
break;
}
} else {
first_of_rest.insert(sym.clone());
can_add_follow = false;
break;
}
}
// Add FIRST(rest) to FOLLOW(symbol)
follow_set.extend(first_of_rest);
if can_add_follow {
// If FIRST(rest) contains ε, add FOLLOW(head) to FOLLOW(symbol)
let head_follow = self.follow_sets.get(head).unwrap().clone();
follow_set.extend(head_follow);
}
let after = follow_set.len();
if after > before {
changed = true;
}
}
}
}
}
}
Ok(())
}
/// Generates Rust code for the parser
fn generate_rust(&self, grammar_type: &str) -> String {
let mut code = String::new();
// Header with documentation
code.push_str("//! # Generated Parser\n");
code.push_str("//!\n");
code.push_str("//! This parser was automatically generated by **Thepg**, a Rust-based parser generator.\n");
code.push_str("//! It is designed to parse inputs based on the provided ENBF grammar.\n");
code.push_str("//!\n");
code.push_str("//! ## Features\n");
code.push_str("//! - Lexer-free parsing\n");
code.push_str("//! - Supports LL(1) and LL(k) parsing strategies\n");
code.push_str("//!\n");
code.push_str("//! ## Usage\n");
code.push_str("//! ```rust\n");
code.push_str("//! use crate::Parser;\n");
code.push_str("//!\n");
code.push_str("//! fn main() {\n");
code.push_str("//! let input = \"your_input_here\";\n");
code.push_str("//! let mut parser = Parser::new(input);\n");
code.push_str("//! match parser.parse() {\n");
code.push_str("//! Ok(_) => println!(\"Parsing succeeded.\"),\n");
code.push_str("//! Err(e) => println!(\"Parsing failed: {}\", e),\n");
code.push_str("//! }\n");
code.push_str("//! }\n");
code.push_str("//! ```\n");
code.push_str("//!\n");
code.push_str("//! ## Example\n");
code.push_str("//! Given the following grammar:\n");
code.push_str("//!\n");
code.push_str("//! ```enbf\n");
code.push_str("//! Expr ::= Term ExprPrime\n");
code.push_str("//! ExprPrime ::= \"+\" Term ExprPrime | \"-\" Term ExprPrime | ε\n");
code.push_str("//! Term ::= Factor TermPrime\n");
code.push_str("//! TermPrime ::= \"*\" Factor TermPrime | \"/\" Factor TermPrime | ε\n");
code.push_str("//! Factor ::= \"(\" Expr \")\" | \"id\"\n");
code.push_str("//! ```\n");
code.push_str("//!\n");
code.push_str("//! After generating the parser, you can use it to parse arithmetic expressions like `id + id * id`.\n");
code.push_str("//!\n");
// Token definitions
code.push_str("#[derive(Debug, PartialEq, Clone)]\n");
code.push_str("pub enum Token {\n");
for terminal in &self.terminals {
code.push_str(&format!(" {},\n", Self::sanitize_token(terminal)));
}
code.push_str(" EOF,\n");
code.push_str("}\n\n");
// Lexer comments
code.push_str("// The lexer is integrated within the parser and is lexer-free.\n");
// Parser struct with documentation
code.push_str("/// The Parser struct responsible for parsing the input string.\n");
code.push_str("#[derive(Debug)]\n");
code.push_str("pub struct Parser<'a> {\n");
code.push_str(" input: Peekable<Chars<'a>>,\n");
code.push_str(" current_token: Token,\n");
code.push_str("}\n\n");
// Implement Parser with documentation
code.push_str("impl<'a> Parser<'a> {\n");
code.push_str(" /// Creates a new parser instance with the given input string.\n");
code.push_str(" ///\n");
code.push_str(" /// # Arguments\n");
code.push_str(" ///\n");
code.push_str(" /// * `input` - A string slice that holds the input to be parsed.\n");
code.push_str(" ///\n");
code.push_str(" /// # Example\n");
code.push_str(" ///\n");
code.push_str(" /// ```rust\n");
code.push_str(" /// let mut parser = Parser::new(\"id + id * id\");\n");
code.push_str(" /// ```\n");
code.push_str(" pub fn new(input: &'a str) -> Self {\n");
code.push_str(" let mut chars = input.chars().peekable();\n");
code.push_str(" let first_token = Parser::next_token(&mut chars);\n");
code.push_str(" Parser {\n");
code.push_str(" input: chars,\n");
code.push_str(" current_token: first_token,\n");
code.push_str(" }\n");
code.push_str(" }\n\n");
// Tokenizer with documentation
code.push_str(" /// Retrieves the next token from the input.\n");
code.push_str(" ///\n");
code.push_str(" /// # Arguments\n");
code.push_str(" ///\n");
code.push_str(" /// * `chars` - A mutable reference to the Peekable character iterator.\n");
code.push_str(" fn next_token(chars: &mut Peekable<Chars>) -> Token {\n");
code.push_str(" while let Some(&c) = chars.peek() {\n");
code.push_str(" match c {\n");
for terminal in &self.terminals {
if terminal.starts_with("\"") && terminal.ends_with("\"") {
let symbol = &terminal[1..terminal.len()-1];
if symbol.len() == 1 {
// Single character token
code.push_str(&format!(" '{}' => {{ chars.next(); Token::{} }},\n",
symbol.chars().next().unwrap(),
Self::sanitize_token(terminal)));
} else {
// Multi-character token (not implemented)
// Placeholder for potential future implementation
code.push_str(&format!(
" _ if chars.as_str().starts_with(\"{}\") => {{ for _ in 0..{} {{ chars.next(); }} Token::{} }},\n",
symbol,
symbol.len(),
Self::sanitize_token(terminal)
));
}
} else if terminal.starts_with("'") && terminal.ends_with("'") {
let symbol = &terminal[1..terminal.len()-1];
if symbol.len() == 1 {
// Single character token
code.push_str(&format!(" '{}' => {{ chars.next(); Token::{} }},\n",
symbol.chars().next().unwrap(),
Self::sanitize_token(terminal)));
}
} else {
// For simplicity, treat as identifier or number
code.push_str(&format!(" _ if c.is_alphabetic() => Token::{},\n",
Self::sanitize_token(terminal)));
}
}
code.push_str(" _ if c.is_whitespace() => { chars.next(); continue },\n");
code.push_str(" _ => { chars.next(); Token::EOF },\n");
code.push_str(" }\n");
code.push_str(" }\n");
code.push_str(" Token::EOF\n");
code.push_str(" }\n\n");
// Match and consume functions with documentation
code.push_str(" /// Consumes the current token if it matches the expected token.\n");
code.push_str(" ///\n");
code.push_str(" /// # Arguments\n");
code.push_str(" ///\n");
code.push_str(" /// * `token` - The expected `Token` to consume.\n");
code.push_str(" ///\n");
code.push_str(" /// # Returns\n");
code.push_str(" ///\n");
code.push_str(" /// * `Ok(())` if the token matches and is consumed successfully.\n");
code.push_str(" /// * `Err(String)` with an error message if the token does not match.\n");
code.push_str(" fn eat(&mut self, token: Token) -> Result<(), String> {\n");
code.push_str(" if self.current_token == token {\n");
code.push_str(" self.current_token = Parser::next_token(&mut self.input);\n");
code.push_str(" Ok(())\n");
code.push_str(" } else {\n");
code.push_str(" Err(format!(\"Expected {:?}, found {:?}\", token, self.current_token))\n");
code.push_str(" }\n");
code.push_str(" }\n\n");
// Generate parsing functions using LL(1) table with documentation
if grammar_type.to_uppercase() == "LL1" {
for rule in &self.rules {
let func_name = Self::sanitize_func(&rule.head);
code.push_str(&format!(" /// Parses the `{}` non-terminal.\n", rule.head));
code.push_str(" ///\n");
code.push_str(" /// # Returns\n");
code.push_str(" ///\n");
code.push_str(" /// * `Ok(())` if parsing succeeds.\n");
code.push_str(" /// * `Err(String)` with an error message if parsing fails.\n");
code.push_str(&format!(" pub fn {}(&mut self) -> Result<(), String> {{\n", func_name));
code.push_str(" match self.current_token {\n");
for body in &rule.bodies {
if let Some(first) = Grammar::first_of_body(body, &self.first_sets) {
for terminal in &first {
if terminal != "ε" {
code.push_str(&format!(" Token::{} => {{\n",
Self::sanitize_token(terminal)));
for symbol in body {
if self.non_terminals.contains(symbol) {
let sub_func = Self::sanitize_func(&symbol[1..symbol.len()-1]);
code.push_str(&format!(" self.{}()?;\n", sub_func));
} else {
let tok = Self::sanitize_token(symbol);
code.push_str(&format!(" self.eat(Token::{})?;\n", tok));
}
}
code.push_str(" Ok(())\n");
code.push_str(" },\n");
}
}
if Grammar::body_can_produce_epsilon(body, &self.first_sets) {
let follow = self.follow_sets.get(&rule.head).unwrap();
for terminal in follow {
code.push_str(&format!(" Token::{} => {{\n",
Self::sanitize_token(terminal)));
code.push_str(" // ε-production\n");
code.push_str(" Ok(())\n");
code.push_str(" },\n");
}
}
}
}
// Error case
code.push_str(" _ => Err(format!(\"Unexpected token {:?} in parsing {}\", self.current_token, \"");
code.push_str(&rule.head);
code.push_str("\")),\n");
code.push_str(" }\n");
code.push_str(" }\n\n");
}
} else {
// LL(k) not implemented in this example
code.push_str(" // LL(k) parsing not implemented yet\n");
for rule in &self.rules {
let func_name = Self::sanitize_func(&rule.head);
code.push_str(&format!(" /// Parses the `{}` non-terminal.\n", rule.head));
code.push_str(" ///\n");
code.push_str(" /// # Returns\n");
code.push_str(" ///\n");
code.push_str(" /// * `Ok(())` if parsing succeeds.\n");
code.push_str(" /// * `Err(String)` with an error message if parsing fails.\n");
code.push_str(&format!(" pub fn {}(&mut self) -> Result<(), String> {{\n", func_name));
code.push_str(" // Implementation for LL(k) would go here\n");
code.push_str(" Ok(())\n");
code.push_str(" }\n\n");
}
}
// Start symbol with documentation
if let Some(start_rule) = self.rules.first() {
let func_name = Self::sanitize_func(&start_rule.head);
code.push_str(" /// Initiates the parsing process starting from the start symbol.\n");
code.push_str(" ///\n");
code.push_str(" /// # Returns\n");
code.push_str(" ///\n");
code.push_str(" /// * `Ok(())` if parsing succeeds.\n");
code.push_str(" /// * `Err(String)` with an error message if parsing fails or extra tokens are found.\n");
code.push_str(" pub fn parse(&mut self) -> Result<(), String> {\n");
code.push_str(&format!(" self.{}()?\n", func_name));
code.push_str(" .and_then(|_| {\n");
code.push_str(" if self.current_token == Token::EOF {\n");
code.push_str(" Ok(())\n");
code.push_str(" } else {\n");
code.push_str(" Err(\"Unexpected tokens after parsing complete\".to_string())\n");
code.push_str(" }\n");
code.push_str(" })\n");
code.push_str(" }\n");
}
// End of impl
code.push_str("}\n\n");
// Example usage with documentation
code.push_str("/// Example usage of the generated parser.\n");
code.push_str("///\n");
code.push_str("/// ```rust\n");
code.push_str("/// use crate::Parser;\n");
code.push_str("///\n");
code.push_str("/// fn main() {\n");
code.push_str("/// let input = \"id + id * id\";\n");
code.push_str("/// let mut parser = Parser::new(input);\n");
code.push_str("/// match parser.parse() {\n");
code.push_str("/// Ok(_) => println!(\"Parsing succeeded.\"),\n");
code.push_str("/// Err(e) => println!(\"Parsing failed: {}\", e),\n");
code.push_str("/// }\n");
code.push_str("/// }\n");
code.push_str("/// ```\n\n");
// Test module with documentation and example tests
code.push_str("#[cfg(test)]\n");
code.push_str("mod tests {\n");
code.push_str(" use super::*;\n\n");
code.push_str(" /// Tests the parser with a valid arithmetic expression.\n");
code.push_str(" #[test]\n");
code.push_str(" fn test_parser_valid() {\n");
code.push_str(" let input = \"id + id * id\";\n");
code.push_str(" let mut parser = Parser::new(input);\n");
code.push_str(" assert!(parser.parse().is_ok());\n");
code.push_str(" }\n\n");
code.push_str(" /// Tests the parser with an input containing parentheses.\n");
code.push_str(" #[test]\n");
code.push_str(" fn test_parser_with_parentheses() {\n");
code.push_str(" let input = \"( id + id ) * id\";\n");
code.push_str(" let mut parser = Parser::new(input);\n");
code.push_str(" assert!(parser.parse().is_ok());\n");
code.push_str(" }\n\n");
code.push_str(" /// Tests the parser with an invalid arithmetic expression.\n");
code.push_str(" #[test]\n");
code.push_str(" fn test_parser_invalid() {\n");
code.push_str(" let input = \"id + * id\";\n");
code.push_str(" let mut parser = Parser::new(input);\n");
code.push_str(" assert!(parser.parse().is_err());\n");
code.push_str(" }\n");
code.push_str("}\n");
code
}
/// Determines the FIRST set for a given body
fn first_of_body(body: &Vec<String>, first_sets: &HashMap<String, HashSet<String>>) -> Option<HashSet<String>> {
let mut first = HashSet::new();
for symbol in body {
if first_sets.contains_key(symbol) {
let sym_first = &first_sets[symbol];
first.extend(sym_first.iter().filter(|&&s| s != "ε").cloned());
if !sym_first.contains("ε") {
return Some(first);
}
} else {
first.insert(symbol.clone());
return Some(first);
}
}
first.insert("ε".to_string());
Some(first)
}
/// Checks if a body can produce ε
fn body_can_produce_epsilon(body: &Vec<String>, first_sets: &HashMap<String, HashSet<String>>) -> bool {
for symbol in body {
if first_sets.contains_key(symbol) {
if !first_sets[symbol].contains("ε") {
return false;
}
} else {
return false;
}
}
true
}
/// Sanitizes token names to be Rust-compatible
fn sanitize_token(token: &str) -> String {
token.trim_matches('"')
.trim_matches('\'')
.replace("+", "PLUS")
.replace("-", "MINUS")
.replace("*", "STAR")
.replace("/", "SLASH")
.replace("(", "L_PAREN")
.replace(")", "R_PAREN")
.replace("{", "L_BRACE")
.replace("}", "R_BRACE")
.replace("[", "L_BRACKET")
.replace("]", "R_BRACKET")
.replace(",", "COMMA")
.replace(";", "SEMICOLON")
.replace(":", "COLON")
.replace("<", "")
.replace(">", "")
.replace("|", "OR")
.replace("=", "EQ")
.replace("!", "EXCLAMATION")
.replace("?", "QUESTION")
.replace("#", "HASH")
.replace("$", "DOLLAR")
.replace("&", "AMPERSAND")
.replace("@", "AT")
.replace("~", "TILDE")
.replace(".", "DOT")
.replace("'", "")
.to_uppercase()
}
/// Sanitizes function names to be Rust-compatible
fn sanitize_func(func: &str) -> String {
func.to_lowercase()
.replace("<", "")
.replace(">", "")
.replace("-", "_")
.replace(" ", "_")
}
#!/usr/bin/env rust-script
//! # Thepg: Enhanced Parser Generator in Rust
//!
//! **Thepg** is an advanced parser generator that processes Extended Backus–Naur Form (ENBF) grammars
//! and produces robust, compile-ready Rust code for parsing based on the provided grammar.
//!
//! ## Features
//! - Supports LL(1) and LL(k) parsing strategies
//! - Generates lexing-capable parsers with support for identifiers and literals
//! - Outputs well-structured Rust code across multiple modules
//! - Constructs Abstract Syntax Trees (AST) during parsing
//! - Includes comprehensive documentation and example usage within the generated parser
//!
//! ## Usage
//! To generate a parser, run the following command:
//!
//! ```bash
//! cargo run --release -- grammar.enbf -o src/generated --grammar-type LL1
//! ```
//!
//! Replace `grammar.enbf` with your ENBF grammar file, and `src/generated` with your desired output directory.
//!
//! ## Example
//! Given the following `arithmetic.enbf` grammar:
//!
//! ```enbf
//! # arithmetic.enbf
//! Expr ::= Term ExprPrime
//! ExprPrime ::= "+" Term ExprPrime | "-" Term ExprPrime | ε
//! Term ::= Factor TermPrime
//! TermPrime ::= "*" Factor TermPrime | "/" Factor TermPrime | ε
//! Factor ::= "(" Expr ")" | "id" | "number"
//! ```
//!
//! Running Thepg:
//!
//! ```bash
//! cargo run --release -- arithmetic.enbf -o src/generated --grammar-type LL1
//! ```
//!
//! The above command will generate Rust modules within `src/generated` containing the lexer, AST definitions, and parser for arithmetic expressions.
//!
//! ## Generated Parser Structure
//! The generated parser will include:
//! - `lexer.rs`: Token definitions and lexer implementation
//! - `ast.rs`: AST node structures
//! - `parser.rs`: Parser struct with parsing functions and AST construction
//! - `main.rs`: Example usage of the parser
//! - Comprehensive documentation and example tests within the modules
use std::env;
use std::fs;
use std::io::{self, Write};
use std::collections::{HashSet, HashMap, VecDeque, HashMap};
use std::path::Path;
/// Represents a grammar rule
#[derive(Debug, Clone)]
struct Rule {
head: String,
bodies: Vec<Vec<String>>,
}
/// Represents the parsed grammar
#[derive(Debug)]
struct Grammar {
rules: Vec<Rule>,
terminals: HashSet<String>,
non_terminals: HashSet<String>,
first_sets: HashMap<String, HashSet<String>>,
follow_sets: HashMap<String, HashSet<String>>,
}
impl Grammar {
/// Parses the ENBF grammar from a string
fn parse(enbf: &str) -> Result<Self, String> {
let mut rules = Vec::new();
let mut non_terminals = HashSet::new(); // To store non-terminals
let mut terminals = HashSet::new();
for (line_no, line) in enbf.lines().enumerate() {
let line = line.trim();
if line.is_empty() || line.starts_with("#") {
continue;
}
let parts: Vec<&str> = line.split("::=").collect();
if parts.len() != 2 {
return Err(format!("Invalid rule at line {}", line_no + 1));
}
let head = parts[0].trim().to_string();
non_terminals.insert(head.clone());
let bodies_str = parts[1];
let bodies: Vec<Vec<String>> = bodies_str.split('|').map(|body| {
body.trim().split_whitespace().map(|s| s.to_string()).collect()
}).collect();
rules.push(Rule { head, bodies });
}
// Identify terminals
for rule in &rules {
for body in &rule.bodies {
for symbol in body {
if !symbol.starts_with("<") && !symbol.ends_with(">")
&& !symbol.starts_with("\"") && !symbol.starts_with("'")
{
terminals.insert(symbol.clone());
}
}
}
}
// Remove non-terminals from terminals set
for nt in &non_terminals {
terminals.remove(nt);
}
let mut grammar = Grammar {
rules,
terminals,
non_terminals,
first_sets: HashMap::new(),
follow_sets: HashMap::new(),
};
grammar.compute_first_sets()?;
grammar.compute_follow_sets()?;
Ok(grammar)
}
/// Computes FIRST sets for all non-terminals
fn compute_first_sets(&mut self) -> Result<(), String> {
// Initialize FIRST sets
for nt in &self.non_terminals {
self.first_sets.insert(nt.clone(), HashSet::new());
}
let mut changed = true;
while changed {
changed = false;
for rule in &self.rules {
let head = &rule.head;
let head_first = self.first_sets.get_mut(head).unwrap();
for body in &rule.bodies {
let mut can_produce_epsilon = true;
for symbol in body {
if self.non_terminals.contains(symbol) {
let symbol_first = self.first_sets.get(symbol).unwrap();
let before = head_first.len();
head_first.extend(symbol_first.iter().filter(|&&s| s != "ε").cloned());
let after = head_first.len();
if after > before {
changed = true;
}
if !symbol_first.contains("ε") {
can_produce_epsilon = false;
break;
}
} else {
let before = head_first.len();
head_first.insert(symbol.clone());
let after = head_first.len();
if after > before {
changed = true;
}
can_produce_epsilon = false;
break;
}
}
if can_produce_epsilon {
let before = head_first.len();
head_first.insert("ε".to_string());
let after = head_first.len();
if after > before {
changed = true;
}
}
}
}
}
Ok(())
}
/// Computes FOLLOW sets for all non-terminals
fn compute_follow_sets(&mut self) -> Result<(), String> {
// Initialize FOLLOW sets
for nt in &self.non_terminals {
self.follow_sets.insert(nt.clone(), HashSet::new());
}
// Assume the first rule's head is the start symbol
if let Some(start_rule) = self.rules.first() {
self.follow_sets.get_mut(&start_rule.head).unwrap().insert("$".to_string()); // End marker
} else {
return Err("No rules found in the grammar.".to_string());
}
let mut changed = true;
while changed {
changed = false;
for rule in &self.rules {
let head = &rule.head;
for body in &rule.bodies {
for i in 0..body.len() {
let symbol = &body[i];
if self.non_terminals.contains(symbol) {
let follow_set = self.follow_sets.get_mut(symbol).unwrap();
let before = follow_set.len();
// Compute FIRST of the rest of the body
let mut first_of_rest = HashSet::new();
let mut can_add_follow = true;
for sym in body.iter().skip(i + 1) {
if self.non_terminals.contains(sym) {
let sym_first = self.first_sets.get(sym).unwrap();
for f in sym_first {
if f != "ε" {
first_of_rest.insert(f.clone());
}
}
if !sym_first.contains("ε") {
can_add_follow = false;
break;
}
} else {
first_of_rest.insert(sym.clone());
can_add_follow = false;
break;
}
}
// Add FIRST(rest) to FOLLOW(symbol)
follow_set.extend(first_of_rest);
if can_add_follow {
// If FIRST(rest) contains ε, add FOLLOW(head) to FOLLOW(symbol)
let head_follow = self.follow_sets.get(head).unwrap().clone();
follow_set.extend(head_follow);
}
let after = follow_set.len();
if after > before {
changed = true;
}
}
}
}
}
}
Ok(())
}
/// Generates Rust code for the parser, lexer, and AST
fn generate_rust(&self, output_dir: &str, grammar_type: &str) -> Result<(), String> {
// Create output directory if it doesn't exist
fs::create_dir_all(output_dir)
.map_err(|e| format!("Failed to create output directory: {}", e))?;
// Generate lexer.rs
let lexer_code = self.generate_lexer();
fs::write(Path::new(output_dir).join("lexer.rs"), lexer_code)
.map_err(|e| format!("Failed to write lexer.rs: {}", e))?;
// Generate ast.rs
let ast_code = self.generate_ast();
fs::write(Path::new(output_dir).join("ast.rs"), ast_code)
.map_err(|e| format!("Failed to write ast.rs: {}", e))?;
// Generate parser.rs
let parser_code = self.generate_parser(grammar_type)?;
fs::write(Path::new(output_dir).join("parser.rs"), parser_code)
.map_err(|e| format!("Failed to write parser.rs: {}", e))?;
// Generate main.rs
let main_code = self.generate_main();
fs::write(Path::new(output_dir).join("main.rs"), main_code)
.map_err(|e| format!("Failed to write main.rs: {}", e))?;
Ok(())
}
/// Generates the lexer module
fn generate_lexer(&self) -> String {
let mut code = String::new();
code.push_str("//! # Lexer Module\n");
code.push_str("//!\n");
code.push_str("use std::iter::Peekable;\n");
code.push_str("use std::str::Chars;\n\n");
code.push_str("/// Represents the different types of tokens.\n");
code.push_str("#[derive(Debug, PartialEq, Clone)]\n");
code.push_str("pub enum Token {\n");
for terminal in &self.terminals {
if terminal == "id" {
code.push_str(" ID(String),\n");
} else if terminal == "number" {
code.push_str(" NUMBER(i32),\n");
} else {
let tok = Self::sanitize_token(terminal);
code.push_str(&format!(" {},\n", tok));
}
}
code.push_str(" EOF,\n");
code.push_str("}\n\n");
code.push_str("pub struct Lexer<'a> {\n");
code.push_str(" input: Peekable<Chars<'a>>,\n");
code.push_str("}\n\n");
code.push_str("impl<'a> Lexer<'a> {\n");
code.push_str(" /// Creates a new Lexer instance.\n");
code.push_str(" pub fn new(input: &'a str) -> Self {\n");
code.push_str(" Lexer { input: input.chars().peekable() }\n");
code.push_str(" }\n\n");
code.push_str(" /// Retrieves the next token from the input.\n");
code.push_str(" pub fn next_token(&mut self) -> Token {\n");
code.push_str(" while let Some(&c) = self.input.peek() {\n");
code.push_str(" match c {\n");
for terminal in &self.terminals {
if terminal == "id" {
code.push_str(" c if c.is_alphabetic() => {\n");
code.push_str(" let mut ident = String::new();\n");
code.push_str(" while let Some(&c) = self.input.peek() {\n");
code.push_str(" if c.is_alphanumeric() {\n");
code.push_str(" ident.push(c);\n");
code.push_str(" self.input.next();\n");
code.push_str(" } else {\n");
code.push_str(" break;\n");
code.push_str(" }\n");
code.push_str(" }\n");
code.push_str(" return Token::ID(ident);\n");
code.push_str(" },\n");
} else if terminal == "number" {
code.push_str(" c if c.is_digit(10) => {\n");
code.push_str(" let mut number = String::new();\n");
code.push_str(" while let Some(&c) = self.input.peek() {\n");
code.push_str(" if c.is_digit(10) {\n");
code.push_str(" number.push(c);\n");
code.push_str(" self.input.next();\n");
code.push_str(" } else {\n");
code.push_str(" break;\n");
code.push_str(" }\n");
code.push_str(" }\n");
code.push_str(" return Token::NUMBER(number.parse().unwrap());\n");
code.push_str(" },\n");
} else if terminal.starts_with("\"") && terminal.ends_with("\"") {
let symbol = &terminal[1..terminal.len()-1];
if symbol.len() == 1 {
code.push_str(&format!(" '{}' => {{\n", symbol.chars().next().unwrap()));
let tok = Self::sanitize_token(terminal);
code.push_str(&format!(" self.input.next();\n"));
code.push_str(&format!(" return Token::{};\n", tok));
code.push_str(" },\n");
} else {
// Handle multi-character symbols if any
code.push_str(&format!(" _ if self.input.as_str().starts_with(\"{}\") => {{\n", symbol));
code.push_str(&format!(" for _ in 0..{} {{ self.input.next(); }}\n", symbol.len()));
let tok = Self::sanitize_token(terminal);
code.push_str(&format!(" return Token::{};\n", tok));
code.push_str(" },\n");
}
}
}
code.push_str(" c if c.is_whitespace() => { self.input.next(); continue },\n");
code.push_str(" _ => { self.input.next(); return Token::EOF },\n");
code.push_str(" }\n");
code.push_str(" }\n");
code.push_str(" Token::EOF\n");
code.push_str(" }\n");
code.push_str("}\n");
code
}
/// Generates the AST module
fn generate_ast(&self) -> String {
let mut code = String::new();
code.push_str("//! # Abstract Syntax Tree (AST) Module\n");
code.push_str("//!\n");
code.push_str("/// Represents the Abstract Syntax Tree.\n");
code.push_str("#[derive(Debug)]\n");
code.push_str("pub enum AST {\n");
// Example for Expr ::= Term ExprPrime
code.push_str(" Expr(Box<AST>, Box<AST>),\n");
code.push_str(" ExprPrime(Box<AST>, Operator, Box<AST>),\n");
code.push_str(" Term(Box<AST>, Box<AST>),\n");
code.push_str(" TermPrime(Box<AST>, Operator, Box<AST>),\n");
code.push_str(" Factor(Factor),\n");
code.push_str("}\n\n");
code.push_str("#[derive(Debug)]\n");
code.push_str("pub enum Operator {\n");
code.push_str(" Add,\n");
code.push_str(" Subtract,\n");
code.push_str(" Multiply,\n");
code.push_str(" Divide,\n");
code.push_str("}\n\n");
code.push_str("#[derive(Debug)]\n");
code.push_str("pub enum Factor {\n");
code.push_str(" Expr(Box<AST>),\n");
code.push_str(" Id(String),\n");
code.push_str(" Number(i32),\n");
code.push_str("}\n");
code
}
/// Generates the parser module
fn generate_parser(&self, grammar_type: &str) -> Result<String, String> {
let mut code = String::new();
code.push_str("//! # Parser Module\n");
code.push_str("//!\n");
code.push_str("use crate::lexer::{Token, Lexer};\n");
code.push_str("use crate::ast::{AST, Operator, Factor};\n");
code.push_str("use std::iter::Peekable;\n\n");
code.push_str("/// The Parser struct responsible for parsing the input and constructing the AST.\n");
code.push_str("#[derive(Debug)]\n");
code.push_str("pub struct Parser<'a> {\n");
code.push_str(" lexer: Lexer<'a>,\n");
code.push_str(" current_token: Token,\n");
code.push_str("}\n\n");
code.push_str("impl<'a> Parser<'a> {\n");
code.push_str(" /// Creates a new Parser instance with the given input string.\n");
code.push_str(" pub fn new(input: &'a str) -> Self {\n");
code.push_str(" let mut lexer = Lexer::new(input);\n");
code.push_str(" let first_token = lexer.next_token();\n");
code.push_str(" Parser {\n");
code.push_str(" lexer,\n");
code.push_str(" current_token: first_token,\n");
code.push_str(" }\n");
code.push_str(" }\n\n");
code.push_str(" /// Consumes the current token if it matches the expected token.\n");
code.push_str(" fn eat(&mut self, token: Token) -> Result<(), String> {\n");
code.push_str(" if self.current_token == token {\n");
code.push_str(" self.current_token = self.lexer.next_token();\n");
code.push_str(" Ok(())\n");
code.push_str(" } else {\n");
code.push_str(" Err(format!(\"Expected {:?}, found {:?}\", token, self.current_token))\n");
code.push_str(" }\n");
code.push_str(" }\n\n");
// Generate parsing functions
if grammar_type.to_uppercase() == "LL1" {
for rule in &self.rules {
let func_name = Self::sanitize_func(&rule.head);
code.push_str(&format!(" /// Parses the `{}` non-terminal.\n", rule.head));
code.push_str(" fn ");
code.push_str(&format!("{}(&mut self) -> Result<AST, String> {{\n", func_name));
code.push_str(" match &self.current_token {\n");
for body in &rule.bodies {
if let Some(first_set) = Grammar::first_of_body(body, &self.first_sets) {
for terminal in &first_set {
if terminal != "ε" {
let tok = Self::sanitize_token_literal(terminal);
code.push_str(&format!(" {}\t=> {{\n", tok));
// Construct AST nodes based on the body
let mut ast_construction = self.construct_ast(&body)?;
code.push_str(&ast_construction);
code.push_str(" },\n");
}
}
if Grammar::body_can_produce_epsilon(body, &self.first_sets) {
let follow = self.follow_sets.get(&rule.head).unwrap();
for terminal in follow {
let tok = Self::sanitize_token_literal(terminal);
code.push_str(&format!(" {}\t=> {{\n", tok));
code.push_str(" // ε-production\n");
code.push_str(" Ok(AST::ε)\n");
code.push_str(" },\n");
}
}
}
}
// Error case
code.push_str(" _ => Err(format!(\"Unexpected token {:?} in parsing {}\", self.current_token, \"");
code.push_str(&rule.head);
code.push_str("\")),\n");
code.push_str(" }\n");
code.push_str(" }\n\n");
}
} else {
// LL(k) not implemented in this example
for rule in &self.rules {
let func_name = Self::sanitize_func(&rule.head);
code.push_str(&format!(" /// Parses the `{}` non-terminal.\n", rule.head));
code.push_str(" pub fn ");
code.push_str(&format!("{}(&mut self) -> Result<AST, String> {{\n", func_name));
code.push_str(" // Implementation for LL(k) would go here\n");
code.push_str(" Ok(AST::ε)\n");
code.push_str(" }\n\n");
}
}
// Start symbol with documentation
if let Some(start_rule) = self.rules.first() {
let func_name = Self::sanitize_func(&start_rule.head);
code.push_str(" /// Initiates the parsing process starting from the start symbol.\n");
code.push_str(" ///\n");
code.push_str(" /// # Returns\n");
code.push_str(" ///\n");
code.push_str(" /// * `Ok(AST)` with the constructed AST if parsing succeeds.\n");
code.push_str(" /// * `Err(String)` with an error message if parsing fails or extra tokens are found.\n");
code.push_str(" pub fn parse(&mut self) -> Result<AST, String> {\n");
code.push_str(&format!(" let node = self.{}()?;\n", func_name));
code.push_str(" if self.current_token == Token::EOF {\n");
code.push_str(" Ok(node)\n");
code.push_str(" } else {\n");
code.push_str(" Err(\"Unexpected tokens after parsing complete\".to_string())\n");
code.push_str(" }\n");
code.push_str(" }\n");
}
// End of impl
code.push_str("}\n");
Ok(code)
}
/// Constructs AST nodes based on the grammar body
fn construct_ast(&self, body: &Vec<String>) -> Result<String, String> {
// This function should be implemented to construct AST nodes based on the production body
// For simplicity, we'll return a placeholder
Ok(String::from(" Ok(AST::ε)\n"))
}
/// Generates the main module with example usage
fn generate_main(&self) -> String {
let mut code = String::new();
code.push_str("//! # Main Module\n");
code.push_str("//!\n");
code.push_str("mod lexer;\n");
code.push_str("mod ast;\n");
code.push_str("mod parser;\n\n");
code.push_str("use parser::Parser;\n\n");
code.push_str("fn main() {\n");
code.push_str(" let input = \"id + id * ( number - id )\";\n");
code.push_str(" let mut parser = Parser::new(input);\n");
code.push_str(" match parser.parse() {\n");
code.push_str(" Ok(ast) => println!(\"Parsing succeeded. AST: {:?}\", ast),\n");
code.push_str(" Err(e) => println!(\"Parsing failed: {}\", e),\n");
code.push_str(" }\n");
code.push_str("}\n");
code
}
/// Determines the FIRST set for a given body
fn first_of_body(body: &Vec<String>, first_sets: &HashMap<String, HashSet<String>>) -> Option<HashSet<String>> {
let mut first = HashSet::new();
for symbol in body {
if first_sets.contains_key(symbol) {
let sym_first = &first_sets[symbol];
first.extend(sym_first.iter().filter(|&&s| s != "ε").cloned());
if !sym_first.contains("ε") {
return Some(first);
}
} else {
first.insert(symbol.clone());
return Some(first);
}
}
first.insert("ε".to_string());
Some(first)
}
/// Checks if a body can produce ε
fn body_can_produce_epsilon(body: &Vec<String>, first_sets: &HashMap<String, HashSet<String>>) -> bool {
for symbol in body {
if first_sets.contains_key(symbol) {
if !first_sets[symbol].contains("ε") {
return false;
}
} else {
return false;
}
}
true
}
/// Sanitizes token names to be Rust-compatible
fn sanitize_token(token: &str) -> String {
token.trim_matches('"')
.trim_matches('\'')
.replace("+", "PLUS")
.replace("-", "MINUS")
.replace("*", "STAR")
.replace("/", "SLASH")
.replace("(", "L_PAREN")
.replace(")", "R_PAREN")
.replace("{", "L_BRACE")
.replace("}", "R_BRACE")
.replace("[", "L_BRACKET")
.replace("]", "R_BRACKET")
.replace(",", "COMMA")
.replace(";", "SEMICOLON")
.replace(":", "COLON")
.replace("<", "")
.replace(">", "")
.replace("|", "OR")
.replace("=", "EQ")
.replace("!", "EXCLAMATION")
.replace("?", "QUESTION")
.replace("#", "HASH")
.replace("$", "DOLLAR")
.replace("&", "AMPERSAND")
.replace("@", "AT")
.replace("~", "TILDE")
.replace(".", "DOT")
.replace("'", "")
.to_uppercase()
}
/// Sanitizes token literals for matching in lexer
fn sanitize_token_literal(token: &str) -> String {
if token == "id" {
String::from("Token::ID(_)") // Match any ID
} else if token == "number" {
String::from("Token::NUMBER(_)") // Match any NUMBER
} else if token.starts_with("\"") && token.ends_with("\"") {
let symbol = &token[1..token.len()-1];
if symbol.len() == 1 {
let tok = Self::sanitize_token(token);
format!("Token::{}", tok)
} else {
// Handle multi-character symbols if any
let tok = Self::sanitize_token(token);
format!("Token::{}", tok)
}
} else {
let tok = Self::sanitize_token(token);
format!("Token::{}", tok)
}
}
/// Sanitizes function names to be Rust-compatible
fn sanitize_func(func: &str) -> String {
func.to_lowercase()
.replace("<", "")
.replace(">", "")
.replace("-", "_")
.replace(" ", "_")
}
}
/// Entry point for Thepg
fn main() -> Result<(), String> {
let args: Vec<String> = env::args().collect();
if args.len() < 3 {
return Err("Usage: thepg <input.enbf> -o <output_directory> --grammar-type <LL1|LLk>".to_string());
}
let input_file = &args[1];
let mut output_dir = String::from("generated");
let mut grammar_type = "LL1".to_string();
let mut i = 2;
while i < args.len() {
match args[i].as_str() {
"-o" => {
if i + 1 < args.len() {
output_dir = args[i + 1].clone();
i += 2;
} else {
return Err("Expected output directory after -o".to_string());
}
},
"--grammar-type" => {
if i + 1 < args.len() {
grammar_type = args[i + 1].clone();
i += 2;
} else {
return Err("Expected grammar type after --grammar-type".to_string());
}
},
_ => i += 1,
}
}
let enbf = fs::read_to_string(input_file)
.map_err(|e| format!("Failed to read input file: {}", e))?;
let grammar = Grammar::parse(&enbf)?;
// Optionally, check if the grammar is LL(1)
if grammar_type.to_uppercase() == "LL1" {
if !is_ll1(&grammar) {
return Err("The provided grammar is not LL(1).".to_string());
}
}
grammar.generate_rust(&output_dir, &grammar_type)?;
println!("Parser generated successfully in {}", output_dir);
Ok(())
}
/// Checks if the grammar is LL(1) by ensuring no parsing table conflicts
fn is_ll1(grammar: &Grammar) -> bool {
// For each non-terminal, ensure that the FIRST sets of its productions are disjoint
for rule in &grammar.rules {
let mut first_sets = Vec::new();
for body in &rule.bodies {
if let Some(first) = Grammar::first_of_body(body, &grammar.first_sets) {
first_sets.push(first);
}
}
// Check pairwise disjoint
for i in 0..first_sets.len() {
for j in (i + 1)..first_sets.len() {
let intersection: HashSet<_> = first_sets[i].intersection(&first_sets[j]).collect();
if !intersection.is_empty() {
return false;
}
}
}
}
true
}