I am learning bison/flex. I successfully parse a simple c code file with bison/flex. Now I was wondering on parsing the included header file in a test c code using bison/flex. Will it can do that ?
To put in a simpler way, I am attaching sample code to give idea of my question.
Here is the Test file that includes a header file (.h) also.
test.c which includes a header file header.h
#include <stdio.h>
#include "header.h"
int main (int c, int b)
{
bigNumber a; /* I wanted that when parser come across to
"bigNumber" then it knows the datatype of
"bigNumber" and print its
type as defined in "header.h" */
while ( 1 )
{
newData d; /* Same should happen to "newData" also */
}
}
header.h
#define newData int
#define bigNumber double
lexer.l
%{
#include <stdio.h>
#include <string.h>
#include "c.tab.h"
%}
alpha [a-zA-Z]
digit [0-9]
%%
[ \t] { ; }
[ \n] { yylineno = yylineno + 1;}
int { return INT; }
float { return FLOAT; }
char { return CHAR; }
void { return VOID; }
double { return DOUBLE; }
for { return FOR; }
while { return WHILE; }
if { return IF; }
else { return ELSE; }
printf { return PRINTF; }
struct { return STRUCT; }
^"#include ".+ { ; }
{digit}+ { return NUM; }
{alpha}({alpha}|{digit})* { return ID; }
"<=" { return LE; }
">=" { return GE; }
"==" { return EQ; }
"!=" { return NE; }
">" { return GT; }
"<" { return LT; }
"." { return DOT; }
\/\/.* { ; }
\/\*(.*\n)*.*\*\/ { ; }
. { return yytext[0]; }
%%
bison file (c.y)
%{
#include <stdio.h>
#include <stdlib.h>
#include"lex.yy.c"
#include<ctype.h>
int count=0;
extern FILE *fp;
%}
%token INT FLOAT CHAR DOUBLE VOID
%token FOR WHILE
%token IF ELSE PRINTF
%token STRUCT
%token NUM ID
%token INCLUDE
%token DOT
%right '='
%left AND OR
%left '<' '>' LE GE EQ NE LT GT
%%
start
: Function
| Declaration
;
/* Declaration block */
Declaration
: Type Assignment ';'
| Assignment ';'
| FunctionCall ';'
| ArrayUsage ';'
| Type ArrayUsage ';'
| StructStmt ';'
| error
;
/* Assignment block */
Assignment
: ID '=' Assignment
| ID '=' FunctionCall
| ID '=' ArrayUsage
| ArrayUsage '=' Assignment
| ID ',' Assignment
| NUM ',' Assignment
| ID '+' Assignment
| ID '-' Assignment
| ID '*' Assignment
| ID '/' Assignment
| NUM '+' Assignment
| NUM '-' Assignment
| NUM '*' Assignment
| NUM '/' Assignment
| '\'' Assignment '\''
| '(' Assignment ')'
| '-' '(' Assignment ')'
| '-' NUM
| '-' ID
| NUM
| ID
;
/* Function Call Block */
FunctionCall
: ID'('')'
| ID'('Assignment')'
;
/* Array Usage */
ArrayUsage
: ID'['Assignment']'
;
/* Function block */
Function
: Type ID '(' ArgListOpt ')' CompoundStmt
;
ArgListOpt
: ArgList
|
;
ArgList
: ArgList ',' Arg
| Arg
;
Arg
: Type ID
;
CompoundStmt
: '{' StmtList '}'
;
StmtList
: StmtList Stmt
|
;
Stmt
: WhileStmt
| Declaration
| ForStmt
| IfStmt
| PrintFunc
| ';'
;
/* Type Identifier block */
Type
: INT
| FLOAT
| CHAR
| DOUBLE
| VOID
;
/* Loop Blocks */
WhileStmt
: WHILE '(' Expr ')' Stmt
| WHILE '(' Expr ')' CompoundStmt
;
/* For Block */
ForStmt
: FOR '(' Expr ';' Expr ';' Expr ')' Stmt
| FOR '(' Expr ';' Expr ';' Expr ')' CompoundStmt
| FOR '(' Expr ')' Stmt
| FOR '(' Expr ')' CompoundStmt
;
/* IfStmt Block */
IfStmt
: IF '(' Expr ')' Stmt
;
/* Struct Statement */
StructStmt
: STRUCT ID '{' Type Assignment '}'
;
/* Print Function */
PrintFunc
: PRINTF '(' Expr ')' ';'
;
/*Expression Block*/
Expr
:
| Expr LE Expr
| Expr GE Expr
| Expr NE Expr
| Expr EQ Expr
| Expr GT Expr
| Expr LT Expr
| Assignment
| ArrayUsage
;
%%
int main(int argc, char *argv[])
{
yyin = fopen(argv[1], "r");
if(!yyparse())
printf("\nParsing complete\n");
else
printf("\nParsing failed\n");
fclose(yyin);
return 0;
}
yyerror(char *s) {
printf("%d : %s %s\n", yylineno, s, yytext );
}
int yywrap()
{
return 1;
}
what modification should be done in lexer (.l) and bison (.y) file so that while parsing c file, if that c file includes some header file then it go to that header file reads it and return to original test c file and if the custom defined datatype is present then it will know its datatype from header file and prints it.
Will it can be possible? What modification I have to make ? Thank you