Sharecodepoint App Download Qrcode Sharecodepoint Android App Banner
Your Ads Here Sharecodepoint Youtube Channel

Category 1

Sponsor Ads
Domyhomework123.com - programming homework help. | Get your programming homework done with expert homework helper.

Trust Writemypaper123.com with your paper and you will never regret it.
Search Result Display Below



Sharecodepoint popular queries

Saturday, 23 February 2019

Program to recognize and count the number of identifiers in a given input file. | Compiler Design Lab Programs

Compiler Design Lap Program : %{  #include<stdio.h>  int id=0; %} %% [a-zA-Z][a-zA-Z0-9_]* { id++ ; ECHO; printf(“...

Compiler Design Lap Program :

%{
 #include<stdio.h>
 int id=0;
%}
%%
[a-zA-Z][a-zA-Z0-9_]* { id++ ; ECHO; printf(“\n”);}
.+ { ;}
\n { ;}
%%
int yywrap()
{
return 1;
}
main (int argc, char *argv[])
{
 if(argc!=2)
 {
 printf(“Usage: <./a.out> <sourcefile>\n”);
 exit(0);
 }
 yyin=fopen(argv[1],”r”);
 printf(“Valid identifires are\n”);
 yylex();
 printf(“No of identifiers = %d\n”,id);

Program to test the validity of a simple expression involving operators +, -, * and / | Compiler Design Lab Programs | Yacc Programs

Yacc Part : %token NUMBER ID NL %left ‘+’ ‘-‘ %left ‘*’ ‘/’ %% stmt : exp NL { printf(“Valid Expression”); exit(0);}  ; ...

Yacc Part :

%token NUMBER ID NL
%left ‘+’ ‘-‘
%left ‘*’ ‘/’
%%
stmt : exp NL { printf(“Valid Expression”); exit(0);}
 ;
exp : exp ‘+’ exp
 | exp ‘-‘ exp
 | exp ‘*’ exp
 | exp ‘/’ exp
 | ‘(‘ exp ‘)’
 | ID
 | NUMBER
 ;
%%
int yyerror(char *msg)
{
 printf(“Invalid Expression\n”);
 exit(0);
}
main ()
{
 printf(“Enter the expression\n”);
 yyparse();

Lex Part :

%{
 #include “y.tab.h”
%}
%%
[0-9]+ { return DIGIT; }
[a-zA-Z][a-zA-Z0-9_]* { return ID; }
\n { return NL ;}
. { return yytext[0]; }
%%

Program to recognize nested IF control statements and display the levels of nesting . | Compiler Design Lab Programs | Yacc Programs

Yacc Part : %token IF RELOP S NUMBER ID %{  int count=0; %} %% stmt : if_stmt { printf(“No of nested if statements=%d\n”...

Yacc Part :

%token IF RELOP S NUMBER ID
%{
 int count=0;
%}
%%
stmt : if_stmt { printf(“No of nested if statements=%d\n”,count); exit(0);}
 ;
if_stmt : IF ‘(‘ cond ‘)’ if_stmt {count++;}
 | S;
 ;
cond : x RELOP x
 ;
x : ID
 | NUMBER
 ;
%%
int yyerror(char *msg)
{
 printf(“Invalid Expression\n”);
 exit(0);
}
main ()
{
 printf(“Enter the statement”);
 yyparse();


Lex Part :

%{
 #include “y.tab.h”
%}
%%
“if” { return IF; }
[sS][0-9]* {return S;}
“<”|”>”|”==”|”!=”|”<=”|”>=” { return RELOP; }
[0-9]+ { return NUMBER; }
[a-zA-Z][a-zA-Z0-9_]* { return ID; }
\n { ; }
. { return yytext[0]; }
%%

Program to check the syntax of a simple expression involving operators +, -, * and / . | Compiler Lab Programs | Yacc Programs

Yacc Part : %token NUMBER ID NL %left ‘+’ ‘-‘ %left ‘*’ ‘/’ %% stmt : exp NL { printf(“Valid Expression”); exit(0);}  ; ...

Yacc Part :

%token NUMBER ID NL
%left ‘+’ ‘-‘
%left ‘*’ ‘/’
%%
stmt : exp NL { printf(“Valid Expression”); exit(0);}
 ;
exp : exp ‘+’ exp
 | exp ‘-‘ exp
 | exp ‘*’ exp
 | exp ‘/’ exp
 | ‘(‘ exp ‘)’
 | ID
 | NUMBER
 ;
%%
int yyerror(char *msg)
{
 printf(“Invalid Expression\n”);
 exit(0);
}
main ()
{
 printf(“Enter the expression\n”);
 yyparse();

Lex Part :

%{
 #include “y.tab.h”
%}
%%
[0-9]+ { return NUMBER; }
[a-zA-Z][a-zA-Z0-9_]* { return ID; }
\n { return NL ;}
. { return yytext[0]; }
%% 

Program to recognize a valid variable, which starts with a letter, followed by any number of letters or digits. | Compiler Design Lab Programs | Yacc Programs

Yacc Part : %token DIGIT LETTER NL UND %% stmt : variable NL { printf(“Valid Identifiers\n”); exit(0);}  ; variable : LE...

Yacc Part :

%token DIGIT LETTER NL UND
%%
stmt : variable NL { printf(“Valid Identifiers\n”); exit(0);}
 ;

variable : LETTER alphanumeric
 ;
alphanumeric: LETTER alphanumeric
 | DIGIT alphanumeric
 | UND alphanumeric
 | LETTER
 | DIGIT
 | UND
 ;
%%
int yyerror(char *msg)
{
 printf(“Invalid Expression\n”);
 exit(0);
}
main ()
{
 printf(“Enter the variable name\n”);
 yyparse();

Lex Part :

%{
 #include “y.tab.h”
%}
%%
[a-zA-Z] { return LETTER ;}
[0-9] { return DIGIT ; }
[\n] { return NL ;}
[_] { return UND; }
. { return yytext[0]; }
%%

Program to evaluate an arithmetic expression involving operating +, -, * and /. | Compiler Design Lab Programs | Yacc Programs

Yacc Part : %token NUMBER ID NL %left ‘+’ ‘-‘ %left ‘*’ ‘/’ %% stmt : exp NL { printf(“Value = %d\n”,$1); exit(0);}  ; ...

Yacc Part :

%token NUMBER ID NL
%left ‘+’ ‘-‘
%left ‘*’ ‘/’
%%
stmt : exp NL { printf(“Value = %d\n”,$1); exit(0);}
 ;
exp : exp ‘+’ exp { $$=$1+$3; }
 | exp ‘-‘ exp { $$=$1-$3; }
 | exp ‘*’ exp { $$=$1*$3; }
 | exp ‘/’ exp { if($3==0)
 {
printf(“Cannot divide by 0”);
exit(0);
 }
 else
 $$=$1/$3;
}
 | ‘(‘ exp ‘)’ { $$=$2; }
 | ID { $$=$1; }
 | NUMBER { $$=$1; }
 ;
%%
int yyerror(char *msg)
{
 printf(“Invalid Expression\n”);
 exit(0);
}
main ()
{
 printf(“Enter the expression\n”);
 yyparse();

Lex Part :

%{
 #include “y.tab.h”
 extern int yylval;
%}
%%
[0-9]+ { yylval=atoi(yytext); return NUMBER; }
\n { return NL ;}
. { return yytext[0]; }
%%

Program to recognize strings ‘aaab’, ‘abbb’, ‘ab’ and ‘a’ using grammar (an b n , n>=0) . | Compiler Design Lab Programs | Yacc Programs

Yacc Part : %token A B NL %% stmt : s NL { printf(“Valid String\n”); exit(0) ;}  ; s : A s B  |  ; %% int yyerror(...

Yacc Part :

%token A B NL
%%
stmt : s NL { printf(“Valid String\n”); exit(0) ;}
 ;
s : A s B
 |
 ;
%%
int yyerror(char *msg)
{
 printf(“Invalid String\n”);
 exit(0);
}
main ()
{
 printf(“Enter the String\n”);
 yyparse();
}


Lex Part :

%{
 #include “y.tab.h”
%}
%%
[aA] { return A; }
[bB] { return B; }
\n { return NL ;}
. { return yytext[0]; }
%%

 

Version 23.2.3.4 - 2016 - 2019 - © Sharecodepoint | Made in India. | Copyright Content