A basic parser for parsing C++ function definition using LEX and YACC
Syntax of C++ function definition:
return_type function_name( parameter list ) {
body of the function
}
- Basic data types are supported (void, bool, char, int, float, double).
- Default arguments are not supported.
- Comments are not supported.
- Variables have to be declared before initialization.
- Basic arithmetic operators are supported (+, -, *, /, %).
- Comparison operators are not supported (>, <, <=, >=, ==, !=).
- Standard stream objects are not supported (cout, cin not supported).
Generate lex.yy.c file
flex fdef.l
Generate y.tab.c and y.tab.h files
bison -yd fdef.y
Compile the C files generated
gcc lex.yy.c y.tab.c
Run program using a.out file
./a.out
float add(int a, float b){
float ans;
ans = a + b;
return ans;
}
int ord(char c){
return c + 0;
}
int (int b){
return b;
}
function(){
return 0;
}
-
Lex Yacc Introduction
-
Lex yacc primer
-
Bison manual references