Carl Barron | 1 Jul 03:10

Re: Attributes: ownership, construction, assignment, etc.


On Jun 30, 2009, at 6:50 PM, John Freeman wrote:

> Howdy,
>
> I've encountered behavior I didn't expect when using attributes.   
> During my efforts to track down the problem, I've been constructing  
> some demonstrative examples.  I hope they will clearly illustrate  
> the differences between my desired behavior and the actual behavior.
>
> In my first example, imagine I am trying to build a payroll---a list  
> of names tied to salaries.  I chose the following type as my payroll  
> data structure:
>
> typedef std::string name_t;
> typedef std::map< name_t, double > payroll_t;
>
>
> A simple grammar for my payroll might look like the following:
>
> name %= lexeme['"' >> *(ascii::print - '"') >> '"'];
>
> salary %= double_;
>
> start = '{'
>     >> -(
>           (name[_a = _1] >> ':' >> salary[_val[_a] = _1])
>           % ','
>         )
>     >> '}';
(Continue reading)

Carl Barron | 1 Jul 03:13

Re: AST parsing?


On Jun 30, 2009, at 2:51 PM, Sohail Somani wrote:

> Carl Barron wrote:
>> On Jun 30, 2009, at 12:14 PM, Sohail Somani wrote:
>>
>>> Maybe an instructive example would be to implement naive, but useful
>>> constant sub-expression elimination for the calc example.
>>
>> calc'ast  uses something like:
>> template <char> struct bin_op;
>>
>> struct Var { /* some type not convertable to double> */};
>
> [snip]
>
> Yes, I suspected this might be what one would do. I can't help but  
> think
> that there should be another way to do this. I would like my original
> made-up DSL to compile and work. Am I crazy? Maybe if someone agreed  
> or
> disagreed with the need for a DSL for this kind of matching.
>
> I think there are other languages which use structural pattern  
> matching
> to implement compilers. I can't remember what languages do this but I
> think there was a book on it (which I haven't yet read, but would like
> to!) If anyone knows about it, please let me know.
>
> -- 
(Continue reading)

Joel de Guzman | 1 Jul 03:27
Picon
Favicon

Re: AST parsing?

Sohail Somani wrote:
> Carl Barron wrote:

> I think there are other languages which use structural pattern matching
> to implement compilers. I can't remember what languages do this but I
> think there was a book on it (which I haven't yet read, but would like
> to!) If anyone knows about it, please let me know.

Haskell comes to mind. Yes, I've been thinking about a DSEL for
structural pattern matching.

Regards,
--

-- 
Joel de Guzman
http://www.boostpro.com
http://spirit.sf.net

------------------------------------------------------------------------------
John Freeman | 1 Jul 04:07
Picon

Re: Attributes: ownership, construction, assignment, etc.

Carl Barron wrote:
>     first write the grammar without any actions.  and note what the  
> expected attributes(EA below) are of the rules
>
>     start =  '{' >> -(contact_item % ',')) >> '};  // EA not needed  
> action on contact_item will insert.
>     contact_item = name >> ':' >> contact;  // EA =  
> tuple<name_t,contact_info_t>
>     contact = '{' >> lit("address") >> ':' >> name
> 	>> ',' >> lit("phone") >> ':' >> int_ >> '}; //EA = tuple<name_t,int>
>
>    now note that contact_info_t is a struct with the same type and  
> same order as EA(contact)
> so converting contact_info_t into a fusion sequence [macro  
> BOOST_FUSION_ADAPT_STRUCT is easiest method] means that contact can be  
> auto assigned once this is done so
>    changing = to %=  assigns to a contact_info_t what is parsed.
>   

Thanks for the response!

I had considered the approach of BOOST_FUSION_ADAPT_STRUCT, but in the 
end I don't believe it will match up with my goals.  The way I first 
wrote the grammar aimed for simplicity, so my goals may not be clear.  
Let me try to present them here:

1.  For a given object type (here contact_info_t), I want to parse 
members in any order, and even just a subset of the members, e.g.:

{ address: "here", phone: 1234 }
(Continue reading)

Sohail Somani | 1 Jul 03:59
Gravatar

Re: AST parsing?

Joel de Guzman wrote:
> Sohail Somani wrote:
>> Carl Barron wrote:
> 
>> I think there are other languages which use structural pattern matching
>> to implement compilers. I can't remember what languages do this but I
>> think there was a book on it (which I haven't yet read, but would like
>> to!) If anyone knows about it, please let me know.
> 
> Haskell comes to mind. Yes, I've been thinking about a DSEL for
> structural pattern matching.

Right, Haskell. Sorry my mind is a bit busy the past couple of days. If
you manage to crystallize your thoughts about the DSEL, I'd be very
happy to read them.

--

-- 
Sohail Somani
http://uint32t.blogspot.com

------------------------------------------------------------------------------
Joel de Guzman | 1 Jul 03:58
Picon
Favicon

Re: AST parsing?

Sohail Somani wrote:
> The thing that I keep thinking is that while there is a DSL to parse a
> sequence of bytes and there is a DSL to format output, there is no DSL
> to match and generate hierarchical data structures.

That, sir, is a battle for another day, so we enthused. Hah! That
day may be nearing. Don't you wish we had more than 24 hours in
a day!

Regards,
--

-- 
Joel de Guzman
http://www.boostpro.com
http://spirit.sf.net

------------------------------------------------------------------------------
John Freeman | 1 Jul 04:53
Picon

Re: Attributes: ownership, construction, assignment, etc.

John Freeman wrote:
> I think the phoenix::insert() function may be what I need.  I will 
> check it out and report back.

It is what I needed.  Thanks!  Attached is the grammar that worked.

I guess what it comes down to, then, is why something similar to

entry %= name >> ':' >> contact;

start = '{' >> -(entry[insert(_val, _1)] % ',') >> '}';

works, but

start = '{' >> -(entry[_val[at_c<0>(_1)] = at_c<1>(_1)] % ',') >> '}';

doesn't.  I know the operator[] is working, because entries are being 
added, but there is a breakdown somewhere with the operator= because the 
values are not being copied.

Any ideas?

- John
#include <iostream>
#include <iomanip>
#include <iterator>
#include <map>

(Continue reading)

Carl Barron | 1 Jul 05:46

Re: Attributes: ownership, construction, assignment, etc.


On Jun 30, 2009, at 10:07 PM, John Freeman wrote:

> Carl Barron wrote:
>>    first write the grammar without any actions.  and note what the
>> expected attributes(EA below) are of the rules
>>
>>    start =  '{' >> -(contact_item % ',')) >> '};  // EA not needed
>> action on contact_item will insert.
>>    contact_item = name >> ':' >> contact;  // EA =
>> tuple<name_t,contact_info_t>
>>    contact = '{' >> lit("address") >> ':' >> name
>> 	>> ',' >> lit("phone") >> ':' >> int_ >> '}; //EA =  
>> tuple<name_t,int>
>>
>>   now note that contact_info_t is a struct with the same type and
>> same order as EA(contact)
>> so converting contact_info_t into a fusion sequence [macro
>> BOOST_FUSION_ADAPT_STRUCT is easiest method] means that contact can  
>> be
>> auto assigned once this is done so
>>   changing = to %=  assigns to a contact_info_t what is parsed.
>>
>
> Thanks for the response!
>
> I had considered the approach of BOOST_FUSION_ADAPT_STRUCT, but in the
> end I don't believe it will match up with my goals.  The way I first
> wrote the grammar aimed for simplicity, so my goals may not be clear.
> Let me try to present them here:
(Continue reading)

Sohail Somani | 1 Jul 05:49
Gravatar

Re: AST parsing?

Joel de Guzman wrote:
> Sohail Somani wrote:
>> The thing that I keep thinking is that while there is a DSL to parse a
>> sequence of bytes and there is a DSL to format output, there is no DSL
>> to match and generate hierarchical data structures.
> 
> That, sir, is a battle for another day, so we enthused. Hah! That
> day may be nearing. Don't you wish we had more than 24 hours in
> a day!

Some would claim that you have already found such a day but are keeping
it secret!

Ok, just me but I can't explain spirit otherwise.

--

-- 
Sohail Somani
http://uint32t.blogspot.com

------------------------------------------------------------------------------
Carl Barron | 1 Jul 05:50

Re: Attributes: ownership, construction, assignment, etc.


On Jun 30, 2009, at 10:53 PM, John Freeman wrote:

>
> It is what I needed.  Thanks!  Attached is the grammar that worked.
>
> I guess what it comes down to, then, is why something similar to
>
> entry %= name >> ':' >> contact;
>
> start = '{' >> -(entry[insert(_val, _1)] % ',') >> '}';
>
> works, but
>
> start = '{' >> -(entry[_val[at_c<0>(_1)] = at_c<1>(_1)] % ',') >> '}';
>
> doesn't.  I know the operator[] is working, because entries are  
> being added, but there is a breakdown somewhere with the operator=  
> because the values are not being copied.
>
> Any ideas?
   No but in general,but at least there is a solution here that works.

------------------------------------------------------------------------------

Gmane