M.S.Ramaiah Institute of Technology Department of Computer Science and Engineering Compilers & Systems Programming Laboratory-CSL618 Part A Question Bank 1. Write a C / C++ program to accept a C program and do error detection & correction for the following. a) Check for un-terminated string constant in the input C program. i.e A string constant begins with double quotes and extends for more than one line. Intimate the error line numbers and the corrective actions to user. 2. Write a C / C++ program to accept a C program and do error detection & correction for the following. a) Check for un- terminated multi line comment statement in your C program. 3. Write a Lex program to accept a C program and do error detection & correction for the following. a) Check for un-terminated string constant in the input C program. i.e A string constant begins with double quotes and extends for more than one line. Intimate the error line numbers and the corrective actions to user. b) Check for valid arithmetic expressions in the input C program. Report the errors in the statements to user. 4. Write a Lex program to accept a C program and do the following error detection & correction. a) Check for the valid usages of numerical constants in the input C program. Intimate the invalid usages to user. b) Check for valid declarative statements in your program. Intimate the invalid statements along with their line numbers to users. 5. Write a Lex program to accept a C program and do the following error detection & correction. a) Check for the valid if statement in the input C program. Report the errors to users. b) Check for un- terminated multi line comment statement in your C program. 6. Write Yacc program to accept a statement and do the following error detection. a) Check for valid arithmetic expressions in the input C statement. Report the errors in the statements to user. Evaluate the arithmetic expression.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
M.S.Ramaiah Institute of TechnologyDepartment of Computer Science and Engineering
Compilers & Systems Programming Laboratory-CSL618Part A
Question Bank1. Write a C / C++ program to accept a C program and do error detection & correction for the
following.
a) Check for un-terminated string constant in the input C program. i.e A string constant begins with double quotes and extends for more than one line. Intimate the error line numbers and the corrective actions to user.
2. Write a C / C++ program to accept a C program and do error detection & correction for the following.
a) Check for un- terminated multi line comment statement in your C program.
3. Write a Lex program to accept a C program and do error detection & correction for the following.
a) Check for un-terminated string constant in the input C program. i.e A string constant begins with double quotes and extends for more than one line. Intimate the error line numbers and the corrective actions to user.
b) Check for valid arithmetic expressions in the input C program. Report the errors in the statements to user.
4. Write a Lex program to accept a C program and do the following error detection & correction.
a) Check for the valid usages of numerical constants in the input C program. Intimate the invalid usages to user.
b) Check for valid declarative statements in your program. Intimate the invalid statements along with their line numbers to users.
5. Write a Lex program to accept a C program and do the following error detection & correction.
a) Check for the valid if statement in the input C program. Report the errors to users.
b) Check for un- terminated multi line comment statement in your C program.
6. Write Yacc program to accept a statement and do the following error detection.
a) Check for valid arithmetic expressions in the input C statement. Report the errors in the statements to user. Evaluate the arithmetic expression.
7. Write Yacc program to accept a statement and do the following error detection.
a)Check for valid declarative statement. Intimate the errors to users.
8. Write Yacc program to accept a statement and do the following error detection.
a) Check for the valid relational expression and evaluate the expression
9. Write Yacc program to accept a statement and do the following error detection.
a) Check for the valid logical expression and evaluate the expression
10.Write Yacc programs for the following grammar. Form valid and invalid input strings
manually and give them as input for your executable code of yacc program and validate the
input strings.
a) S->SS+ | SS*|a
b)S->L=R | R
L-> *R | id R->L
11.Write Yacc programs for the following grammar. Form valid and invalid input strings manually and give them as input for your executable code of yacc program and validate the input strings.
a)D->TL
T->int | floatL->L,id | id
b)S-> L.L | L
L->LB | BB->0 | 1
12. Write a Yacc program to accept arithmetic expression. Accept the data types of variables from the user as input. Check the semantic validity of the expression entered by the user. Note: For addition and subtraction operators : integers, float and character operands are allowed but string is not allowed. For multiplication and division operators: integer and float operands are allowed but character and string operands cant be allowed.
13. Write a Yacc program to accept relational expression. Accept the data types of variables from the user as input. Check the semantic validity of the expression entered by the user. Note: For relational operators : integers, float and character operands are allowed but string is not allowed.
14. Write a Yacc program to accept function call statement for the following function: strcpy(str1,str2),exit(0 or 1) and sqrt(num). Accept the data types of arguments passed to the function as input. Check the semantic validity of the statement entered by the user. Note: For sqrt() : the arguments should be of the data types int or float. For strcpy(): the arguments should be of the data types strings. For exit(): the arguments should be of the data type int. Apart from the mentioned data types of arguments , if any other different data types arguments are passed it will be invalid.
Sample codes:
1.
#include<stdio.h>
#include<string.h>
int main()
{
FILE *fp;
int strcheck=0;
int i;
int lineno=0;
int string=0;
char line[100];
int open,close;
clrscr();
fp=fopen("file.txt", "r");
if(fp==NULL)
{
printf("File cant be opened\n");
exit(0);
}
printf("File opened correctly!\n");
while(fgets(line, sizeof(line), fp)!=NULL)
{
lineno++;
strcheck=0;
string=0;
open=close=0;
for(i=0;i<strlen(line);i++)
{
if(line[i]=='"')
{
string=1;
if(open==1&&close==0) close=1;
else if(open==0&&close==0) open=1;
else if(open==1&&close==1) close=0;
}
}
if(open==1 &&close==0)
{
printf("\n Unterminated string in line %d. String Has to be closed", lineno);
strcheck=1;
}
else if(string==1 && strcheck==0){
printf("\n String usage in line %d is validated!",lineno);
}
}
return 0;
}
file.txt
#include<stdio.h>
#include<conio.h>
int s[35]="gh";
void main(){
int a;
char c[10]="msrit",f[]="lk;
strlen("hjkl);
a=a+/*b;
}
2.
#include<stdio.h>
#include<string.h>
int main()
{
FILE *fp;
int commentcheck=0;
int i;
int lineno=0;
int comment=0;
char line[100];
int open=0,close=0,openlineno,closelineno;
clrscr();
fp=fopen("file2.txt", "r");
if(fp==NULL)
{
printf("File cant be opened\n");
exit(0);
}
printf("File opened correctly!\n");
while(fgets(line, sizeof(line), fp)!=NULL)
{
lineno++;
getch();
commentcheck=0;
comment=0;
if(open==1&&close==0)
printf("\n%s",line);
if(strstr(line,"/*")&&open==0)
{
open=1;close=0;
comment=1;
openlineno=lineno;
printf("\n%s",line);
}
if(strstr(line,"*/")&&close==0&&open==1)
{
closelineno=lineno;
if(open==1&&close==0)
{
close=1;
open=0;
printf("\n Comment is displayed above!\nComment opened in line no %d and closed in line no %d",openlineno,closelineno);
}
}
}
if(open==1 &&close==0)
{
printf("\n Unterminated comment in begin in line no %d. It Has to be closed", openlineno);
commentcheck=1;
}
else if(comment==1 && commentcheck==0){
printf("\n Comment usage in line %d is validated!",lineno);
%{#include<stdio.h>int c=0;FILE *fp;%}%%\n { c++; }["][a-zA-Z0-9]*["] {ECHO; printf(" Valid String in line number %d\n ",c+1);}["][a-zA-Z0-9]* { ECHO; printf(" InValid String in line number %d\n ",c+1);}. ;%%main(){yyin=fopen("source.txt","r");yylex();fclose(yyin);}
%{ #include<stdio.h> int c=0; FILE *fp; %} operator [-+*/] identifier [a-zA-Z][a-zA-Z0-9-]* number [0-9]+ expression ({identifier}|{number}){operator}({identifier}|{number}) %% \n { c++; } ^"#".+ ; ^("int "|"float "|"char ").+ ; "void main()" ; {identifier}"="({expression}+";") { printf("Valid expression in line no : %d\t",c+1);ECHO;printf("\n");} {identifier}"="({number}|{identifier}";") { printf("Valid expression in line no : %d\t",c+1);ECHO;printf("\n");} ({number}|([0-9]*[a-zA-Z0-9-]+))"="{expression}+ { printf("InValid expression in line no : %d; L-value should satisfy the identifier rules\n",c+1);ECHO;printf("\n");} {identifier}"=;" { printf("InValid expression in line no : %d; R-value required; Expression is needed at right hand side of assignment operation\n",c+1);ECHO;printf("\n");} {operator}{operator}+ {printf(" Invalid expression in line no: %d;More than one operator can't be used in expression consequetively",c+1);ECHO;printf("\n");} .|\n ; %% main() { yyin=fopen("source.txt","r"); yylex(); fclose(yyin);
<DECLARE>{identifier}("="{string}) {BEGIN VAR; printf("\n Invalid variable declaration in line no %d; string can't be assigned to integer or float variable:",c+1);ECHO;printf("\n");}
<VAR>";" {BEGIN 0;}
<VAR>{identifier}("="{number})? {}
<VAR>{identifier}("="{string}) {printf("\n Invalid variable declaration in line no %d; string can't be assigned to integer or float variable:",c+1);ECHO;printf("\n");}
<VAR>\n {BEGIN 0; c++;}
<VAR>"," {BEGIN DECLARE;}
<VAR>[,][,]+ {printf("\n Invalid usage of more than one comma in declaration in line no %d",c+1);BEGIN DECLARE;ECHO;printf("\n");}
case 3: {printf("\nSemantic error: string cant be added/subtracted to a number");exit(0);}
}break;
}
case 1:{switch($3)
{case 0: { $$=1;break;}
case 1: {$$=1;break;}
case 2: {$$=1;break;}
case 3: {printf("\nSemantic error: string cant be added/subtracted to a number");exit(0);}
}break;
}
case 2:{switch($3)
{case 0: { $$=0;break;}
case 1: {$$=1;break;}
case 2: {$$=0;break;}
case 3: {printf("\nSemantic error: string cant be added/subtracted to a number");exit(0);}
}break;
}
case 3:{printf("\nSemantic error: string cant be the operand for addition/subtraction operator");exit(0);}
}
printf("\ndata type: %d where 0-int,1-float,2-char,3-string",$$);}
| expression plusminus { printf ("\n Syntax error: Right operand is missing ");exit(0);}
| expression muldiv expression {printf("\nMul or division recog!");printf("$1=%d $3=%d",$1,$3);
switch($1)
{
case 0:{switch($3)
{case 0: { $$=0;break;}
case 1: {$$=1;break;}
case 2: {printf("\nSemantic error: character cant be the operand for product/divide operator");exit(0);}
case 3: {printf("\nSemantic error: string cant be the operand for product/divide operator");exit(0);}
}break;
}
case 1:{switch($3)
{case 0: { $$=1;break;}
case 1: {$$=1;break;}
case 2: {printf("\nSemantic error: character cant be the operand for product/divide operator");exit(0);}
case 3: {printf("\nSemantic error: string cant be the operand for product/divide operator");exit(0);}
}break;
}
case 2:{printf("\nSemantic error: character cant be the operand for product/divide operator");exit(0);}
case 3:{printf("\nSemantic error: string cant be the operand for product/divide operator");exit(0);}
}
printf("\ndata type: %d where 0-int,1-float,2-char,3-string",$$);}
| expression muldiv { printf ("\n Syntax error: Right operand is missing ");exit(0);}
| id
| num
;
%%
main()
{
printf(" Enter an arithmetic expression\n");
yyparse();
}
yyerror()
{
printf(" Invalid arithmetic Expression\n");
exit(1);
}
LEX:
%{
#include "y.tab.h"
#include<stdio.h>
#include<ctype.h>
extern int yylval;
int val;
%}
%%
[a-zA-Z][a-zA-Z0-9]* {printf("\n enter the data type of variable %s:\n 0-int\n1-float\n2-char\n3-string",yytext);scanf("%d",&val);yylval=val;return id;}
[0-9]+ {yylval=0;return num;}
[0-9]+[.]?[0-9]* {yylval=1;return num;}
[ \t] ;
\n {return 0;}
[+-] {return plusminus;}
[*/] {return muldiv;}
. {return yytext[0];}
%%
int yywrap()
{
return 1;
}
Logic used :
• In lex program the data type will be accepted and passed through yylval for that operand to the yacc parser.
• In yacc program for plus and minus operators if integers are passed then the final resultant data type is integer
* In yacc program for plus and minus operators if integer and float are passed then the final resultant data type is float
* In yacc program for plus and minus operators if integer and char are passed then the final resultant data type is integer.
* In yacc program for plus and minus operators if integer and string are passed then it is invalid
• In yacc program for division and multiplication operators if integer and integer are passed then the final resultant data type is integer.
• In yacc program for division and multiplication operators if integer and float are passed then the final resultant data type is float.
• In yacc program for division and multiplication operators if integer and char are passed then it is invalid.
• In yacc program for division and multiplication operators if integer and string are passed then it is invalid
• Similarly all the combinations are checked for their semantic validity
13.
LEX:
%{
#include "y.tab.h"
#include<stdio.h>
#include<ctype.h>
extern int yylval;
int val;
%}
%%
[a-zA-Z][a-zA-Z0-9]* {printf("\n enter the data type of variable %s:\n 0-int\n1-float\n2-char\n3-string",yytext);scanf("%d",&val);yylval=val;return id;}
[0-9]+ {yylval=0;return num;}
[0-9]+[.]?[0-9]* {yylval=1;return num;}
[ \t] ;
\n {return 0;}
[<>] {return relop;}
"<=" {return relop;}
">=" {return relop;}
"==" {return relop;}
"!=" {return relop;}
. {return yytext[0];}
%%
int yywrap()
{
return 1;
}
YACC:
%{
#include<stdio.h>
int flag=1;
%}
%token id num relop
%left '(' ')'
%nonassoc UMINUS
%%
stmt: expression { printf("\n valid exprn");}
;
expression : '(' expression ')'
| '(' expression {printf("\n Syntax error: Missing right paranthesis");}
{case 0: { printf("\ninteger is compared with an integer:valid");break;}
case 1: {printf("\n integer is compared with an float::valid");break;}
case 2: {printf("\n integer is compared with a character:valid");break;}
case 3: {printf("\nInteger compared with string operand:valid");
printf("\nSemantic error: string cant be compared using relational operator");exit(0);}
}break;
}
case 1:{switch($3)
{case 0: { printf("\n float operand is compared with an integer:valid");break;}
case 1: { printf("\n float operand is compared with a float:valid");break;}
case 2: {printf("\nSemantic error: float cant be compared against a char data");exit(0);}
case 3: {printf("\nSemantic error: string cant be compared using relational operator with float data");exit(0);}
}break;
}
case 2:{switch($3)
{case 0: {printf("\n integer is compared with a character:valid");break;}
case 1: {printf("\nSemantic error: Char cant be compared against a floating point data");exit(0);}
case 2: {printf("\n char operand is compared with a character:valid");break;}
case 3: {printf("\nSemantic error: string cant be compared using relational operator");exit(0);}
}break;
}
case 3:{printf("\nSemantic error: string cant be compared using relational operator with any data opeand");exit(0);}
}
}
| expression relop { printf ("\n Syntax error: Right operand is missing ");exit(0);}
| id
| num
;
%%
main()
{
printf(" Enter an relational expression\n");
yyparse();
}
yyerror()
{
printf(" Invalid relational Expression\n");
exit(1);
}
Logic used:
-In Lex program, the data types of the variables are accepted and passed to the yacc program using yylval attached for the identifiers.
-In yacc program the following things are checked:
Valid one are following
- two integer operands can be checked using relational operators.
- integer can be compared with a float operand
- integer can be compared with char operand
- integer cant be compared with string operand
-float can be compared with float operand
Invalid ones are following
-float cant be compared with character data
-character cant be compared with string data
-string cant be compared with any operand
14.
LEX:
%{
#include "y.tab.h"
#include<stdio.h>
#include<ctype.h>
extern int yylval;
int datatype;
%}
%s function
%%
"sqrt" {return sqrt;}
"strcpy" {return strcpy;}
"exit" {return exit1;}
"(" {printf("%c",yytext[0]); return yytext[0];}
[a-zA-Z][a-zA-Z0-9]* {ECHO;printf("\n enter the data type aof variable %s:\n 0-int\n1-float\n2-char\n3-string",yytext);scanf("%d",&datatype);yylval=datatype;return id;}
[0-9]+ {yylval=0;return num;}
[0-9]+[.]?[0-9]* {yylval=1;return num;}
"," {printf("%c",yytext[0]);return yytext[0];}
")" {printf("%c",yytext[0]);return yytext[0];}
. ;
[ \t] ;
\n {return 0;}
. {return yytext[0];}
%%
int yywrap()
{
return 1;
}
YACC:
%{
#include<stdio.h>
int flag=1;
%}
%token id num sqrt strcpy exit1
%%
stmt : functioncall { printf("\n valid exprn");}
;
functioncall : sqrt '(' id ')' {printf("\n sqrt function is called");
| sqrt '(' id ',' id ')' { printf("\n Number of arguments for the function is more!");exit(0);}
| sqrt {printf("\nError:function call is incompleted,use function notation and pass argumetns with out fail");exit(0);}
| sqrt '(' ')' {printf("\n Error: Pass atleast one number argument for this function sqrt()");}
| strcpy '(' id ',' id ')' {printf("\n strcpy function is called");
if($3==0||$3==1||$3==2||$5==0||$5==1||$5==2){printf("\n Error:For strcpy(), integer/float/char cant be passed as arg; String has to be passed!");exit(0);}
if($3==3&&$5==3)
{printf("\n Valid :string arguments are passed");}
}
| strcpy '(' id ',' id ',' id ')' { printf("\n Number of arguments for the function is more!");exit(0);}
| strcpy {printf("\nError:function call is incompleted,use function notation and pass argumetns with out fail");exit(0);}
| strcpy '(' ')' {printf("\n Error: Pass atleast 2 string argument for this function strcpy()");exit(0);}
| exit1 '(' num ')' {printf("\n exit function is called");
if($3==0)printf("\n valid :int arg passed!");
else
{printf("\n Semantic error:float /char/ string arg passed;U need to pass int arg");exit(0);}
}
| exit1 '(' id ',' id ')' { printf("\n Number of arguments for the function is more!");exit(0);}
| exit1 {printf("\nError:function call is incompleted,use function notation and pass argumetns with out fail");exit(0);}
| exit1 '(' ')' {printf("\n Error: Pass atleast one number argument for this function exit()");}