Thread overview
Re: Compile Time D Expression Parser?
Mar 11, 2012
Philippe Sigaud
Mar 11, 2012
d coder
Mar 11, 2012
d coder
March 11, 2012
dcoder:
> I need to parse simple D expressions at compile time. I was wondering if
somebody on the list has some example code that could be of help to me.
>
> I am working on an opensource constraint solver  and expressions that I
need to parse can be reasonably complex such as "x + y*n < 32 && x > 4". I want to code a string mixin that parses such expressions and writes out code that creates a parse tree for the given expression.

OK, doing a bit of thread necromancy here (3 weeks, still acceptable here?)

Puneet, I might have something for you. IIUC, you want to parse expressions that are

- an association of boolean expressions (&&, ||, !)
- each boolean expression is an equation (=, <=, >, etc)
- each equation lhs or rhs is an arithmetic expression (+, -, *, /)
- atoms in an arithmetic expression can be numbers or variables

Is that it? You do realize that any parse tree will for these constructs will be quite deep, right? I mean, 10-levels deep or somesuch.

Philippe


March 11, 2012
Hello  Philippe

> OK, doing a bit of thread necromancy here (3 weeks, still acceptable here?)
>
You are more than welcome. I am still working on the stuff.


> Puneet, I might have something for you. IIUC, you want to parse expressions that are
>
> - an association of boolean expressions (&&, ||, !)
> - each boolean expression is an equation (=, <=, >, etc)
> - each equation lhs or rhs is an arithmetic expression (+, -, *, /)
> - atoms in an arithmetic expression can be numbers or variables
>
> Is that it?
>
That would be sufficient for a start. But later I would also like to parse simple D style if-else conditionals and foreach loops.


> You do realize that any parse tree will for these constructs will be quite deep, right? I mean, 10-levels deep or somesuch.
>
10 levels would be good enough.

Regards
- Puneet


March 11, 2012
On 11-03-2012 18:54, d coder wrote:
> Hello  Philippe
>
>     OK, doing a bit of thread necromancy here (3 weeks, still acceptable
>     here?)
>
> You are more than welcome. I am still working on the stuff.
>
>     Puneet, I might have something for you. IIUC, you want to parse
>     expressions that are
>
>     - an association of boolean expressions (&&, ||, !)
>     - each boolean expression is an equation (=, <=, >, etc)
>     - each equation lhs or rhs is an arithmetic expression (+, -, *, /)
>     - atoms in an arithmetic expression can be numbers or variables
>
>     Is that it?
>
> That would be sufficient for a start. But later I would also like to
> parse simple D style if-else conditionals and foreach loops.
>
>     You do realize that any parse tree will for these constructs will be
>     quite deep, right? I mean, 10-levels deep or somesuch.
>
> 10 levels would be good enough.
>
> Regards
> - Puneet
>

Pegged should have no problem parsing all of D, at least theoretically (I don't know of any severe ambiguities in D). So IOW, it can probably do what you need it to do.

-- 
- Alex
March 11, 2012
> Pegged should have no problem parsing all of D, at least theoretically (I don't know of any severe ambiguities in D). So IOW, it can probably do what you need it to do.


Oh. I realized that I had missed Pegged announcement by Philippe. I will have a look at it.

Regards
- Puneet