EXPERIMENT 4
To implement three address code using Lex & YACC and generate quadruples and triples.

========================================================================

LEX Program (demo.l)
--------------------
%{
    #include "y.tab.h"
    %}
%%
[0-9]+      { yylval.sym = yytext[0]; return NUMBER; }
[a-zA-Z]+   { yylval.sym = yytext[0]; return LETTER; }
[ \t]        ;
\n           { return 0; }
.            { return yytext[0]; }
%%
int yywrap()
{
    return 1;
}

YACC Program (demo.y)
---------------------
%{
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    char AddToTable(char, char, char);
    void ThreeAddressCode();
    void quadraple();
    void triple();
    int ind = 0;
    char temp = '1';
    struct incod
    {
        char opd1;
        char opd2;
        char opr;
    };
struct incod code[20];
%}
%union
{
    char sym;
}
%token <sym> LETTER NUMBER
%type <sym> expr
%left '+' '-'
%left '*' '/'
%right UMINUS
%%
statement:
LETTER '=' expr ';'
{
    AddToTable($1, $3, '=');
}
| expr ';'
;
expr:
expr '+' expr
{
    $$ = AddToTable($1, $3, '+');
}
| expr '-' expr
{
    $$ = AddToTable($1, $3, '-');
}
| expr '*' expr
{
    $$ = AddToTable($1, $3, '*');
}
| expr '/' expr
{
    $$ = AddToTable($1, $3, '/');
}
| '(' expr ')'
{
    $$ = $2;
}
| NUMBER
{
    $$ = $1;
}
| LETTER
{
    $$ = $1;
}
| '-' expr %prec UMINUS
{
    $$ = AddToTable($2, '\t', '-');
}
;
%%
int yyerror(char *s)
{
    printf("Error: %s\n", s);
    return 0;
}
char AddToTable(char opd1, char opd2, char opr)
{
    code[ind].opd1 = opd1;
    code[ind].opd2 = opd2;
    code[ind].opr = opr;
    ind++;
    return temp++;
}
void ThreeAddressCode()
{
    int cnt = 0;
    char t = '1';
    printf("\n\n\tTHREE ADDRESS CODE\n\n");
    while (cnt < ind)
    {
        if (code[cnt].opr != '=')
        printf("t%c :=\t", t++);
        if (isalpha(code[cnt].opd1))
        printf("%c\t", code[cnt].opd1);
        else if (code[cnt].opd1 >= '1' && code[cnt].opd1 <= '9')
        printf("t%c\t", code[cnt].opd1);
        printf("%c\t", code[cnt].opr);
        if (isalpha(code[cnt].opd2))
        printf("%c\n", code[cnt].opd2);
        else if (code[cnt].opd2 >= '1' && code[cnt].opd2 <= '9')
        printf("t%c\n", code[cnt].opd2);
        cnt++;
    }
}
void quadraple()
{
    int cnt = 0;
    char t = '1';
    printf("\n\n\tQUADRUPLE CODE\n\n");
    printf("Operator\tArg1\tArg2\tResult\n");
    while (cnt < ind)
    {
        printf("%c\t\t", code[cnt].opr);
        if (isalpha(code[cnt].opd1))
        printf("%c\t", code[cnt].opd1);
        else if (code[cnt].opd1 >= '1' && code[cnt].opd1 <= '9')
        printf("t%c\t", code[cnt].opd1);
        else
        printf("-\t");
        if (isalpha(code[cnt].opd2))
        printf("%c\t", code[cnt].opd2);
        else if (code[cnt].opd2 >= '1' && code[cnt].opd2 <= '9')
        printf("t%c\t", code[cnt].opd2);
        else
        printf("-\t");
        if (code[cnt].opr == '=')
        printf("%c\n", code[cnt].opd1);
        else
        printf("t%c\n", t++);
        cnt++;
    }
}
void triple()
{
    int cnt = 0;
    char t = '1';
    printf("\n\n\tTRIPLE CODE\n\n");
    printf("No.\tOperator\tArg1\tArg2\n");
    while (cnt < ind)
    {
        printf("(%c)\t%c\t\t", t++, code[cnt].opr);
        if (isalpha(code[cnt].opd1))
        printf("%c\t", code[cnt].opd1);
        else if (code[cnt].opd1 >= '1' && code[cnt].opd1 <= '9')
        printf("(%c)\t", code[cnt].opd1);
        else
        printf("-\t");
        if (isalpha(code[cnt].opd2))
        printf("%c\n", code[cnt].opd2);
        else if (code[cnt].opd2 >= '1' && code[cnt].opd2 <= '9')
        printf("(%c)\n", code[cnt].opd2);
        else
        printf("-\n");
        cnt++;
    }
}
int main()
{
    printf("\nEnter the Expression: ");
    yyparse();
    ThreeAddressCode();
    quadraple();
    triple();
    return 0;
}
int yywrap(void)
{
    return 1;
}

Example Input
-------------
for(int i=0;i<n;i++){ sum+=i;}
