[antlr-interest] anybody help with me a python grammar rule?
John Pybus
john-yahoo at pybus.org
Sun Feb 22 06:16:20 PST 2004
> On Feb 21, 2004, at 6:10 PM, Terence Parr wrote:
>
>
>>Folks, the formal parameter list for functions looks wacky in the
>>distribution and on the net.
>>
>>First the grammar in the distribution:
>>
>>varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME] | '**'
>>NAME) | fpdef ['=' test] (',' fpdef ['=' test])* [',']
>>fpdef: NAME | '(' fplist ')'
>>fplist: fpdef (',' fpdef)* [',']
>>
>>The one from the doc is
>>
>>parameter_list ::=
>> (defparameter ",")*
>> ("*" identifier [, "**" identifier]
>> | "**" identifier
>> | defparameter [","])
>>
>>defparameter ::=
>> parameter ["=" expression]
>>
>>sublist ::=
>> parameter ("," parameter)* [","]
>>
>>parameter ::=
>> identifier | "(" sublist ")"
>>
>>My question here is why have the final '| defparameter [","]' alt in
>>parameter_list???? Surely it's redundant (might have to move a comma,
>>but...)
>>
>>Can anybody give me some examples of the various things parameters
>>might look like in the wacky cases?
def foo(x,y,z): #GOOD
def foo(x,y,z,): #GOOD trailing comma allowed
def bar(*baz): # STILL GOOD
def bar(*baz,): # BAD Syntax error !!
i.e. You're allowed a trailing comma in a parameter list, unless you're
using either of the excess positional parameter forms. Though I won't
to pretend to answer why this should be the rule ;-)
Terence Parr wrote:
> For example, won't this rule generate the same language?
>
> parameter_list:
> defparameter (',' defparameter)*
> ( ','
> ('*' NAME [',' '**' NAME] | '**' NAME
> )
> )
> | '*' NAME [',' '**' NAME]
> | '**' NAME
>
> and it's LL(1).
>
> arglist has the same problem. I wonder what kinda parser they build
> from this grammar. Ick.
>
> Ter
How does this match a param_list not ending in one of the 'excess
parameter' forms?
foo(a) or foo(a,)
I think you'll need the form they've used (or something similarly
complex) to get the optional trailing comma on simple parameters, but
not on *'d ones.
Yours,
John
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/antlr-interest/
<*> To unsubscribe from this group, send an email to:
antlr-interest-unsubscribe at yahoogroups.com
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/
More information about the antlr-interest
mailing list