From 896e4859333c6be801391ae9bbeec425f6e32f86 Mon Sep 17 00:00:00 2001 From: Akshay Date: Fri, 7 May 2021 10:37:28 +0530 Subject: add prog 12: function definition --- 12/main.y | 112 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 12/main.y (limited to '12/main.y') diff --git a/12/main.y b/12/main.y new file mode 100644 index 0000000..5431c55 --- /dev/null +++ b/12/main.y @@ -0,0 +1,112 @@ +%{ +#include +#include +int yylex(); +int yyerror(char *); +%} + +%token ADD SUB MUL DIV INC DEC +%token LT GT LTE GTE EQ NEQ ASSIGN +%token VOID INT DOUBLE FLOAT CHAR +%token LONG UNSIGNED +%token COMMA LPAREN RPAREN LBRACE RBRACE +%token ENDL +%token FOR RET IF ELSE WHILE +%token NUM ID + +%start function_definition + +%% +type_modifier: LONG + | UNSIGNED + ; + +type_specifier: VOID + | CHAR + | INT + | FLOAT + | DOUBLE + | type_modifier type_specifier + ; + +argument: type_specifier ID + ; + +argument_list: argument + | argument COMMA argument_list + ; + +primary_expr: NUM + | ID + ; + +postfix_expr: postfix_expr INC + | postfix_expr DEC + | primary_expr + ; + +unary_expr: INC unary_expr + | DEC unary_expr + | postfix_expr + ; + +boolean_expr: unary_expr LT unary_expr + | unary_expr GT unary_expr + | unary_expr LTE unary_expr + | unary_expr GTE unary_expr + | unary_expr EQ unary_expr + | unary_expr NEQ unary_expr + ; + +assignment_expr: unary_expr ASSIGN unary_expr + | unary_expr ASSIGN boolean_expr + ; + +expression: unary_expr + | boolean_expr + | assignment_expr + ; + +for_expr: FOR LPAREN statement statement expression RPAREN statement + | FOR LPAREN statement statement RPAREN statement + ; + +while_expr: WHILE LPAREN expression RPAREN block + | WHILE LPAREN RPAREN block + ; + +if_expr: IF LPAREN expression RPAREN block + | IF LPAREN expression RPAREN block ELSE block + | IF LPAREN expression RPAREN block ELSE if_expr + ; + +block: LBRACE statement block_cont + ; + +block_cont: statement block_cont + | RBRACE + ; + +statement: RET expression ENDL + | expression ENDL + | block + | for_expr + | if_expr + | while_expr + | ENDL + ; + +function_definition: argument LPAREN argument_list RPAREN block + | argument LPAREN RPAREN block + ; +%% + +int main() { + yyparse(); + printf("valid expression"); +} + +int yyerror(char *s) { + printf("error: %s", s); + exit(0); +} -- cgit v1.2.3