%{ #include <stdio.h> #include <stdlib.h>

int yylex(); void yyerror(); %}

%union { int num; char id; }

%token <id> ID %token <num> NUM %token INT MAIN PRINTF

%%

program: INT MAIN '(' ')' '{' stmts '}' { printf("\nAssembly Code:\n"); } ;

stmts: stmts stmt | stmt ;

stmt: INT ID '=' NUM ';' { printf("movl $%d, %c\n", $4, $2); } | ID '=' ID '+' ID ';' { printf("movl %c, %%eax\n", $3); printf("addl %c, %%eax\n", $5); printf("movl %%eax, %c\n", $1); } | ID '=' ID '-' ID ';' { printf("movl %c, %%eax\n", $3); printf("subl %c, %%eax\n", $5); printf("movl %%eax, %c\n", $1); } | ID '=' ID '*' ID ';' { printf("movl %c, %%eax\n", $3); printf("imull %c, %%eax\n", $5); printf("movl %%eax, %c\n", $1); } | ID '=' ID '/' ID ';' { printf("movl %c, %%eax\n", $3); printf("idivl %c\n", $5); printf("movl %%eax, %c\n", $1); } | PRINTF '(' ID ')' ';' { printf("movl %c, %%edi\n", $3); printf("call printf\n"); } ;

%%

int main(){ printf("Enter code:\n"); yyparse(); return 0; }

void yyerror(){ printf("Invalid\n"); }