Write a Program to Detect Operators using Lexical Analyzer in JavaScript | Compiler Design

Write a Program to Detect Operators using Lexical Analyzer in JavaScript | Compiler Design

What is Lexical Analyzer?

Lexical analysis, also known as scanning, is a crucial phase in the compilation process of programming languages. It involves breaking down the input program into a sequence of tokens. In this article, we will explore how to detect operators using lexical analysis in JavaScript.

What is Operators?

Operators are symbols that instruct the compiler or interpreter to perform specific mathematical, relational, or logical operations.

For example, + is an operator used for addition, while - is an operator used for subtraction and many more. 

Types of Operators in Programming Languages with Examples:

There are several types of operators commonly used in programming languages. They include:


1) Arithmetic operators: Used for mathematical operations such as addition (+), subtraction (-), multiplication (*), division (/), and modulo (%).


2) Relational operators: Used to compare values, such as greater than (>), lesser than (<), equal to (==), not equal to (!=), greater than or equal to (>=), and lesser than or equal to (<=).


3) Logical operators: Used for logical operations, such as logical AND (&&), logical OR (||), and logical NOT (!).


4) Bitwise operators: Used for bit-level operations on binary numbers, such as bitwise AND (&), bitwise OR (|), and bitwise XOR (^).


5) Assignment operators: Used to assign values, such as simple assignment (=), addition assignment (+=), subtraction assignment (-=), multiplication assignment (*=), and division assignment (/=).


6) Type Information Operators: Special operators that provide information about the type of a value, such as typeof and instanceof.


Detecting Operators in JavaScript:

To detect operators in JavaScript, we can use a lexical analyzer. Here is an example implementation in JavaScript:

let operators = {
    '+' :'addition',
    '-' :'subtraction',
    '*' :'multiplication',
    '/' :'division',
    '%' :'modulo',
    '>' :'greater than',
    '<' :'lesser than',
    '=' :'assignment',
    '++':'increment',
    '--':'decrement',
    '<=':'less or equal',
    '>=':'greater or equal',
    '==':'equal',
    '!=':'not equal',
    '&&':'logical AND',
    '||':'logical OR',
    '!':'logical NOT',
    '^':'logical XOR',
    '?:':'ternary operator',
    '+=': 'assignment addition',
    '-=': 'assignment subtraction',
    '/=': 'assignment division',
    '*=': 'assignment multiplication',
    '**': 'exponential'
};

function detectOperator(expression, operators) {
    console.log(`The given expression:\n${expression}\n`);

    // Handle double operators
    let double = ['!=','--','++','<=','>=','==','&&','||','+=','-=','/=','*=','**'];
    for(let dup of double) {
        if(expression.includes(dup)) {
            expression = expression.replace(`${dup}`,'');
            console.log(`${dup} is ${operators[dup]} operator`);
        }
    }

    // Handle ternary operator
    if(expression.includes('?' && ':')){
        expression = expression.replace('?', '').replace(':', '');
        console.log(`?: is a ternary operator`);
    }

    // Handle other operators
    for(let operator in operators) {
        let hasOperator = expression.includes(operator);
        if(hasOperator) {
            console.log(`${operator} is ${operators[operator]} operator`);
        }
    }
}

//takes input and runs only in browser console
let expression = prompt("Enter the expression:");
detectOperator(expression, operators); 

 

0 Response to Write a Program to Detect Operators using Lexical Analyzer in JavaScript | Compiler Design

Post a Comment