In the quest of Smacc idioms

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

In the quest of Smacc idioms

Stephane Ducasse-3
Hi 

I'm trying to understand what are the idioms in Smacc when converting from
ANTLR

######### the case of * ########################
Function = ‘(‘ (',' Arguments )* ‘)’

     should be transformed into 

Function
: <name> "(" 'leftParen' _ArgumentsOption  ")" 'rightParen' {{}}
;
_ArgumentsOption
:
| Arguments 
;
Arguments
: Expression 'argument'
| Arguments "," Expression 'argument'
;

Here since there is a recursion in Arguments 
the instance variable containing a list of argument will be added to the FunctionNode
Pay attention that Expression 'argument' should not have a {{}} nor its parent rule.


######### the case of + ########################
Function = ‘(‘  Arguments + ‘)’

     should be transformed into 

Function
: <name> "(" 'leftParen' _ArgumentsOption  ")" 'rightParen' {{}}
;
_ArgumentsOption
: Arguments 
;
Arguments
: Expression 'argument'
| Arguments "," Expression 'argument'
;



######### the case of ? ########################
Function = ‘(‘  Arguments+ ‘)’

     should be transformed into 

Function
: <name> "(" 'leftParen' _ArgumentsOption  ")" 'rightParen' {{}}
;
_ArgumentsOption
:
| Arguments 
;
Arguments
: Expression 'argument' 
;



Is there a more compact way to express?
Thierry do you have example of *, + , ? in the grammar?
Stef
Reply | Threaded
Open this post in threaded view
|

Re: In the quest of Smacc idioms

Thierry Goubier


Le 01/04/2017 à 19:31, Stephane Ducasse a écrit :
> Hi
>
> I'm trying to understand what are the idioms in Smacc when converting from
> ANTLR
>
> ######### the case of * ########################
> Function = ‘(‘ (',' Arguments )* ‘)’

Function: "(" ( Argument ("," Argument) * ) ? ")" {{}} ;

Argument: <identifier> 'argument' ;

This one in SmaCC github, gives you exactly what you want (a Function
ast node with an instance variable named arguments containing a possibly
empty list of arguments) and is equivalent to your transform below.

Even more concise; one can put the instance variable name in the
top-level expression, and SmaCC will infer the list behavior:

Function: "(" (Expression 'argument' ("," Expression 'argument')*)? {{}} ;

>      should be transformed into
>
> Function
> : <name> "(" 'leftParen' _ArgumentsOption  ")" 'rightParen' {{}}
> ;
> _ArgumentsOption
> :
> | Arguments
> ;
> Arguments
> : Expression 'argument'
> | Arguments "," Expression 'argument'
> ;
>
> Here since there is a recursion in Arguments
> the instance variable containing a list of argument will be added to the
> FunctionNode
> Pay attention that Expression 'argument' should not have a {{}} nor its
> parent rule.
>
>
> ######### the case of + ########################
> Function = ‘(‘  Arguments + ‘)’

Function: "(" Argument 'argument' ("," Argument 'argument') * ")";

Argument: <identifier> ;

>      should be transformed into
>
> Function
> : <name> "(" 'leftParen' _ArgumentsOption  ")" 'rightParen' {{}}
> ;
> _ArgumentsOption
> : Arguments
> ;
> Arguments
> : Expression 'argument'
> | Arguments "," Expression 'argument'
> ;
>
>
>
> ######### the case of ? ########################
> Function = ‘(‘  Arguments+ ‘)’

Function = '(' Arguments? ')', that is:

Function: "(" (Arguments 'argument') ? ")";

You need the parenthesis here. This is a bug, it should accept

Function: "(" Arguments 'argument' ? ")" ;

I'll add an issue and improve the SmaCC grammar grammar.

>
>      should be transformed into
>
> Function
> : <name> "(" 'leftParen' _ArgumentsOption  ")" 'rightParen' {{}}
> ;
> _ArgumentsOption
> :
> | Arguments
> ;
> Arguments
> : Expression 'argument'
> ;
>
>
>
> Is there a more compact way to express?

With +, *, ? and () of course :)

> Thierry do you have example of *, + , ? in the grammar?

No, not that many. I did not use them much professionally, and then I
didn't make the effort to check that the AST annotations would work
correctly with them, so I had a tendancy to remove them.

This is the first time I'm really checking that annotations work
properly over those syntax extensions.

The initial (1.X) version of thoses annotations were not compatibles
with the AST annotations, so I had back then to hack the SmaCC 2.0.3
base to support them, without having the time to check it was done
properly on the annotation level.

Thierry

> Stef


Reply | Threaded
Open this post in threaded view
|

Re: In the quest of Smacc idioms

Stephane Ducasse-3
Tx thierry I will check and make some experiments. 

On Sat, Apr 1, 2017 at 9:45 PM, Thierry Goubier <[hidden email]> wrote:


Le 01/04/2017 à 19:31, Stephane Ducasse a écrit :
Hi

I'm trying to understand what are the idioms in Smacc when converting from
ANTLR

######### the case of * ########################
Function = ‘(‘ (',' Arguments )* ‘)’

Function: "(" ( Argument ("," Argument) * ) ? ")" {{}} ;

Argument: <identifier> 'argument' ;

This one in SmaCC github, gives you exactly what you want (a Function ast node with an instance variable named arguments containing a possibly empty list of arguments) and is equivalent to your transform below.

Even more concise; one can put the instance variable name in the top-level expression, and SmaCC will infer the list behavior:

Function: "(" (Expression 'argument' ("," Expression 'argument')*)? {{}} ;

     should be transformed into

Function
: <name> "(" 'leftParen' _ArgumentsOption  ")" 'rightParen' {{}}
;
_ArgumentsOption
:
| Arguments
;
Arguments
: Expression 'argument'
| Arguments "," Expression 'argument'
;

Here since there is a recursion in Arguments
the instance variable containing a list of argument will be added to the
FunctionNode
Pay attention that Expression 'argument' should not have a {{}} nor its
parent rule.


######### the case of + ########################
Function = ‘(‘  Arguments + ‘)’

Function: "(" Argument 'argument' ("," Argument 'argument') * ")";

Argument: <identifier> ;

     should be transformed into

Function
: <name> "(" 'leftParen' _ArgumentsOption  ")" 'rightParen' {{}}
;
_ArgumentsOption
: Arguments
;
Arguments
: Expression 'argument'
| Arguments "," Expression 'argument'
;



######### the case of ? ########################
Function = ‘(‘  Arguments+ ‘)’

Function = '(' Arguments? ')', that is:

Function: "(" (Arguments 'argument') ? ")";

You need the parenthesis here. This is a bug, it should accept

Function: "(" Arguments 'argument' ? ")" ;

I'll add an issue and improve the SmaCC grammar grammar.


     should be transformed into

Function
: <name> "(" 'leftParen' _ArgumentsOption  ")" 'rightParen' {{}}
;
_ArgumentsOption
:
| Arguments
;
Arguments
: Expression 'argument'
;



Is there a more compact way to express?

With +, *, ? and () of course :)

Thierry do you have example of *, + , ? in the grammar?

No, not that many. I did not use them much professionally, and then I didn't make the effort to check that the AST annotations would work correctly with them, so I had a tendancy to remove them.

This is the first time I'm really checking that annotations work properly over those syntax extensions.

The initial (1.X) version of thoses annotations were not compatibles with the AST annotations, so I had back then to hack the SmaCC 2.0.3 base to support them, without having the time to check it was done properly on the annotation level.

Thierry

Stef