You are on page 1of 4

ASSIGNMENT#01

COMPILER CONSTRUCTION

Submitted to:
Dr. Muhammad Nadeem

Submitted by:
Ahmed Khan Reg#4045
Wahaj Tahir Khokhar Reg#3972
Malik Riaz Khan Reg#3974
Muhammad Asim Reg# 3956
function isKeyword(buffer) {
var keywords = [
'auto',
'break',
'case',
'char',
'const',
'continue',
'default',
'do',
'double',
'else',
'enum',
'extern',
'float',
'for',
'goto',
'if',
'int',
'long',
'register',
'return',
'short',
'signed',
'sizeof',
'static',
'struct',
'switch',
'typedef',
'union',
'unsigned',
'void',
'volatile',
'while',
];
var i;

for (i = 0; i < 32; i++) {


if (keywords[i] === buffer) {
return true;
}
}
return false;
}
const generateTokenList = (buffer) => {
var lexTokenList = [];
const checkIfAlphNum = /^[a-z0-9]+$/i;
const checkIfValidId = /^[_$a-zA-Z\xA0-\uFFFF][_$a-zA-Z0-9\xA0-\uFFFF]*$/i;
var tempToken = '',
ch,
specialSymbols = '+-*/%=#<>;:{}()';
var i,
j = 0,
k = 0;

while (k < buffer.length) {


ch = buffer.charAt(k++);
for (i = 0; i < specialSymbols.length; ++i) {
if (ch == specialSymbols[i]) {
lexTokenList.push(["Special Symbol", ch]);
}
}

if ((ch === "-" || ch === "_" ) || checkIfAlphNum.test(ch)) {


tempToken = tempToken.concat(ch);
} else if( tempToken.length !== 0) {
if (!isNaN(tempToken)) {
lexTokenList.push(["Constant", tempToken]);
}
else if (isKeyword(tempToken)) {
lexTokenList.push(["Keyword", tempToken]);
} else {
if(checkIfValidId.test(tempToken)){
lexTokenList.push(["VALID_Id", tempToken]);
} else {
lexTokenList.push(["InVALID_Id", tempToken]);
}

}
tempToken = '';
}

}
return lexTokenList;
};
export default generateTokenList;
REGEX USED:

Remove Strings ("(?:[^"\\]|\\"|\\)*")


Remove Preprocessor \#.*(?=[\n\r])
Remove Single Line Comment \/\/.*(?=[\n\r])
Remove Block Comment \/\*(.|\n)+?\*\/
Remove Spaces & EOL /(\s\n*)/
Identifier Validation ^[_$a-zA-Z\xA0-\uFFFF][_$a-zA-
Z0-9\xA0-\uFFFF]*$

*****

For Full Code Check out this Snack!


https://snack.expo.dev/@ahmedkhan743/lexical-analyzer

**********

You might also like