help-bison
[Top][All Lists]
Advanced

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

How to construct an abstract semantic tree with c/cpp file in bison? all


From: Guan Wanxian
Subject: How to construct an abstract semantic tree with c/cpp file in bison? all i need is the function definition part
Date: Sun, 4 Jan 2015 15:32:07 +0800

I am using flex and bison to recognize the functions in C or CPP file and
test whether each function is in good style (ie. The function body is no
more than 120 lines).

 

In my lexer.l file ,I return these tokens:


[a-zA-Z_][a-zA-Z0-9_]*  SAVE_TOKEN; return TIDENTIFIER;

[0-9]+\.[0-9]*          SAVE_TOKEN; return TDOUBLE;

[0-9]+                  SAVE_TOKEN; return TINTEGER;

"("                     return TOKEN(TLPAREN);

")"                     return TOKEN(TRPAREN);

"{"                     return TOKEN(TLBRACE);

"}"                     return TOKEN(TRBRACE);

"."                     return TOKEN(TDOT);

";"                                                   return TOKEN(TSEMI)

","                     return TOKEN(TCOMMA);

\n                                                 {std::cout << yytext; } 

" "                                                {std::cout <<yytext;}

 

And in my bison file, I want to construct the abstract semantic tree as
follow


%start program

%%

program : stmts {  }

        ;

        

stmts : stmt {  }

      | stmts stmt { }

      ;

 

stmt : func_decl

     | normal_decl {  }

     ;

 

 

 

        

func_decl : ident ident TLPAREN func_decl_args TRPAREN block {}

          ;

                     

block : TLBRACE exprs TRBRACE {}

  | TLBRACE TRBRACE {  }

  ;

           

func_decl_args : /*blank*/  {  }

          | var_decl { }

          | func_decl_args TCOMMA var_decl { }

          ;

 

ident : TIDENTIFIER {}

      ;

 

numeric : TINTEGER {}

        | TDOUBLE {  }

        ;

                   

call_args : /*blank*/  { $$ = new ExpressionList(); }

          | expr { $$ = new ExpressionList(); $$->push_back($1); }

          | call_args TCOMMA expr  { $1->push_back($3); }

          ;

 

stmt has two types, one is func_decl which is the definition of function and
the other one is normal_decl which represents other normal statements and
expressions. My problems is that how can I construct my abstract semantic
tree for 

normal_decl so that it can cover all the other contents in this file except
comments.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]