In this question, you will implement a lex and yacc code that
will read a C code snippet that contains declarations and
comparisons. In the input code, there can be any number of if
statements and declarations. Your program should cover all the
valid inputs, and reject the invalids.
1) If statement
Contains a comparison part and body part
The comparison part can contain 1 comparison.
o There will be
no “&&” and “||” operation like we did
in prelab2.
Comparison will be only “==”.
If the body can contain other “if” statements or can
contain declarations.
Note: There will be no “else if” or “else” In the input file
2) Declaration statement
• There can be 2 types of
declarations: “int” and “string”.
o You can assume that there will be only 1 declaration per
line. ▪ int a;
▪ string b;
o In other words, the input file will not contain declarations
like:
▪ int a,b,c;
▪ string a,b;
3) Invalid comparison: The input file can contain
invalid comparisons. You should detect
and print an error message for that.
• The comparisons should be between the same types. If it is
not print an error
message: “Type mismatch in line x”
4) Output:
• For each int declaration, you should print:
o integer variable X is created in line Y
• For each string declaration, you should
print: o string variable X is created in line Y
• For each “If” comparison, you should print:
o There is a comparison in line 3
• Look at the example input and output files to understand
better.
exampleoutput1.txt
integer variable a is created in line 1
integer variable c is created in line 2
There is a comparison in line 3
There is a comparison in line 5
integer variable d is created in line 7
integer variable a is created in line 9
exampleoutput2.txt
integer variable a is created in line 1
string variable c is created in line 2
There is a comparison in line 3
Type mismatch in line 5
integer variable d is created in line 7
Type mismatch in line 12
input1.txt
int a;
int c;
if(a==c)
{
if(a==3)
{
int d;
}
int a;
}
input2.txt
int a;
string c;
if(a==2)
{
if(a==c)
{
int d;
}
}
if(c==3)
{
}
makefile
all: lex yacc
g++ lex.yy.c y.tab.c -ll -o prelab3
yacc: prelab3.y
yacc -d -v prelab3.y
lex: prelab3.l
lex prelab3.l
clean: lex.yy.c y.tab.c prelab3 y.tab.h
rm lex.yy.c y.tab.c prelab3 y.tab.h
How to test your code:
● Your code should be compiled with the Makefile given to
you.
● Do not change the Makefile.
● Run your code and redirect output to a file.
Example:
○ ./folder input.txt > output.txt
● Check whether your output and our output file are the same
or not by using the diff
command
○ diff output.txt exapleOutput.txt -wB
○ if your output and our output are the same there will be
no message on the
screen
○ Otherwise, the difference will be shown on the
screen.
Example 1 int a; string b; if (a==b) { Example 2 string b; if (b==4) { } }
In this question, you will implement a lex and yacc code that will read a C code snippet that contains declarations and
-
answerhappygod
- Site Admin
- Posts: 899604
- Joined: Mon Aug 02, 2021 8:13 am
In this question, you will implement a lex and yacc code that will read a C code snippet that contains declarations and
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!