Assignment-1 Nadeem Sarwar

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Bahria University Lahore Campus

Department of Computer Science


BS(CS) Program, Semester 05 Spring 2022
Course: Compiler Construction (CSC-323)
INSTRUCTOR: Nadeem Sarwar
Student name: ID: Section: A

Date: 14/03/2022 Assignment # 01 Marks: 5

Name: Abdullah Arshad

Enrollment:03-134201-030
Q1. Identify the <token ,lexeme> pairs

1. For ( int x= 0; x<=5; x++)

Tokens Lexeme

For keyword

( symbols

int keyword

x identifier

= operators

0 constants

; symbols

x identifier

< operators

= operators

5 constants

; symbols

x identifier

+ operators
+ operators

) symbols
2. B= (( c + a) * d ) / f

Tokens Lexeme
B Identifier
= operators
( symbol
( symbol
c identifier
+ operators
a identifier
) symbol
* operators

d Identifier
) symbol
/ symbol
f identifier
3. While ( a < 5 )
a= a+1

Tokens Lexeme
While keyword
( symbol
a identifier
< operators
5 constant
) symbol

a identifier
= operators
a identifier
+ operator
1 constant

4. Char MyCourse[5];

Tokens Lexeme
Char keyword
MyCourse identifier
[ symbol
5 constants
] symbol
; symbol
5. if ( a< b)
a=a*a;
else
b=b*b;

Tokens Lexeme
if keyword
( symbol
a identifier
< operators
b identifier
) symbol
a identifier
= operators
a identifier
* operators
a identifier
; symbol
else keyword
b identifier
= operators
b identifier
* operators
b identifier
; symbol
Q2. Write a program in C++ or Java that reads a source file and performs the followings operations:

#include <iostream>
#include<string>
using namespace std;
void removeblanks(char* str)
{
int count = 0;
for (int i = 0; str[i]; i++)
if (str[i]!= ' ')
str[count++] = str[i];
str[count] = '\0';
}
int main()
{
char str[] = "My name is Abdullah";
removeblanks(str);
cout << str;
return 0;
}

1. Removal of white space

2. Removal of comments
3. Recognizes constants
4. Recognizes Keywords
5. Recognizes identifiers
6. Recognize operators
7. Recognize numbers
Deadline:
Sunday, 20-March-2022 before 11:45 PM (Via LMS)

You might also like