Showing posts with label System Programming and Compiler Construction (SPCC). Show all posts
Showing posts with label System Programming and Compiler Construction (SPCC). Show all posts

Error recovery techniques used by compiler

The error recovery techniques used by the compiler are as follows:
  1. Panic Mode Recovery
    1. This is the simplest method to implement and can be used by most parsing methods.
    2. On discovering an error, the parser discards the input symbol one at a time. until one of the designated set of synchronizing token is found.
    3. The synchronizing tokens are usually delimiters, such as semicolon or end, whose role in the source program is clear.
  2. Phrase-Level Recovery
    1. On discovering an error, a parser may perform local correction on the remaining input, that is it may replace a prefix of the remaining input by some string that allows the parser to continue.
    2. A typical local correction would be to replace a comma by a semicolon, delete an extraneous semicolon or insert a missing semicolon.
    3. It is implemented by filling in the blank entries with the pointers to error routines.
    4. These routines may,
      • change, insert or delete symbols on the input.
      • pop from the stack, and issue appropriate error messages.
  3. Error Productions
    1. If we have a good idea that the common errors that might be encountered, we can augment the grammar for the language at hand with productions that generate the erroneous constructs.
    2. We then use the grammar augmented by these errors productions to construct a parser.
    3. If an error production is used by the parser, we can generate appropriate diagnostics to indicate the erroneous construct that has been recognized in the input.
  4. Global Corrections
    1. In this compiler will make few changes (insertions, deletions, and changes of tokens) as possible in processing an incorrect input string.

Absolute Loader


  • The absolute loader is a kind of loader in which relocated object files are created, loader accepts these files and places them at a specified location in the memory.
  • This type of loader is called absolute loader because no relocating information is needed, rather it is obtained from the programmer or assembler.
  • The starting address of every module is known to the programmer, this corresponding starting address is stored in the object file then the task of loader becomes very simple that is to simply place the executable form of the machine instructions at the locations mentioned in the object file.
  • In this scheme, the programmer or assembler should have knowledge of memory management. The programmer should take care of two things:
    • Specification of starting address of each module to be used. If some modification is done in some module then the length of that module may vary. This causes a change in the starting address of immediate next modules, it's then the programmer's duty to make necessary changes in the starting address of respective modules.
    • While branching from one segment to another the absolute starting address of respective module is to be known by the programmer so that such address can be specified at respective JMP instruction.
Fig. Process of Absolute Loader

Advantages:
  1. It is simple to implement.
  2. This scheme allows multiple programs or the source programs written in different languages. If there are multiple programs written in different languages then the respective language assembler will convert it to the language and common object file can be prepared with all the ad resolution.
  3. The task of loader becomes simpler as it simply obeys the instruction regarding where to place the object code to the main memory.
  4. The process of execution is efficient.
Disadvantages:
  1. In this scheme, it's the programmer's duty to adjust all the inter-segment addresses and manually do the linking activity. For that, it is necessary for a programmer to know the memory management.
  2. If at all any modification is done to some segment the starting address of immediate next segments may get changed the programmer has to take care of this issue and he/she needs to update the corresponding starting address on any modification in the source.

Explain 'Compile and Go' Loader

    In this type of loader, the instruction is read line by line, its machine code is obtained and it is directly put in the main memory at some known address. That means the assembler runs in one part of memory and the assembled machine instructions and data is directly put into their assigned memory locations. After completion, the assembly process assigns the starting address of the program to the location counter. The typical example ie WATFUR-77, a FORTRAN compiler which uses such "load and go" scheme. This loading scheme is also called as "assemble and go".

Fig. Compile and Go Loader
Advantages:
  • This scheme is simple to implement because assembler is placed at one part of the memory and loader simply loads assembled machine instructions into the memory.
Disadvantages:
  • In this scheme, some portion of memory is occupied by assembler which is simply a wastage of a memory. As this scheme is a combination of assembler and loader activities this combination program occupies a large block of memory.
  • There is no production of .obj file, the source code is directly converted to executable form. Hence even though there is no modification in the source program it needs to be assembled and executed each time which then become a time-consuming activity.
  • It cannot handle multiple source program or multiple programs written in different languages. This is because assembler can translate one source language to another target language.
  • The execution time will be more in this scheme as every time program is assembled and then executed.

General Loader Scheme

In this loader scheme, the source program is converted to object program by some translator (assembler). The loader accepts these object modules and puts the machine instruction and data in an executable form at their assigned memory. The loader occupies the same portion of main memory.

Fig. General Loader Scheme

Advantages:
  1. The program need not be retranslated each time while running it. Thus us because initially when source program gets executed and object program gets generated. If a program is not modified, then the loader can make use of this object program to convert it to executable form.
  2. There is no wastage of memory because the assembler is not placed in the memory instead of it, loader occupies some portion of the memory. And the size of the loader is smaller than assembler so more memory is available to the user.
  3. It is possible to write source program with multiple programs and multiple languages because the source program is first converted to an object program always and loader accepts these object modules to convert it to an executable format.

Difference between LL Parser vs LR Parser. LL Parser vs LR Parser


LL Parser LR Parser
It is also known as top-down parsing. It is also known as bottom-up parsing.
First L of LL is for left to right (i.e, input is processed in the order it is read) and second L for leftmost derivation. L-Left to right (i.e, input is processed in the order it is read) and R - Rightmost derivation
LL starts with only the root non-terminal on the stack. LR ends with only the root non-terminal on stack.
LL ends when the stack is empty. LR starts with an empty stack.
LL expands non-terminal. LR reduces non-terminal.
LL reads terminal when its pop one of the stack. LR reads terminal when it pushes them on the stack.
LL uses pre-order traversal of the parse tree. LR uses post-order traversal of the parse tree.
During LL parser the parser continuosly chooses between two action.
Predict: Based on the leftmost non-terminal and some number of lookahead tokens.
Match: Match the leftmost guessed terminal symbol with the leftmost unconsumed symbol of input.
During an LR parser the parser continuosly chooses between two action.
Shift: Add the next token of input to a buffer for consideration.
Reduce: Reduce a collection of terminals and non-terminals.
LL parser are easier to write but less powerful and comes in many flavours like LL(1), etc. LR parsers are much powerful and they come in many flavours like LR(0), SLR(1), LALR(1), LR(1), etc.

Define Compiler, Interpreter, Assembler, Linker, Loader, Macro.







  1. Compiler
       
    • A compiler is a computer program (or a set of programs) that transforms source code written in a programming language (the source language) into another computer language (the target language).
    • Typically, from high level source code to low level machine code or object code.

  2. Interpreter
       
    • An interpreter is a common kind of language processor. Instead of producing target program as a translation, an interpreter appears to directly execute the operations specified in the source program on inputs supplied by the user.
    • In contrast, an interpreter reads a statement from the input, converts it to an intermediate code, execute it, then takes the next statement in sequence.
    • If an error occurs, an interpreter stops execution and reports it.

  3. Assembler
       
    • An assembler translates assembly language programs into machine code. The output of a assembler is called an object file, which contains a combination of machine instruction as well as the data required to place these intstructions in memory.

  4. Linker
       
    • Linker is a computer program that links and merges various object files together in order to make an executable file. All these files might have been compiled by separate assembler.
    • The major task of a linker is to search and locate referenced module/routines in a program and to determine the memory location where these codes will be loaded making the program instruction to have absolute reference.

  5. Loader
       
    • Loader is a part of operating system and is responsible for loading executable files into memory and execute them.
    • It calculates the size of a program (instructions and data) and create memory space for it. It initializes various registers to initiate execution.

  6. Macro
       
    • Macro is a single line abbreviation for group of instructions.
    • With the help of macro, programmer can define a single "instruction" to represent block of code.

Difference between Compiler and Interpreter. Compiler vs Interpreter










Compiler: A compiler is a computer program(or a set of program) that transforms source code written in a programming language into another computer language(the target language).
Interpreter: An interpreter is a common kind of language processor
Compiler Interpreter
A compiler is a computer program(or a set of program) that transforms source code written in a programming language into another computer language(the target language). An interpreter is a common kind of language processor, instead of producing a target program as a translation an interpreter appears to directly execute the operations specified in the source program on inputs supplied by the user.
Compiler works on the complete program at once. It takes the entire program as input. Interpreter program works line-by-line. It takes one statement at a time as input.
Compiler generate the intermediate code called as object code or machine code. Interpreter does not generate intermediate code.
Compiler executes conditional control statements(like if-else and switch case) faster than interpreter. Interpreter executes conditional control statements at a slower rate.
It takes more memory since the entire compiled code(object code) has to be reside in memory. Interpreted programs are more memory efficient as they do not generate any object code.
Compile once and run anytime, they doesn't need to run eveytime. Interpreted programs are interpreted line-by-line every time they are run.
Errors are reported after the whole program is checked for syntactical and other errors. Errors are reported as soon as the first error is encountered. Rest of the program will not be checked until the existing error is removed.
A compiled language is more difficult to debug. Debugging is easy because interpreter stops and reports errors as it encounter them.
Compiler does not allow a program to run until it is completely error free. Interpreter runs the program from first line and stops execution only if it encounters an error.
Compiled language are more efficient but difficult ot debug. Interpreted language are less efficient but easy to debug.
Examples of programming languages that use compiler are C, C++, COBOL, JAVA. Examples of programming languages that use interpreter are BASIC, Python, PHP, Perl, LISP.