10.question bank with answers.doc

Upload: niravmodh

Post on 05-Nov-2015

181 views

Category:

Documents


6 download

DESCRIPTION

System Programming

TRANSCRIPT

SAFFRONY INSTITUTE OF TECHNOLOGY2150708 System Programming (SP)AssemblerQ. 1Describe elements of Assembly language. An assembly language provides three basic features:1. Mnemonics operation codes 2. Symbolic operands3. Data declaration Let us consider an assembly instruction MOVER AREG,XMOVER is a mnemonic opcode for the operation to be performed. AREG is a register operand in a symbolic form.X is a memory operand in a symbolic form. Let us consider another instruction for data declaration X DS 1DS(Declare storage) reserves area of memory. Name of variable is XIt reserves a memory area of 1 word and associates the name X with itQ. 2Explain types of Assembly statement1) Imperative statement An imperative statement indicates an action to be performed during the execution of theassembled statement. Each imperative statement typically translates into one machine instruction. These are executable statements. Some example of imperative statement are given belowMOVER BREG,XSTOPREAD X PRINT Y ADD AREG,Z2) Declaration statement Declaration statements are for reserving memory for variables. The syntax of declaration statement is as follow:[Label] DS[Label] DC DS: stands for Declare storage, DC: stands for Declare constant. The DS statement reserves area of memory and associates name with them.A DS 10 Above statement reserves 10 word of memory for variable A. The DC statement constructs memory words containing constants.ONE DC 1 Above statement associates the name ONE with a memory word containing the value 1Prepared By: TEJAS PATELPage 1 SAFFRONY INSTITUTE OF TECHNOLOGY2150708 System Programming (SP)Assembler Any assembly program can use constant in two ways- as immediate operands, and asliterals. Many machine support immediate operands in machine instruction. Ex: ADD AREG, 5 But hypothetical machine does not support immediate operands as a part of the machineinstruction. It can still handle literals. A literal is an operand with the syntax=. EX: ADD AREG,=5 It differs from constant because its location cannot be specified in assembly program.3) Assembler Directive Assembler directives instruct the assembler to perform certain action during the assemblyprogram. I.START This directive indicates that first word of machine should be placed in the memory word with address . START Ex: START 500 First word of the target program is stored from memory location 500 onwards. II.END This directive indicates end of the source program. The operand indicates address of the instruction where the execution of program should begin. By default it is first instruction of the program. END Execution control should transfer to label given in operand field. III.ORIGIN This directive is like START instruction, which indicates address of the next consecutive instruction or data. Format of this statement is as follows ORIGIN Operand may constant, symbol or symbolic expression.Sr. no.Assembly programLC1START 1002LOOP MOVER BREG=21003MOVER AREG,N1014ADD AREG=11025ORIGIN LOOP6NEXT BC ANY,LOOP100 The ORIGIN directive is useful when the machine code is not stored in consecutive memory location.IV.EQU This directive simply associate the name with .where may be constant or symbol.Prepared By: TEJAS PATELPage 2SAFFRONY INSTITUTE OF TECHNOLOGY2150708 System Programming (SP)Assembler EQU Ex: A EQU B Address of B is assigned to A in symbol table.V.LTORG This directive allocates memory to all literals of current pool and update literaltable, pool table. Format of this instruction is as follows LTORG. If LTORG statement is not present, literals are placed after the END statement.Q. 3Explain assembly scheme.ORExplain analysis and synthesis phases of an assembler by clearly stating their tasks. ORDesign specification of an assembler.Analysis Phase The primary function performed by the analysis phase is the building of the symbol table. For this purpose it must determine address of the symbolic name. It is possible to determine some address directly, however others must be inferred. And this function is called memory allocation. To implement memory allocation a data structure called location counter (LC) is used, it is initialized to the constant specified in the START statement. We refer the processing involved in maintaining the location counter as LC processing. Tasks of Analysis phase1. Isolate the label, mnemonics opcode, and operand fields of a constant.2. If a label is present, enter the pair (symbol, ) in a new entry of symbol table.3. Check validity of mnemonics opcode. 4. Perform LC processing.mnemonicsopcodelengthADD011SUB021Sourcepr og. Analysis phase SynthesisTarget phase prog.symboladdressAGAIN104N113Symbol tablePrepared By: TEJAS PATELPage 3SAFFRONY INSTITUTE OF TECHNOLOGY2150708 System Programming (SP)AssemblerSynthesis Phase Consider the assembly statement,MOVERBREG, ONE We must have following information to synthesize the machine instruction correspondingto this statement:1. Address of name ONE2. Machine operation code corresponding to mnemonics MOVER. The first item of information depends on the source program; hence it must be available by analysis phase. The second item of information does not depend on the source program; it depends on the assembly language. Based on above discussion, we consider the use of two data structure during synthesis phase:1. Symbol table:Each entry in symbol table has two primary field- name and address. This table is built by analysis phase2. Mnemonics table:An entry in mnemonics table has two primary field- mnemonics and opcode. Task of Synthesis phase1. Obtain machine opcode through look up in the mnemonics table. 2. Obtain address of memory operand from the symbol table.3. Synthesize a machine instruction.Q. 4Explain single pass and two pass assembler.ORWrite difference between one pass and two pass assembler.OR Pass structure of assembler.Two pass translation Two pass translationsconsist of pass I and pass II. LC processing is performed in the first pass and symbols defined in the program are entered into the symbol table, hence first pass performs analysis of the source program. So, two pass translation of assembly lang. program can handle forward reference easily. The second pass synthesizes the target form using the address information found in the symbol table. First pass constructs an intermediate representation of the source program and that will be used by second pass. IR consists of two main components: data structure + IC (intermediate code) Single pass translation A one pass assembler requires 1 scan of the source program to generate machine code. The process of forward references is talked using a process called back patching. The operand field of an instruction containing forward references is left blank initially.Prepared By: TEJAS PATELPage 4SAFFRONY INSTITUTE OF TECHNOLOGY2150708 System Programming (SP)Assembler A table of instruction containing forward references is maintained separately called table ofincomplete instruction (TII). This table can be used to fill-up the addresses in incomplete instruction. The address of the forward referenced symbols is put in the blank field with the help ofback patching list.Q. 5Explain Data structures of assembler pass IORExplain the role of mnemonic opcode table, symbol table, literal table, and pool table in assembling process of assembly language program.ORDescribe following data structures:OPTAB, SYMTAB, LITTAB& POOLTAB. OPTAB A table of mnemonics opcode and related information OPTAB contains the field mnemonics opcodes, class and mnemonics info. The class field indicates whether the opcode belongs to an imperative statement (IS), a declaration statement (DS), or an assembler directive (AD). If an imperative, the mnemonics info field contains the pair (machine code, instruction length), else it contains the id of a routine to handle the declaration or directive statement.Mnemonicsopcode MnemonicsClassinfoMOVERIS(04,1)DSDLR#7STARTADR#11..SYMTAB A SYMTAB entry contains the symbol name, field address and length. Some address can be determining directly, e.g. the address of the first instruction in the program, however other must be inferred. To find address of other we must fix the addresses of all program elements preceding it. This function is called memory allocation.SymbolAddressLengthLOOP2021NEXT2141LAST2161A2171BACK2021B2181Prepared By: TEJAS PATELPage 5SAFFRONY INSTITUTE OF TECHNOLOGY2150708 System Programming (SP)AssemblerLITTAB A table of literals used in the program. A LITTAB entry contains the field literal and address. The first pass uses LITTAB to collect all literals used in a program. Awareness of different literal pools is maintained using the auxiliary table POOLTAB. This table contains the literal number of the starting literal of each literal pool. At any stage, the current literal pool is the last pool in the LITTAB. On encountering an LTORG statement (or the END statement), literals in the current poolare allocated addresses starting with the current value in LC and LC is appropriately incremented.literalAddress1=52=13=1LITTAB Literal no#1#3POOLTABQ. 6Detail design of two pass assembler.Pass IAlgorithm for Pass I1) loc_cntr=0(default value)pooltab_ptr=1; POOLTAB[1]=1; littab_ptr=1;2) While next statement is not END statement a) If a label is present thenthis_label=symbol in label fieldEnter (this_label, loc_cntr) in SYMTAB b) If an LTORG statement then(i)Process literals LITTAB to allocate memory and put the address field.updateloc_cntr accordingly(ii)pooltab_ptr= pooltab_ptr+1;(iii)POOLTAB[ pooltab_ptr]= littab_ptr c) If a START or ORIGIN statement thenloc_cntr=value specified in operand field; d) If an EQU statement then(i)this_address=value specified in ;(ii)Correct the symtab entry for this_label to (this_label, this_address); e) If a declaration(i)Code= code of the declaration statement(ii)Size= size of memory area required by DC/DSPrepared By: TEJAS PATELPage 6SAFFRONY INSTITUTE OF TECHNOLOGY2150708 System Programming (SP)Assembler(iii)loc_cntr=loc_cntr+size;(iv)Generate IC (DL,code)..f) If an imperative statement then(i)Code= machine opcode from OPTAB(ii)loc_cntr=loc_cntr+instruction length from OPTAB; (iii)if operand is a literal thenthis_literal=literal in operand field; LITTAB[littab_ptr]=this_literal; littab_ptr= littab_ptr +1;elsethis_entry= SYMTAB entry number of operand generate IC (IS, code)(S, this_entry);3) (processing END statement) a) Perform step2(b)b) Generate IC (AD,02) c) Go to pass IIIntermediate code forms: Intermediate code consist of a set of IC units, each unit consisting of the following threefields1. Address2. Representation of mnemonics opcode 3. Representation of operandsMnemonics field The mnemonics field contains a pair of the form (statement class, code) Where statement class can be one of IS, DL, and AD standing for imperative statement, declaration statement and assembler directive respectively. For imperative statement, code is the instruction opcode in the machine language. For declarations and assembler directives, code is an ordinal number within the class. Thus, (AD, 01) stands for assembler directive number 1 which is the directive START. Codes for various declaration statements and assembler directives.Declaration statementDC01DS02 Assembler directiveSTART01END02ORIGIN03EQU04LTORG05 The information in the mnemonics field is assumed to have the same representation in allthe variants.Prepared By: TEJAS PATELPage 7SAFFRONY INSTITUTE OF TECHNOLOGY2150708 System Programming (SP)AssemblerIntermediate code for Imperative statementVariant I First operand is represented by a single digit number which is a code for a register or the condition codeRegisterCodeAREG01BREG02CREG03DREG04 ConditionCodeLT01LE02EQ03GT04GE05ANY06 The second operand, which is a memory operand, is represented by a pair of the form(operand class, code) Where operand class is one of the C, S and L standing for constant, symbol and literal. For a constant, the code field contains the internal representation of the constant itself. Ex: the operand descriptor for the statement START 200 is (C,200). For a symbol or literal, the code field contains the ordinal number of the operands entry in SYMTAB or LITTAB.Variant II This variant differs from variant I of the intermediate code because in variant II symbols, condition codes and CPU register are not processed. So, IC unit will not generate for that during pass I.START200READALOOPMOVERAREG, A. .SUB AREG, =1 BC GT, LOOPSTOPA DS 1 LTORG ..(AD,01)(C, 200)(IS, 09)(S, 01)(IS, 04)(1)(S, 01). .(IS, 02)(1)(L, 01) (IS, 07)(4)(S, 02) (IS, 00)(DL, 02)(C,1) (AD, 05)Variant I(AD,01) (C, 200)(IS, 09) A(IS, 04) AREG, A. .(IS, 02) AREG,(L, 01) (IS, 07) GT, LOOP (IS, 00)(DL, 02) (C,1) (AD, 05)Variant IIPrepared By: TEJAS PATELPage 8SAFFRONY INSTITUTE OF TECHNOLOGY2150708 System Programming (SP)AssemblerComparison of the variantsVariant IVariant II Extra work in pass I Extra work in pass II Simplifies tasks in pass II Simplifies tasks in pass I Occupies more memory then pass II Memory utilization of two passes getbetter balanced.Pass II(Algorithm) It has been assumed that the target code is to be assembled in the are named code_area.1. Code_area_adress= address of code_ares;Pooltab_ptr=1;Loc_cntr=0;2. While next statement is not an END statement a) Clear machine_code_buffer;b) If an LTORG statementi)ProcessliteralsinLITTABandassembletheliteralsin machine_code_buffer.ii)Size= size of memory area required for literals iii)Pooltab_ptr=pooltab_ptr +1;c) If a START or ORIGIN statementi)Loc_cntr=value specified in operand field; ii)Size=0;d) If a declaration statementi)If a DC statement then assemble the constatnt in machine_code_buffer; ii)Size= size of memory area required by DC/DS;e) If an imperative statementi)Get operand address from SYMTAB or LITTAB ii)Assemble instruction in machine_code_buffer; iii)Size=size of instruction;f) If size 0 theni)Movecontentsofmachine_code_buffertotheaddress code_area_address+loc_cntr;ii)Loc_cntr=loc_cntr+size; 3. Processing end statementa) Perform steps 2(b) and 2(f)b) Write code_area into output file.Prepared By: TEJAS PATELPage 9SAFFRONY INSTITUTE OF TECHNOLOGY2150708 System Programming (SP)Assembler Q. 7Explain error reporting of assembler. Error reporting in pass I Listing an error in first pass has the advantage that source program need not be preservedtill pass II But, listing produced in pass I can only reports certain errors not all. From the below program, error is detected at statement 9 and 21. Statement 9 gives invalid opcode error because MVER does not matchwith any mnemonicsin OPTAB. Statement 21 gives duplicate defination error because entry of A is already exist in symbol table. Undefined symbol B at statement 10 is harder to detect during pass I, this error can be detected only after completing pass I.Sr.noStatementsAddress1START 2002MOVER AREG,A2003....9MVER BREG, A207**ERROR* Invalid opcode10ADD BREG, B20814A DS 1209......21A DC 5227**ERROR**dulicatedefinationofsymbol A..35END**ERROR** undefined symbol B instatememt 10Error reporting in pass II During pass II data structure like SYMTAB is availble.Error indication at statement 10 is also easy because symbol table is searched for an entry B. if match is not found, error is reported.Prepared By: TEJAS PATELPage 10SAFFRONY INSTITUTE OF TECHNOLOGY2150708 System Programming (SP)AssemblerQ. 8Write N! program and its equivalent machine code.Op-code (2 digit)Register operand (1 digit)Memory operand (3 digit)1START1012READN101)0901133MOVERBREG, ONE102)0421154MOVEMBREG, TERM103)0521165AGAINMULTBREG,TERM104)0321166MOVERCREG, TERM105)0431167ADDCREG, ONE106)01311512MOVEMCREG, TERM107)05311613COMPCREG, N108)06311314BCLE, AGAIN109)07210415MOVEMBREG, RESULT110)05211416PRINTRESULT111)10011417STOP112)00000018NDS1113)19RESULTDS1114)20ONEDC1115)00000121TERMDS1116)22ENDQ.9Generate intermediate code and symbol table for following programsProgram-1START100READAREADBREADCMOVERAREG,AADDAREG,BADDAREG,CMULTAREG,CMOVEMAREG,RESULTPRINTRESULTSTOPADS1BDS1CDS1RESULTDS1ENDPrepared By: TEJAS PATELPage 11SAFFRONY INSTITUTE OF TECHNOLOGY2150708 System Programming (SP)AssemblerProgram-1 IC in variant-IProgram-1 Symbol table(AD,01)(IS,09)(IS,09)(IS,09)(IS,04)(IS,01)(IS,01)(IS,03)(IS,05)(IS,10)(IS,00)(DL,02)(DL,02)(DL,02)(DL,02)(AD,02) (C,100)(S,01)(S,02)(S,03)(01)(S,01)(01)(S,02)(01)(S,03)(01)(S,03)(01)(S,04)(S,04)(C,01)(C,01)(C,01)(C,01) SymbolAddress1A1112B1123C1134RESULT114Program-2Program-2 symbol tableSTART101READAREADBMOVERBREG,AMULTBREG,BMOVEMBREG,DSTOPADS1BDS1DDS1END SymbolAddress1A1082B1093D110Program-2 Variant-IProgram-2 Variant-II(AD,01)(IS,09)(IS,09) (C,101)(S,01)(S,02) (AD,01)(IS,09)(IS,09) (C,101)ABPrepared By: TEJAS PATELPage 12SAFFRONY INSTITUTE OF TECHNOLOGY2150708 System Programming (SP)Assembler(IS,04)(IS,03)(IS,05)(IS,00)(DL,02)(DL,02)(DL,02)(AD,02) (2)(S,01)(2)(S,02)(2)(S,03)(C,01)(C,01)(C,01) (IS,04)BREG,A(IS,03)BREG,B(IS,05)BREG,D(IS,00)(DL,02)(C,01)(DL,02)(C,01)(DL,02)(C,01)(AD,02)Prepared By: TEJAS PATELPage 13SAFFRONY INSTITUTE OF TECHNOLOGY2150708 System Programming (SP)CompilerQ.1List out aspects of compilation and its implementation issue.Two aspects of compilation are:a) Generate code to implement meaning of a source program in the execution domain (target code generation)b) Provide diagnostics for violations of PL semantics in a program (Error reporting)There are four issue involved in implementing these aspects(Q. What are the issue in code generation in relation to compilation of expression? Explain each issue in brief. (June-13 GTU))1. Data types : semantics of a data type require a compiler to ensure that variable of a type are assigned or manipulated only through legal operationCompiler must generate type specific code to implement an operation.2. Data structures: to compile a reference to an element of a data structure, the compiler must develop a memory mapping to access the memory word allocated to the element.3. Scope rules: compiler performs operation called scope analysis and name resolution to determine the data item designated by the use of a name in the source program4. Control structure: control structure includes conditional transfer of control, conditional execution, iteration control and procedure calls. The compiler must ensure that source program does not violate the semantics of control structures.Issues in design of code generator are: 1. Input to the Code Generator input to the code generator consists of the intermediate representation of the source program There are several types for the intermediate language, such as postfix notation, quadruples, and syntax trees or DAGs. The detection of semantic error should be done before submitting the input to the code generator The code generation phase require complete error free intermediate code as an input. 2. Target program The output of the code generator is the target program. The output may take on a variety of forms: absolute machine language, relocatable machine language, or assembly language. Producing an absolute machine language program as output has the advantage that it can be placed in a location in memory and immediately executed. Producing a relocatable machine language program as output is that the subroutine can be compiled separately. A set of relocatable object modules can be linked together and loaded for execution by a linking loader. Producing an assembly language program as output makes the process of code generation somewhat easier .We can generate symbolic instructions and use the macro facilities of the assembler to help generate codeTEJAS PATELPage 1SAFFRONY INSTITUTE OF TECHNOLOGY2150708 System Programming (SP)Compiler3. Memory management Mapping names in the source program to addresses of data objects in run time memory isdone cooperatively by the front end and the code generator. We assume that a name in a three-address statement refers to a symbol table entry for the name.4. Instruction selection If we do not care about the efficiency of the target program, instruction selection is straightforward. It requires special handling. For example, the sequence of statementsa := b + c d := a + ewould be translated into MOV b, R0ADD c, R0 MOV R0, a MOV a, R0 ADD e, R0 MOV R0, d Here the fourth statement is redundant, so we can eliminate that statement. 5. Register allocation If the instruction contains register operands then such a use becomes shorter and faster than that of using in memory. The use of registers is often subdivided into two sub problems: During register allocation, we select the set of variables that will reside in registers at a point in the program. During a subsequent register assignment phase, we pick the specific register that a variable will reside in.6. Choice of evaluation The order in which computations are performed can affect the efficiency of the target code. Some computation orders require fewer registers to hold intermediate results than others. Picking a best order is another difficult, NP-complete problem7. Approaches to code generation The most important criterion for a code generator is that it produces correct code. Correctness takes on special significance because of the number of special cases that code generator must face. Given the premium on correctness, designing a code generator so it can be easily implemented, tested, and maintained is an important design goal.TEJAS PATELPage 2SAFFRONY INSTITUTE OF TECHNOLOGY2150708 System Programming (SP)CompilerQ.2What is Memory binding? Explain types of memory allocation.Memory Binding: A memory binding is an association between the 'memory address' attribute of a data item and the address of a memory area.Three important tasks of memory allocation are:1. Determine the amount of memory required to represent the value of a data item.2. Use an appropriate memory allocation model to implement the lifetimes and scopes of data items.3. Determine appropriate memory mappings to access the values in a non scalar data item, e.g. values in an array.Memory allocation are mainly divides into two types: 1. Static binding2. Dynamic binding Static memory allocation In static memory allocation, memory is allocated to a variable before the execution of a program begins. Static memory allocation is typically performed during compilation. No memory allocation or deallocation actions are performed during the execution of a program. Thus, variables remain permanently allocatedDynamic memory allocation In dynamic memory allocation, memory bindings are established and destroyed during the execution of a program Dynamic memory allocation has two flavors-automatic allocation and program controlled allocation. In automatic dynamic allocation, memory is allocated to the variables declared in a program unit when the program unit is entered during execution and is deallocated when the program unit is exit. Thus the same memory area may be used for the variables of different program units In program controlled dynamic allocation, a program can allocate or deallocate memory at arbitrary points during its execution. It is obvious that in both automatic and program controlled allocation, address of the memory area allocated to a program unit cannot be determined at compilation timeDynamic memory allocation techniques1. Explicit deallocation-Explicit Allocation of Fixed Sized Blocks-It is the simplest form of dynamic allocation.-By linking the blocks in a list allocation and deallocation can be done quickly with little or no storage overhead.-Initialization of the area is done by using a portion of each block for a link to the nextblock. Pointer available points to the first block.-Allocation consists of taking a block off the list and deallocation consists of putting theblock back on the list. We can treat each block as a variant record.TEJAS PATELPage 3SAFFRONY INSTITUTE OF TECHNOLOGY2150708 System Programming (SP)Compiler-There is no space overhead because the user program can use the entire block for its own purposes.-When the block is de-allocated then the compiler routine uses some of the space from the block itself to link it into the list of available blocks.-Explicit Allocation of Variable-Sized Blocks-When blocks are allocated and de-allocated storage can become fragmented that is the heap may consists of alternate blocks that are free and in use.-Fragmentation will not occur if blocks are of fixed size, but if they are of variable-size then it occurs.-One method for allocating variable sized blocks is first fit method. When a block of size s if allocated we search for the first free block that is of size f s (where f - size of free block). This block is then subdivided into a used block of size s and a free block of size (f - s). Because of that it incurs a time overhead as we have to search for a free block that is large enough.-When a block is de-allocated, we check to see if it is next to a free block. If possible the de-allocated block is combined with a free block next to it to create a larger free block.-Combining a adjacent free blocks into a larger free block prevent further fragmentationfrom occurring.2. Implicit De-allocation-Implicit de-allocation requires cooperation between the user program and the run-time package, because run time package needs to know when a storage block is no longer in use.-This cooperation is implemented by fixing the format of storage blocks.-The first problem is that of recognizing block boundaries. If the size of blocks 75 fixed, then position information can be used.-For example if each block occupies 20 words then a new block begins every 20 words. Otherwise in the inaccessible storage attached to a block we keep the size of a block. So we can determine where the next block begins.-The second problem it that of recognizing if a block is in use we assume that a block is in use if it is possible for the user program to refer to the information in the block.-The reference may occur through a pointer or after following a sequence of pointers, so the compiler needs to know the position in storage of all pointers.-Two approaches can be used for implicit de-allocation -Reference counts-We keep track of the number of blocks that point directly to the present block. If this count ever drops to 0 then the block can be de-allocated because it cannot be referred to i.e. the block has become garbage that can be collected. Maintaining the reference counts can be costly. Reference counts are best used when pointer between blocks never appear in cycles-Marking Techniques-An alternative approach is to suspend temporarily execution of the user program and use the frozen pointers to determine which blocks are in use. This approach requires all the pointers into the heap to be known.-Conceptually we pour paint into the heap through these pointers. Any block that is reachedTEJAS PATELPage 4SAFFRONY INSTITUTE OF TECHNOLOGY2150708 System Programming (SP)Compilerby the paint is in use and the rest can be de-allocated.-In more detail, we go through the heap and mark all blocks unused. Then we follow pointers marking as used any block that is reached in the process. A final sequential scan of the heap allows all blocks still marked unused to be allocated.Q.3Explain memory allocation in block structured language. The block is a sequence of statements containing the local data and declarations which areenclosed within the delimiters.Ex:A {Statements ..} The delimiters mark the beginning and the end of the block. There can be nested blocks for ex: block B2 can be completely defined within the block B1. Finding the scope of the variable means checking the visibility within the block Following are the rules used to determine the scope of the variable:1. Variable X is accessed within the block B1 if it can be accessed by any statement situated in block B1.2. Variable X is accessed by any statement in block B2 and block B2 is situated in block B1. There are two types of variable situated in the block structured language1. Local variable2. Non local variable To understand local and non local variable consider the following example Procedure A{Intx,y,z Procedure B {Inta,b }Procedure C {Intm,n }}ProcedureLocal variablesNon local variablesAx,y,zBa,bx,y,zCm,nx,y,z Variables x,y and z are local variables to procedure A but those are non local to block B andTEJAS PATELPage 5SAFFRONY INSTITUTE OF TECHNOLOGY2150708 System Programming (SP)CompilerC because these variable are not defined locally within the block B and C but are accessiblewithin these blocks.Q.4Explain activation record. The activation record is a block of memory used for managing information needed by asingle execution of a procedure.Return valueActual parameterControl linkAccess linkSaved M/c statusLocal variablesTemporaries1. Temporary values: The temporary variables are needed during the evaluation ofexpressions. Such variables are stored in the temporary field of activation record.2. Local variables: The local data is a data that is local to the execution procedure is storedin this field of activation record.3. Saved machine registers: This field holds the information regarding the status of machinejust before the procedure is called. This field contains the registers and program counter.4. Control link: This field is optional. It points to the activation record of the calling procedure. This link is also called dynamic link.5. Access link: This field is also optional. It refers to the non-local data in other activationrecord. This field is also called static link field.6. Actual parameters: This field holds the information about the actual parameters. Theseactual parameters are passed to the called procedure.7. Return values: This field is used to store the result of a function call.Q.5What is side effect? Explain parameter passing methods.Side effect: A side effect of a function call is a change in a value of a variable which is not localto the called function.Parameter passing mechanism 1. Call by value:This is the simplest method of parameter passing.The actual parameters are evaluated and their values are passed to caller procedure(formal parameter).TEJAS PATELPage 6SAFFRONY INSTITUTE OF TECHNOLOGY2150708 System Programming (SP)CompilerThe operations on formal parameters do not change the values of a parameter. Example: Languages like C, C++ use actual parameter passing method2. Call by value nameThis extends the capability of the call by value mechanism by copying the value of formal parameter back to corresponding actual parameter at returnThus side effect realize at return. 3. Call by reference :This method is also called as call by address or call by locationThe address of actual parameter is passed to the formal parameter. 4. Call by name:This is less popular method of parameter passing.Procedure is treated like macro. The procedure body is substituted for call in caller with actual parameters substituted for formals.The actual parameters can be surrounded by parenthesis to preserve their integrity. The local names of called procedure and names of calling procedure are distinctQ.6Explain operand descriptor and register descriptor with exampleAn operand descriptor has the following fields:1. Attributes: Contains the subfields type, length and miscellaneous information2. Addressability: Specifies where the operand is located, and how it can be accessed. It hastwo subfields Addressability code: Takes the values 'M' (operand is in memory), and 'R' (operand is in register). Other addressability codes, e.g. address in register ('AR') and address in memory ('AM'), are also possible, Address: Address of a CPU register or memory word. Ex: a*bMOVER AREG, A MULT AREG, B Three operand descriptors are used during code generation. Assuming a, b to be integers occupying 1 memory word, these are:Register descriptors AttributeAddressability(int, 1)Address(a)(int, 1)Address(b)(int, 1)Address(AREG)A register descriptor has two fields1. Status: Contains the code free or occupied to indicate register status.2. Operand descriptor #: If status = occupied, this field contains the descriptorfor the operand contained in the register. Register descriptors are stored in an array called Register_descriptor. One register descriptor exists for each CPU register.TEJAS PATELPage 7SAFFRONY INSTITUTE OF TECHNOLOGY2150708 System Programming (SP)Compiler In above Example the register descriptor for AREG after generating code for a*b would beOccupied#3 This indicates that register AREG contains the operand described by descriptor #3.Q.7Explain intermediate codes for an expressionThere are two types of intermediate representation1. Postfix notation2. Three address code.1) Postfix notationPostfix notation is a linearized representation of a syntax tree.it a list of nodes of the tree in which a node appears immediately after its children the postfix notation of x=-a*b + -a*b will bex a b * a-b*+=2) Three address codeIn three address code form at the most three addresses are used to represent statement. The general form of three address code representation is -a:= b op cWherea,b or c are the operands that can be names, constants.For the expression like a = b+c+d the three address code will be t1=b+ct2=t1+dHere t1 and t2 are the temporary names generated by the compiler. There are most three addresses allowed. Hence, this representation is three-address code.Q.8Explain implementation of three address codeThere are three representations used for three code such as quadruples, triples and indirect triples.Quadruple representation The quadruple is a structure with at the most tour fields such as op,arg1,arg2 and result. The op field is used to represent the internal code for operator, the arg1 and arg2represent the two operands used and result field is used to store the result of an expression.Consider the input statement x:= -a*b + -a*b:OpArg1Arg2resultt1=uminus a(0)uminusat1t2 := t1 * b(1)*t1bt2t3= - a(2)uminusat3TEJAS PATELPage 8SAFFRONY INSTITUTE OF TECHNOLOGY2150708 System Programming (SP)Compilert4 := t3 * b(3)*t3bt4t5 := t2 + t4(4)+t2t4t5x= t5(5):=t5XTriplesThe triple representation the use of temporary variables is avoided by referring the pointersin the symbol table.the expression x : = - a * b + - a * b the triple representation is as given belowNumberOpArg1Arg2(0)uminusa(1)*(0)b(2)uminusa(3)*(2)b(4)+(1)(3)(5):=X(4)Indirect TriplesThe indirect triple representation the listing of triples is been done. And listing pointers are used instead of using statements.NumberOpArg1Arg2(0)uminusa(1)*(11)b(2)uminusa(3)*(13)b(4)+(12)(14)(5):=X(15) Statement(0)(11)(1)(12)(2)(13)(3)(14)(4)(15)(5)(16)Q.9Explain code optimization methodsI.Compile Time EvaluationCompile time evaluation means shifting of computations from run time to compilation.There are two methods used to obtain the compile time evaluation.TEJAS PATELPage 9SAFFRONY INSTITUTE OF TECHNOLOGY2150708 System Programming (SP)Compiler1. FoldingIn the folding technique the computation of constant is done at compile time instead of run time.example : length = (22/7) * dHere folding is implied by performing the computation of 22/7 at compile time2. Constant propagationIn this technique the value of variable is replaced and computation of an expression is done at the compilation time.example :pi = 3.14; r = 5; Area = pi * r * rHere at the compilation time the value of pi is replaced by 3.14 and r by 5 then computation of 3.14 * 5 * 5 is done during compilation.II.Common Sub Expression EliminationThe common sub expression is an expression appearing repeatedly in the program which is computed previously.Then if the operands of this sub expression do not get changed at all then result of such sub expression is used instead of recomputing it each timeExample: t1 := 4 * i t2 := a[t1]t3 := 4 * j t4 : = 4 * i t5:= nt6 := b[t4]+t5The above code can be optimized using common sub expression elimination t1=4*it2=a[t1] t3=4*j t5=n t6=b[t1]+t5The common sub expression t4:= 4 * i is eliminated as its computation is already in t1 and value of i is not been changed from definition to use.}III.Loop invariant computation (Frequency reduction)Loop invariant optimization can be obtained by moving some amount of code outside the loop and placing it just before entering in the loop.This method is also called code motion.TEJAS PATELPage 10SAFFRONY INSTITUTE OF TECHNOLOGY2150708 System Programming (SP)CompilerExample:while(iTheboyateanappleTEJAS PATELPage 7SAFFRONY INSTITUTE OF TECHNOLOGY2150708 System Programming (SP)Language ProcessorQ.6 Give classification of grammarsType-0 grammars This grammar known as phrase structure grammar or unrestricted grammar, contains productionof the formA->XWhere A and X can be strings of Ts and NTs.Type-1 grammarThis grammar is also known as context sensitive grammar because their productions specify that derivation or reduction of strings can take place only in specific contexts.A grammar G is said to context sensitive if all the production are in the form of X->YWhereX->combination of T and NT with at least one NTY->combination of T and NT and should be non empty. Length of Y must be greater than or equal to X.Type-2 grammar Type2 grammar is also called context free grammar. A grammar is said to be context free grammar if all the production in the form A->X Where A-> single NTX-> combination of T and NT. Type-3 grammar (regular grammar) Left linear grammar A grammar is said to be left linear grammar if the leftmost character symbol of RHS of production rule is NT. A->Ba | a Right linear grammar A grammar is said to be right linear grammar if the rightmost character symbol of RHS of production rule is NT. A->a | aB Operator Grammar An operator grammar is the grammar none of whose production contains two or more consecutive NT, in any RHS alternative.TEJAS PATELPage 8SAFFRONY INSTITUTE OF TECHNOLOGY2150708 System Programming (SP)Linker & LoaderQ.1Define the following terms1) Translation time address: Translation time address is used at the translation time. Thisaddress is assigned by translator2) Linked time address:Link time address is used at the link time. This address is assigned by linker3) Load time address:Load time address is used at the load time. This address is assigned by loader4) Translated origin: Address of origin assumed by the translator5) Linked origin: Address of origin assumed by the linker while producing a binary program6) Load origin: Address of origin assumed by the loader while loading the program for execution.Q.2Describe in detail how relocation and linking is performed.Program relocation is the process of modifying the addresses used in the address sensitive instruction of a program such that the program can execute correctly from the designated area of memory.If linked origin translated origin, relocation must be performed by the linker. If load origin linked origin, relocation must be performed by the loader.Let AA be the set of absolute address - instruction or data addresses used in the instruction of a program P.AA implies that program P assumes its instructions and data to occupy memory words with specific addresses.Such a program called an address sensitive program contains one or more of the following:o An address sensitive instruction: an instruction which uses an address i AA. o An address constant: a data word which contains an address i AA.An address sensitive program P can execute correctly only if the start address of the memory area allocated to it is the same as its translated origin.To execute correctly from any other memory area, the address used in each address sensitive instruction of P must be corrected.Performing relocationLet the translated and linked origins of program P be t_originp and l_originp, respectively. Consider a symbol symb in P.Let its translation time address be tsymb and link time address be lsymb. The relocation factor of P is defined asRelocation _factorp=l_originp-t_originp.....(1)Note that relocation_factorp can be positive, negative or zero.Consider a statement which uses symb as an operand. The translator puts the address tsymb in the instruction generated for it. Now,TEJAS PATELPage 1SAFFRONY INSTITUTE OF TECHNOLOGY2150708 System Programming (SP)Linker & LoaderTsymb= t_originp + dsymbWhere dsyml_bis the offset of symb in P. Hence lsymb = l_originp + dsymbUsing (1),lsymb = t_originp + Relocation _factorp + dsymb = t_originp + dsymb+ Relocation _factorp = tsymb + Relocation _factorp.....(2)Let IRPp designate the set of instructions requiring relocation in program P. Following (2) , relocation of program P can be performed by computing the relocation factor for P and adding it to the translation time address(es) in every instruction i IRPp.LinkingConsider an application program AP consisting of a set of program units SP = {Pi}.A program unit Pi interacts with another program unit Pj by using addresses of Pjs instructions and data in its own instructions.To realize such interactions, Pj and Pi must contain public definitions and external references as defined in the following: (Explain public definition and external reference)o Public definition: a symbol pub_symb defined in a program unit which may be referenced in other program units.o External reference: a reference to a symbol ext_symb which is not defined in the program unitQ.3What is program relocation? Explain characteristics of self-relocating programs.Definition (program relocation): Program relocation is the process of modifying the addresses used inthe address sensitive instruction of a program such that the program can execute correctly from thedesignated area of memory.Self Relocating ProgramsA self relocating program is a program which can perform the relocation of its own address sensitive instructions.It contains the following two provisions for this purpose:o A table of information concerning the address sensitive instructions exists as a part of the program.o Code to perform the relocation of address sensitive instructions also exists as a part of the program. This is called the relocating logic.The start address of the relocating logic is specified as the execution start address of the program.Thus the relocating logic gains control when the program is loaded in memory for the execution.It uses the load address and the information concerning address sensitive instructions toTEJAS PATELPage 2SAFFRONY INSTITUTE OF TECHNOLOGY2150708 System Programming (SP)Linker & Loaderperform its own relocation.Execution control is now transferred to the relocated program.A self relocating program can execute in any area of the memory.This is very important in time sharing operating systems where the load address of a program is likely to be different for different executions.Q.4Explain design of linker1. Relocation & linking requirement in segmented addressingUse of the segmented addressing structure reduces the relocation requirement of a program.Sr.No.0001DATA_HERE 0002ABC0003B0012SAMPLE 00130014 0015 0016 00170027A statementSEGMENT DWDW?SEGMENT ASSUMEMOV MOV JMP MOVMOV Offset0000 250002CS:SAMPLEDS:DATA_HEREAX, DATA_HERE0000 DS, AX0003 A0005 AL, B0008AX, BX01960043SAMPLEENDS 0044 ENDConsider the above program, the ASSUME statement declares the segment register CS and DS to be available for memory addressing.Hence all memory addressing is performed using suitable displacement for their contents.Translation time address of A is 0196. In statement 16, a reference to A is assembled as a displacement of 196 form the content of the CS register.This avoids the use of an absolute address; hence the instruction is not address sensitive. Now no relocation is needed is segment SAMPLE is to be loaded with the address 2000 because the CS register would be loaded with the address 2000 by a calling program.The effective operand address would be calculated as +0196, which is the correct address 2196.A similar situation exists with the reference to B in statement 17. The reference to BTEJAS PATELPage 3SAFFRONY INSTITUTE OF TECHNOLOGY2150708 System Programming (SP)Linker & Loaderis assembled as a displacement of 0002 from the content of DS register.Since the DS register would be loaded with the execution time address of DATA_HERE, the reference to B would be automatically relocated to correct address.2. Linking requirementIn FORTRAN all program units are translated separately, hence all sub program calls and common variable references require linking.Pascal procedures are typically nested inside the main program; hence procedure references do not require linking.In C, program files are program files translated separately so, only function calls that cross file boundaries and references to global data require linking.A name table (NTAB) is defined for use in program linking. Each entry of the table contains the following fields:Symbol: symbolic name of an external reference or an object module Linked_address: for a public definition, this field contains linked address of the symbol. For an object module, it contains the linked origin of the object module.Q.5Write a brief note on MS-DOS linkerWe discuss the design of a linker for the Intel 8088/80x86 processors which resembles LINK of MS DOS in many respects.It may be noted that the object modules of MS DOS differ from the Intel specifications in some respects.Object Module Format (Explain object module of the program)An Intel 8088 object module is a sequence of object records, each object record describing specific aspects of the programs in the object module.There are 14 types of object records containing the following five basic categories of information:o Binary image (i.e. code generated by a translator) o External referenceso Public definitionso Debugging information (e.g. line number in source program).o Miscellaneous information (e.g. comments in the source program).We only consider the object records corresponding to first three categories-a total of eight object record types.Each object record contains variable length information and may refer to the contents of previous object records.Each name in an object record is represented in the following format:length( 1 byte)nameTHEADR, LNAMES and SEGDEF recordsTEJAS PATELPage 4SAFFRONY INSTITUTE OF TECHNOLOGY2150708 System Programming (SP)Linker & LoaderTHEADR record80HlengthT-module namecheck-sumThe module name in the THEADR record is typically derived by the translator from the source file name.This name is used by the linker to report errors.An assembly programmer can specify the module name in the NAME directive.LNAMES record96Hlengthname-listcheck-sumThe LNAMES record lists the names for use by SEGDEF records.SEGDEF record98Hlengthattributes(1-4)segment length(2)name index(1)check-sumA SEGDEF record designates a segment name using an index into this list.The attributes field of a SEGDEF record indicates whether the segment is relocatable or absolute, whether (and in what manner) it can be combined with other segments, as also the alignment requirement of its base address (e.g. byte, word or paragraph, i.e. 16 byte, alignment).Stack segments with the same name are concatenated with each other, while common segments with the same name are overlapped with one another.The attribute field also contains the origin specification for an absolute segment.EXTDEF and PUBDEF record8CHlengthexternal reference listcheck-sum90Hlengthbase(2-4)nameoffsetcheck-sumThe EXTDEF record contains a list of external references used by the programs of this module.A FIXUPP record designates an external symbol name by using an index into this list.A PUBDEF record contains a list of public names declared in a segment of the object module. The base specification identifies the segment.Each (name, offset) pair in the record defines one public name, specifying the name of the symbol and its offset within the segment designated by the base specification.LEDATA recordsA0Hlengthsegment index(1-2)data offset(2)datacheck-sumAn LEDATA record contains the binary image of the code generated by the language translator.Segment index identifies the segment to which the code belongs, and offset specifies theTEJAS PATELPage 5SAFFRONY INSTITUTE OF TECHNOLOGY2150708 System Programming (SP)Linker & Loaderlocation of the code within the segment.FIXUPP record9CHlengthlocat(1)fixdat (1)framedatum (1)targetdatum (1)targetoffset (2)check-sumA FIXUPP record contains information for one or more relocation and linking fixups to be performed.Loc codeMeaning0Low order byte is to be fixed.1Offset is to be fixed.2Segment is to be fixed.3Pointer (i.e., segment: offset) is to be fixed.The locat field contains a numeric code called loc code to indicate the type of a fixup. The meanings of these codes are given in Tablelocat also contains the offset of the fixup location in the previous LEDATA record.The frame datum field, which refers to a SEGDEF record, identifies the segment to which the fixup location belongs.The target datum and target offset fields specify the relocation or linking information.Target datum contains a segment index or an external index, while target offset contains an offset from the name indicated in target datum.The fix dat field indicates the manner in which the target datum and target offset fields are to be interpreted.The numeric codes used for this purpose are given in below table.codecontents of target datum and offset fields0Segment index and displacement.2External index and target displacement.4Segment index (offset field is not used).6External index (offset field is not used).MODEND record8AHlengthtype (1)start addr (5)check-sumThe MODEND record signifies the end of the module, with the type field indicating whether it is the main program.This record also optionally indicates the execution start address.This has two components: (a) the segment, designated as an index into the list of segment names defined in SEGDEF record(s), and (b) an offset within the segment.TEJAS PATELPage 6SAFFRONY INSTITUTE OF TECHNOLOGY2150708 System Programming (SP)Linker & LoaderQ.6What is an overlay? Explain overlay structured program and its execution.An overlay is part of a program (or software package) which has the same load origin as some other part of the program.Overlay is used to reduce the main memory requirement of a program.Overlay structured programWe refer to a program containing overlays as an overlay structured program. Such a program consists ofo A permanently resident portion, called the root. o A set of overlays.Execution of an overlay structured program proceeds as follows:To start with, the root is loaded in memory and given control for the purpose of execution. Other overlays are loaded as and when needed.Note that the loading of an overlay overwrites a previously loaded overlay with the same load origin.This reduces the memory requirement of a program.It also makes it possible to execute programs whose size exceeds the amount of memory which can be allocated to them.The overlay structure of a program is designed by identifying mutually exclusive modules that is, modules which do not call each other.Such modules do not need to reside simultaneously in memory.Execution of an overlay structured programFor linking and execution of an overlay structured program in MS DOS the linker produces a single executable file at the output, which contains two provisions to support overlays.First, an overlay manager module is included in the executable file. This module is responsible for loading the overlays when needed.Second, all calls that cross overlay boundaries are replaced by an interrupt producing instruction.To start with, the overlay manager receives control and loads the root. A procedure call which crosses overlay boundaries leads to an interrupt.This interrupt is processed by the overlay manager and the appropriate overlay is loaded into memory.When each overlay is structured into a separate binary program, as in IBM mainframe systems, a call which crosses overlay boundaries leads to an interrupt which is attended by the OS kernel.Control is now transferred to the OS loader to load the appropriate binary program.Q.7TEJAS PATELPage 7SAFFRONY INSTITUTE OF TECHNOLOGY2150708 System Programming (SP)Linker & LoaderExplain different loading scheme1) Compile & go loaderAssembler is loaded in one part of memory and assembled program directly into their assigned memory locationAfter the loading process is complete, the assembler transfers the control to the starting instruction of the loaded program.AdvantagesThe user need not be concerned with the separate steps of compilation, assembling, linking, loading, and executing.Program loader in memoryAssemblerExecution speed is generally much superior to interpreted systems. They are simple and easier to implement.Source program Compiler & go assemblerDisadvantagesThere is wastage in memory space due to the presence of the assembler. The code must be reprocessed every time it is run.2) Absolute loaderIt is a simple type of loader scheme which fits object code into main memory without relocation.This load accepts the machine text and placed into main memory at location prescribe by the translator.AdvantageVery simple DisadvantageProgrammer must specify load addressIn multiple subroutines environment programmer requires to do linking. 3) Subroutine linkage loaderA program unit Pi interacts with another program unit Pj by using address of Pj s instruction and data in its own instruction.To realize such instruction pj an dpi must contain public definitions and external reference Public definition: program unit which may be referenced in other program unitExternal reference: This is not defined in program unit containing the reference. ENTRY statement: this list the public definition of the program unit.EXTRN statement: lists the symbol in which external references are made in the programTEJAS PATELPage 8SAFFRONY INSTITUTE OF TECHNOLOGY2150708 System Programming (SP)Linker & Loaderunit.4) Relocating loader (BSS loader)To avoid possible assembling of all subroutine when a single subroutine is changed and to perform task of allocation and linking for the programmer, the general class of relocating loader was introduced.Binary symbolic loader (BSS) is an example of relocating loader. The output of assembler using BSS loader is1. Object program2. Reference about other program to be accessed 3. Information about address sensitive entities.Let us consider a program segment as shown belowOffset=10ADD AREG,X Offset=30X DS 1In the above program the address of var5iable X in the instruction ADD AREG, X will be 30If this program is loaded from the memory location 500 for execution then the address of X in the instruction ADD AREG, X must become 530.Offset=10Offset=30 ADD AREG,XX DS 1 500ADD AREG,X530X DS 1Use of segment register makes a program address insensitiveActual address is given by content of segment register + address of operand in instruction So, 500+30=530 is actual address of variable X.5) Direct linking loaderIt is a general re-locatable loader and is perhaps the most popular loading scheme presently used.AdvantagesAllowing multiple segmentsAllowing multiple data segmentFlexible intersegment referencing Accessing abilityRelocation facility DisadvantageNot suitable in multitaskingTEJAS PATELPage 9SAFFRONY INSTITUTE OF TECHNOLOGY2150708 System Programming (SP)Linker & Loader6) Dynamic loaderIt uses overlay structure schemeIn order for the overlay structure to work, it is necessary for the module loader to load their various procedures as they are needed.The portion of the loader that actually interprets the calls and loads the necessary procedure is called overlay supervisor or flipper.This overlay scheme is called dynamic loading or load on call( LOCAL)Q.8An algorithm for first pass of a linker1. Extract load_origin from the command line.2. Repeat step 3 for each module to be linked.3. Select the next object module from the command line. For each record in the object module:(a) If an LNAMES record then enter the name of the module in the name directory (NAMED).(b) If a SEGDEF record then(i)i= name index from the record(ii) segment_name= NAMED [i](iii) If an absdute segment then enter (segment_name, segment_addr) in ESD.(iv) If the segment is relocatable then-Align load_origin with the next paragraph. It should be multiple of lb.-Enter (segment_name, load_origin) in ESD.-Load_origin= load_origint segment length.(c) If a PUBDEF record then(i) i= base(ii) Segment_name= NAMED [i] Symbol = name(iii) Segment_addr= load address of segment_name in ESD(iv) SymboLaddr= segment_addr+ offset(v) Enter (symbol, s)'mbol_addr) in ESD.Q.9Object module of linkerThe objet module of a program contains all information necessary to relocate and link the program with other programs.The object module of a program P consists of 4 components:1. Header: The header contains translated origin, size and execution start address of P. 2. Program: This component contains the machine language program corresponding to P.3. Relocation table: (RELOCTAB) This table describes IRRP. Each RELOCTAB entry contains aTEJAS PATELPage 10SAFFRONY INSTITUTE OF TECHNOLOGY2150708 System Programming (SP)Linker & Loadersingle field:Translated address: Translated address of an address sensitive instruction.4. Linking table (LINKTAB): This table contains information concerning the public definitions and external references in P.Each LINKTAB entry contains three fields: Symbol:Symbolic nameType:PD/EXT indicating whether public definition or external referenceTranslated address:For a public definition, this is the address of the first memory word allocated to the symbol. For an external reference, it is the address of the memory word which is required to contain the address of the symbol.Example:StatementAddressCodeSTART ENTRY EXTRN READLOOP .. .MOVER BC. . . BCSTOP ADS TOTALDSEND 500 TOTALHAX, ALPHAA 500) 501)AREG, ALPHA518)ANY, HAX519)LT, LOOP 538) 539)1540)1541) + 09 0 540+ 04 1 000+ 06 6 000+ 06 1 601+ 00 0 0001. Translated origin=500, size=42, execution start address=500. 2. Machine language instruction shown in code3. Relocation table 5005384. Linking tableALPHAEXT518MAXEXT519APD540TEJAS PATELPage 11SAFFRONY INSTITUTE OF TECHNOLOGY2150708 System Programming (SP)MacroProcessorsQ.1Explain macro, macro definition and Macro callMacro: macro is a unit of specification for program generation through expansion.Macro definition: macro definition is enclosed between macro header and macro end statement Macro definition consist of1. Macro prototype statement: it declares macro name and formal parameter list2. One or more model statement: from which an assembly statement can be generated 3. Macro preprocessor statement: used to perform auxiliary functionMacro call:A macro is called by writing macro name in the mnemonics field and set of actual parameters.[]Q.2Explain macro expansionA macro call leads to macro expansion. During macro expansion, the macro call statement is replacedby a sequence of assembly statements.Each expanded statement is marked with a + preceding its label field. Two key notations concerning macro expansion are:1. Expansion time control flow 2. Lexical Substitution1. Flow of control during expansionThis determines the order in which model statements are visited during macro expansion. Default flow of control during macro expansion is sequential.A preprocessor statement can alter flow of control during expansion such that model statements are never visited during expansion (conditional expansion) or repeatedly visited during expansion (expansion time loop).The flow control during macro expansion is implemented using a macro expansion counter(MEC) Algorithm:1. MEC:= statement number of first statement following the prototype statement; 2. While statement pointed by MEC is not a MEND statement(a) If a model statement then (i) Expand the statement. (ii) MEC:= MEC+1;(b) Else (i.e. a preprocessor statement)(i) MEC:= new value specified in the statement; 3. Exit from macro expansionMEC is set to point at the statement following the prototype statement. It is incremented by 1after expanding a model statementTEJAS PATELPage 1SAFFRONY INSTITUTE OF TECHNOLOGY2150708 System Programming (SP)MacroProcessors2. Lexical substitutionAmodel statement consist of 3 types of strings:1. An ordinary string, which stand for itself2. Name of formal parameter which is preceded by the character &. 3. Name of preprocessor variable, is preceded by the character &.During lexical expansion, strings of type 1 are retained without substitution. Strings of types 2 and 3 are replaced by the values of the formal parameters or preprocessor variables.2.1 Positional parametersA positional formal parameter starts with '&' sign and it is defined in operand field of macro name.The actual parameters of macro call on macro using positional parameters are simply ordinary string.The value of first actual parameter of macro call is assigned to first positional formal parameter defined in operand field of macro name.The value of second actual parameter of macro call is assigned to second positional" formal parameter defined in operand field of macro name.Similarly the value of nth actual parameter is assigned to nth positional formal parameter defined in operand field of macro name.Positional parameter is always used at the place of operand2. Value of positional parameter should not be keywords.2.2 Keyword parametersA keyword formal parameter starts with &KW string or &OP string or &REG or &CC depending on macro processor. It is defined in operand field of macro name.A keyword formal parameter ends with = sign depending on macro processor. It is defined in operand field of macro name.Formal keyword parameter mayor may not have default value. Again this is depends on macro processor.The actual parameter of macro call on macro using keyword parameter is simply ordinary string if they are used as positional parameters.Keyword parameter is always used at the place of mnemonic instruction or at the place of operand 1.Value of keyword parameter is always keywords. That are ADD, SUB, AREG, BREG, LT, LE etc. 2.3 Label parametersA label formal parameter starts with &LAB string depending on macro processor. It is defined in operand field of macro name.A label formal parameter ends with = sign depending on macro processor. It is defined in operand field of macro name.Every label formal parameter should not have any default value. Again this depends on macro processor.The actual parameter of macro call on macro using label parameter is simply ordinary string if they are used as a positional parameter.TEJAS PATELPage 2SAFFRONY INSTITUTE OF TECHNOLOGY2150708 System Programming (SP)MacroProcessorsLabel parameter is always used at the place of label field. Value of label parameter should not be keyword.2.4 Macros with mixed parameters listsA macro may be defined to all parameters i.e. positional parameter, keyword parameter and label parameterQ.3Explain types of parameterPositional Parameter A positional formal parameter is written as & The value of a positional parameter XYZ is determined by the rule of positional association asfollows:1. Find the original position of XYZ in the list of formal parameter in the macro prototype statement.2. Find the actual parameter specification occupying the same ordinal position in the list of actual parameter in macro call statement.Keyword parameter Keyword parameters are used for following purposes: 1. Default value can be assigned to the parameter2. During a macro call, a keyword parameter is specified by its name. it takes the following form:=MACROINCR &VARIABLE=X, &INCR=Y, &REG=AREGMEND VARIABLE is a keyword parameter with default value as X INCR is a keyword parameter with default value as Y REG is a keyword parameter with default as AREG The position of keyword parameter during macro call is not important.Q.4Compare the features of subroutine and macros with respect to following: (i) ExecutionSpeed (ii) Processing requirement by assembler (iii) Flexibility and generalityMacros use string replacement for its invocation whereas subroutines use calls.Due to replacement nature, macro can exist multiple copies in the programs whereas subroutines can exist only in one copy.Because of multiple copies possibility, you cannot obtain a macros address, whereas you can obtain a subroutines address.Macros can be faster since it doesnt have calling and return time penalty.Macros can be harder to debug since the replacement may be obstacle in the resulting code.TEJAS PATELPage 3SAFFRONY INSTITUTE OF TECHNOLOGY2150708 System Programming (SP)MacroProcessors(i) Execution speedMACRO-At the time of execution each and every - SUBROUTINEAt the time of execution, execution controlmacro call replaced with macro definition i.e.transfers to the subroutine and afterit expands main programexecution of subroutine it returns to themainprogramagainandexecuting remaining instructions.-This process not required any stack -This process requires stack manipulation manipulation operation during the executionoperation during the execution of program. of program -That means it stores current address instack and the execution control goes to sub routine, after execution of subroutine it pop address from stack and return to the main program.-It requires extra processing time for -It not requires extra processing time forexpansion but at once.-Speed of its object code is very fast becauseit not requires any stack manipulation. expansion but every time at each subroutinecall it requires stack manipulation operation. -Speed of its object code is becomes slowbecause it requires stack manipulation ateach subroutine.(ii)Processing requirement by assemblerMACRO-In assembly level macro should be defined before main program.-In high level macro can be defined anywhere i.e. before or after the main program. SUBROUTINE-In assembly level as well as in high level subroutine can be defined anywhere.-i.e. before or after the main program. Thisdepends on high level language.-macro statement in assembly language is asfollows -subroutine call in assembly language isfollows[Label][]-Example: [Label]-Example:FACTORIALA,FACTCALL FACTORIALWhere FACTORIAL is the name of macro andWhereFACTORIAListhenameof A, FACT is the list of actual parameters.subroutine.(iii)Flexibility and generalityMACROSUBROUTINE-In assembly level programming looping and-We can use looping and nesting innested looping like facilities used in macro -In high level programming looping andnested looping should not be used subroutines in low level as well as in highlevel.TEJAS PATELPage 4SAFFRONY INSTITUTE OF TECHNOLOGY2150708 System Programming (SP)MacroProcessors-Its object code requires large amount ofmain memory as well as secondary memory -Its object code requires less amount of mainmemory as well as secondary memory-It is suitable in real time operating system or -It is not suitable in real time operatingenvironment-Here time factor is more important thanspace system or environment-Here space factor is more important thantimeQ.5Explain nested macro calls ORDefine two macros of your choice to illustrate nested calls to these macros. Also show theircorresponding expansion.A model statement in a macro may constitute a call on another macro. Such calls are known as nested macro calls.We refer to the macro containing the nested call as the outer macro and the called macro as the inner macro.Expansion of nested macro calls follows the last-in-first-out (LIFO) rule. Thus, in a structure of nested macro calls, expansion of the latest macro call (i.e. the innermost macro call in the structure) is completed first.ExampleThe below defined is the definition of INCR_D macro.MACROINCR_D&MEM_VAL=,&INCR_VAL=, &REG=AREG MOVER&REG, &MEM_VALADD&REG, &INCR_VAL MOVEM&REG, &MEM_VAL MENDMacro COMPUTE defined below contains a nested call on macro INCR_D defined above.+MOVEMBREG, TMP+MOVERBREG, X+ADDBREG, Y+MOVEMBREG, X+MOVERBREG, TMPMACROCOMPUTE&FIRST, & SECONDMOVEMBREG, TMPINCR_D&FIRST, & SECOND, REG=BREGMOVERBREG, TMPMENDTEJAS PATELPage 5SAFFRONY INSTITUTE OF TECHNOLOGY2150708 System Programming (SP)MacroProcessorsThe expanded code for the callCOMPUTEX, Yis described as follows.+ MOVEM BREG TEMP[1]+ MOVER BREG, X 2 COMPUTE X , Y+ INCR_DX,Y+ ADD BREG, Y 3+ MOVEM BREG, X 4 + MOVER BREG,TEMP[5]Q.6Advanced macro facilities1. Alteration of flow of control during expansionExpansion time statement: OR (Explain expansion time statements AIF and AGO for macroprogramming)AIFAn AIF statement has the syntax:o AIF () where is a relational expression involving ordinary strings, formal parameters and their attributes, and expansion time variables.If the relational expression evaluates to true, expansion time control is transferred to the statement containing in its label field.AGOAn AGO statement has the syntax: o AGO It unconditionally transfers expansion time control to the statement containing in its label field.Expansion time loopsor (Explain expansion time loop)It is often necessary to generate many similar statements during the expansion of a macro. This can be achieved by writing similar model statements in the macro.Expansion time loops can be written using expansion time variables (EVs) and expansion time control transfer statements AIF and AGO.Example&M.MORE&M MACROCLEAR&X, &NLCL&MSET0MOVERAREG, =0 MOVEMAREG, &X+&M SET&M + 1AIF(&M NE N) .MORETEJAS PATELPage 6SAFFRONY INSTITUTE OF TECHNOLOGY2150708 System Programming (SP)MacroProcessorsMENDThe LCL statement declares M to be a local EV.At the start of expansion of the call, M is initialized to zero.The expansion of model statement MOVEM, AREG, &X+&M thus leads to generation of the statement MOVEM AREG, B.The value of M is incremented by 1 and the model statement MOVEM..is expanded repeatedly until its value equals the value of N.2. Expansion time variable or (Explain expansion time variable with example)Expansion time variables (EV's) are variables which can only be used during the expansion of macro calls.A local EV is created for use only during a particular macro call.A global EV exists across all macro calls situated in a program and can be used in any macro which has a declaration for it.Local and global EV's are created through declaration statements with the following syntax: o LCL [, .. ]o GBL [, .. ] has the syntax &, where is an ordinary string. Values of EV's can be manipulated through the preprocessor statement SET.A SET statement is written as:o < EV specification > SET where< EV specification > appears in the label field and SET in mnemonic field.A SET statement assigns value of to the EV specified in < EV specification >.ExampleMACRO CONSTANTSLCL&A &ASET1DB&A &ASET&A+lDB&A MENDThe local EV A is created.The first SET statement assigns the value '1' to it.The first DB statement thus declares a byte constant 1.The second SET statement assigns the value '2' to A and the second DB statement declares a constant '2'.3. Attributes of formal parameterAn attribute is written using the syntax It represents information about the value of the formal parameter, i.e. about theTEJAS PATELPage 7SAFFRONY INSTITUTE OF TECHNOLOGY2150708 System Programming (SP)MacroProcessorscorresponding actual parameter.The type, length and size attributes have the names T, L and S.Example.NEXT MACRODCL_CONST AIF------MEND &A(L'&A EQ 1) .NEXTHere expansion time control is transferred to the statement having .NEXT field only if the actual parameter corresponding to the formal parameter length of ' 1'.Q.7Explain lexical and semantic expansion ORExplain tasks involved in macro expansion.Lexical expansion:Lexical expansion implies replacement of a character string by another character string during program generation.Lexical expansion is to replace occurrences of formal parameters by corresponding actual parameters.Semantic expansion:Semantic expansion implies generation of instructions tailored to the requirements of a specific usage.Semantic expansion is characterized by the fact that different uses of a macro can lead to codes which differ in the number, sequence and opcodes of instructions.Eg: Generation of type specific instructions for manipulation of byte and word operands.Semantic expansion is the generation of instructions tailored to the requirements of a specific usage.It can be achieved by a combination of advanced macro facilities like AIF, AGO statements and expansion time variables.Here, the number of MOVEM AREG, Statements generated bya call on CLEAR is determined by the value of the second parameter of CLEAR.Macro EVAL of example is another instance of conditional expansion wherein one of two alternative code sequences is generated depending on the peculiarities of actual parameters of a macro call.Below example illustrates semantic expansion using the type attribute. ExampleMACROCREATE_CONST&X, &YTEJAS PATELPage 8SAFFRONY INSTITUTE OF TECHNOLOGY2150708 System Programming (SP)MacroProcessorsAIF&YDW&AAGO .BYTEANOP &YDB.OVERMEND (T &X EQ B) .BYTE25.OVER25This macro creates a constant 25 with the name given by the 2nd parameter. The type of the constant matches the type of the first parameter.Q.8Describe task and data structures considered for the design of a macro preprocessorMacro preprocessorThe macro preprocessor accepts an assembly program containing macro definitions and calls and translates it into an assembly program which does not contain any macro definition or calls.Below figure shows a schematic of a macro preprocessor.The program from output by the macro preprocessor can now be handed over to an assembler to obtain the target form output by macro preprocessor can now be handed over to an assembler to obtain language form of program.Target programProgram WithoutMacrosMacro Pre-Assembler processorProgram with macro definitionand callsFollowing are the task of macro preprocessor: 1. Identify macro calls in the program.2. Determine the values of formal parameters.3. Maintain the values of expansion time variables declared in a macro. 4. Organize expansion time control flow.5. Determine the values of sequencing symbols. 6. Perform expansion of a model statement.Data StructuresTask has identified the key data structures of the macro preprocessor. To obtain a detailed design of the data structures it is necessary to apply the practical criteria of processing efficiency and memory requirements.The tables APT, PDT and EVT contain pairs which are searched using the first component of the pair as a key-for example, the formal parameter name is used as the key to obtain its value from APT. This search can be eliminated if the position of an entity within a table is known when its value is to be accessed. We will see this in the context of APT.The value of a formal parameter ABC is needed while expanding a model statement using it, viz.TEJAS PATELPage 9SAFFRONY INSTITUTE OF TECHNOLOGY2150708 System Programming (SP)MacroProcessorsMOVER AREG, &ABCLet the pair (ABC, ALPHA) occupy entry #5 in APT. The search in APT can be avoided if the model statement appears asMOVER AREG, (P, 5)in the MDT, where(P, 5) stand for the words parameter #5.Thus, macro expression can be made for efficient by storing an intermediate code for a statement, rather than its source form, in the MDT.All parameter names could be replaced by pairs of the form (P, n) in the model statement and preprocessor statement stored in MDT.An interesting offshoot of this decision is that the first component of the pairs stored in APT is no longer used during macro expansion, e.g. the information (P, 5) appearing in a model statement is sufficient to access the value of formal parameter ABC. Hence APT containing (, ) pairs is replaced by another table called APTAB which only contains 's.To implement this simplification, ordinal numbers are assigned to all parameter of a macro. A table named parameter name table (PNTAB) is used for this purpose.Parameter names are entered in PNTAB in the same order in which they appear in the prototype statement.The entry # of a parameter's entry in PNTAB is now its ordinal number. This entry is used to replace the parameter name in the model and preprocessor statements the macro while storing it in the MDT.In effect, the information (, ) in APT been split into two tables -PNTAB which contains formal parameter name.APTAB - which contains formal parameter value.(i.e. contains actual parameter)Other data structures are given below:TableField in each entryMacro name table (MNT)Macro name, Number of positional parameters (#PP),Number of keyword parameters (#KP), "Number ofexpansion time variables (#EV), MDT pointer (MDTP), KPDTAB pointer (KPDTP), SSTAB pointer (SSTP)Parameter Name Table (PNTAB)Parameter nameEV Name Table (EVNTAB)EV nameSS Name Table (SSNTAB)SS nameKeyword Parameter Default Table (KPDTAB)Parameter name, default valueMacro Definition Table (MDT)Label, Opcode, OperandsActual Parameter Table (EVTAB)ValueSS Table (SSTAB)MDT entry #Q.9Explain design specification task for macro preprocessor with suitable exampleTEJAS PATELPage 10SAFFRONY INSTITUTE OF TECHNOLOGY2150708 System Programming (SP)MacroProcessorsDesign OverviewWe begin the design by listing all tasks involved in macro expansion. 1. Identify macro calls in the program.2. Determine the values of formal parameters. 3. Organize expansion time control flow.4. Maintain the values of expansion time variables declared in a macro. 5. Determine the values of sequencing symbols.6. Perform expansion of a model statement.The following 4 step procedure is followed to arrive at a design specification for each task: 1. Identify the information necessary to perform a task.2. Design a suitable data structure to record the information.3. Determine the processing necessary to obtain the information. 4. Determine the processing necessary to perform the task.Application of this procedure to each of the preprocessor tasks is described as follows.Identify macro callsA table called the macro name table (MNT) is designed to hold the names of macros defined in a program. A macro name is entered in this table when a macro definition is processed. While processing a statement in the source program, the preprocessor compares the string found in its mnemonic field with the macro names in MNT. A match indicates that the current statement is a macro call.Determine value of formal parametersA table called the actual parameter table (APT) is designed to hold the values formal parameters during the expansion of a macro call. Each entry in the table is a pair(,)Two items of information are needed to construct this table, names of formal parameters, and default values of keyword parameters. For this purpose, a table called parameter default table (PDT) is used for each macro. This table would be accessible from the MNT entry of a macro and would contain pairs of the form (, ). If a macro call statement does not specify a value for some parameter par, its default value would be copied from PDT to APT.Maintain expansion time variablesAn expansion time variables table(EVT) contains pairs of the form (, )The value field of a pair is accessed when a preprocessor statement or a model statement under expansion refers to an EV.Organize expansion time control fileTEJAS PATELPage 11SAFFRONY INSTITUTE OF TECHNOLOGY2150708 System Programming (SP)MacroProcessorsMacro definition table (MDT) stores set of preprocessor statements and model statements. The flow ofcontrol during macro expansion determines when a model statement is to be visited for expansion. Itis updated after expanding a model statement or on processing a macro preprocessor statement. Determine values of sequencing symbolsA sequencing symbols table(SST) is maintained to hold this information. The table contains pairs of the form(, )where is the number of the MDT entry which contains the model statement defining the sequencing symbol.Perform expansion of a model statement This is a trivial task given the following:1. MEC points to the MDT entry containing the model statement.2. Values of formal parameters and EV's are available in APT and EVT, respectively. 3. The model statement defining a sequencing symbol can be identified from SST.4. Expansion of a model statement is achieved by performing a lexical substitution for the parameters and EV's used in the model statement.Q.10Write a macro that moves n number from the first operand to the second operand, where n is specified as third operand of the macro.MACROMOVEA LLLCL &MSET .NEMOVER XTMOVEM &MSETAIFMEND &source, &dest, &N&M0AREG, &source + &MAREG, &dest + &M&M + 1( &M NE &N) .NEXTQ.11Write a macro which takes B, C and D as parameters and calculates B*C+C*D.MACROEVAL&X, &Y, &Z MOVERAREG, &X MULAREG, &Y MOVEMAREG,&X MOVERAREG, &Y MULAREG, &Z ADDAREG, &X MENDQ.12Draw a flow chart and explain simple one pass macro processor.TEJAS PATELPage 12SAFFRONY INSTITUTE OF TECHNOLOGY2150708 System Programming (SP)MacroProcessorsStartMDTC =1 MNTC =1Read line From sourceIs MacroNo Pseudo upYesRead line From sourceUpdate MNTUpdate PNTAB N IsoENDSearch YesinGo forAssemblFoundYesFind out Definition NoWrite into output source fileRead line From Replace formal parameterWrite into outputMDTC++IsNo MEND? YesIn this type of preprocessor only one pass is used to construct data structure and use that datastructure.It is also called as preprocessor, Because it is processed before translator. It is shown in figure.TEJAS PATELPage 13SAFFRONY INSTITUTE OF TECHNOLOGY2150708 System Programming (SP)MacroProcessorsSource code with macro One pass Macro processor Source code without macroMNTMDTPNTABAPTABSSTABKPPTABData StructureMacro name table (MNT): Figure: One pass macro processorThis is used to store all information of macro definition that is macroname, MDTP, Total number of positional parameters.Macro definition table (MDT): This is used to store all program of macro definition.Parameter name table (PNTAB): This is used to store all positional parameter name of macro definition.Keyword parameter default table (KPDTAB or KPT): This is used to store all keyword parameter name of macro definition with its default values.EV Name table (EVNTAB or EVT): this is used to store all expansion time variable name of macro definition with its type (global or local).SS Name table (SSNTAB): This is used to store all labels of macro definition.SS Table (SSTAB): This is used to store MDT entry where sequencing symbol is defined in MDT.EV Table (EVTAB): This is used to store current all value of the expansion time variables of macro definition.Actual parameter table (APTAB): This is used to store name of actual parameters defined in macro call.Algorithm:Step 1: Initialize all other pointer variables to 1 or 0. MDTP=1, MNTP=1, KPTP=1, LC=1.Step 2: Read LCth line from source code that means input program. Step 3: Isolate label instruction and operand from line.Step 4: If instruction="MACRO" If yes4.1: LC=LC+ 1.4.2: Read LCth line from source code that means input program. 4.3: Isolate label instruction and operand from line.4.4: Enter macro name in MNT.TEJAS PATELPage 14SAFFRONY INSTITUTE OF TECHNOLOGY2150708 System Programming (SP)MacroProcessorsFind out total number of parameter, keyword parameter and expansion time variablesand store it in MNT.Store the value of all pointers in MNT.4.5: Update PNTAB, KPDTAB, EVNTAB, SSNTAB, SSTAB. 4.6: Increments all the pointers of updated tables.4.7: MNTP=MNTP+1. 4.8: LC=LC+1.4.9: Read LCthline from source code that means input program.4.10: Isolate label instruction and operand from line and store it into MDT at MDTP location. 4.11: MDTP=MDTP+ 1.4.12 : If instruction="MEND" If yesGo to step 2. If noGo to step 4.6. If noGo to step 4. Step 5: Search instruction in MNT.Step 6: If instruction found in MNT? If yes6.1: Find out Actual parameter &store it in APTAB. 6.2: Find out MDTP from MNT.6.3: Search macro definition from MDT at MDTP position. 6.4: Adjust all model statements as follows.6.4.1: Replace Actual parameters with formal parameters using PNTAB, KPDTAB, and APTAB. 6.4.2: Replace each expansion time variable name with its value using EVNTAB, EVTAB. 6.4.3: Find out labels from SSNTAB and its address from SSTAB, sequence label withsequence number and replace it in old place. 6.5: Write all these adjusted model statements in output source file. 6.6: LC=LC+1.6.7: Go to step 2. If no6.8: If instruction ="END" If yesGo to Assembler. If noWrite line in output source file LC=LC+1. Go to step 2.TEJAS PATELPage 15SAFFRONY INSTITUTE OF TECHNOLOGY2150708 System Programming (SP)ParsingQ.1What is parsing? Explain types of parsing. Parsing or syntactic analysis is the process of analyzing a string of symbols accordingto the rules of a formal grammar Parsing is a technique that takes input string and produces output either a parse tree if string isvalid sentence of grammar, or an error message indicating that string is not a valid sentence of given grammar. There are mainly two types of parsing1. Top down parsing: A top down parser for a given grammar G tries to derive a string through a sequence of derivation starting with a start symbol.Top down parsing methods are: Top down parsing(with backtracking/ without backtracking) Recursive decent parser LL(1) parser2. Bottom up parsing: In bottom up parsing, the source string is reduced to the start symbol of the grammar. Bottom up parsing method is also called shift reduce parsing. Bottom up parsing methods are: Nave bottom up parsing Operator precedence parsing Q.2Explain parse tree and abstract syntax tree. A set of derivations applied to generate a string can be represented using a tree. Such a tree isknown as a parser tree. While Abstract syntax tree represents the structure of a source string in more economicalmanner. EX: Write unambiguous production rules (grammar) for arithmetic expression containing +, -, *, / and ^ (exponentiation).Construct parse tree and abstract syntax tree for: - * ^ + .(GTU DEC_11) Unambiguous grammar for arithmetic expression containing +, -, *, / and ^E->E-T|TT->T*F|FF->F/G|GG->G^H|H H->H+I|I I->TEJAS PATELPage 1SAFFRONY INSTITUTE OF TECHNOLOGY2150708 System Programming (SP)Parsing Parse treeEETTT*FFFG FGGG^HHHHH+IIIII Abstract Syntax Treeid*id^id+ididQ.3Explain left factoring and left recursion.Left Factoring: For each non-terminal A with two or more alternatives(production rules) with a common nonempty prefix, let sayA->1 |.| n| 1|m Converted it intoA->A| 1|mTEJAS PATELPage 2SAFFRONY INSTITUTE OF TECHNOLOGY2150708 System Programming (SP)ParsingA-> 1 |.| n EX:A->xByA | xByAzA | a B->bLeft factored, the grammar becomes A->xByAA | aA->zA | B-> bLeft Recursion: A grammar is left-recursive if we can find some non-terminal A which will eventually derivea sentential form with itself as the left-symbol. Immediate left recursion occurs in rules of the formA -> Aa|bWhere a andb are sequences of non-terminals and terminals, and b doesn't start withA. For example, the ruleis immediately left-recursive.It could be replaced by the non-left recursive productions as A -> bAA-> aA The general algorithm to remove immediate left recursion follows. A -> Aa1 | Aa2 | Aa3 |...| Aam | b1 | b2 | b3 |....| bnwhere:A is a left-recursive nonterminala is a sequence of non-terminals and terminals that is not null (a )b is a sequence of non-terminals and terminals that does not start with A.replace the A-production by the production:A -> b1A -> bmAAnd create a new nonterminalA->a1AanAQ.4Top down parsing methods1) Nave top down parsing or brute force parsingNaive top down parsing algorithmCurrent sentential form (CSF) = SLet CSF be of the form A, such that is a string of Ts and A is the leftmost NT in CSF. Exit with success if CSF=Make a derivation A->1B according to a production A=1B of G such that 1 is a string of Ts. This makes CSF= 1B.TEJAS PATELPage 3SAFFRONY INSTITUTE OF TECHNOLOGY2150708 System Programming (SP)ParsingGo to step 2. Ex:Consider a given grammar S->aAbA->cd | c derive string acbSSSaAbaAc bbacktrackingd a c b2) Top down parsing without backtracking Elimination of backtracking in top down parsing have several advantages: Parsing would become more efficient and it would be possible to perform semantic action and precise error reporting during parsing We use left factoring to ensure that the RHS alternatives will produce a unique terminal symbol in first position Consider the grammar E-> T+ E | TT-> V*T | V V-> Id Perform left factoring on given grammar Now grammar willE->TEE->+E| T->VTT->*T| V->Id Now parsing of the string +*Sr No.CSFsymbolprediction1EE->TE2TET->VT3VTEV->4TE+T->5E+E->+E6+EE->TE7+TET->VT8+VTEV->9+ TE*T->*TTEJAS PATELPage 4SAFFRONY INSTITUTE OF TECHNOLOGY2150708 System Programming (SP)Parsing10+ *TET->VT11+*V TEV->12+*TE-T->13+*E-E->14+*--3) Recursive decent parser A top down parser that executes a set of recursive procedures to process the inputwithout backtracking is called recursive-decent parser, and parsing is called recursivedecent parsing Ex:S->E E->VEE->+VE | V->Id