# I need a hint on how to begin with the parser Booleans & Nil #sc2 (rust)

**URL:** <https://forum.codecrafters.io/t/i-need-a-hint-on-how-to-begin-with-the-parser-booleans-nil-sc2-rust/10421>\
**Category:** General\
**Tags:** challenge:interpreter\
**Created:** [May 12, 2025, 4:26pm UTC](https://forum.codecrafters.io/t/i-need-a-hint-on-how-to-begin-with-the-parser-booleans-nil-sc2-rust/10421 "2025-05-12T16:26:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![FelixTheC](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/felixthec/32/11021_2.png) [@FelixTheC](https://forum.codecrafters.io/u/FelixTheC)\
**Post date:** [May 12, 2025, 4:26pm UTC](https://forum.codecrafters.io/t/i-need-a-hint-on-how-to-begin-with-the-parser-booleans-nil-sc2-rust/10421/1 "2025-05-12T16:26:30Z")

</div>

I’m a bit stuck on defining the parsing rules into code.

```auto
expression → equality ;
equality → comparison ( ( "!=" | "==" ) comparison )* ;
comparison → term ( ( ">" | ">=" | "<" | "<=" ) term )* ;
term → factor ( ( "-" | "+" ) factor )* ;
factor → unary ( ( "/" | "*" ) unary )* ;
unary → ( "!" | "-" ) unary
               | primary ;
primary → NUMBER | STRING | "true" | "false" | "nil"
               | "(" expression ")" ;

```

Would you create a struct for each of these? Something like this

```rust
struct equality {
    left: comparison
}

```

Or am I on the wrong track?

---

<div class="post-metadata">

**Author:** ![EshaanAgg](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/eshaanagg/32/399_2.png) [@EshaanAgg](https://forum.codecrafters.io/u/EshaanAgg)\
**Post date:** [May 13, 2025, 5:57pm UTC](https://forum.codecrafters.io/t/i-need-a-hint-on-how-to-begin-with-the-parser-booleans-nil-sc2-rust/10421/4 "2025-05-13T17:57:19Z")

</div>

I had created a single enum called `Expr` to represent the result of all these branches, and kept its variants on the basis of the number of operators and operands they take.

Something like this:

```rust
#[derive(Debug)]
pub enum Expr {
    Unary(Token, Box<Expr>),
    Binary(Box<Expr>, Token, Box<Expr>),
    Grouping(Box<Expr>),
    Literal(Token),
}

```

Here, the `Token` is the operator that is associated with both the operands (which themselves are `Expr`). Not sure if it’s the most idiomatic way to do the same, but worked well for me.

---

<div class="post-metadata">

**Author:** ![FelixTheC](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/felixthec/32/11021_2.png) [@FelixTheC](https://forum.codecrafters.io/u/FelixTheC)\
**Post date:** [May 14, 2025, 4:46am UTC](https://forum.codecrafters.io/t/i-need-a-hint-on-how-to-begin-with-the-parser-booleans-nil-sc2-rust/10421/6 "2025-05-14T04:46:33Z")

</div>

Thanks for your suggestion.  
When I think at enums I only think of them as

```python
class Expr(enum.Enum):
    Unary = "Unary"

```

I will read a little bit more about them in rust.

---

<div class="post-metadata">

**Author:** ![EshaanAgg](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/eshaanagg/32/399_2.png) [@EshaanAgg](https://forum.codecrafters.io/u/EshaanAgg)\
**Post date:** [May 14, 2025, 11:02am UTC](https://forum.codecrafters.io/t/i-need-a-hint-on-how-to-begin-with-the-parser-booleans-nil-sc2-rust/10421/7 "2025-05-14T11:02:33Z")

</div>

Can’t blame you for that. Coming from other languages like Typescript and even CPP, enums have a reputation of being glorified constants. But in Rust, they are one of the most powerful concepts because of ability of store different shapes of data and pattern matching expressiveness!

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/3X/7/0/700657133935c15703e22c7c8870394f6e6dc27d.svg) [@system](https://forum.codecrafters.io/u/system)\
**Post date:** [May 21, 2025, 2:45pm UTC](https://forum.codecrafters.io/t/i-need-a-hint-on-how-to-begin-with-the-parser-booleans-nil-sc2-rust/10421/8 "2025-05-21T14:45:38Z")

</div>

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.
