Simple trailing commas ,

39 views
Skip to first unread message

Tarek Ziadé

unread,
Jul 26, 2011, 5:04:21 PM7/26/11
to ply-hack
Hey

I used PLY for an experiment, and I was trying to improve my DSL, in
particular allow trailing commas.

For example in this expression :

path foo (
a b,
c d
);

I would like to allow:

path foo (
a b,
c d,
);

and I would like to allow this in most places. Is there a simple
pattern to do this ?

My DSL: https://bitbucket.org/tarek/redbarrel/src/tip/redbarrel/parser.py

Cheers
Tarek

Simon Cross

unread,
Jul 27, 2011, 7:51:50 AM7/27/11
to ply-...@googlegroups.com
On Tue, Jul 26, 2011 at 11:04 PM, Tarek Ziadé <ziade...@gmail.com> wrote:
> I used PLY for an experiment, and I was trying to improve my DSL, in
> particular allow trailing commas.

I haven't thought about this particularly hard (I just tested simple
cases) but does:

def p_statements(p):
"""statements : statements COMMA statement
| statements COMMA
| statement
"""
if len(p) == 2:
p[0] = [p[1]]
elif len(p) == 3:
p[0] = p[1]
else:
p[0] = p[1] + [p[3]]

Help?

Schiavo
Simon

Tarek Ziadé

unread,
Aug 17, 2011, 6:07:05 AM8/17/11
to ply-...@googlegroups.com

Yeah that's what I was going to do, but I was wondering if there was a
generic way to handle this case, because I am going to have to add it
in many statements.

Thanks

>
> Schiavo
> Simon
>
> --
> You received this message because you are subscribed to the Google Groups "ply-hack" group.
> To post to this group, send email to ply-...@googlegroups.com.
> To unsubscribe from this group, send email to ply-hack+u...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/ply-hack?hl=en.
>
>

--
Tarek Ziadé | http://ziade.org

A.T.Hofkamp

unread,
Aug 17, 2011, 6:53:23 AM8/17/11
to ply-...@googlegroups.com
On 17/08/11 12:07, Tarek Ziad� wrote:
> On Wed, Jul 27, 2011 at 1:51 PM, Simon Cross<hodg...@gmail.com> wrote:
>> On Tue, Jul 26, 2011 at 11:04 PM, Tarek Ziad�<ziade...@gmail.com> wrote:
>>> I used PLY for an experiment, and I was trying to improve my DSL, in
>>> particular allow trailing commas.
>>
>> I haven't thought about this particularly hard (I just tested simple
>> cases) but does:
>>
>> def p_statements(p):
>> """statements : statements COMMA statement
>> | statements COMMA
>> | statement
>> """
>> if len(p) == 2:
>> p[0] = [p[1]]
>> elif len(p) == 3:
>> p[0] = p[1]
>> else:
>> p[0] = p[1] + [p[3]]
>>
>> Help?
>
> Yeah that's what I was going to do, but I was wondering if there was a
> generic way to handle this case, because I am going to have to add it
> in many statements.

You could add an empty statenment, except that would allow "foo,,,"

Albert

Tarek Ziadé

unread,
Aug 17, 2011, 7:01:39 AM8/17/11
to ply-...@googlegroups.com
On Wed, Aug 17, 2011 at 12:53 PM, A.T.Hofkamp <a.t.h...@tue.nl> wrote:
> On 17/08/11 12:07, Tarek Ziadé wrote:
>>
>> On Wed, Jul 27, 2011 at 1:51 PM, Simon Cross<hodg...@gmail.com>  wrote:
>>>
>>> On Tue, Jul 26, 2011 at 11:04 PM, Tarek Ziadé<ziade...@gmail.com>

>>>  wrote:
>>>>
>>>> I used PLY for an experiment, and I was trying to improve my DSL, in
>>>> particular allow trailing commas.
>>>
>>> I haven't thought about this particularly hard (I just tested simple
>>> cases) but does:
>>>
>>> def p_statements(p):
>>>    """statements : statements COMMA statement
>>>                  | statements COMMA
>>>                  | statement
>>>    """
>>>    if len(p) == 2:
>>>        p[0] = [p[1]]
>>>    elif len(p) == 3:
>>>        p[0] = p[1]
>>>    else:
>>>        p[0] = p[1] + [p[3]]
>>>
>>> Help?
>>
>> Yeah that's what I was going to do, but I was wondering if there was a
>> generic way to handle this case, because I am going to have to add it
>> in many statements.
>
> You could add an empty statenment, except that would allow "foo,,,"

good idea, I'll try this

Thanks

>
> Albert

Reply all
Reply to author
Forward
0 new messages