[antlr-interest] Checking condition in a rule, set by another rule

amol Bhagavat amol_bhagavat at yahoo.co.uk
Tue Nov 13 20:34:43 PST 2007


Hi ,
I am developing a grammar for detecting function pointers in c code.
I want to check condition in rule1 which calls rule 2 & rule 2 changes the condition.
   
  I have wrote the grammar below which does this.
  Not able to understand , even though the rule "structurebody" returns 
true it does not executes the rule "body123". I am trying to detect if a structure has function pointer in it or not.
   
  Grammar:-----------------------
  declaration
scope {
  boolean fptrInStruct; 
}
@init {
  $declaration::fptrInStruct = false;
}
: 'struct' IDENTIFIER d=structurebody 
  {$declaration::fptrInStruct = $d.retval;}
  (
   {$declaration::fptrInStruct}?=> body123  //Useful, Store the identifier.
  |{!$declaration::fptrInStruct}?=> ';'            // No use.
  
  )
;
body123
:structureobject? ';'
;
  structurebody returns [boolean retval]
scope {boolean ContainFptr;}
 :{$structurebody::ContainFptr = false;}'{' insidestruct ( insidestruct)* '}'
 {$retval = $structurebody::ContainFptr;}  
 ;
  insidestruct 
 : stfptrDeclarationsub ';' {$structurebody::ContainFptr = true;}
 | Normal_struct_declaration
 ;
  stfptrDeclarationsub
:declaration_specifiers+ '(''*'IDENTIFIER')' '('(parameter_type_list| )')'
; 
   
  Input to the grammar:-------------------------------------

  struct ST_Tag
{
int (*fptr)(int k);
};
  main()
{
  }
   
  Pls tell me what is going wrong in the grammar....Thanks in advance.
   
  amol

       
---------------------------------
 For ideas on reducing your carbon footprint visit Yahoo! For Good this month.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20071114/2d30800a/attachment-0001.html 


More information about the antlr-interest mailing list