0% found this document useful (0 votes)
23 views8 pages

CD Lab Exp 9 & 10

Uploaded by

gouravtiwary598
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views8 pages

CD Lab Exp 9 & 10

Uploaded by

gouravtiwary598
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

EXPERIMENT 9

AIM:- TO DETERMINE THE FIRST AND FOLLOW FOR THE GIVEN GRAMMER
CODE:-
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
int n,m=0,p,i=0,j=0;
char a[10][10],f[10];
void follow(char c);
void first(char c);
int main(){

int i,z;
char c,ch;
//clrscr();
printf("Enter the no of productions:\n");
scanf("%d",&n);
printf("Enter the productions:\n");
for(i=0;i<n;i++)
scanf("%s%c",a[i],&ch);
do{
m=0;
printf("Enter the elements whose first & follow is to be
found:");
scanf("%c",&c);
first(c);
printf("First(%c)={",c);
for(i=0;i<m;i++)
printf("%c",f[i]);
printf("}\n");
strcpy(f," ");
//flushall();
m=0;
follow(c);
printf("Follow(%c)={",c);
for(i=0;i<m;i++)
printf("%c",f[i]);
printf("}\n");
printf("Continue(0/1)?");
scanf("%d%c",&z,&ch);
}while(z==1);
return(0);
}
void first(char c)
{
int k;
if(!isupper(c))
f[m++]=c;
for(k=0;k<n;k++)
{
if(a[k][0]==c)
{
if(a[k][2]=='$')
follow(a[k][0]);
else if(islower(a[k][2]))
f[m++]=a[k][2];
else first(a[k][2]);
}
}
}
void follow(char c)
{
if(a[0][0]==c)
f[m++]='$';
for(i=0;i<n;i++)
{
for(j=2;j<strlen(a[i]);j++)
{
if(a[i][j]==c)
{
if(a[i][j+1]!='\0')
first(a[i][j+1]);
if(a[i][j+1]=='\0' && c!=a[i][0])
follow(a[i][0]);
}
}
}
}
EXPERIMENT 10
AIM:- TO FORMATION OF ABSTRACT SYSTAX TREE
CODE:-
#include<stdio.h>

#include <stdlib.h>

typedef struct AST AST; // Forward reference

struct AST {

enum {

AST_MAIN,

AST_NUMBER,

AST_ADD,

AST_MUL,

} tag;

union {

struct AST_MAIN { AST *body; } AST_MAIN;

struct AST_NUMBER { int number; } AST_NUMBER;

struct AST_ADD { AST *left; AST *right; } AST_ADD;

struct AST_MUL { AST *left; AST *right; } AST_MUL;

} data;

};

AST *ast_new(AST ast) {

AST *ptr = malloc(sizeof(AST));

if (ptr) *ptr = ast;

return ptr;

void ast_free(AST *ptr) {

AST ast = *ptr;


switch (ast.tag) {

case AST_MAIN: {

struct AST_MAIN data = ast.data.AST_MAIN;

ast_free(data.body);

break;

case AST_NUMBER: {

struct AST_NUMBER data = ast.data.AST_NUMBER;

break;

case AST_ADD: {

struct AST_ADD data = ast.data.AST_ADD;

ast_free(data.left);

ast_free(data.right);

break;

case AST_MUL: {

struct AST_MUL data = ast.data.AST_MUL;

ast_free(data.left);

ast_free(data.right);

break;

free(ptr);

#define AST_NEW(tag, ...) ast_new((AST){tag, {.tag=(struct tag){__VA_ARGS__}}})

void ast_print(AST *ptr) {

AST ast = *ptr;


switch (ast.tag) {

case AST_MAIN: {

struct AST_MAIN data = ast.data.AST_MAIN;

printf("main() = ");

ast_print(data.body);

return;

case AST_NUMBER: {

struct AST_NUMBER data = ast.data.AST_NUMBER;

printf("%d", data.number);

return;

case AST_ADD: {

struct AST_ADD data = ast.data.AST_ADD;

printf("(");

ast_print(data.left);

printf(" + ");

ast_print(data.right);

printf(")");

return;

case AST_MUL: {

struct AST_MUL data = ast.data.AST_MUL;

printf("(");

ast_print(data.left);

printf(" * ");

ast_print(data.right);

printf(")");

return;

}
}

#define emitf printf

void ast_emit(AST *ptr) {

AST ast = *ptr;

switch (ast.tag) {

case AST_MAIN: {

struct AST_MAIN data = ast.data.AST_MAIN;

emitf(".global _main\n");

emitf("_main:\n");

ast_emit(data.body);

emitf(" ret\n");

emitf("\n");

return;

case AST_NUMBER: {

struct AST_NUMBER data = ast.data.AST_NUMBER;

emitf(" mov rax, %d\n", data.number);

return;

case AST_ADD: {

struct AST_ADD data = ast.data.AST_ADD;

ast_emit(data.left);

emitf(" push rax\n");

ast_emit(data.right);

emitf(" pop rbx\n");

emitf(" add rax, rbx\n");

return;
}

case AST_MUL: {

struct AST_MUL data = ast.data.AST_MUL;

ast_emit(data.left);

emitf(" push rax\n");

ast_emit(data.right);

emitf(" pop rbx\n");

emitf(" mul rbx\n");

return;

int main() {

// main() = 4 + 2 * 10 + 3 * (5 + 1)

AST *term = AST_NEW(AST_MAIN,

AST_NEW(AST_ADD,

AST_NEW(AST_NUMBER, 4),

AST_NEW(AST_ADD,

AST_NEW(AST_MUL,

AST_NEW(AST_NUMBER, 2),

AST_NEW(AST_NUMBER, 10),

),

AST_NEW(AST_MUL,

AST_NEW(AST_NUMBER, 3),

AST_NEW(AST_ADD,

AST_NEW(AST_NUMBER, 5),

AST_NEW(AST_NUMBER, 1),

),

),
),

),

);

printf("/* "); ast_print(term); printf(" */\n");

ast_emit(term);

ast_free(term);

You might also like