Unit 2

2,260 views

Published on

Macro processor, Compilers and Interpreters

Published in: Education, Technology
  • Be the first to comment

Unit 2

  1. 1. Macro processor, Compliers and Interpreters
  2. 2. Macro definition and call, macro expansion, Machine Independentmacro processor features,Nested macro calls, advanced macro facilities, Design of macropreprocessor.preprocessor.Basic compliers function, Phases of compilation, memory allocation,compilation of expression,compilation of expressions, compilation of control structures, code ofoptimization, interpreter.
  3. 3. Macro are used to provide a program generation facility throughmacro expansion.Definition :- A macro is a unit of specification for programgeneration through expansion.A macro consists of a name, set of formal parameter and body ofA macro consists of a name, set of formal parameter and body ofcode.The use of a macro name with a set of actual parameters is replacedby some code generated from its body. This is called macroexpansion
  4. 4. Examples of MacroIf we have to Re-writing a program then with the help of macros we canwrite¡¡.Start of definitionStart of definitionStart of definitionStart of definition MacroMacroMacroMacroMacro name My macroMacro body ADD AREG,XADD BREG, XEnd of the macrodefinitionMENDwrite¡¡.Original program program with macroADD AREG,XADD BREG, XADD AREG,XADD BREG, XADD AREG,XADD BREG, XMy macroMy macroMy macro
  5. 5. A macro processor takes a source with macro definition and macrocalls and replace each with its body.Target codeTarget codeTarget codeTarget codeADD AREG,XADD BREG, XSource programSource programSource programSource programMACROMymacroADD AREG,XADD AREG,XADD BREG, XADD AREG,XADD BREG, XMacroMacroMacroMacroProcessorProcessorProcessorProcessorADD AREG,XADD BREG, XMENDmymacromymacromymacroInputoutput
  6. 6. Two kinds of expansion1. Lexical Expansion2. Semantic ExpansionLexical Expansion :- Lexical Expansion implies replacement ofcharacter string by another character string during programcharacter string by another character string during programgeneration .Lexical expansion employed to replace occurrences offormal parameters by corresponding actual parameters.
  7. 7. Semantic expansion : Semantic expansion implies generation ofinstructions tailored to the requirements of specific usage. ForExamples Generation of type specific instructions for manipulationof byte and words operands.
  8. 8. A macro is a set of tasks combined together so that you can run orreplay the entire task together with a single command. Macros are apowerful productivity tool.With macros you can perform long or boring tasks just by a singleclick.click.Macro allows a sequence of source language code to be defined onceand then referred to by name each time it is to be referred..
  9. 9. A macro definition is enclosed between a macro header statementand macro end statement. Macro definition are typically located atthe start of a program. A macro definition consists ofA macro prototype statementOne or more model statementsOne or more model statementsMacro preprocessor statements
  10. 10. A macro prototype statement:- The Macro protoype statementdeclares the name of macro and kinds of its parameters.One or more model statements: A model statement is a statementfrom which an assembly language statement may be generatedduring macro expansion.during macro expansion.Macro preprocessor statements: A preprocessor statement is used toperform auxiliary functions during macro expansion.
  11. 11. Macros are typically defined at a start of a program. A macrodefinitionconsists of(1) MACRO pseudo opcode.(2) MACRO name.(3) Sequence of statements to be abbreviated.(4) MEND pseudo opcode terminating macro definition.The structure of a macro is:The structure of a macro is:%MACRO macro_name;<macro_text>%MEND <macro_name>;Example:MACROINCR &ARGADD AREG ,& ARGADD BREG ,& ARGADD CREG ,& ARGMEND
  12. 12. A macro is called by writing macro name with actual parameters inan assembly program.The macro call has following syntax:< macro name > [ < list of parameters > ]For example,For example,INCR XWill call the macro INCR with the actual parameter X.
  13. 13. A macro call leads to macro expansion .During macro expansion, the macro call statement is replaced by asequence of assembly statements.To differentiate between the original statements of program and theTo differentiate between the original statements of program and thestatement resulting from macro expansion ,each expanded statementis marked with a ‘+’ preceding its label filed.Two key notions concerning macro expansion are:1.Expansion time control flow2.Lexical substitution
  14. 14. 1.Expansion time control flow:- This determine the order in which modelstatement are visited during macro expansionThe flow of control during macro expansion is implemented using a macroexpansion counter(MEC).Algorithm: (outline of macro expansion)1. MEC: statement number of first statement following the prototype statement2. While statement pointed by MEC is not a MEND statement2. 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 preprocessors statement )(i) MEC:= new value specified in the statement ;3. Exit from macro expansion .
  15. 15. 2.Lexical substitution :- Lexical substitution is used to generate anassembly statement from a model statement.A model statement consists of 3 types of string(1) An ordinary string, which stands for itself(2) The name of Formal parameter which is preceded(2) The name of Formal parameter which is precededby the character ‘&’.(3) The name of a preprocessor variable , which isalso preceded by the character ‘&’
  16. 16. A macro may be defined with:(1) Positional parameters.(2) Keyword parameters.Positional Parameters: A positional parameter is written as & parameter_name.For example , INCR &VARIABLE.Keyword Parameters: During call to a micro , a keyword parameter is specifiedKeyword Parameters: During call to a micro , a keyword parameter is specifiedby its name.It takes following form:< parameter name > = < parameter value >
  17. 17. DEFINATION:-A model statement in a macro may constitute a call onanother macro i.e. macro call within a macro. Such calls are knownas “Nested Macro Calls”.
  18. 18. Macro containing the nested call is known as “Outer Macro". Acalled macro is known as “Inner macro”.Expansion of nested macro calls follows “last-in- first-out(LIFO)rules". Thus inner structure of nested macro calls ,the expansion oflatest macro call is completed first.latest macro call is completed first.`
  19. 19. Example:-Consider program segment :-MACROCOMPUTE &ARGMOVER AREG ,&ARGADD AREG ,=‘1’MOVEN AREG,&ARGMENDMACROCOMPUTE &ARG1,ARG2,ARG3COMPUTE &ARG1,ARG2,ARG3COMPUTE &ARG1COMPUTE &ARG2COMPUTE &ARG3MENDThe definition of macros “COMPUTE1” contains three separate calls to a define macro“ COMPUTE”.Such macros are expanded on multiple levels.Expansion of “COMPUTE1 x,y,z” as follows:-
  20. 20. Source line expanded source expanded source (level 2)(level 1)COMPUTE1 X,Y,Z COMPUTE X MOVER AREG,XCOMPUTE Y ADD AREG,=‘1’COMPUTE Z MOVEM AREG ,XMOVER AREG,YMOVER AREG,YADD AREG,=‘1’MOVEM AREG ,YMOVER AREG,ZADD AREG,=‘1’MOVEM AREG ,Z
  21. 21. Advanced macro facilities are aimed at supporting semantic expansion. These facilities canbe grouped into :1) Facilities for alteration of flow of control during expansion.2) Expansion time variables.3) Attributes of parameters.
  22. 22. Two features are provided to facilitate alteration of flow of controlduring expansion:-1)Expansion time sequencing symbols.2)Expansion time statements AIF,AGO and ANOP.AIF is similar to IF statement,the label used for branching is knownAIF is similar to IF statement,the label used for branching is knownas a sequencing symbol.AGO is similar to GOTO statement.Sequencing symbol has the syntax:<ordinary string>
  23. 23. An AIF statement has the syntax:AIF(<expression>)<sequencing symbol>where <expression> is relational expression involving ordinarystring formal parameters etc.AIF is conditional branch Pseudo-opcode.AIF is conditional branch Pseudo-opcode.An AGO statement has the syntax:AGO<sequencing symbol>AGO is unconditional branch Pseudo-opcodewhere transfer of expansion time control to the statement containing<sequencing symbol> in its label field takes place unconditionally.
  24. 24. An ANOP statement has the syntax<sequencing symbol> ANOPIt simply has the effect of defining the sequencing symbol.
  25. 25. Expansion of time variables(EV) are variables which can only be useduring the expansion of macro calls.A local EV is created for use only during the a particular macro call .Its syntax is:-LCL<&variable name>[,< variable name>¡.]A global EV exist across all macro calls situated in a program & can beA global EV exist across all macro calls situated in a program & can beused in any macro which has a declaration for it.Its syntax is:GBL<&variable name>[,< variable name>¡.]Expansion of time variables(EV) can be manipulated through the statementSET .Syntax:-< Expansion time variables>SET< expression>
  26. 26. Example:-MACROCONSTANTSLCL &A&A SET 1DB &A&A SET &A+1&A SET &A+1DB &AMENDA call on macro CONSTANTS is explained as follows:The local EV A is created.The first SET statement assigns the value ‘1’ to it .The first DBstatement 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’.
  27. 27. An attribute is written using the syntax:-<attribute name>’<formal parameter spec>and represents information about the value of the formal parameter i.e.about the corresponding actual parameter. The type,length and sizeattributes have the names T,L and S.attributes have the names T,L and S.
  28. 28. Example:-MACRODCL_CONST &AAIF (L’ &A EQ 1) .NEXT_ _.NEXT _ _.NEXT _ __ _MENDHere expansion time control is transferred to the statement having.NEXTin its label field only if the actual parameter corresponding to the formalparameter corresponding to the formal parameters A has the length of ‘1’.
  29. 29. The macro preprocessor accepts an assembly program containingdefinitions and calls and translate it into an assembly program whichdoes not contain any macro definitions or calls.The program from output by macro preprocessor can now be handedover to an assembler to obtain the target language from of theover to an assembler to obtain the target language from of theprogram.Thus the macro preprocessor segregates macro expansion from theprocess of program assembly.It is economical because
  30. 30. We begin the design by listing all tasks involved in macroexpansion.1. Identify macro calls in the program.2. Determine the values of formal parameters.3. Maintain the values of expansion time variables declared in macros.3. Maintain the values of expansion time variables declared in macros.4. Organize expansion time control flow.5. Determine the values of sequencing symbols.6. Perform expansion of model statement.
  31. 31. 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.4. Determine the processing necessary to perform the task.
  32. 32. A compiler is a program takes a program written in a source language andtranslates it into an equivalent program in a target language.Source program COMPILER target programerror messages33( Normally a program written ina high-level programming language)( Normally the equivalent program inmachine code – relocatable object file)
  33. 33. In addition to the development of a compiler, the techniques used in compiler design can beapplicable to many problems in computer science.◦ Techniques used in a lexical analyzer can be used in text editors, information retrievalsystem, and pattern recognition programs.◦ Techniques used in a parser can be used in a query processing system such as SQL.◦ Many software having a complex front-end may need techniques used in compiler design.Many software having a complex front-end may need techniques used in compiler design.A symbolic equation solver which takes an equation as input. That program shouldparse the given input equation.◦ Most of the techniques used in compiler design can be used in Natural LanguageProcessing (NLP) systems.34
  34. 34. There are two major parts of a compiler: Analysis and SynthesisIn analysis phase, an intermediate representation is created from thegiven source program.◦ Lexical Analyzer, Syntax Analyzer and Semantic Analyzer are the parts of this phase.◦ Lexical Analyzer, Syntax Analyzer and Semantic Analyzer are the parts of this phase.In synthesis phase, the equivalent target program is created from thisintermediate representation.◦ Intermediate Code Generator, Code Generator, and Code Optimizer are the parts of this phase.35
  35. 35. LexicalAnalyzerSemanticAnalyzerSyntaxAnalyzerIntermediateCode GeneratorCodeOptimizerCodeGeneratorTargetProgramSourceProgram36• Each phase transforms the source program from one representationinto another representation.• They communicate with error handlers.• They communicate with the symbol table.
  36. 36. Lexical analyzerSyntax analyzerSemantic analyzerERRORSYMBOLSemantic analyzerIntermediate code generatorCode optimizerCode generatorRHNDLERTBLMNGR
  37. 37. Lexical Analyzer reads the source program character by character andreturns the tokens of the source program.A token describes a pattern of characters having same meaning in thesource program. (such as identifiers, operators, keywords, numbers,delimeters and so on)Ex: newval := oldval + 12 => tokens: newval identifierEx: newval := oldval + 12 => tokens::= assignment operatoroldval identifier+ add operator12 a numberPuts information about identifiers into the symbol table.Regular expressions are used to describe tokens (lexical constructs).A (Deterministic) Finite State Automaton can be used in theimplementation of a lexical analyzer.38
  38. 38. A Syntax Analyzer creates the syntactic structure (generally a parse tree)of the given program.A syntax analyzer is also called as a parser.A parse tree describes a syntactic structure.assgstmtidentifier := expressionnewval expression + expressionidentifier numberoldval 1239• In a parse tree, all terminals are at leave• All inner nodes are non-terminals ina context free grammar.
  39. 39. The syntax of a language is specified by a context free grammar(CFG).The rules in a CFG are mostly recursive.A syntax analyzer checks whether a given program satisfies the rulesimplied by a CFG or not.implied by a CFG or not.◦ If it satisfies, the syntax analyzer creates a parse tree for the given program.Ex: We use BNF (Backus Naur Form) to specify a CFGassgstmt -> identifier := expressionexpression -> identifierexpression -> numberexpression -> expression + expression40
  40. 40. Which constructs of a program should be recognized by the lexical analyzer,and which ones by the syntax analyzer?◦ Both of them do similar things; But the lexical analyzer deals with simplenon-recursive constructs of the language.◦ The syntax analyzer deals with recursive constructs of the language.◦The syntax analyzer deals with recursive constructs of the language.◦ The lexical analyzer simplifies the job of the syntax analyzer.◦ The lexical analyzer recognizes the smallest meaningful units (tokens) in asource program.◦ The syntax analyzer works on the smallest meaningful units (tokens) in asource program to recognize meaningful structures in our programminglanguage.41
  41. 41. Depending on how the parse tree is created, there are different parsing techniques.These parsing techniques are categorized into two groups:◦ Top-Down Parsing,◦ Bottom-Up ParsingTop-Down Parsing:◦ Construction of the parse tree starts at the root, and proceeds towards the leaves.◦ Efficient top-down parsers can be easily constructed by hand.◦ Recursive Predictive Parsing, Non-Recursive Predictive Parsing (LL Parsing).◦ Recursive Predictive Parsing, Non-Recursive Predictive Parsing (LL Parsing).Bottom-Up Parsing:◦ Construction of the parse tree starts at the leaves, and proceeds towards the root.◦ Normally efficient bottom-up parsers are created with the help of some software tools.◦ Bottom-up parsing is also known as shift-reduce parsing.◦ Operator-Precedence Parsing – simple, restrictive, easy to implement◦ LR Parsing – much general form of shift-reduce parsing, LR, SLR, LALR42
  42. 42. A semantic analyzer checks the source program for semantic errors andcollects the type information for the code generation.Type-checking is an important part of semantic analyzer.Normally semantic information cannot be represented by a context-freelanguage used in syntax analyzers.Context-free grammars used in the syntax analysis are integrated withContext-free grammars used in the syntax analysis are integrated withattributes (semantic rules)◦ the result is a syntax-directed translation,◦ Attribute grammarsEx:newval := oldval + 12The type of the identifier newval must match with type of the expression (oldval+12)43
  43. 43. A compiler may produce an explicit intermediate codes representing thesource program.These intermediate codes are generally machine (architectureindependent). But the level of intermediate codes is close to the level ofmachine codes.Ex:Ex:newval := oldval * fact + 1id1 := id2 * id3 + 1MULT id2,id3,temp1 Intermediates Codes(Quadraples)ADD temp1,#1,temp2MOV temp2,,id144
  44. 44. The code optimizer optimizes the code produced by the intermediatecode generator in the terms of time and space.Ex:MULT id2,id3,temp1ADD temp1,#1,id1CS416 Compiler Design 45
  45. 45. Produces the target language in a specific architecture.The target program is normally is a reloadable object file containing themachine codes.Ex:( assume that we have an architecture with instructions whose at least one ofits operands isa machine register)MOVE id2,R1MULT id3,R1ADD #1,R1MOVE R1,id1CS416 Compiler Design 46
  46. 46. A compiler is a computer program thattranslates a program in a source languageinto an equivalent program in a targetlanguage.A source program/code is a program/codewritten in the source language, which isusually a high-level language. compilerSourceprogramTargetprogramusually a high-level language.A target program/code is a program/codewritten in the target language, which oftenis a machine language or an intermediatecode. Errormessage
  47. 47. scannerparserSemantic analyzerStream of charactersStream of charactersStream of charactersStream of charactersStream of tokensStream of tokensStream of tokensStream of tokensParse/syntax treeParse/syntax treeParse/syntax treeParse/syntax treeAnnotated treeAnnotated treeAnnotated treeAnnotated treeIntermediate code generatorCode optimizationCode generatorCode optimizationIntermediate codeIntermediate codeIntermediate codeIntermediate codeIntermediate codeIntermediate codeIntermediate codeIntermediate codeTarget codeTarget codeTarget codeTarget codeTarget codeTarget codeTarget codeTarget code
  48. 48. Fig. Operation of a typical multi-language,multi-target compiler.
  49. 49. • Lexical analysis involves scanning of the source programme fromleft to right and• Separating them into tokens.• A token is a sequence of characters having a collective meaning.• Tokens are seperated by blanks,operators and special symbols.• A lexical analysis on the statement.• A lexical analysis on the statement.• X=Y+Z*30;• Will generate the following tokensX = ;30*Y + Z
  50. 50. X is an identifier.= is a terminal symbol.Y is an identifier.+ is a terminal symbol.Z is an identifier.* Is a terminal symbol.30 is literal.30 is literal.;is a terminal symbol.• Blank s seperating the token are eliminated.• Lexical phase discards comments since they have no effect on the processing Of the program.• Identifiers are stored in the symbol table.• Literals are stored in the literal table.
  51. 51. • Syntax analysis deals with the syntax error detection and correction.• In the program if the spelling is wrong,then in syntax analysis the spelling isCorrected according to the syntax table.• This syntax table is the table in which for a wrong keyword in code,thedifferent Corrections are available.• Example:- for iffff & thennn in the code, thee syntax table will be- thenthanifIf then
  52. 52. It gets the parse tree from the parser together with informationabout some syntactic elementsIt determines if the semantics or meaning of the program iscorrect.This part deals with static semantic.◦This part deals with static semantic.◦ semantic of programs that can be checked by reading off fromthe program only.◦ syntax of the language which cannot be described in context-free grammar.Mostly, a semantic analyzer does type checking.It modifies the parse tree in order to get that (static) semanticallycorrect code.
  53. 53. An intermediate code generator◦ takes a parse tree from the semantic analyzer◦ generates a program in the intermediate language.In some compilers, a source program is translated into anintermediate code first and then the intermediate code is translatedintermediate code first and then the intermediate code is translatedinto the target language.In other compilers, a source program is translated directly into thetarget language.54
  54. 54. Using intermediate code is beneficial when compilers whichtranslates a single source language to many target languages arerequired.◦ The front-end of a compiler – scanner to intermediate code generator – can be◦ The front-end of a compiler – scanner to intermediate code generator – can beused for every compilers.◦ Different back-ends – code optimizer and code generator– is required for eachtarget language.One of the popular intermediate code is three-address code. Athree-address code instruction is in the form of x = y op z.
  55. 55. Replacing an inefficient sequence of instructions with a bettersequence of instructions.Sometimes called code improvement.Code optimization can be done:◦ after semantic analyzingperformed on a parse treeperformed on a parse tree◦ After intermediate code generationperformed on a intermediate code◦ after code generationperformed on a target code
  56. 56. A code generator◦ takes either an intermediate code or a parse tree◦ produces a target program.
  57. 57. Static memory allocation refers to the process of allocating memoryat compile-time before the associated program is executed.Dynamic memory allocation is automatic memory allocationwhere memory is allocated as required at run-time.
  58. 58. • An application of this technique involves a program module (e.g. function orsubroutine) declaring static data locally, such that these data are inaccessible inother modules unless references to it are passed as parameters or returned.• A single copy of static data is retained and accessible through many calls to thefunction in which it is declared.• Static memory allocation therefore has the advantage of modularising data within aprogram design in the situation where these data must be retained through theruntime of the program.runtime of the program.• The use of static variables within a class in object oriented programming enables asingle copy of such data to be shared between all the objects of that class.• Object constants known at compile-time, like string literals, are usually allocatedstatically. In object-oriented programming, the virtual method tables of classes areusually allocated statically.• A statically defined value can also be global in its scope ensuring the sameimmutable value is used throughout a run for consistency.
  59. 59. Code AData ACode B Code ACode ACode BData BCode CCode BCode CData ACode CData AData BData Ca)Staticallocationb) Dynamic allocationThe main program AIs active.c)Dynamic allocationThe main program A callsThe function B
  60. 60. Advantages of static memory allocation:-• It is simple to implement.• Binding is performed during compilation.• Execution is faster as there is no binding during the run time and addresses of• Variables is directly encoded in machine instructions.Disadvantages of static memory allocation:-• It is almost impossible to handle recursion.• Memory requirement is higher.Variables remain allocated even when theFunction is not active.
  61. 61. Advantages of Dynamic memory allocation:-In dynamic memory allocation , memory is allocated and deallocated during execution of a program.• Dynamic memory allocation has optimal utilization of memory.• Recursion can be implemented using dynamic memory allocation.• Recursion can be implemented using dynamic memory allocation.
  62. 62. Compilation Of Control Structure:-Some of the control structures are:There are language features which govern the sequencing of controlthrough a Program.Some of the control structures are:1)If statement.2)While loop statement.
  63. 63. If Statement:-We can translate an If statement into three address code. A program containingIf statement can be mapped into an equivalent program containing explicit goto’s.A sample if statement is compiled into three-address code in following fig.If (E) 100:if(E) then goto 102{ 101 : goto 104s1 ; 102 : s1 ;} => 103 : goto 105Else 104 : s2 ;{ 105 : s3 ;s2;}S3;
  64. 64. It should be clear that if E is true then the two statements s1 and s3 will be executeIf E is false then the two statements s2 and s3 will be executed.While statement:-•We can translate while-statement into three address code. A program containing w•Statement can be mapped into an equivalent program containing explicit goto’s.•A sample while statement is compiled into three address codes.While (E) 100 : if (E) then goto 102{ 101 : goto 105s1 ; 102: s1
  65. 65. S2; => 103 : s2} 104 : goto 100S3; 105 : s3• It should be clear that if E is true then control enters the body of the looexecutes S1 and s2.subsequently,it goes back to the beginning of the lexecutes S1 and s2.subsequently,it goes back to the beginning of the l• If E is false then control is transferred to the statement s3.
  66. 66. An interpreter accepts the source program and performs the actionsassociated with the instructionsIt creates virtual execution environmentAn interpreter reads source code one instruction or a line at a time,converts this into machine code or some intermediate form andconverts this into machine code or some intermediate form andexecutes it.
  67. 67. COMPILERCOMPILERCOMPILERCOMPILER INTERPRETERINTERPRETERINTERPRETERINTERPRETER1. Compiler translates theentire program intomachine code2. If there is a problem in thecode, compiled programsmake the programmer waitfor the execution of entireprogram.1. Interpreter translates codeone line at a timeexecuting each line as it istranslated.2. An interpreter letsprogrammer knowimmediately when andwhere problems exist inprogram.3. Compilers produce betteroptimized codes thatgenerally runs fast.4. It spends lot of timeanalyzing and generatingmachine code. It is notsuited during programdevelopment.where problems exist inthe code.3. Program execution isrelatively slow due tointerpreter overhead.4. Relatively little time isspent in analyzing as itexecutes line by line.

×