This action might not be possible to undo. Are you sure you want to continue?
Name: Abhijeet Kumar Pandey Compiler Design Branch: Computer Science & Engg. Year/Sem:2010/VII
Explain why we should study about compiler. What is Compiler & its various phases with diagram & by taking a example of a= b*cd. Ans: Reasons for Studying Compilers: • An essential programming tool • Improves software productivity by hiding low-level details • A tool for designing and evaluating computer architectures • Inspired RISC, VLIW machines • Machines’ performance measured on compiled code • Techniques for developing other programming tools Examples: error detection tools • Little languages and program translations can be used to solve other problems Compiler : A compiler is a computer program (or set of programs) that transforms source code written in a programming language(the source language) into another computer language (the target language, often having a binary form known as object code). Phases of Compiler: Lexical Analyzer: The lexical analysis stage transforms a sequence of characters to a sequence of lexical elements. These lexical entities correspond principally to integers, floating point numbers, characters, strings of characters and identifiers. The message Illegal character might be generated by this analysis. Syntax Analysis: The parsing stage constructs a syntax tree and verifies that the sequence of lexical elements is correct with respect to the grammar of the language. The message Syntax error indicates that the phrase analyzed does not follow the grammar of the language. Semantic Analysis: The semantic analysis stage traverses the syntax
Compiler Design (CS-701)
part of pattern matching will not be used). Type error messages may occur during this phase. The analysis consists principally of type inference. Year/Sem:2010/VII Subject: tree. This stage also detects whether any members of a sequence are not of type unit. produces the most general type of an expression or declaration.ACROPOLIS RESEARCH INSTITUTE OF TECHNOLOGY AND Name: Abhijeet Kumar Pandey Compiler Design Branch: Computer Science & Engg. Code Generation: Generation and the optimization of intermediate code does not produce errors or warning messages. Compiler Design (CS-701) .g pattern matching is not exhaustive. Other warnings may result. The final step in the compilation process is the generation of a program binary. which if successful. checking another aspect of program correctness. including pattern matching analysis (e.
temp2=id2. Syntax Analyzer Semantic Analyzer Intermediate code generator Temp1=id3. Id1=temp1. Code generator Compiler Design (CS-701) .ACROPOLIS RESEARCH INSTITUTE OF TECHNOLOGY AND Name: Abhijeet Kumar Pandey Compiler Design Branch: Computer Science & Engg. Year/Sem:2010/VII Given Expression is a=b*c Subject: Laxical Analyzer id1=id2*id3. id1=temp3. code optimizer Temp1=id2*id3. Temp3=temp2*temp1.
Quite naturally this technique is called bottom-up.BX MOV AX. Top-down parsing: grammar for the language anb nc n SS -> aSQ Compiler Design (CS-701) . There are only two techniques to do parsing. which describes how the grammar was used to produce the sentence. words). What is parsing? How many types of techniques used in the parsing explain with diagram? Ans: Parsing is the process of analyzing a text. because the production tree is reconstructed from the top downwards. made of a sequence of tokens (for example. especially in reference to how phrases are divided up in garden path sentences. Year/Sem:2010/VII Subject: MOVE ID2.AX MOVE ID3. The basic connection between a sentence and the grammar it derives from is the parse tree.ID1 Q2.ACROPOLIS RESEARCH INSTITUTE OF TECHNOLOGY AND Name: Abhijeet Kumar Pandey Compiler Design Branch: Computer Science & Engg.BX MUL AX. The first method tries to imitate the original production process by rederiving the sentence from the start symbol. This method is called topdown. The second methods tries to roll back the production process and to reduce the sentence back to the start symbol. Parsing can also be used as a linguistic term. to determine its grammatical structure with respect to a given (more or less) formal grammar.
a top-down parser tries to expand nonterminals into righthand-sides and a bottom-up parser tries to replace (reduce) right-hand-sides with nonterminals. Year/Sem:2010/VII S -> abc bQc -> bbcc cQ -> Qc and suppose the (input) sentence is aabbcc.ACROPOLIS RESEARCH INSTITUTE OF TECHNOLOGY AND Name: Abhijeet Kumar Pandey Compiler Design Branch: Computer Science & Engg. performing the following reverse derivation sequence: ax → Ax → S Intuitively. Then it would replace Ax with S. Compiler Design (CS-701) . Subject: Top-down parsing tends to identify the production rules (and thus to characterize the parse tree) in prefix order. it has reached the goal and stops. Bottom-up parsing: A bottom up parser is trying to go backwards. The first action of the bottom-up parser would be to replace a with A yielding Ax. indicating success. Once it arrives at a sentential form with exactly S.
Second. the parsing-table entry is empty). They may also pop from the stack. These routines may change. Checking that any recovery action eventually results in an input symbol being consumed (or the stack being shortened if the end of the input has been reached) is a good way to protect against such loops. Year/Sem:2010/VII Subject: Q3. Construct a Syntax Directed Definition to convert infix to postfix translation and also show annotated parse tree for expression 9-5+2. Ans: Error Recovery in Predictive Parsing: An error is detected during predictive parsing when the terminal on top of the stack does not match the next input symbol or when nonterminal A is on top of the stack. Compiler Design (CS-701) . a] is error (i. The sets should be chosen so that the parser recovers quickly from errors that are likely to occur in practice. Phrase-level Recovery Phrase-level error recovery is implemented by filling in the blank entries in the predictive parsing table with pointers to error routines. or delete symbols on the input and issue appropriate error messages. Define Syntax Directed Definition. a is the next input symbol. we must ensure that there is no possibility of an infinite loop. and M[A. What do you understand by error recovery & error handling in LL & LR parsing also explains its types. Q4.. First. Alteration of stack symbols or the pushing of new symbols onto the stack is questionable for several reasons. insert. Panic Mode Panic-mode error recovery is based on the idea of skipping symbols on the the input until a token in a selected set of synchronizing tokens appears. the steps carried out by the parser might then not correspond to the derivation of any word in the language at all.ACROPOLIS RESEARCH INSTITUTE OF TECHNOLOGY AND Name: Abhijeet Kumar Pandey Compiler Design Branch: Computer Science & Engg. Its effectiveness depends on the choice of synchronizing set.e.
a type. Semantic rules set up dependencies between attributes that will be represented by a graph.ACROPOLIS RESEARCH INSTITUTE OF TECHNOLOGY AND Name: Abhijeet Kumar Pandey Compiler Design Branch: Computer Science & Engg. The value of an attribute at a parse tree node is defined by a semantic rule associated with a production used at that node. An attribute can represent anything we choose: a string. partitioned into two subsets called the synthesized and inherited attributes of that grammar symbol. we derive an Compiler Design (CS-701) . From the dependency graph. The value of a synthesized attribute at a node is computed from the values of attributes at the children of that node in the parse tree. or whatever. a number. Year/Sem:2010/VII Subject: Ans: A syntax directed definition is a generalization of a context free grammar in which each grammar symbol has an associated set of attributes. the value of an inherited attribute is computed from the values of attributes at the siblings and parent of that node. a memory location.
ACROPOLIS RESEARCH INSTITUTE OF TECHNOLOGY AND Name: Abhijeet Kumar Pandey Compiler Design Branch: Computer Science & Engg. Evaluation of the semantic rules defines the values of the attributes at the nodes in parse tree for the input string.t|| ‘-‘ E. A parse tree showing the values of attributes at each node is called an annotated parse tree. The inherited attributes of A.Xn. depends only on: The attributes (both inherited as well as synthesized) of the symbols X1. the symbols to the left of Xj in the production.t := E.….t=E.t||T. T-> 9 Q5. T..t=’0’ T. and 2. Xj−1 (i.t||T.e.. Compiler Design (CS-701) .t=’1’ ……….type depends on T.t=T. and on the right side of production A → X1X2…. Syntax Directed Definition for infix to postfix: Productions E -> E+T E-> E-T E-> T T-> 0 T-> 1 ……. Semantic rule E. Year/Sem:2010/VII Subject: evaluation order for the semantic rules.t=’9’ What is S attributed & L attributed definitions.t T. The process of computing the attributes at the nodes is called annotating or decorating the parse tree. Ans: L-ATTRIBUTED DEFINITIONS : A syntax-directed definition is Lattributed if each inherited attribute of Xj for i between 1 and n.X2. because the inherited attribute L.type. 1. The syntax-directed definition above is an example of the L-attributed definition.t||’+’ E.
and L is parent of L1 in the production L → L1. A parse tree for an S-attributed definition can always be annotated by evaluating the semantic rules for the attributes at each node bottom up. -(a+b)*(c+d)-(a+b+c) Quadruples: Operator + uminus + + * Triples: (0) (1) (2) (3) Operator + uminus + + Arg1 a (0) c (0) Arg2 b d c Arg1 a T1 c c T2 T5 Arg2 b d T1 T3 T4 result T1 T2 T3 T4 T5 T6 Compiler Design (CS-701) .attributed definition.ACROPOLIS RESEARCH INSTITUTE OF TECHNOLOGY AND Name: Abhijeet Kumar Pandey Compiler Design Branch: Computer Science & Engg. from the leaves to the root. the inherited attribute L1. Q6. Ans: Translate the expression into Quadruples. S-ATTRIBUTED DEFINITIONS A syntax directed definition that uses synthesized attributes exclusively is said to be an S.id. Year/Sem:2010/VII Subject: and T is to the left of L in the production D → TL. Triples and Indirect Triples.type depends on the inherited attribute L. Similarly.type.
Year/Sem:2010/VII (4) (5) Indirect Triples: (14) (15) (16) (17) (18) (19) Operator + uminus + + * Arg1 a (14) c (14) (15) (18) * (1) (4) Subject: (2) (3) Arg2 b d c (16) (17) Pointers to triples (0) (1) (2) (3) (4) (5) (14) (15) (16) (17) (18) (19) Q7. The latter activation has its record at the top of the stack. What is activation record and what are its contents with diagram.ACROPOLIS RESEARCH INSTITUTE OF TECHNOLOGY AND Name: Abhijeet Kumar Pandey Compiler Design Branch: Computer Science & Engg. and the entire sequence of activation records on the stack corresponding to the path in the activation tree to the activation where control currently resides. with the root of the activation tree at the bottom. Ans: Procedure calls and returns are usually managed by a run-time stack called the control stack. Each live activation has an activation record (sometimes called a frame) on the control stack. The contents of activation records Compiler Design (CS-701) .
in another activation record. Temporary values.. with information about the state of the machine just before the call to the procedure. A saved machine status. 2. such as those arising from the evaluation of expressions. Compiler Design (CS-701) . 3. e. Here is a list of the kinds of data that might appear in an activation record 1.g.ACROPOLIS RESEARCH INSTITUTE OF TECHNOLOGY AND Name: Abhijeet Kumar Pandey Compiler Design Branch: Computer Science & Engg. An "access link" may be needed to locate data needed by the called procedure but found elsewhere. in cases where those temporaries cannot be held in registers. Local data belonging to the procedure whose activation record this is. This information typically includes the return address (value of the program counter. Year/Sem:2010/VII Subject: vary with the language being implemented. to which the called procedure must return) and the contents of registers that were used by the calling procedure and that must be restored when the return occurs. 4.
or it may be embedded in the output of that process for later exploitation. not all called procedures return a value. List The simplest and easiest to implement data structure for symbol table is a linear list of records. Year/Sem:2010/VII Subject: 5.ACROPOLIS RESEARCH INSTITUTE OF TECHNOLOGY AND Name: Abhijeet Kumar Pandey Compiler Design Branch: Computer Science & Engg. a linker will use these symbol tables to resolve any unresolved references. these values are not placed in the activation record but rather in registers. Space for the return value of the called function. if any. when possible. 6. during an interactive debugging session. A control link. for greater efficiency. for example. Explain Symbol Table Organization and its Data Structure. Ans: In computer science. we show a space for them to be Q8. such as its type. and if one does. Commonly. we may prefer to place that value in a register for efficiency. scope level and sometimes its location. End of array always marks by a point known as space. However. Compiler Design (CS-701) . The various data structure used to implement the data structure. 7. a symbol table is a data structure used by a language translator such as a compiler or interpreter.An object file will contain a symbol table of the identifiers it contains that are externally visible. Again. A symbol table may only exist during the translation process. pointing to the activation record of the caller. or as a resource for formatting a diagnostic report during or after execution of a program. The actual parameters used by the calling procedure. Now names are added to end of array. where each identifier in a program's source code is associated with information relating to its declaration or appearance in the source. During the linking of different object files. We use singlearray or collection of several arrays for this purpose to store name and their associated information.
there is a property that no limit on number of entries that can be made in table. In above figure (a) represent the simple list and (b) represent self organzing list in which Id1 is related to Id2 and Id3 is related to Id1.ACROPOLIS RESEARCH INSTITUTE OF TECHNOLOGY AND Name: Abhijeet Kumar Pandey Compiler Design Branch: Computer Science & Engg. When a name is inserted then it will insert at ‘space’ and manage all linkers to other existing name. or a hash map. In hashing –open. Hash table consist an array ‘HESH’ and several buckets attached to array HESH according to hash function. Hash table: A hash table. Compiler Design (CS-701) . is a data structure that associates keys with values ‘Open hashing’ is a key that is applied to hash table. Year/Sem:2010/VII Subject: Self Organizing List To reduce the time of searching we can add an addition field ‘linker’ to each record field or each array index.
Describe the Storage Allocation Strategies of Symbol Table. we use these field as binary search tree. These two statements show that all smaller name than Namei must be left child of name otherwise right child of namej.e. All names are created as child of root node that always follow the property of binary tree i. Q9.ACROPOLIS RESEARCH INSTITUTE OF TECHNOLOGY AND Name: Abhijeet Kumar Pandey Compiler Design Branch: Computer Science & Engg. The data structure for a particular implementation of a symbol table is sketched in a separate array ‘arr_lexemes’ holds the character string forming an identifier.e. The string is terminated by an Compiler Design (CS-701) . For inserting any name it always follow binary search tree insert algorithm. name <name ie and Namej <name. left and right child. Year/Sem:2010/VII Subject: Search Tree: Another approach to organize symbol table is that we add two link fields i.
denoted by EOS. is a directed graph with no directed cycles. pointing to the beginning of a lexeme. ‘plus’ ‘b’ ‘and’. and ‘d’ where 2nd. ‘minus’. Each entry in symbol-table array‘arr_symbol_table’ is a record consisting of two fields. ‘c’. 3rd. and token. the 0th entry is left empty. 4th. each edge connecting one vertex to Compiler Design (CS-701) . because lookup return 0 to indicate that there is no entry for a string. as “lexeme_pointer”. In figure 9. and 7th entries are for the ‘a’. 2nd. a directed acyclic graph (commonly abbreviated to DAG). that may not appear in identifiers. 4th and 6th entries are for reserve keyword. 6th. 5th. What is DAG? How basic blocks are represented through DAG. Year/Sem:2010/VII Subject: end-of-string character. it is formed by a collection of vertices and directed edges. The 1st. That is. Additional fields can hold attribute values. Q10.What are its Advantages and Applications.ACROPOLIS RESEARCH INSTITUTE OF TECHNOLOGY AND Name: Abhijeet Kumar Pandey Compiler Design Branch: Computer Science & Engg. Ans: In mathematics and computer science.1.
interior nodes are labeled by an operator symbol. Subscript with 0. Example of DAG Representation - Compiler Design (CS-701) .or r-value. such that there is no way to start at some vertex v and follow a sequence of edges that eventually loops back to v again. The DAG Representation of Basic Blocks Directed acyclic graphs (DAGs) give a picture of how the value computed by each statement in the basic block is used in the subsequent statements of the block.ACROPOLIS RESEARCH INSTITUTE OF TECHNOLOGY AND Name: Abhijeet Kumar Pandey Compiler Design Branch: Computer Science & Engg. they are unique identifiers from operators we determine whether l. .interior node ≡ computed values . Year/Sem:2010/VII Subject: another. represent initial values of names.identifiers in the sequence – have that value. Definition: a dag for a basic block is a directed acyclic graph with the following labels on nodes: leaves are labeled with either variable names or constants. Nodes are also (optionally) given a sequence of identifiers for labels.
ACROPOLIS RESEARCH INSTITUTE OF TECHNOLOGY AND Name: Abhijeet Kumar Pandey Compiler Design Branch: Computer Science & Engg. Year/Sem:2010/VII t1:= 4*i t2:= a[t1] t3:= 4*i t4:= b[t3] t5:= t2 * t4 t6:= prod + t5 prod:= t6 t7:= i + 1 i:= t7 if i <= 20 goto 1 Three address code Subject: Compiler Design (CS-701) .
or as a resource for formatting a diagnostic report during or after execution of a program. During the linking of different object files. While reverse engineering an executable a lot of tools refer the symbol table to check what addresses have been assigned to global variables and known functions. Year/Sem:2010/VII Subject: + prod *  t2 b 4 t5  t4 t1. for example. i 1 20 a An object file will contain a symbol table of the identifiers it contains that are externally visible.ACROPOLIS RESEARCH INSTITUTE OF TECHNOLOGY AND Name: Abhijeet Kumar Pandey Compiler Design Branch: Computer Science & Engg. If the symbol table has been stripped or cleaned out before Compiler Design (CS-701) . during an interactive debugging session. t3 * i0 (1) <= + t7. or it may be embedded in the output of that process for later exploitation. A symbol table may only exist during the translation process. a linker will use these symbol tables to resolve any unresolved references.
Compiler Design (CS-701) . Year/Sem:2010/VII Subject: converting it into an executable tools will find it hard to find out addresses and understand anything about the program.ACROPOLIS RESEARCH INSTITUTE OF TECHNOLOGY AND Name: Abhijeet Kumar Pandey Compiler Design Branch: Computer Science & Engg.
This action might not be possible to undo. Are you sure you want to continue?
We've moved you to where you read on your other device.
Get the full title to continue reading from where you left off, or restart the preview.