View mode: basic / threaded / horizontal-split · Log in · Help
February 26, 2012
Compile Time D Expression Parser?
Greetings

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.

Thanks and Regards
- Puneet
February 26, 2012
Re: Compile Time D Expression Parser?
Am 26.02.2012 03:25, schrieb d coder:
> Greetings
>
> 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.
>
> Thanks and Regards
> - Puneet
>

How different is what you want to do from CTFE?
http://dlang.org/function.html
February 26, 2012
Re: Compile Time D Expression Parser?
>
> How different is what you want to do from CTFE?
> http://dlang.org/function.html


I do not want to evaluate the expression in the string at compile time. I
want to parse it at compile time and later at run time, I am required to
process the parse tree and evaluate it in a different fashion (using binary
decision diagrams).

In summary, I need CTFE to parse the expression and create a parse tree
from it. I think this requires a string mixin that takes the expression
string as input and outputs code that builds the corresponding parse tree.

Regards
- Puneet
February 26, 2012
Re: Compile Time D Expression Parser?
Hisayuki Mima's ctpg is compile-time parser generater, and the
generated parser works in compile time!
https://github.com/youkei/ctpg

Kenji Hara

2012/2/26 d coder <dlang.coder@gmail.com>:
>> How different is what you want to do from CTFE?
>> http://dlang.org/function.html
>
>
> I do not want to evaluate the expression in the string at compile time. I
> want to parse it at compile time and later at run time, I am required to
> process the parse tree and evaluate it in a different fashion (using binary
> decision diagrams).
>
> In summary, I need CTFE to parse the expression and create a parse tree from
> it. I think this requires a string mixin that takes the expression string as
> input and outputs code that builds the corresponding parse tree.
>
> Regards
> - Puneet
February 26, 2012
Re: Compile Time D Expression Parser?
On 02/26/2012 03:25 AM, d coder wrote:
> Greetings
>
> 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.
>
> Thanks and Regards
> - Puneet
>


Operator precedence parsers are simple to implement:

http://effbot.org/zone/simple-top-down-parsing.htm
February 26, 2012
Re: Compile Time D Expression Parser?
> Hisayuki Mima's ctpg is compile-time parser generater, and the
> generated parser works in compile time!
> https://github.com/youkei/ctpg
>
> Kenji Hara
>

Thanks Kenji

This is very interesting. Also I think ctpg is being actively developed. I
will try to figure out how to make use of it.

Regards
- Puneet
February 26, 2012
Re: Compile Time D Expression Parser?
>
>
> Operator precedence parsers are simple to implement:
>
> http://effbot.org/zone/simple-**top-down-parsing.htm<http://effbot.org/zone/simple-top-down-parsing.htm>
>

Timon

I want to do all this parsing at compile time in D (using mixins). I have
just started working on this. I am not sure if CTFE allows so much
flexibility. I believe I will have to use functional style as in the link
provided by Kenji. Let me know if I am missing something.

Regards
- Puneet
February 26, 2012
Re: Compile Time D Expression Parser?
On 02/26/2012 01:00 PM, d coder wrote:
>
>     Operator precedence parsers are simple to implement:
>
>     http://effbot.org/zone/simple-__top-down-parsing.htm
>     <http://effbot.org/zone/simple-top-down-parsing.htm>
>
>
> Timon
>
> I want to do all this parsing at compile time in D (using mixins). I
> have just started working on this. I am not sure if CTFE allows so much
> flexibility. I believe I will have to use functional style as in the
> link provided by Kenji. Let me know if I am missing something.
>
> Regards
> - Puneet

I know. CTFE is flexible enough and implementing this would be quite 
simple. However, if ctpg serves your needs well, just use that.
February 26, 2012
Re: Compile Time D Expression Parser?
>
>
> I know. CTFE is flexible enough and implementing this would be quite
> simple. However, if ctpg serves your needs well, just use that.
>

Thanks Timon

 You mean I do not need to use function style when using CTFE?
 I will try parsing myself as you suggested. This approach would give me a
better handle.

Regards
- Puneet
February 26, 2012
Re: Compile Time D Expression Parser?
On 02/26/2012 01:55 PM, d coder wrote:
>
>     I know. CTFE is flexible enough and implementing this would be quite
>     simple. However, if ctpg serves your needs well, just use that.
>
>
> Thanks Timon
>
>    You mean I do not need to use function style when using CTFE?
>    I will try parsing myself as you suggested. This approach would give
> me a better handle.
>
> Regards
> - Puneet
>

Almost the complete language is available in CTFE, therefore classes 
could be used to implement the parse tree representation. However, a 
limitation that does exist is that classes that were created in CTFE 
cannot yet be stored in static variables or enums. How will the 
interface to your library look like?
« First   ‹ Prev
1 2 3
Top | Discussion index | About this forum | D home