Compiler Construction Theory Assognment - 1

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

COMSATS University Islamabad, Wah

Campus
Theory Assignment 1

COMPUTER
Department of: SCIENCE

Due
Class/Program: BS(7-A,C,D) Date: 29 Sep 2022 11:59 AM
Subject: Compiler Construction Instructor: Muhammad Nadeem
Note: copying will lead to ZERO
This is hand written assignment.
You are required to write your Name and Registration Number on top of each
paper of your assignment.
You need to submit hard and soft form of assignment. Both must be
submitted in time.
Also Scan assignment using any scanner tool (like CAMSCANNER), create a
pdf , and upload to server (CUONLINE), prior to due time.
No email will be accepted, except the students having registration issue.

Q1. Java Script is a scripting and powerful language for client side
scripting. It has different type of tokens, with unique properties.
Different tokens with associated properties are given below. You need to
write a Regular Expression for each type of token.

Token Type Property


Variable Name Start with _ and then there can be a repetition of Letter,
Number and _ .
Number Contain one or more digits
Float Start with one or more digits , then there is . followed by
one or more digits.
Power Number Start with one or more digits followed by e followed by
one or more digits. (e.g 123e67)
Negative Start with one or more digits followed by e followed by –
Power and followed by one or more digits. (e.g 123e-67)
String Start with “ and end with ” and in between there can be
number, float, power number, negative power
Comparison > or < or = or == or != or >= or <=
Operator (Here or is used as choice between these operators)
Change ++ or - - or +=Number
operator (Here or is choice, while Number is a RE expressing
Number)
Keywords goto
In
instanceof
Static
finally
arguments
public
Do
else
Const
Function
class*
return
let*
Catch
Eval
For
if
This
try

Separators + or – or ! or != or == or >> or << or % or / or # or & or


space or \n or \t
Comment Start with // followed by Variable Name and Digits and
ends with //
Also consider the following code and identify each type of token. (There
can be some error tokens as well. You need to write each token Value along
its type , for example
let a = 90;
a == 5;
b = 6;
c = a + b;
// program to check if a number is prime or not //

// take input from the user //


const number = parseInt(prompt("Enter a positive number:
"));
let isPrime = true;

// check if number is equal to 1 //


if (number === 1) {
console.log("1 is neither prime nor composite number.");
}

// check if number is greater than 1


else if (number > 1) {

// looping through 2 to number //


for (let i = 2; i < number; i++) {
if (number % i == 0) {
isPrime = false;
break;
}
}

if (isPrime) {
console.log(`${number} is a prime number`);
} else {
console.log(`${number} is a not prime number`);
}
}

// check if number is less than 1 //


else {
console.log("The number is not a prime number.");
}

You might also like