Assignment-2 2
Assignment-2 2
Assignment-2
1 Language Specication
Consider a programming language, basic PASCAL, which supports integer constants, keywords,
variables, expressions, assignments, conditional statements, loops, arrays, single line comments
(using //) and read and write statements:
1. Keywords: program, integer, real, boolean, char, var, to, downto, if, else, while, for,
do, array, and, or, not, begin, end, read, and write. The keywords are not case-sensitive.
(Include any keywords used in the below, but missing from the list.)
All variables must be declared before we use them in the program. All variable declarations
are followed by the var keyword. A declaration species a list of variables, followed by a
colon (:) and the type. Syntax of variable declaration is
var
variable_list : type ;
Here, variable_list is a list of variables separated by a comma (,) and type from the list
{char, integer, real, boolean}.
Example:
var
age , weekdays : integer ;
taxrate , net_income : real ;
choice , isready : boolean ;
initials , grade : char ;
Note that at the variable declaration, assigning a value to one or more variables is not
allowed.
3. Operators: An operator is a symbol that tells the compiler to perform specic mathe-
matical or logical manipulations. We allow the following types of operators:
Arithmetic operators:
+ (addition) ,
− (subtraction),
∗ (multiplication),
/ (division, return real value),
% (reminder, returns integer type)
Relational operators:
Boolean operators:
and : boolean AND operator, if both the operands are true, then condition becomes
true.
or : boolean OR Operator. If any of the two operands is true, then condition be-
comes true.
not: boolean NOT Operator. Used to reverse the logical state of its operand. If a
condition is true, then Logical NOT operator will make it false.
1
4. Statements: In the program, a statement can be of any of the following:
Note that in write(“text”), text is just a sequence of characters, it will not have
any meaning. Hence, do not tokenize the text.
example:
write("Welcome to CS F363");
write(day, age, cost); //day, age, cost are variables and the type of these variables
need not be the same.
read: takes the value from the user as an input and the syntax is given below:
variable_name := expression ;
(c) Block of statements: A set of one or more statements is consider as a block and
begin and ends with end.
each block starts with
begin
statement_1 ;
....
......
statement_k ;
end
i := 10;
if i > 10 then
begin
i :=10;
i := i - 1;
write ( i);
end ;
i := 10;
if i > 10 then
begin
i :=10;
i := i - 1;
write ( i);
end
else
begin
i :=20;
write ( i);
end ;
page 2 of 6
(e) Looping statements: We allow while-do and for-do loops as in PASCAL. For the
sake of simplicity, we do not consider nested loops.
b :=20;
for a := 10 to b +10 do
begin
write (" value of a: ");
write (a);
end ;
b :=20;
a :=0;
// example to illustrate downto
for a := a +5 downto b -15 do
begin
write (" value of a: ");
write (a);
end ;
5. Program structure: the program starts with keyword program followed by the
name of the program and terminates with ; (semicolon). Next is the variable declaration
section, then followed by main program block. The main program block starts with
keyword begin and ends with keyword end followed by a period (.).
program name_of_the_program ;
var
variabl_list : type ; // declaration of varaibles
page 3 of 6
Example:
program AddTwoNumbers ;
var
num1 , num2 , sum : Integer ;
begin
Write (" Enter the first number : ");
read ( num1 );
// Perform addition
sum := num1 + num2 ;
where array _name is an identier (variable), array and of are keywords. Further, c1
and c2 are integer constants such that c1 ≤ c2, and type ∈ {integer, char, real, boolean}.
See an example in the below:
program ArraySum ;
var
numbers : array [1..10] of Integer ;
i , sum : Integer ;
begin
// Read 10 values into the array
writeln ( " Enter 10 integer values : ");
for i := 1 to 10 do
begin
read ( numbers [ i ]) ;
end ;
end .
page 4 of 6
2 Tasks
1. Lexical Analysis [6 marks] - For this phase, write a LEX program that brakes the
input program into tokens (identiers, operators, numbers, keywords, punctuators, etc.).
2. Syntax Analysis [6 marks] - For this phase, write a YACC program that takes tokens
generated after lexical analysis and checks the given input program is syntactically correct
or not. A symbol table is created with the list of tokens obtained from the input. No
need to print symbol take at this phase.
3. Semantic Analysis [8 marks] - Extend your YACC program written in the last phase
to examine semantic errors and the abstract syntax tree is printed.
Semantic errors like type checking, undeclared variables, multiple declarations of variables,
and using the variable before a value is set to it are veried.
4. Code Generation [8 marks] - Extend your YACC program in the last phase to generate
the intermediate code in three address code format.
5. Final stage: [8 marks] Finally, extend your YACC program to print the output of the
given input program. Also, print the symbol table.
3 Instructions
1. You work on the assignment with your groups members and strongly discouraged to
discuss with other group members.
2. You can refer internet/web resources only to understand the syntax of PASCAL language.
3. Taking code from internet/web is strictly prohibited. If you do so, it will lead to severe
penalty.
(a) Part-1: Submit the tasks 1, and 2 (Dead line: 20 April 2024 11:59 PM)
(b) Part-2: Submit the tasks 3, 4 and 5 (Dead line: 30 April 2024 11:59 PM)
5. Late submissions will be allowed up to maximum 24 hrs after the deadline with penalty
of 2% per each hour.
page 5 of 6
4 Submission guidelines
1. Only one submission per group.
2. Place the LEX and YACC programs (name the programs with your group submission
code) for each task in a separate folder and name the folder with the task name.
3. Create a readme.txt le that contains the information of the group members and mention
how to compile the programs for each task.
4. Finally, place all the folders and readme.txt inside a new folder folder (name it with your
group submission code) and zip the folder and submit it.
2. You print the output on the terminal and the format for each task is given below.
3. Lexical Analysis: print the list of valid tokens in the given input program in the following
format
4. Syntax Analysis: if the input program has any syntax error print syntax error message,
otherwise print valid input. (No need to print the type of the syntax error.)
5. Semantic Analysis: Print the abstract syntax tree (if the input program has no syn-
tax errors). For the structure and further details refer the web link https://www.
epaperpress.com/lexandyacc/calcg.html
If a variable is used before declaration, print an error message undeclared variable along
with the name of the variable.
If a variable is declared more than once, print an error message multiple declarations of
a variable along with the name of the variable.
Further, print if there are any type mismatches in the given input program like, a real
value is assigned to an integer variable (similar cases), a character type is added to integer
type, etc.
6. Code Generation: print the three-address code generated in the form of Quadruple
(see lab sheet 10 for the exact structure (will upload in the coming weeks).)
7. Final stage: print the output of the given input and print the symbol table in the
following format:
page 6 of 6