[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

AW: position of the production in SableCC



Hi,
the class Token has the following methods.
 
    public int getLine()
    {
        return line;
    }
 
    public int getPos()
    {
        return pos;
    }
These are the ones you need to use. The problem now is that you need
to get the relevant token out of the production.
 
Lets take the following example. You got the line
     int a=5;
 
Now you want to know the position of the variable a. In the java grammar
the declaration with assignment is handeld the following production.
 
variable_declarator =
    {variable_declarator_id}
        variable_declarator_id |
 
    {assign}
        variable_declarator_id assign variable_initializer;
This tells you that the in-method of the visitor will get an object of class
AAssignVariableDeclarator. Now take a look at that class, it has the following
attributes.
 
    private PVariableDeclaratorId _variableDeclaratorId_;
    private TAssign _assign_;
    private PVariableInitializer _variableInitializer_;
But no attribute of type token, so you ain't got no chance to get the position
right away. What you need to do now is to check the inheritance tree of class
PVariableDeclaratorId. You need to find a child which has an attribute of type token.
This is what I do in the second method below. After I found the token which belongs to
the a I'm able to get a's position.
The object the helper method returns is of type TIdentifier and not Token, but all
classes generated by sablecc starting with an uppercase T are children of class Token.
 
   public void outAAssignVariableDeclarator(AAssignVariableDeclarator node)
   {
      if( bInAttributeDeclaration == true )
      {
         String zVariableName = node.getVariableDeclaratorId().toString();
 
         TIdentifier oVariableIdentifier
            = getVariableIdentifier(node.getVariableDeclaratorId());
 
         ClassEnvironment oEnvironment = (ClassEnvironment)_oEnvironmentStack.peek();
 
         oEnvironment.addVariable( vAttributeModifier
                                 , zAttributeType
                                 , zVariableName
                                 , super.getKnownImports()
                                 , getFileCurrentlyVisited()
                                 , oVariableIdentifier.getLine()
                                 , oVariableIdentifier.getPos() - 1 );
      }
   }
 
         protected
         TIdentifier
   getVariableIdentifier(PVariableDeclaratorId oVariableDeclaratorId)
   {
      if( oVariableDeclaratorId instanceof AIdentifierVariableDeclaratorId )
      {
         return ((AIdentifierVariableDeclaratorId)oVariableDeclaratorId).getIdentifier();
      }
      else if( oVariableDeclaratorId instanceof AVariableDeclaratorIdVariableDeclaratorId )
      {
         AVariableDeclaratorIdVariableDeclaratorId oDummy
            = (AVariableDeclaratorIdVariableDeclaratorId)oVariableDeclaratorId;
 
         return getVariableIdentifier(oDummy.getVariableDeclaratorId());
      }
      else
      {
         StringBuffer zMessage = new StringBuffer();
         zMessage.append("Can't cast PVariableDeclaratorId\n");
         zMessage.append("neither to AIdentifierVariableDeclaratorId\n");
         zMessage.append("nor to AVariableDeclaratorIdVariableDeclaratorId.\n");
         zMessage.append("Make sure grammar hasn't changed.");
 
         throw new RuntimeException(zMessage.toString());
      }
   }
I'm having problems to explain the hole stuff clearly but I hope the example
helps a bit.
 
Cheers, Thomas

===============================================================================
Thomas Leonhardt          email  leonhardt@informatik.tu-darmstadt.de
TU Darmstadt              WWW    http://www.pi.informatik.tu-darmstadt.de
FG Praktische Informatik  phone  +49 / (0)6151 / 16 - 5313
Wilhelminenstr. 7         fax    +49 / (0)6151 / 16 - 5472
D-64283 Darmstadt         
Germany
===============================================================================
 

-----Ursprüngliche Nachricht-----
Von: yuntao [mailto:yuntao@eventmonitor.com]
Gesendet: Donnerstag, 4. Oktober 2001 17:34
An: leonhardt@informatik.tu-darmstadt.de
Cc: yuntao
Betreff: position of the production in SableCC

Hi,
 
I am going through the sablecc mailing list and found your response to one of the question.
The original question and the answer are attached at the bottom of this message.
I have the similar problem. I would like to throw a RuntimeException and in this exception,
I would like to include the line number of where this error happens.
 
I am looking at your answer and I can not quite get your solution. Could you please illustrate a
little bit more detail?
 
For example: I have "a=2-"aaa";", since my minus operation can only apply to numeric data,
I would like to throw an Exception. This statement is derived from the assign statement in the
grammer. So if I can find the node is an instance of the Expression node, how can I determine the
line number of this statement appeared in the original input.
 
Thanks a lot.
 
Yuntao Cui
 
 
Original message:
 
What I'm doing in this case is to check the actual type in the
in or out method of my visitor, like:
 
if( node instanceof alternative_one )
{
}
else
if( node instanceof alternative_two )
{
}
 
I do this until I get an token at one point. It's not nice but it works.
I do it for java source analysis to get the position of method usages and
such.
 
Cheers,
Thomas

==========================================================================
Thomas Leonhardt           email  leonhardt@informatik.tu-darmstadt.de
TU Darmstadt               WWW    http://www.pi.informatik.tu-darmstadt.de
FG Praktische Informatik   phone  +49 / (0)6151 / 16 - 5313
D-64283 Darmstadt          fax    +49 / (0)6151 / 16 - 5472
Germany
==========================================================================
 

-----Ursprüngliche Nachricht-----
Von: owner-sablecc-list@sable.mcgill.ca [mailto:owner-sablecc-list@sable.mcgill.ca]Im Auftrag von Brandner Florian
Gesendet: Freitag, 11. Mai 2001 14:57
An: sablecc list
Betreff: position of productions.

Hallo,
 
I have a gramma with something like an assignment statement in it like this:
p = a ':=' b;
 
a and b are typed and these types must be identical, to get a valid assignment.
If this is not the case i want to emit an error message, with the position of production p.
 
Is it posible to get the line and position of an production (like the Token.getLine() and Token.getPos() )?
 
Florian