MLR Institute of Technology Dundigal, Quthbullapur (M), Hyderabad 500 043 Course Name Course Code : 56031 Class Branch COMPUTER SCIENCE AND ENGINEERING COMPILER DESIGN LAB MANUAL : COMPILER DESIGN : III B. Tech II Semester Year : 2014 2015 Course Faculty OBJECTIVES : Computer Science and Engineering : Mrs. K. Archana, Assistant Professor Mrs. Durga Sree, Assistant Professor To meet the challenge of ensuring excellence in engineering education, the issue of quality needs to be addressed, debated and taken forward in a systematic manner. Accreditation is the principal means of quality assurance in higher education. The major emphasis of accreditation process is to measure the outcomes of the program that is being accredited In line with this, Faculty of Institute of MLR Institute of Technologuy, Hyderabad has taken a lead in incorporating philosophy of outcome based education in the process of problem solving and career development. So, all students of the institute should understand the depth and approach of course to be taught through this question bank, which will enhance learner s learning process.
COMPILER DESIGN LAB SYSTEM REQUIREMENTS 1. Intel based desktop PC of 166MHz or faster processor with at least 64 MB RAM and 100 MB free disk space. 2. C compiler LAB OBJECTIVES 1. To provide an Understanding of the language translation peculiarities by designing complete translator for mini language. 2. To provide an understanding of the design aspect of operating system SYLLABUS 1. Design a Lexical analyzer for the above language. The lexical analyzer should ignore redundant spaces, tabs and newlines. It should also ignore comments. Although the syntax specification states that identifiers can be arbitrarily long, you may restrict the length to some reasonable value. 2. Implement the lexical analyzer using flex, flex or lex or other lexical analyzer generating tools. 3. Design Predictive parser for the given language 4. Design SLR bottom up parser for the above language. 5. Convert the BNF rules into Yacc form and write code to generate abstract syntax tree. 6. Write program to generate machine code from the abstract syntax tree generated by the parser. following instruction set may be considered as
LAB CODE Students should report to the concerned lab as per the time table. Students who turn up late to the labs will in no case be permitted to do the program schedule for the day. After completion of the program, certification of the concerned staff in-charge in the observation book is necessary. Student should bring a notebook of 100 pages and should enter the readings /observations into the notebook while performing the experiment. The record of observations along with the detailed experimental procedure of the experiment in the immediate last session should be submitted and certified staff member in-charge. Not more than 3-students in a group are permitted to perform the experiment on the set. The group-wise division made in the beginning should be adhered to and no mix up of students among different groups will be permitted. The components required pertaining to the experiment should be collected from stores in-charge after duly filling in the requisition form. When the experiment is completed, should disconnect the setup made by them, and should return all the components/instruments taken for the purpose. Any damage of the equipment or burn-out components will be viewed seriously either by putting penalty or by dismissing the total group of students from the lab for the semester/year. Students should be present in the labs for total scheduled duration. Students are required to prepare thoroughly to perform the experiment before coming to laboratory.
EXPERIMENT : 1 NAME OF THE EXPERIMENT: Design A Lexical Analyzer AIM Design a Lexical analyzer. The lexical analyzer should ignore redundant s tabs and new lines. It should also ignore comments. Although the syntax specification s those identifiers can be arbitrarily long, you may restrict the length to some reasonable Value. THEORY Lexical analyzer or scanner is the process where the stream of characters making up the source program is read from left-to-right and grouped into tokens. Tokens are sequences of characters with a collective meaning. There are usually only a small number of tokens for a programming language: constants (integer, double, char, string, etc.), operators (arithmetic, relational, logical), punctuation, and reserved words. ALGORITHM step1: Start step2: Declare the variable char exp[20],id[10],dig[10],ch, int i,j; step3: Enter the "enter expression:"; step4: Read the expression step5: Initialize i=0 step6: Repeat step 5 until i<strlen(exp); upto step 25 step7: Assign ch=exp[i]; step8: Declare j=0; step9: Repeat step 8 (ch>='a'&&ch<='z') upto step14 step10: Assign id[j++]=ch; step11: Increment i =i+1 step12: Repeat step 11 while((exp[i]>='a'&&exp[i]<='z') (exp[i]>='0'&&exp[i]<='9')) upto step12 step13: Assign id[j++]=exp[i++] step14: Declare id[j]='\0' step15: Display " identifier" step16: Otherwise repeat step 15 if(ch=='+' ch=='-' ch=='*' ch=='/' ch=='%' ch=='=')upto step17 step17: Display "operator" step18: Increment i=i+1 step19: Otherwise repeat step 18 if(ch>='0'&&ch<='9')upto step24 step20: Assign dig[j++]=ch step21: Increment i=i+1 step22: Repeat while(exp[i]>='0'&&exp[i]<='9') step23: Assign dig[j++]=exp[i++];
step24: Declare dig[j]='\0'; step25: Display "constant" step26: Stop OUTPUT VIVA QUESTIONS 1. What is lexical analyzer? 2. Which compiler is used for lexical analyzer? 3. What is pattern? 4. What is the output of Lexical analyzer? 5. What is the lexeme? 6. what is the token?
EXPERIMENT: 2 NAME OF THE EXPERIMENT: Implement lexical analyzer using lexical analyzer generating tools. AIM: Implement the lexical analyzer using JLex, flex or lex other lexical analyzer generating tools. THEORY Lex tool used to generate lexical analyzers and parsers. The Overview describes the basic building blocks of a compiler and explains the interaction with lex. Conventional arithmetic operations and control statements, such as ifelse and while, are implemented. ALGORITHM: Step1: Step2: Step3: Step4: Step5: Start Declare the declarations for the given language tokens like digit, alphabet, white space, delimiters, etc. digit[0-9] letter[a-z a-z] delim[\t\n] W$delim+ID(letter)(letter/digit)+ Integer digit+ % % ws print ( SpecialCharacters ) ID print( Identifiers ) digit print( \n Integer) if printf( keyword ) else print(keyword) & & print(logoical operators) > print(logoical operators) < print(logoical operators) <= print(logoical operators) >= print(logoical operators) = printf( \n \n )! printf( \n \n ) + printf( arithmetic operator ) - printf ( arithmetic ) * printf( arithmetic ) % printf(arithmetic ) % % printf( arithmetic ) Write the auxillary procedure in main() function end Stop OUTPUT [root@localhost]# lex lexprog.l [root@localhost]# cc lex.yy.c [root@localhost]#./a.out lexprog
VIVA QUESTIONS 1. What is Parsing? 2. What is Token? 3. How many section in LEX program? 4. What is meant by extension of dot l(.l) in LEX? 5. What is the syntax of LEX compiler program to save it? 6. what is the output command of LEX?
EXPERIMENT: 3 NAME OF THE EXPERIMENT : Predictive Parser of the RECURSIVE DESCENT PARSER AIM : Design Predictive Parser of the RECURSIVE DESCENT PARSER given language Input String: cdefbcdbccdefbcdabcdefdef THEORY Recursive descent parsing is probably the most well-known and intuitive technique applicable to a subclass of context-free grammars. A subroutine for each non-terminal should determine a rule according to which the substring shall be parsed. Parse Tree:
ALGORITHM: Step1: Start Step2: Declaration of the functions int S(),int A(),int B(),int C(),int D(); Step3: Declare the variable char input[100];int i; Step4: Enter the string Step5: Read the by gets(input); Step6: The given input is true when (S()==1) Step7: Print string is accepted Step8: Otherwise print string is not accepted Step 9: called method int S() j if(a()==1) if(b()==1) if(input[i]=='a') if(input[i]=='b') if(c()==1) if(d()==1) if(input[i]=='e') if(input[i]=='f') return 1; Step10: called method int A() if(b()==1) if(input[i]=='b') if(input[i]=='c') return 1;
Step11: called method int B() if(c()==1) if(input[i]=='b') if(input[i]=='c') if(input[i]=='d') return 1; Step12: Called method int C() if(input[i]=='c') if(input[i]=='d') if(input[i]=='e') if(input[i]=='f') return 1; Step13: Called method int D() if(input[i]=='d')
return 1; Step14: Stop OUTPUT
VIVA QUESTIONS 1. What is Predictive parser? 2. How many types of analysis can we do using Parser? 3. What is Recursive Decent Parser? 4. What is LR Parser? 5. What is back Tracking? 6. What are the problems in Top-Down parsing?
EXPERIMENT: 4 NAME OF THE EXPERIMENT: Design of SLR AIM: Design SLR bottom up parser for the above language THEORY The easiest technique for generating LR-based parse table is known as SLR (Simple LR).Understanding this technique should provide and what you need to know to understand how LR parsers work in general. Remember that the idea behind LR parsing is to produce a DFA that defines the handles (string of terminals and non-terminals that indicate a reduction) of the input language. The SLR technique is based on generating sets of LR(0) items that describe the states of the DFA, as well as a transition function that maps between these states. Defn: An LR(0) item of a grammar G is a production of G with a dot (.) at some point on the right side. Defn: Closure(I) where I is a set of LR(0) items consists of Every item in I If A -> α. B β in closure(i), for all productions B -> γ, add B ->. γ to closure(i) if not already there. Keep applying this rule until no more items can be added. Initial elements (I) are often referred to as the kernel elements of closure(i). Defn: Goto(I,X), where I is a set of items, X is a terminal or non-terminal, is the closure(a a X. b) where A a. X b is in I. Algorithm: C =closure(s. S), where S S is the production added for augmentation Repeat For each item I in C and grammar symbol X such that Goto(I,X) is not empty and not in already an element of C Add Goto(I,X) to C ALGORITHM Step1: Start Step2: Initially the parser has s0 on the stack where s0 is the initial state and w$ is in buffer Step3: Set ip point to the first symbol of w$ Step4: repeat forever, begin Step5: Let S be the state on top of the stack and a symbol pointed to by ip Step6: If action [S, a] =shift S then begin Push S1 on to the top of the stack Advance ip to next input symbol Step7: Else if action [S, a], reduce A->B then begin Pop 2* B symbols of the stack Let S1 be the state now on the top of the stack Step8: Output the production A B End Step9: else if action [S, a]=accepted, then return Else Error() End Step10: Stop
TEST CASES OUTPUT1 OUTPUT2 VIVA QUESTIONS 1. What is SLR parsing? 2. What is Shift reduced parser? 3. What are the operations of Parser? 4. What is the use of parsing table? 5. What is bottom up parsing?
EXPERIMENT: 5 NAME OF THE EXPERIMENT: Convert the BNF rules into YACC AIM: Convert the BNF rules into YACC form and write code to generate abstract syntax tree THEORY Yacc is a tool for building syntax analyzers, also known as parsers, yacc has been used to implement hundreds of languages. Its applications range from small desk calculators, to medium-sized preprocessors for typesetting, to large compiler front ends for complete programming languages. A yacc specification is based on a collection of grammar rules that describe the syntax of a language; yacc turns the specification into a syntax analyzer. A pure syntax analyzer merely checks whether or not an input string conforms to the syntax of the language. ALGORITHM: Step1: Start Step2: declare the declarations as a header file include<ctype.h> Step3: token digit Step4: define the translations rules like line, expr, term, factor Line:exp \n print( \n %d \n,$1) Expr:expr + term ($$=$1=$3 Term:term + factor($$ =$1*$3 Factor Factor: ( enter ) $$ =$2) % % Step5: define the supporting C routines Step6: Stop OUTPUT: [root@localhost]# lex codegen.l [root@localhost]# yacc d codegen.y [root@localhost]# cc lex.yy.c y.tab.c ll -lm [root@localhost]#./a.out INPUT OUTPUT 2+3 4-2 5 2 VIVA QUESTIONS 1. What is Abstract Syntax tree? 2. What are BNF Rules? 3. What is DAG representation? 4. How LALR(1) states are generates? 5. In which condition the user has to supply more information to YACC?
EXPERIMENT : 6 NAME OF THE EXPERIMENT: To generate machine code AIM: Write program to generate machine code from the abstract syntax tree generated by the Parser.The following instruction set may considered as target code. THEORY Machine code, also known as machine language, is the elemental language of computers, comprising a long sequence of binary digital zeros and ones ALGORITHM Step1: Step2: Step3: Step4: Step5: Step6: Step7: Step8: Step9: Start for every three address statement of the form x=y op z begin Call getreg() to obtain the location L which the computation y op z should be performed Obtain the current location of the operand y by consulting its address descriptor,and if the values of Y are currently both in the memory location as well as in the register, then prefer the register.if the value of y is not currently available in 1,then generate an instruction MOV y,l Generate the instruction OP Z,l and update the address descriptor of X to indicate that X is now available in l and in register then update t\ its descriptor to indicate that it will contain the run time value of x If the current values of y ad/or z are in register and we have no further use for them,and they are live at the end of the block,then after the register descriptor to indicate that after the execution of the statem,ent x=y op z,those registers will no longer contain y and / or z. store all results Stop OUTPUT
VIVA QUESTIONS 1. What is target code? 2. What is machine code? 3. What is Cross compiler? 4. Give the example for cross compiler? 5. What is the difference between syntax & Semantics? Prepared by: Mrs K. Archana, Mrs. Durga Sree HOD, COMPUTER SCIENCE AND ENGINEERING