Showing posts with label YACC. Show all posts
Showing posts with label YACC. Show all posts

Parser for SQL Nested Queries - Compiler Design - Yacc Program

Program:

// Lex file: sq.l
alpha [A-Za-z]
digit [0-9]
%%

[ \t\n]
select        return SELECT;
distinct     return DISTINCT;
from         return FROM;
where       return WHERE;
like           return LIKE;
desc         return DESC;
asc           return ASC;
"group by"    return GROUP;
having           return HAVING;
"order by"     return ORDER;
or                  return OR;
and                return AND;
in                   return IN;
{digit}+        return NUM;
{alpha}({alpha}|{digit})* return ID;
"<="              return LE;
">="              return GE;
"=="              return EQ;
"!="               return NE;
.                     return yytext[0];
%%

// Yacc file: sq.y

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

%}
%token ID NUM SELECT DISTINCT FROM WHERE LE GE EQ NE OR AND LIKE GROUP HAVING ORDER ASC DESC IN
%right '='
%left AND OR
%left '<' '>' LE GE EQ NE

%%

    S    : ST1';' {printf("INPUT ACCEPTED...\n");exit(0);};
    ST1    : SELECT attributeList FROM tableList ST2
        | SELECT DISTINCT attributeList FROM tableList ST2
        ;
    ST2    : WHERE COND ST3
        | ST3
        ;
    ST3    : GROUP attributeList ST4
        | ST4
        ;
    ST4    : HAVING COND ST5
        | ST5
        ;
    ST5    : ORDER attributeList ST6
        |
        ;
    ST6    : DESC
        | ASC
        |
        ;
  attributeList :     ID','attributeList
        | '*'
        | ID
        ;
 tableList    : ID',' tableList
        | ID
        ;
    COND    : COND OR COND
        | COND AND COND
        | E
        | ID IN '(' ST1 ')'
        ;
    E    : F'=' F
        | F '<' F
        | F '>' F 
        | F LE F
        | F GE F
        | F EQ F
        | F NE F
        | F OR F
        | F AND F
        | F LIKE F
        ;
    F    : ID
        | NUM 
        ;
%%
#include"lex.yy.c"
#include<ctype.h>
main()
{
    printf("Enter the query:");
    yyparse();
}       

Output:

nn@linuxmint ~ $ lex sq.l
nn@linuxmint ~ $ yacc sq.y
nn@linuxmint ~ $ gcc y.tab.c -ll -ly
nn@linuxmint ~ $ ./a.out
Enter the query:select model from product where manufacture in ( select manuid from manufactures where manufacture = keltron);
INPUT ACCEPTED...
nn@linuxmint ~ $

Parser for Switch Statements with If-then and While Statements inside - Compiler Design - Yacc Program

Program:

// Lex file: pars.l


alpha [a-zA-Z]
digit [0-9]

%%

[ \n\t]
if                  return IF;
then             return THEN;
while           return WHILE;
switch         return SWITCH;
case             return CASE;
default         return DEFAULT;
break           return BREAK;
{digit}+       return NUM;
{alpha}({alpha}|{digit})* return ID;
"<="            return LE;
">="            return GE;
"=="            return EQ;
"!="             return NE;
"&&"           return AND;
"||"               return OR;
.                   return yytext[0];

%%

// Yacc file: pars.y

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

%token ID NUM SWITCH CASE DEFAULT BREAK LE GE EQ NE AND OR IF THEN WHILE
%right '='
%left AND OR
%left '<' '>' LE GE EQ NE
%left '+''-'
%left '*''/'
%right UMINUS
%left '!'

%%

S    :    ST{printf("\nInput accepted.\n");exit(0);};
      ;
ST  :    SWITCH'('ID')''{'B'}'
      ;
B    :    C
      |    C D
      ;
C  :    C C
    |    CASE NUM':'ST1 BREAK';'
    ;
D  :    DEFAULT':'ST1 BREAK';'
    |    DEFAULT':'ST1
    ;
ST1    :    WHILE'('E2')' E';'
    |    IF'('E2')'THEN E';'
    |    ST1 ST1
    |    E';'
    ;
E2    :    E'<'E
    |    E'>'E
    |    E LE E
    |    E GE E
    |    E EQ E
    |    E NE E
    |    E AND E
    |    E OR E
    ;
E    :    ID'='E
    |    E'+'E
    |    E'-'E
    |    E'*'E
    |    E'/'E
    |    E'<'E
    |    E'>'E
    |    E LE E
    |    E GE E
    |    E EQ E
    |    E NE E
    |    E AND E
    |    E OR E
    |    ID
    |    NUM
    ;

%%

#include"lex.yy.c"

main()
{
    printf("\nEnter the expression: ");
    yyparse();
}

Output:

nn@linuxmint ~ $ lex pars.l
nn@linuxmint ~ $ yacc pars.y
conflicts: 5 shift/reduce
nn@linuxmint ~ $ gcc y.tab.c -ll -ly
nn@linuxmint ~ $ ./a.out

Enter the expression: switch(s)
{
case 1:a=b+c;break;
case 2:    if(a<10)
    then a=b*c;
    break;
case 3:    while(a<5)
    b=b+a;
    break;
}

Input accepted.
nn@linuxmint ~ $

Intermediate Code Generator for For Loop - Compiler Design - Yacc Program

Program:

// Lex file: im4.l

alpha [A-Za-z]
digit [0-9]

%%

[\t \n]
for             return FOR;
{digit}+    return NUM;
{alpha}({alpha}|{digit})* return ID;
"<="         return LE;
">="         return GE;
"=="         return EQ;
"!="          return NE;
"||"            return OR;
"&&"         return AND;
.                return yytext[0];

%%

// Yacc file: im4.y

%{
#include <stdio.h>
#include <stdlib.h>
%}
%token ID NUM FOR LE GE EQ NE OR AND
%right "="
%left OR AND
%left '>' '<' LE GE EQ NE
%left '+' '-'
%left '*' '/'
%right UMINUS
%left '!'

%%

S       : FOR '(' E ';'{lab1();} E {lab2();}';' E {lab3();}')' E';'{lab4(); exit(0);}
         ;
E       : V '='{push();} E{codegen_assign();}
         | E '+'{push();} E{codegen();}
         | E '-'{push();} E{codegen();}
         | E '*'{push();} E{codegen();}
         | E '/'{push();} E{codegen();}
         | '(' E ')'
         | '-'{push();} E{codegen_umin();} %prec UMINUS
         | V
         | NUM{push();}
         ;
V       : ID {push();}
         ;

%%

#include "lex.yy.c"
#include<ctype.h>
char st[100][10];
int label[20];
int top=0;
char i_[2]="0";
char temp[2]="t";

int lno=0,ltop=0;
int start=1;

main()
{
    printf("Enter the expression:\n");
    yyparse();
}

push()
{
   strcpy(st[++top],yytext);
}

codegen()
{
    strcpy(temp,"t");
    strcat(temp,i_);
    printf("%s = %s %s %s\n",temp,st[top-2],st[top-1],st[top]);
    top-=2;
    strcpy(st[top],temp);
    i_[0]++;
}

codegen_umin()
{
    strcpy(temp,"t");
    strcat(temp,i_);
    printf("%s = -%s\n",temp,st[top]);
    top--;
    strcpy(st[top],temp);
    i_[0]++;
}

codegen_assign()
{
    printf("%s = %s\n",st[top-2],st[top]);
    top-=2;
}
lab1()
{
    printf("L%d: \n",lno++);
}
lab2()
{
    strcpy(temp,"t");
    strcat(temp,i_);
    printf("%s = not %s\n",temp,st[top]);
    printf("if %s goto L%d\n",temp,lno);
    i_[0]++;
    label[++ltop]=lno;
    lno++;
    printf("goto L%d\n",lno);
    label[++ltop]=lno;
    printf("L%d: \n",++lno);
 }
lab3()
{
    int x;
    x=label[ltop--];
    printf("goto L%d \n",start);
    printf("L%d: \n",x);
   
}

lab4()
{
    int x;
    x=label[ltop--];
    printf("goto L%d \n",lno);   
    printf("L%d: \n",x);
}

Output:
nn@linuxmint ~ $ lex im4.l
nn@linuxmint ~ $ yacc im4.y
conflicts: 4 shift/reduce
nn@linuxmint ~ $ gcc y.tab.c -ll -ly
nn@linuxmint ~ $ ./a.out
Enter the expression:
for(i=0;i=b;i=i+1) a=a+b;
i = 0
L0:
i = b
t0 = not i
if t0 goto L1
goto L2
L3:
t1 = i + 1
i = t1
goto L0
L2:
t2 = a + b
a = t2
goto L3
L1:
nn@linuxmint ~ $

Postfix to Infix - Yacc Program - Compiler Design


Program:-"Beta version"-(Partial Output Only)

// Lex file: ptoi.l

DIGIT [0-9]
%%
{DIGIT}+    {yylval=atoi(yytext);return ID;}
[-+*/]            {return yytext[0];}
.                      ;
\n         yyterminate();

// Yacc file: ptoi.y
%{
    #include<stdio.h>
    #include<string.h>
    void push();
    char* top();
    void a1(char* a);
%}

%token ID

%%

S    : E  { printf("= %s \n",top());}
    ;
E    : E E '+' {a1(" + ");}
    | E E '*' {a1(" * ");}
    | E E '-' {a1(" - ");}
    | E E '/' {a1(" / ");}
    | ID    {push();}
    ;

%%
#include"lex.yy.c"

char st[100][10];
int indx=0;

void push()
{
   strcpy(st[indx++],yytext);
}

char* pop()
{
    return st[--indx];
}

char* top()
{
    return st[indx-1];
}
void a1(char* a)
{
    char buffer[20];
    char* c1=pop();
    char* c2=pop();
    bzero(buffer,20);
    strcat(buffer,c2);
    strcat(buffer,a);
    strcat(buffer,c1);
    strcpy(st[indx++],buffer);
}
main()
{
    yyparse();

}

Output:
nn@linuxmint ~ $ lex ptoi.l
nn@linuxmint ~ $ yacc ptoi2.y
nn@linuxmint ~ $ gcc y.tab.c -ll -ly
nn@linuxmint ~ $ ./a.out
2 5 * 3 2 * +
= 2 * 5 + 3 * 2
nn@linuxmint ~ $ ./a.out
2 5 +
= 2 + 5
nn@linuxmint ~ $ ./a.out
2 3 *
= 2 * 3
nn@linuxmint ~ $ ./a.out
2 3 -
= 2 - 3
nn@linuxmint ~ $ ./a.out
2 5 /
= 2 / 5
nn@linuxmint ~ $

Intermediate Code Generator for Arithmetic Expression II (Advanced)- Yacc Program - Compiler Lab

Program:

// Lex file: imadv.l

ALPHA [A-Za-z]
DIGIT [0-9]

%%

{ALPHA}({ALPHA}|{DIGIT})*        return ID;
{DIGIT}+ {yylval=atoi(yytext);       return NUM;}
[\n\t]                                             yyterminate();
.                                                   return yytext[0];

%%


// Yacc file: imadv.y

%token ID NUM
%right '='
%left '+' '-'
%left '*' '/'
%left UMINUS
%%

S    :    ID{push();} '='{push();} E{codegen_assign();}
      ;
E    :    E '+'{push();} T{codegen();}
      |    E '-'{push();} T{codegen();}
      |    T
      ;
T    :    T '*'{push();} F{codegen();}
      |    T '/'{push();} F{codegen();}
      |    F
      ;
F    :    '(' E ')'
      |    '-'{push();} F{codegen_umin();} %prec UMINUS
      |    ID{push();}
      |    NUM{push();}
      ;

%%

#include "lex.yy.c"
#include<ctype.h>
#include<string.h>
char st[100][25];
int top=0,ptr=0;
int tint=0; int tintar[200];

main()
{
    printf("Enter the expression : ");
    yyparse();
}

push()
{
  strcpy(st[++top],yytext);
  ptr++;
}

codegen()
{
    printf("t%d = %s",tint,st[top-2]);
    printnum(2);
    printf(" %s %s",st[top-1],st[top]);
    printnum(0);
    printf("\n");
    top-=2;ptr-=2;
    strcpy(st[top],"t");
    tintar[ptr]=tint;
    tint++;
}

codegen_umin()
{
    printf("t%d = -%s\n",tint,st[top]);
    printnum(0);
    top--;ptr--;
    strcpy(st[top],"t");
    tintar[ptr]=tint;
    tint++;
}
codegen_assign()
{
    printf("%s = ",st[top-2]);
    printnum(2);
    printf("%s",st[top]);
    printnum(0);
    printf("\n");
    top-=2;ptr-=2;
}
printnum(int n)
{
    if( strcmp(st[top-n],"t")==0)
    {
         printf("%d",tintar[ptr-n]);
    }
}

Output:

nn@linuxmint ~ $ lex imadv.l
nn@linuxmint ~ $ yacc imadv.y
nn@linuxmint ~ $ gcc y.tab.c -ll -ly
nn@linuxmint ~ $ ./a.out
Enter the expression : a=b*c+d*c-c/5+2*8-5+5*5/8-8*9/8+8*7
t0 = b * c
t1 = d * c
t2 = t0 + t1
t3 = c / 5
t4 = t2 - t3
t5 = 2 * 8
t6 = t4 + t5
t7 = t6 - 5
t8 = 5 * 5
t9 = t8 / 8
t10 = t7 + t9
t11 = 8 * 9
t12 = t11 / 8
t13 = t10 - t12
t14 = 8 * 7
t15 = t13 + t14
a = t15
nn@linuxmint ~ $

Parser for FOR Loop Statements - YACC Program - Compiler Design

Program:

// Lex file: for.l

alpha [A-Za-z]
digit [0-9]

%%

[\t \n]
for             return FOR;
{digit}+    return NUM;
{alpha}({alpha}|{digit})* return ID;
"<="         return LE;
">="         return GE;
"=="         return EQ;
"!="          return NE;
"||"            return OR;
"&&"         return AND;
.                return yytext[0];

%%


// Yacc file: for.y

%{
#include <stdio.h>
#include <stdlib.h>
%}
%token ID NUM FOR LE GE EQ NE OR AND
%right "="
%left OR AND
%left '>' '<' LE GE EQ NE
%left '+' '-'
%left '*' '/'
%right UMINUS
%left '!'

%%
   
S         : ST {printf("Input accepted\n"); exit(0);}
ST       : FOR '(' E ';' E2 ';' E ')' DEF
           ;
DEF    : '{' BODY '}'
           | E';'
           | ST
           |
           ;
BODY  : BODY BODY
           | E ';'       
           | ST
           |            
           ;
       
E        : ID '=' E
          | E '+' E
          | E '-' E
          | E '*' E
          | E '/' E
          | E '<' E
          | E '>' E
          | E LE E
          | E GE E
          | E EQ E
          | E NE E
          | E OR E
          | E AND E
          | E '+' '+'
          | E '-' '-'
          | ID 
          | NUM
          ;

   
E2     : E'<'E
         | E'>'E
         | E LE E
         | E GE E
         | E EQ E
         | E NE E
         | E OR E
         | E AND E
         ;   
%%

#include "lex.yy.c"
main() {
    printf("Enter the expression:\n");
    yyparse();
}     
     


Output:
nn@linuxmint ~ $ lex for.l
nn@linuxmint ~ $ yacc for.y
conflicts: 25 shift/reduce, 4 reduce/reduce
nn@linuxmint ~ $ gcc y.tab.c -ll -ly
nn@linuxmint ~ $ ./a.out
Enter the expression:
for(i=0;i<n;i++)
i=i+1;
Input accepted
nn@linuxmint ~ $

Postfix Expression Evaluation - Compiler Design - YACC Program

Program:

// Lex file: pos.l

DIGIT [0-9]
%%
{DIGIT}+    {yylval=atoi(yytext);return ID;}
[-+*/]        {return yytext[0];}
. ;
\n         yyterminate();

// Yacc File: pos.y

%{
    #include<stdio.h>
    #include<assert.h>
    void push(int val);
%}

%token ID

%%

S     : E  {printf("= %d\n",top());}
      ;
E     : E E '+' {push(pop()+pop());}
     | E E '-' {int temp=pop();push(pop()-temp);}
     | E E '*' {push(pop()*pop());}
     | E E '/' {int temp=pop();push(pop()/temp);}
     | ID    {push(yylval);}
     ;

%%
#include"lex.yy.c"

int st[100];
int i=0;

void push(int val)
{
    assert(i<100);
    st[i++]=val;
   
}

int pop()
{
    assert(i>0);
    return st[--i];

}

int top()
{
    assert(i>0);
    return st[i-1];
}
int main()
{
    yyparse();
    return 0;
}

Output:

nn@linuxmint ~ $ lex pos.l
nn@linuxmint ~ $ yacc pos.y
nn@linuxmint ~ $ gcc y.tab.c -ll -ly
nn@linuxmint ~ $ ./a.out
5 5 -
= 0
nn@linuxmint ~ $

Desk Calculator with Error Recovery - Yacc Program - Compiler Design

Program:

// Lex file: desk.l

DIGIT [0-9]+\.?|[0-9]*\.[0-9]+

%%

[ ]
{DIGIT}    {yylval=atof(yytext);return NUM;}
\n|.             {return yytext[0];}

// Yacc file: desk.y

%{
    #include<ctype.h>
    #include<stdio.h>
    #define YYSTYPE double
%}

%token NUM

%left '+' '-'
%left '*' '/'
%right UMINUS

%%

S         : S E '\n' { printf("Answer: %g \nEnter:\n", $2); }
           | S '\n'
           |
           | error '\n' { yyerror("Error: Enter once more...\n" );yyerrok; }
           ;
E         : E '+' E    { $$ = $1 + $3; }
           | E'-'E    { $$=$1-$3; }
           | E'*'E    { $$=$1*$3; }
           | E'/'E    { $$=$1/$3; }
           | '('E')'    { $$=$2; }
           | '-'E %prec UMINUS { $$= -$2; }
           | NUM       
           ;

%%

#include "lex.yy.c"

int main()
{
    printf("Enter the expression: ");
    yyparse();
}

Output:

nn@linuxmint ~ $ lex desk.l
nn@linuxmint ~ $ yacc desk.y
nn@linuxmint ~ $ gcc y.tab.c -ly -lfl
nn@linuxmint ~ $ ./a.out
Enter the expression: 1.8+2.8
Answer: 4.6
Enter:
1.8*10
Answer: 18
Enter:
20.34/.2
Answer: 101.7
Enter:
101.7*.2
Answer: 20.34
Enter:
20.34-18.31
Answer: 2.03
Enter:
^C
nn@linuxmint ~ $

Parser for SQL Query Select - YACC Program - Compiler Design

Program:

// Lex Program :sq.l


alpha [A-Za-z]
digit [0-9]
%%


[ \t\n]
select            return SELECT;
distinct         return DISTINCT;
from             return FROM;
where           return WHERE;
like               return LIKE;
desc              return DESC;
asc                return ASC;
"group by"    return GROUP;
having          return HAVING;
"order by"    return ORDER;
or                 return OR;
and               return AND;
{digit}+       return NUM;
{alpha}({alpha}|{digit})* return ID;
"<="             return LE;
">="             return GE;
"=="             return EQ;
"!="              return NE;
.                   return yytext[0];

%%


// Yacc Program: sq.y

%{
#include <stdio.h>
#include <stdlib.h>
%}
%token ID NUM SELECT DISTINCT FROM WHERE LE GE EQ NE OR AND LIKE GROUP HAVING ORDER ASC DESC
%right '='
%left AND OR
%left '<' '>' LE GE EQ NE

%%

    S         : ST1';' {printf("INPUT ACCEPTED.... \n\t\t...www.2k8cse.cu.cc\n");exit(0);};
    ST1     : SELECT attributeList FROM tableList ST2
               | SELECT DISTINCT attributeList FROM tableList ST2
               ;
    ST2     : WHERE COND ST3
               | ST3
               ;
    ST3     : GROUP attributeList ST4
               | ST4
               ;
    ST4     : HAVING COND ST5
               | ST5
               ;
    ST5     : ORDER attributeList ST6
               |
               ;
    ST6     : DESC
               | ASC
               |
               ;
  attributeList :     ID','attributeList
               | '*'
               | ID
               ;
 tableList    : ID',' tableList
               | ID
               ;
    COND    : COND OR COND
               | COND AND COND
               | E
               ;
    E         : F '=' F
               | F '<' F
               | F '>' F 
               | F LE F
               | F GE F
               | F EQ F
               | F NE F
               | F OR F
               | F AND F
               | F LIKE F
               ;
    F         : ID
               | NUM 
               ;
%%
#include"lex.yy.c"
#include<ctype.h>
main()
{
    printf("Enter the query:");
    yyparse();
}          


Output:
nn@linuxmint ~ $ lex sq.l
nn@linuxmint ~ $ yacc sq.y
nn@linuxmint ~ $ gcc y.tab.c -ll -ly
nn@linuxmint ~ $ ./a.out
Enter the query:select name,address from emp where age>20 group by name having age<40 order by name desc;
INPUT ACCEPTED...
                                   ...www.2k8cse.cu.cc
nn@linuxmint ~ $

Parser for Infix Expression to Postfix Expression - YACC Program - Compiler Lab

Program:

(Lex program: ip.l)

ALPHA [A-Z a-z]
DIGIT [0-9]
%%
{ALPHA}({ALPHA}|{DIGIT})*    return ID;
{DIGIT}+                                      {yylval=atoi(yytext); return ID;}
[\n \t]                                              yyterminate();
.                                                      return yytext[0];
%%

(Yacc program: ip.y)
%{
#include <stdio.h>
#include <stdlib.h>
%}
%token    ID
%left    '+' '-'
%left    '*' '/'
%left    UMINUS

%%

S    :    E
E    :    E'+'{A1();}T{A2();}
      |    E'-'{A1();}T{A2();}
      |    T
      ;
T    :    T'*'{A1();}F{A2();}
      |    T'/'{A1();}F{A2();}
      |    F
      ;
F    :    '('E{A2();}')'
      |    '-'{A1();}F{A2();}
      |    ID{A3();}
      ;

%%

#include "lex.yy.c"
char st[100];
int top=0;

main()
{
    printf("Enter infix expression:  ");
    yyparse();
    printf("\n");
}

A1()
{
    st[top++]=yytext[0];
}

A2()
{
    printf("%c",st[--top]);
}

A3()
{
    printf("%c",yytext[0]);
}

Output:
nn@linuxmint ~ $ lex ip.l
nn@linuxmint ~ $ yacc ip.y
nn@linuxmint ~ $ gcc y.tab.c -ll -ly
nn@linuxmint ~ $ ./a.out
Enter infix expression:  (3+5)*(6-2)
35+62-*
nn@linuxmint ~ $ 

Intermediate Code Generator for While - Yacc Program - Compiler Lab

Program:

(Lex Program: intwh.l)


ALPHA [A-Za-z]
DIGIT [0-9]
%%
while                return WHILE;
{ALPHA}({ALPHA}|{DIGIT})*    return ID;
{DIGIT}+             {yylval=atoi(yytext); return NUM;}
[ \t]                 ;
\n                yyterminate();
.                 return yytext[0];
%%


(Yacc Program: intwh.y) 



%token ID NUM WHILE
%right '='
%left '+' '-'
%left '*' '/'
%left UMINUS
%%

S : WHILE{lab1();} '(' E ')'{lab2();} E ';'{lab3();}
  ;
E :V '='{push();} E{codegen_assign();}
  | E '+'{push();} E{codegen();}
  | E '-'{push();} E{codegen();}
  | E '*'{push();} E{codegen();}
  | E '/'{push();} E{codegen();}
  | '(' E ')'
  | '-'{push();} E{codegen_umin();} %prec UMINUS
  | V
  | NUM{push();}
  ;
V : ID {push();}
  ;
%%

#include "lex.yy.c"
#include<ctype.h>
char st[100][10];
int top=0;
char i_[2]="0";
char temp[2]="t";

int lnum=1;
int start=1;
main()
 {
 printf("Enter the expression : ");
 yyparse();
 }



push()
 {
  strcpy(st[++top],yytext);
 }

codegen()
 {
 strcpy(temp,"t");
 strcat(temp,i_);
  printf("%s = %s %s %s\n",temp,st[top-2],st[top-1],st[top]);
  top-=2;
 strcpy(st[top],temp);
 i_[0]++;
 }

codegen_umin()
 {
 strcpy(temp,"t");
 strcat(temp,i_);
 printf("%s = -%s\n",temp,st[top]);
 top--;
 strcpy(st[top],temp);
 i_[0]++;
 }

codegen_assign()
 {
 printf("%s = %s\n",st[top-2],st[top]);
 top-=2;
 }



lab1()
{
printf("L%d: \n",lnum++);
}


lab2()
{
 strcpy(temp,"t");
 strcat(temp,i_);
 printf("%s = not %s\n",temp,st[top]);
 printf("if %s goto L%d\n",temp,lnum);
 i_[0]++;
 }

lab3()
{
printf("goto L%d \n",start);
printf("L%d: \n",lnum);
}



Output :
nn@linuxmint ~ $ lex intwh.l
nn@linuxmint ~ $ yacc intwh.y
nn@linuxmint ~ $ gcc y.tab.c -ll -ly
nn@linuxm./a.out
Enter the expression : while(k=c/s)k=k*c+d;
L1:
t0 = c / s
k = t0
t1 = not k
if t1 goto L0
t2 = k * c
t3 = t2 + d
k = t3
goto L1
L0:
nn@linuxmint ~ $

Intermediate Code Generator for If then else - Yacc Program - Compiler Lab

Program :

(lex Program : intif.l)

ALPHA [A-Za-z]
DIGIT [0-9]
%%
if                 return IF;
then                 return THEN;
else                 return ELSE;
{ALPHA}({ALPHA}|{DIGIT})*    return ID;
{DIGIT}+             {yylval=atoi(yytext); return NUM;}
[ \t]                 ;
\n                yyterminate();
.                 return yytext[0];
%%

(Yacc Program : intif.y)

%token ID NUM IF THEN ELSE
%right '='
%left '+' '-'
%left '*' '/'
%left UMINUS
%%

S : IF '(' E ')'{lab1();} THEN E ';'{lab2();} ELSE E ';'{lab3();}
  ;
E :V '='{push();} E{codegen_assign();}
  | E '+'{push();} E{codegen();}
  | E '-'{push();} E{codegen();}
  | E '*'{push();} E{codegen();}
  | E '/'{push();} E{codegen();}
  | '(' E ')'
  | '-'{push();} E{codegen_umin();} %prec UMINUS
  | V
  | NUM{push();}
  ;
V : ID {push();}
  ;
%%

#include "lex.yy.c"
#include<ctype.h>
char st[100][10];
int top=0;
char i_[2]="0";
char temp[2]="t";

int label[20];
int lnum=0;
int ltop=0;

main()
 {
 printf("Enter the expression : ");
 yyparse();
 }

push()
 {
  strcpy(st[++top],yytext);
 }

codegen()
 {
 strcpy(temp,"t");
 strcat(temp,i_);
  printf("%s = %s %s %s\n",temp,st[top-2],st[top-1],st[top]);
  top-=2;
 strcpy(st[top],temp);
 i_[0]++;
 }

codegen_umin()
 {
 strcpy(temp,"t");
 strcat(temp,i_);
 printf("%s = -%s\n",temp,st[top]);
 top--;
 strcpy(st[top],temp);
 i_[0]++;
 }

codegen_assign()
 {
 printf("%s = %s\n",st[top-2],st[top]);
 top-=2;
 }

lab1()
{
 lnum++;
 strcpy(temp,"t");
 strcat(temp,i_);
 printf("%s = not %s\n",temp,st[top]);
 printf("if %s goto L%d\n",temp,lnum);
 i_[0]++;
 label[++ltop]=lnum;
}

lab2()
{
int x;
lnum++;
x=label[ltop--];
printf("goto L%d\n",lnum);
printf("L%d: \n",x);
label[++ltop]=lnum;
}

lab3()
{
int y;
y=label[ltop--];
printf("L%d: \n",y);
}

Output:

nn@linuxmint ~ $ lex intif.l
nn@linuxmint ~ $ yacc intif.y
nn@linuxmint ~ $ gcc y.tab.c -ll -ly
nn@linuxmint ~ $ ./a.out
Enter the expression : if(k+8) then k=18;else c=s;
t0 = k + 8
t1 = not t0
if t1 goto L1
k = 18
goto L2
L1:
c = s
L2:
nn@linuxmint ~ $

Intermediate Code Generator for Arithmetic Expression - Yacc Program - Compiler Lab

Program:

(Lex Program : intar.l)

ALPHA [A-Za-z]
DIGIT [0-9]
%%

{ALPHA}({ALPHA}|{DIGIT})* return ID;
{DIGIT}+ {yylval=atoi(yytext); return NUM;}
[\n\t] yyterminate();
. return yytext[0];
%%

(Yacc Program : intar.y)

%token ID NUM
%right '='
%left '+' '-'
%left '*' '/'
%left UMINUS
%%

S : ID{push();} '='{push();} E{codegen_assign();}
   ;
E : E '+'{push();} T{codegen();}
   | E '-'{push();} T{codegen();}
   | T
   ;
T : T '*'{push();} F{codegen();}
   | T '/'{push();} F{codegen();}
   | F
   ;
F : '(' E ')'
   | '-'{push();} F{codegen_umin();} %prec UMINUS
   | ID{push();}
   | NUM{push();}
   ;
%%

#include "lex.yy.c"
#include<ctype.h>
char st[100][10];
int top=0;
char i_[2]="0";
char temp[2]="t";

main()
 {
 printf("Enter the expression : ");
 yyparse();
 }

push()
{
  strcpy(st[++top],yytext);
 }

codegen()
 {
 strcpy(temp,"t");
 strcat(temp,i_);
  printf("%s = %s %s %s\n",temp,st[top-2],st[top-1],st[top]);
  top-=2;
 strcpy(st[top],temp);
 i_[0]++;
 }

codegen_umin()
 {
 strcpy(temp,"t");
 strcat(temp,i_);
 printf("%s = -%s\n",temp,st[top]);
 top--;
 strcpy(st[top],temp);
 i_[0]++;
 }

codegen_assign()
 {
 printf("%s = %s\n",st[top-2],st[top]);
 top-=2;
 }


Output:

nn@linuxmint ~ $ lex intar.l
nn@linuxmint ~ $ yacc intar.y
nn@linuxmint ~ $ gcc y.tab.c -ll -ly
nn@linuxmint ~ $ ./a.out
Enter the expression : a=(k+8)*(c-s)
t0 = k + 8
t1 = c - s
t2 = t0 * t1
a = t2
nn@linuxmint ~ $

Parser for SWITCH Statements - YACC Program - Compiler Design



Program:
(sw.l)
alpha [A-Za-z]
digit [0-9]
%%
[ \t\n]  
switch    return SWITCH;
case return CASE;
break return BREAK;
default return DEFAULT;
{digit}+    return NUM;
{alpha}({alpha}|{digit})*    return ID;
"<="    return LE;
">="    return GE;
"=="    return EQ;
"!="    return NE;
"||"    return OR;
"&&"    return AND;
.    return yytext[0];
%%

(sw.y)
%{
#include <stdio.h>
#include <stdlib.h>
%}
%token ID NUM SWITCH CASE DEFAULT BREAK LE GE EQ NE OR AND ELSE
%right '='
%left AND OR
%left '<' '>' LE GE EQ NE
%left '+''-'
%left '*''/'
%right UMINUS
%left '!'
%%

S       : ST {printf("Input accepted.\n");exit(0);}
         ;
ST     :    SWITCH '(' ID ')' '{' B '}'
         ;
   
B       :    C
         |    C    D
        ;
   
C      :    C    C
        |    CASE NUM ':' E ';'
        | BREAK ';'
        ;

D      :    DEFAULT    ':' E ';' BREAK ';'
        ;
    
E    : ID'='E
    | E'+'E
    | E'-'E
    | E'*'E
    | E'/'E
    | E'<'E
    | E'>'E
    | E LE E
    | E GE E
    | E EQ E
    | E NE E
    | E OR E
    | E AND E
    | ID
    | NUM
    ;

%%

#include "lex.yy.c"

main()
{
printf("Enter the exp: ");
yyparse();
}

Output:
students@cselab-desktop:~$ lex sw.l
students@cselab-desktop:~$ yacc sw.y
conflicts: 2 shift/reduce
students@cselab-desktop:~$ gcc y.tab.c -ll -ly
students@cselab-desktop:~$ ./a.out
Enter the exp: switch(a)
{
case 1: a=1;
    break;
case 2: a=2;
    break;
default:a=0;
break;
}
Input accepted.
students@cselab-desktop:~$

Parser for DO WHILE Statements - YACC Program - Compiler Design





Program:

(dowh.l)
alpha [A-Za-z]
digit [0-9]
%%
[ \t\n]  
do    return DO;
while    return WHILE;
{digit}+    return NUM;
{alpha}({alpha}|{digit})*    return ID;
"<="    return LE;
">="    return GE;
"=="    return EQ;
"!="    return NE;
"||"    return OR;
"&&"    return AND;
.    return yytext[0];
%%

(dowh.y)
%{
#include <stdio.h>
#include <stdlib.h>
%}
%token ID NUM DO WHILE LE GE EQ NE OR AND
%right '='
%left AND OR
%left '<' '>' LE GE EQ NE
%left '+''-'
%left '*''/'
%right UMINUS
%left '!'

%%

S : ST1 {printf("Input accepted.\n");exit(0);};
ST1    :    DO '{' ST '}' WHILE'(' E2 ')'';';
ST      :     ST ST
          | E';'
          ;
E       : ID'='E
         | E'+'E
         | E'-'E
         | E'*'E
         | E'/'E
         | E'<'E
         | E'>'E
         | E LE E
         | E GE E
         | E EQ E
         | E NE E
         | E OR E
         | E AND E
         | ID
         | NUM
         ;
E2     : E'<'E
         | E'>'E
         | E LE E
         | E GE E
         | E EQ E
         | E NE E
         | E OR E
         | E AND E
         | ID
         | NUM
         ;

%%

#include "lex.yy.c"

main()
{
   printf("Enter the exp: ");
   yyparse();
}

Output:

students@ccflab-desktop:~$ lex dowh.l
students@ccflab-desktop:~$ yacc dowh.y
conflicts: 2 shift/reduce
students@ccflab-desktop:~$ gcc y.tab.c -ll -ly
students@ccflab-desktop:~$ ./a.out
Enter the exp: do{a=b+1;}while(a<10);
Input accepted.
students@ccflab-desktop:~$

Parser for WHILE Statements - YACC Program - Compiler Design



Program:

(wh.l)

alpha [A-Za-z]
digit [0-9]
%%
[ \t\n]  
while    return WHILE;
{digit}+    return NUM;
{alpha}({alpha}|{digit})*    return ID;
"<="    return LE;
">="    return GE;
"=="    return EQ;
"!="    return NE;
"||"    return OR;
"&&"    return AND;
.    return yytext[0];
%%

(wh.y)

%{
#include <stdio.h>
#include <stdlib.h>
%}
%token ID NUM WHILE LE GE EQ NE OR AND
%right '='
%left AND OR
%left '<' '>' LE GE EQ NE
%left '+''-'
%left '*''/'
%right UMINUS
%left '!'
%%
S        : ST1 {printf("Input accepted.\n");exit(0);};
ST1    :    WHILE'(' E2 ')' '{' ST '}'
ST      :     ST ST
          | E';'
          ;
E       : ID'='E
          | E'+'E
          | E'-'E
          | E'*'E
          | E'/'E
          | E'<'E
          | E'>'E
          | E LE E
          | E GE E
          | E EQ E
          | E NE E
          | E OR E
          | E AND E
          | ID
          | NUM
          ;
E2     : E'<'E
          | E'>'E
          | E LE E
          | E GE E
          | E EQ E
          | E NE E
          | E OR E
          | E AND E
          | ID
          | NUM
          ;

%%

#include "lex.yy.c"

main()
{
   printf("Enter the exp: ");
   yyparse();
}


Output:

students@ccflab-desktop:~$ lex wh.l
students@ccflab-desktop:~$ yacc wh.y
conflicts: 2 shift/reduce
students@ccflab-desktop:~$ gcc y.tab.c -ll -ly
students@ccflab-desktop:~$ ./a.out
Enter the exp: while(a>1){ b=1;}
Input accepted.

Parser for IF-THEN-ELSE Statements - YACC Program - Compiler Design





Program:

(Lex Program: ift.l)
alpha [A-Za-z]
digit [0-9]
%%
[ \t\n]   
if    return IF;
then    return THEN;
else    return ELSE;
{digit}+    return NUM;
{alpha}({alpha}|{digit})*    return ID;
"<="    return LE;
">="    return GE;
"=="    return EQ;
"!="    return NE;
"||"    return OR;
"&&"    return AND;
.    return yytext[0];
%%

(Yacc Program: ift.y)

%{
#include <stdio.h>
#include <stdlib.h>
%}
%token ID NUM IF THEN LE GE EQ NE OR AND ELSE
%right '='
%left AND OR
%left '<' '>' LE GE EQ NE
%left '+''-'
%left '*''/'
%right UMINUS
%left '!'
%%

S      : ST {printf("Input accepted.\n");exit(0);};
ST    : IF '(' E2 ')' THEN ST1';' ELSE ST1';'
        | IF '(' E2 ')' THEN ST1';'
        ;
ST1  : ST
        | E
        ;
E    : ID'='E
      | E'+'E
      | E'-'E
      | E'*'E
      | E'/'E
      | E'<'E
      | E'>'E
      | E LE E
      | E GE E
      | E EQ E
      | E NE E
      | E OR E
      | E AND E
      | ID
      | NUM
      ;
E2  : E'<'E
      | E'>'E
      | E LE E
      | E GE E
      | E EQ E
      | E NE E
      | E OR E
      | E AND E
      | ID
      | NUM
      ;

%%

#include "lex.yy.c"

main()
{
  printf("Enter the exp: ");
  yyparse();
}
       
Output:

students@cselab-desktop:~$ lex ift.lex
students@cselab-desktop:~$ yacc ift.y
students@cselab-desktop:~$ gcc y.tab.c -ll -ly
students@cselab-desktop:~$ ./a.out
Enter the exp: if(a==1)  then b=1; else b=2;
Input accepted.
students@cselab-desktop:~$

Expression Evaluation - Yacc Program - Compiler Design

Program:

// Yacc Program: expr.y

%{
#include"stdio.h"
%}
%token DIGIT
%%
S       : E '\n' {printf("%d\n",$1);return 1;}
        ;
E       : E '+' T { $$ = $1 + $3 ;}
        | T
        ;
T       :T '-' FF { $$ = $1 - $3 ;}
        | FF
        ;
FF      :FF '/' FFF { $$ = $1 / $3 ;}
        |FFF;
FFF     :FFF '*' F { $$ =$1*$3 ;}
        |F
        ;
F       : '(' E ')' { $$ = $2 ;}
        |DIGIT
        ;
%%
yylex()
{
int c;
c=getchar();
if(isdigit(c))
{
        yylval=c-'0';
        return DIGIT;
}
return c;
}

main()
{
printf("Enter the exp:");
yyparse();
}

Output:
nn@linuxmint ~ $ lex io.l
nn@linuxmint ~ $ yacc expr.y
nn@linuxmint ~ $ gcc y.tab.c -ll -ly
nn@linuxmint ~ $ ./a.out
Enter the exp:5*(5+1)-9/2
26
nn@linuxmint ~ $

Parser for IF-THEN Statements - YACC - LEX - Compiler Design

Program:

(if.l)

ALPHA [A-Za-z]
DIGIT [0-9]
%%
[ \t\n]
if                return IF;
then                return THEN;
{DIGIT}+            return NUM;
{ALPHA}({ALPHA}|{DIGIT})*         return ID;
"<="                return LE;
">="                return GE;
"=="                return EQ;
"!="                return NE;
"||"                return OR;
"&&"                return AND;
.                return yytext[0];
%%          

(if.y)

%{
#include <stdio.h>
#include <stdlib.h>
%}
%token ID NUM IF THEN LE GE EQ NE OR AND
%right '='
%left AND OR
%left '<' '>' LE GE EQ NE
%left '+''-'
%left '*''/'
%right UMINUS
%left '!'
%%
S   : ST {printf("Input accepted.\n");exit(0);};
ST  :    IF '(' E2 ')' THEN ST1';'
    ;
ST1 : ST
    | E
    ;      
E   : ID'='E
    | E'+'E
    | E'-'E
    | E'*'E
    | E'/'E
    | E'<'E
    | E'>'E
    | E LE E
    | E GE E
    | E EQ E
    | E NE E
    | E OR E
    | E AND E
    | ID
    | NUM
    ;
E2  : E'<'E
    | E'>'E
    | E LE E
    | E GE E
    | E EQ E
    | E NE E
    | E OR E
    | E AND E
    | ID
    | NUM
    ;
%%

#include "lex.yy.c"

main()
{
printf("Enter the statement: ");
yyparse();
}  
Output:
nn@linuxmint ~ $ lex if.l
nn@linuxmint ~ $ yacc if.y
nn@linuxmint ~ $ gcc y.tab.c -ll -ly
nn@linuxmint ~ $ ./a.out
Enter the statement: if(i>) then i=1;
syntax error
nn@linuxmint ~ $ ./a.out
Enter the statement: if(i>8) then i=1;
Input accepted.
nn@linuxmint ~ $

Postfix Evaluaion YACC LEX S6

YACC Program:(p1.y)

%{
#include<stdio.h>
#include<assert.h>
 int Pop();
 int Top();
 void Push(int val);
%}

%token T_Int

%%
S : S E    {printf(" %d\n", Top());}
  |
  ;

E : E E '+' {Push(Pop()+Pop());}
  | E E '-' {int op2=Pop();Push(Pop()-op2);}
  | E E '*' {Push(Pop() * Pop());}
  | E E '/' {int op2=Pop();Push(Pop()/op2);}
  | T_Int   {Push(yylval);}
  ;
%%
#include "lex.yy.c"
 int stack[100],count=0;

 int Pop()
 {
  assert(count>0);
  return stack[--count];
 }

 int Top()
 {
  assert(count>0);
  return stack[count-1];
 }

 void Push(int val)
 {
  assert(count<sizeof(stack)/sizeof(int));
  stack[count++]=val;
 }

int main()
 {
  return yyparse();
 }

Lex progam:(p1.l)

DIGIT    [0-9]
%%
{DIGIT}+ {yylval=atoi(yytext);return T_Int;}
[-+*/] {return yytext[0];}
\n    yyterminate();
.    ;

Output:

students@ccflab-desktop:~$ lex p1.l
students@ccflab-desktop:~$ yacc p1.y
conflicts: 1 shift/reduce
students@ccflab-desktop:~$ gcc y.tab.c -ll -ly
students@ccflab-desktop:~$ ./a.out
2 3 +
5
students@ccflab-desktop:~$ ./a.out
2 3 *
6
students@ccflab-desktop:~$ ./a.out
3 2 -
1
students@ccflab-desktop:~$ ./a.out
6 3 /
2
students@ccflab-desktop:~$


Related Posts Plugin for WordPress, Blogger...