Skip to content

Commit 213f065

Browse files
committed
Added basic code for DAG and python modifications
1 parent ef106f3 commit 213f065

File tree

8 files changed

+665
-65
lines changed

8 files changed

+665
-65
lines changed

Lab12-AST/3address(old).l

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
%{
2+
#include "3address(old).tab.h"
3+
extern char yyval;
4+
%}
5+
6+
%%
7+
8+
[0-9]+ {yylval.symbol=(char)(yytext[0]);return NUMBER;}
9+
[a-z] {yylval.symbol= (char)(yytext[0]);return LETTER;}
10+
. {return yytext[0];}
11+
\n {return 0;}
12+
13+
%%
14+
15+
int yywrap()
16+
{
17+
return 1;
18+
}

Lab12-AST/3address(old).y

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
%{
2+
#include<stdio.h>
3+
char addtotable(char,char,char);
4+
void yyerror(char *s);
5+
int index1=0;
6+
char temp = 'A'-1;
7+
struct expr{
8+
char operand1;
9+
char operand2;
10+
char operator;
11+
char result;
12+
};
13+
%}
14+
15+
%union{
16+
char symbol;
17+
}
18+
19+
%left '+' '-'
20+
%left '/' '*'
21+
22+
%token <symbol> LETTER NUMBER
23+
%type <symbol> exp
24+
%%
25+
26+
statement: LETTER '=' exp ';' {addtotable((char)$1,(char)$3,'=');};
27+
exp: exp '+' exp {$$ = addtotable((char)$1,(char)$3,'+');}
28+
|exp '-' exp {$$ = addtotable((char)$1,(char)$3,'-');}
29+
|exp '/' exp {$$ = addtotable((char)$1,(char)$3,'/');}
30+
|exp '*' exp {$$ = addtotable((char)$1,(char)$3,'*');}
31+
|'(' exp ')' {$$= (char)$2;}
32+
|NUMBER {$$ = (char)$1;}
33+
|LETTER {(char)$1;};
34+
35+
%%
36+
37+
struct expr arr[20];
38+
39+
void yyerror(char *s){
40+
//printf("Error %s",s);
41+
}
42+
43+
char addtotable(char a, char b, char o){
44+
temp++;
45+
arr[index1].operand1 =a;
46+
arr[index1].operand2 = b;
47+
arr[index1].operator = o;
48+
arr[index1].result=temp;
49+
index1++;
50+
return temp;
51+
}
52+
53+
void threeAddressCode(){
54+
int i=0;
55+
char temp='A';
56+
while(i<index1){
57+
printf("%c := ",arr[i].result);
58+
printf("%c ",arr[i].operand1);
59+
printf("%c ",arr[i].operator);
60+
printf("%c ",arr[i].operand2);
61+
i++;
62+
temp++;
63+
printf("\n");
64+
}
65+
}
66+
67+
int find(char l){
68+
int i;
69+
for(i=0;i<index1;i++)
70+
if(arr[i].result==l) break;
71+
return i;
72+
}
73+
74+
75+
int main(){
76+
printf("Enter the expression: ");
77+
yyparse();
78+
threeAddressCode();
79+
return 0;
80+
}

Lab12-AST/3address.l

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,36 @@
11
%{
2-
#include "3address.tab.h"
3-
extern char yyval;
2+
#include <stdio.h>
3+
#include <string.h>
4+
char symtab[100][100];
5+
char* pop();
6+
void push(char *s);
7+
int top=-1;
48
%}
5-
69
%%
710

8-
[0-9]+ {yylval.symbol=(char)(yytext[0]);return NUMBER;}
9-
[a-z] {yylval.symbol= (char)(yytext[0]);return LETTER;}
11+
[0-9]+ {
12+
node *newnode=(node *)malloc(sizeof(node));
13+
strcpy(newnode->addr,yytext);
14+
int i;
15+
for(i=0;i<100;i++)
16+
newnode->code[i]='\0';
17+
//printf("Address and code are: %s,%s\n",newnode->addr,newnode->code);
18+
yylval=newnode;
19+
push(yytext);
20+
return NUMBER;
21+
}
22+
[a-z]+ {
23+
node *newnode=(node *)malloc(sizeof(node));
24+
strcpy(newnode->addr,yytext);
25+
int i;
26+
for(i=0;i<100;i++)
27+
newnode->code[i]='\0';
28+
//printf("Address and code are: %s,%s\n",newnode->addr,newnode->code);
29+
yylval=newnode;
30+
push(yytext);
31+
return ID;
32+
}
33+
1034
. {return yytext[0];}
1135
\n {return 0;}
1236

@@ -15,4 +39,29 @@ extern char yyval;
1539
int yywrap()
1640
{
1741
return 1;
42+
}
43+
/*
44+
//---RELOP---
45+
[<>]|<=|>= {
46+
node *newnode=(node *)malloc(sizeof(node));
47+
strcpy(newnode->addr,yytext);
48+
int i;
49+
for(i=0;i<100;i++)
50+
newnode->code[i]='\0';
51+
//printf("Address and code are: %s,%s\n",newnode->addr,newnode->code);
52+
yylval=newnode;
53+
push(yytext);
54+
return RELOP;
55+
}
56+
*/
57+
58+
void push(char *s)
59+
{
60+
//printf("Pushed %s\n",s);
61+
strcpy(symtab[++top],s);
62+
}
63+
char *pop()
64+
{
65+
//printf("Popped %s\n",symtab[top]);
66+
return symtab[top--];
1867
}

0 commit comments

Comments
 (0)