FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Help/suggestion needed.

 
Post new topic   Reply to topic     Forum Index -> The Language Machine
View previous topic :: View next topic  
Author Message
Remy Moueza



Joined: 08 Nov 2005
Posts: 7
Location: france

PostPosted: Tue Jun 03, 2008 4:19 pm    Post subject: Help/suggestion needed. Reply with quote

Hello,
Using the language machine I am trying to recognize some kind of C like attribute recognition: "int *a, **b, c ;" to be able to treat that as several simpler declaration "int *a ; int **b ; int c ;" but I am having trouble doing it. Currently I am stucked; I hope that by submitting this problem to the community I could get some suggestions that will help me out.
Below is the rule I am struggling with. I have already tried to split it into several imbricated and simpler rules but this hasn't help.
- storageClass :Sc typeSpecifier :Cv :Btype :Decl :CbId :CbArg
/** Callback attributes have their identifier :Id replaced by CbId */
{ option identifier :Id }
{ option repeat "," declarator :Decl identifier :Id } ";" <- attribute :{ each doAttribute :Sc :Cv :Btype :Decl :CbId :CbArg :Id } ;

storageClass :Sc is an symbol in "virtual", "static", etc. It can also be empty.
:Cv is a "const" value (possibly empty)
:Btype is a basic type
:Decl is a declarator, like some pointer stars
:CbId and :CbArg are callback identifier and callback argument since callback definition recognition is not far from a type declaration recognition. :CbId and :CbArg are empty for non callback types.

My goal is to first recognize the type information with storageClass and typeSpecifier and then use each identifier :Id and related declarator :Decl in as many doAttribute rule as needed. doAttribute manages the output of a single attribute.

When running some tests, the language machine does not seem to be able to capture the :Sc, :Cv and :Btype variables for mutiple uses in each doAttribute rule call (and it does not work even for single attributes).

I really want to be able to cut a condensed declaration "int *a, **b, c ;" in several simpler ones "int *a ; int **b ; int c ;".
Any suggestions are welcome.
Back to top
View user's profile Send private message
mpah



Joined: 18 Jul 2005
Posts: 29
Location: UK

PostPosted: Sun Jun 08, 2008 3:02 am    Post subject: Reply with quote

Hi Remy, good to hear from you.

I think you need to think along these lines:

Code:

    - declareHead :H declareList :X ";"           <- declarationStatement :X;
    - declareItem :X repeat "," declareItem :X    <- declareList :{ each X };
    - declared    :X                              <- declareItem :{ declareHead :H declareItem :X };
    '*' declared  :X                              <- declared    :{ pointer :{ X }};
    - identifier  :X                              <- declared    :{ identifier :X };


The head of the declaration is available in the enclosing context for use in the intermediate representation of each item declared. The effect is to treat 'int a, *b, **c;' as if it had been written 'int a; int *b; int **c;', which is what you were looking for.

The point is that the variable H is visible throughout the analysis of the whole of the "declareList", so it can be used in constructing a representation of each element of the list.

I hope that helps a bit.

Peri
_________________
The Language Machine - a toolkit for language and grammar
Back to top
View user's profile Send private message
Remy Moueza



Joined: 08 Nov 2005
Posts: 7
Location: france

PostPosted: Mon Jun 09, 2008 4:16 pm    Post subject: Help/suggestion needed. Reply with quote

Hello Peri,

Thanks a lot! It was some great food for thought. I finally managed to have it working. Below is my code. Actually I had been closed to completion at some point since I tried a similar pattern; however it seems that in order to capture any variables from an enclosing scope I need to be in the "right" side of a rule (or a left side that have been once produced by a right side) and not at the beginning of a new pattern matching (which I call a "external" left side rule in my comment, maybe not a good name) but I had failed to figure that particular point.

Code:

        - storageClass :Sc typeSpecifier :Cv :Btype :Decl :CbId :CbArg attributeList :List ";" <- attribute :List ;
        - attributeItem :Item repeat "," attributeItem  :Item                                  <- attributeList :{ each Item };
        // Note: I ready need an intermediary attributeAtom rule otherwise, variable capture (:Sc :Cv :Btype) does not work.
        //       It seems because we are in a "external" left side rule and not a result from a previous right side rule like attributeAtom.
        - attributeAtom :Decl :Id                                                              <- attributeItem :{ doAttribute :Sc :Cv :Btype :Decl :CbId :CbArg :Id };
        - declarator :Decl identifier :Id                                                      <- attributeAtom :Decl :Id ;

I have also witness a similar phenomenon while working on rules doing code generation: I could not "capture" any variable defined with "var" on a pattern matching side of a rule (a kind of "external" case or "leftmost pattern matching side" for the current context). I have worked arround this problem by defining the needed variables in an far outer scope. Now I think I may try to get a better solution.

Thanks again.
Best Regards,

Remy.
Back to top
View user's profile Send private message
mpah



Joined: 18 Jul 2005
Posts: 29
Location: UK

PostPosted: Thu Jun 12, 2008 3:36 am    Post subject: Reply with quote

Hello Remy

I'm glad that helped.

But I'm not sure about your 'capturing' problem. For me 'capturing' relates to the ":" mechanism for communicating information from the right-side phase of a rule that has matched to the left-side phase of a rule that is being matched.

Once a variable has been created (either by 'capturing' or as an explicit "var" initialization), it is visible (unless masked by another of the same name) from the rest of the left-side in which it was created and from the resulting right-side, and from the left- and right- sides of subsequent inner rule applications.

By inner rule applications I mean rule applications whose left-side phases are contained within the left-side phase of the rule in which the variable was created. I have always found the lm-diagram useful for visualizing this.

So you don't need to 'capture' a variable to refer to it, provided it is visible according to this system of dynamic variable scope.

Of course the visibility I mean here is visibility for simple reference, not visibility for "each", which is restricted to the rule application in which the variables are created.

I may have misunderstood your problem!

Best wishes
Peri
_________________
The Language Machine - a toolkit for language and grammar
Back to top
View user's profile Send private message
Remy Moueza



Joined: 08 Nov 2005
Posts: 7
Location: france

PostPosted: Wed Jun 18, 2008 2:29 pm    Post subject: Reply with quote

Hello Peri,

Thanks for this last post: it helped me with some other rules I was having trouble with.

Actually, in my last post, I misused "capture": I meant "visibility". I am still learning the language machine and despite having made a lot of progress in using it these lasts months, I still run into some kinds of unexpected behaviours from time to time. I think this is because I am focusing too much on small chunks of rules rather than thinking about the overall grammar.

However, this ability to be able to focus and work arround a small number of rules without interfering with much code arround is what makes using the language machine so pleasant: with LALR tools, little changes in some insignificant looking rules can ruin the whole grammar with shift/reduce conflicts. This barely happen with the language machine and when some rules interfere, the lm diagram really help to quicky understand what is wrong.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic     Forum Index -> The Language Machine All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group