interpreter? Answer: Compiler: Compiler is a program that translates code written in a high-level programming language (like JavaScript or Java) into low-level code (like Assembly) directly executable by the computer or another program such as a virtual machine. Compiler Interpreter It takes an entire program It takes a single line of at a time. code or instruction at a time It generates intermediate It does not produce object code. intermediate object code The compilation is done Compilation and execution is before execution. done simultaneously. Comparatively faster slower
Memory requirement is more It requires less memory
due to the creation of object because it does not create code. intermediate code. Display all errors after Display errors of each line compilation, all at the same one by one. time Error detection is difficult Easier comparatively Q7-1(b).Write the purpose of a compiler. What’s the role of a lexical analyze. Answer:
Purpose of a compiler: Apart from translating source code from
high level language to low level language, compiler has other responsibilities too. After reading source code written in high level language it performs below operations -
1. Performs a pre-processing of source code. Gather all files required
for the source code to compile. 2. Parses the entire source code. Checks for any syntax errors in the source code. 3. Performs a thorough syntax analysis of the source code. To understand the structure and semantic of the source code. 4. Optionally translates the source code in an intermediate code known as object code to enhance the performance. 5. Translates the object code to binary language known as executable code.
Roles of lexical analyzer: Lexical analysis is the first phase
of a compiler. It takes the modified source code from language preprocessors that are written in the form of sentences. The lexical analyzer breaks these syntaxes into a series of tokens, by removing any whitespace or comments in the source code. If the lexical analyzer finds a token invalid, it generates an error. The lexical analyzer works closely with the syntax analyzer. It reads character streams from the source code, checks for legal tokens, and passes the data to the syntax analyzer when it demands.