Skip to content

Commit

Permalink
checkpoint: tweaking typed grammar
Browse files Browse the repository at this point in the history
  • Loading branch information
Devin-Yeung committed Nov 11, 2024
1 parent c6c95cc commit 6ab62ea
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 110 deletions.
22 changes: 10 additions & 12 deletions examples/set-algebra-typed.bnfgen
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
<Algebra> ::= <Expr>
| <Predicate> ;

// <Expr0: "set"> ::= "{" <Id> ":" <Predicate> "}" ;
<Expr0: "set"> ::= "{" <Id> ":" "Predicate" "}" ;
<Expr0: "set"> ::= "{" <Id> ":" <Predicate> "}" ;
// <Expr0: "set"> ::= "{" <Id> ":" "Predicate" "}" ;

<Expr0: "int"> ::= "(" <Expr: "int"> ")"
| 100 <Num>
Expand All @@ -38,21 +38,19 @@
<Expr: "set"> ::= 2 <Expr1: "set">
| <Expr: "set"> "U" <Expr1: "set"> ;

<Predicate0: "relation"> ::= <Expr: "int"> "<" <Expr: "int">
| <Expr: "int"> ">" <Expr: "int">
| <Expr: "int"> "=" <Expr: "int"> ;

<Predicate0: "relation"> ::= <Expr: "int"> "@" <Expr: "set"> ;

<Predicate0: "relation"> ::= 2 <Expr: "int"> "<" <Expr: "int">
| 2 <Expr: "int"> ">" <Expr: "int">
| 2 <Expr: "int"> "=" <Expr: "int">
| <Expr: "int"> "@" <Expr: "set"> ;

<Predicate1> ::= "(" <Predicate> ")"
| 2 "!" <Predicate0>
| 2 <Predicate0> ;
| 10 <Predicate0> ;

<Predicate2> ::= 10 <Predicate1>
| <Predicate2> "&" <Predicate1> ;
<Predicate2> ::= 2 <Predicate1>
| <Predicate2> "&" <Predicate1> ;

<Predicate> ::= 10 <Predicate2>
<Predicate> ::= 2 <Predicate2>
| <Predicate> "|" <Predicate2> ;

<EOL> ::= 1000 "\n" | "" ; // For generation purposes, make it looks better
8 changes: 4 additions & 4 deletions src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ impl Generator {
// pop out the first symbol
match self.grammar.reduce(stack.remove(0), &mut state) {
ReduceOutput::Terminal(s) => {
println!("{s}");
use std::io::Write;
std::io::stdout().flush().unwrap();
buf.push(s);
}
ReduceOutput::NonTerminal { mut syms, .. } => {
Expand Down Expand Up @@ -128,7 +125,10 @@ mod test {
let grammar = RawGrammar::parse(text).unwrap().to_checked().unwrap();
let gen = Generator { grammar };
let mut seeded_rng = rand::rngs::StdRng::seed_from_u64(42);
let out = (0..100).map(|_| gen.generate("Expr", &mut seeded_rng)).collect::<Vec<_>>().join("\n");
let out = (0..100)
.map(|_| gen.generate("Expr", &mut seeded_rng))
.collect::<Vec<_>>()
.join("\n");
insta::assert_snapshot!(out);
}
}
1 change: 0 additions & 1 deletion src/grammar/symbol.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::regex::Regex;
use crate::span::Span;
use std::borrow::Borrow;
use std::hash::Hash;
use std::rc::Rc;

Expand Down
Loading

0 comments on commit 6ab62ea

Please sign in to comment.