You are on page 1of 64

SEC

CSE/VI

PCD LAB MANUAL

SRINIVASAN ENGINEERING COLLEGE


COMPUTER SCIENCE AND ENGINEERING ANNA UNIVERSITY OF TECHNOLOGY TRICHIRAPALLI REGULATION 2008 III CSE (2010-2014)

CS1356 COMPILER DESIGN LABORATORY LAB MANUAL

BY
S.JAYANTHI, Assistant Professor. A.SATHIYA, Assistant Professor.

SEC

CSE/VI

PCD LAB MANUAL

Preface
This laboratory manual is prepared by the Department of Computer Science and engineeringfor Compiler Design Laboratory (CS-1356). Thislab manual can be used as

instructional book for students, staff and instructors to assist in performing and understanding the experiments. In the first part of the manual, experiments as per syllabus are described and in the second part of the manual, experiments that are beyond the syllabus but expected for university laboratory examination are displayed. Thismanual will be available in electronic form from Colleges official website,for the betterment of students.

Acknowledgement
Wewould like to express our profound gratitude and deep regards to the support offered by the ChairmanShri. A.Srinivasan. We also take this opportunity to express a deep sense of gratitude to our Principal Dr.B.Karthikeyan,M.E,Ph.D, for his valuable information and guidance, which helped us in completing this task through various stages.We extend our hearty thanks to our head of the department Prof.J.Mercy Geraldine M.E, (Ph.D), for her constant encouragement and constructive comments. Finally the valuable comments from fellow faculty and assistanceprovided by the department are highly acknowledged.

SEC TOPIC

CSE/VI

PCD LAB MANUAL PAGE NO 5

CHAPTER NO

Introduction to the language


I

Main Features Advantages Limitations Applications

II III

Syllabus System Requirements Description about each experiment Aim Learning Objective General procedure to execute programs Experiments
1 &2 3 4

6 7 8

IV

ImplementalexicalanalyzerinC. UseLEXtooltoimplementalexicalanalyzer. Implementarecursivedescentparserforanexpressiongramm arthat generatesarithmeticexpressionswithdigits,+and*. UseYACCandLEXtoimplementaparserforthesamegra mmarasgiveninproblem. Write semantic rules to the YACC program in problem 5 and implementacalculatorthattakesanexpressionwithdigits ,+and*andcomputesandprintsitsvalue. Implementthefrontendofacompilerthatgeneratesthethreead dresscode for a simple language with: one data type integer, arithmetic operators, relational operators, variable declaration statement, oneconditional construct,oneiterativeconstructandassignmentstatement. Implementthebackendofthecompilerwhichtakesthethreead dresscodegeneratedinproblems7and8,andproducesthe8086 assembly language instructions that can be assembled and run using a 8086 assembler.Thetargetassemblyinstructionscanbesimplemov e,add, sub,jump.Alsosimpleaddressingmodesareused.

10 22 29

5 6

33 37

7&8

44

9&10

51

VII VIII

Experiments beyond the syllabus Future Enhancements References

56 64

SEC

CSE/VI

PCD LAB MANUAL

INTRODUCTION OF THE LANGUAGE (Turbo C language) MAIN FEATURES

It is a robust language whose rich set of built-in functions and operators can be used to write any complex program C is highly portable. This means that C programs written for one computer can be run onanother with little or no modification. C language is well suited for structured programming, thus requiring the user to think of a problem in terms of function modules or blocks. Programs written in C are efficient and fast. This is due to its variety of data types andpowerful operators. It is many times faster than BASIC. Another important feature of C is its ability to extend itself. A C program is basically acollection of functions, which are supported by the C library.
ADVANTAGES

C source code can be optimized much more than higher-level languages because the language set is relatively small and very efficient C has which is its application in Firmware programming (hardware). That is due to its ability to use/work with assembly and communicate directly with controllers, processors and other devices. C is a building block for many other currently known languages. C is a compiled language versus an interpreted language. Explained simply, this means that the code is compacted into executable instruction (in the case of windows anyway) rather than being "translated" on the fly at run time.
LIMITATIONS

Weak text processing capabilities - C's string library is extremely primitive (it doesn't even define an actual string type), and text processing is labor-intensive and error-prone. Security and safety issues- several library functions (gets() being the most notorious) provide easy exploits for malware, and anything involving pointers is going to be unsafe (it's hard to guard against being passed an invalid pointer); Weak memory management capabilities - Like text processing, managing memory in C is labor-intensive and error-prone No built-in collections library - C doesn't provide readymade collections (lists, queues, stacks, etc.), meaning you have to roll your own. No built-in support for networking, sound, graphics, etc. - C is a product of the early 1970s and it shows; byte streams are about the highest level of abstraction you're going to find in the C library.

SEC APPLICATIONS

CSE/VI

PCD LAB MANUAL

C language is used for creating computer applications and also used a lot in writing embedded software/firmware for various electronics, industrial and communications products which use micro-controllers. It is also used in developing verification software, test code, simulators etc. for various applications and hardware products. C has been used successfully for every type of programming problem imaginable from operating systems to spreadsheets to expert systems. C was initially used for system development work, in particular the programs that make-up the operating system. C has been used as a general-purpose language because of its popularity with programmers.

SEC

CSE/VI SYLLABUS CS1356 COMPILERDESIGNLABORATORY

PCD LAB MANUAL

LISTOFEXPERIMENTS

1& ImplementalexicalanalyzerinC. 2 3 UseLEXtooltoimplementalexicalanalyzer. 4

Implementarecursivedescentparserforanexpressiongrammarthat generatesarithmeticexpressionswithdigits,+and*. UseYACCandLEXtoimplementaparserforthesamegrammarasgiveninproblem Write semantic rules to the YACC program in problem 5 and implementacalculatorthattakesanexpressionwithdigits,+and*andcomputesandprintsitsval ue.

7& Implementthefrontendofacompilerthatgeneratesthethreeaddresscode for a simple 8 language with: one data type integer, arithmetic operators, relational operators, variable

declaration statement, construct,oneiterativeconstructandassignmentstatement.

oneconditional

9& Implementthebackendofthecompilerwhichtakesthethreeaddresscodegeneratedinproblems 10 7and8,andproducesthe8086assembly language instructions that can be assembled and

run using a 8086 assembler.Thetargetassemblyinstructionscanbesimplemove,add, sub,jump.Alsosimpleaddressingmodesareused.

SEC

CSE/VI SYSTEM REQUIREMENTS

PCD LAB MANUAL

HARDWARE REQUIREMENTS Processors RAM Hard Disk Operating System 2.0 GHz or Higher 256 MB or Higher 20 GB or Higher Linux andWindows 2000/XP/NT

SOFTWAREREQUIREMENTS TURBO C (Freeware)

SEC

CSE/VI

PCD LAB MANUAL

ABOUT THE COMPILER LABORATORY AIM To provide an understanding of the design aspect of operating system. To introduce the major concept areas of language translation and compiler design. To develop an awareness of the function and complexity of modern compilers. To provide practical, hands-on experience in compiler design, writing and modification. To provide an introduction to the system software like assemblers, compilers, and macros. OVERVIEW OF THE EXPERIMENTS Lexical analyzerprogram that reads source program and produce a list of tokens (linear analysis) Lexis a program generator that takes lexical specification as input, and produces a lexical processor written in C. (The code for lex file can be saved with .l extension) YACCandLEX tools can generate code for a single function respectively, get a token form input stream and parse a sequence of tokens to see if it matches a grammer.. (The code for YACC file can be saved with .y extension) YACCtool is well implemented to perform calculator functionthattakesanexpressionwithdigits,+and*andcomputesandprintsitsvalue. Front end of the compiler parses and tokenizes the source file, verify syntax and semantics (the rules of the programming language), and translate the source code into an intermediate representation (threeaddresscode). Backend takes the intermediate representation of the code and converts it into targetassemblyinstructions. LEARNING OBJECTIVES The objective of this lab is to provide a student with an understanding of the fundamental principles in compiler design and to provide the skills needed for building compilers.They can also have knowledge of the underlying machine architecture, the limitations and efficiency of various design techniques of compilers implementation. It also covers programming in various tools like LEX and YACC for scanning and parsing etc.

SEC

CSE/VI

PCD LAB MANUAL

GENERAL PROCEDURE FOR EXECUTING PROGRAMS Steps to execute a C program with Linux utility. Step 1 :Use an editor, such as vi, ex, or ed to write the program. The name of the file containing the program should end in .c(Ex: vishow.c) For example, the file show.ccontains the following lines: main()
{

printf( welcome to GNEC ); } Step 2: To save the file :wq Step 3: Submit the file to CC ( the C Compiler ) $ cc show.c If the program is okay, the compiled version is placed in a file called a.out Step 4: To run the program, type a.out $ a.out

SEC
EX NO: 1&2

CSE/VI

PCD LAB MANUAL

IMPLEMENTATION OF A LEXICAL ANALYZER DATE :

DESCRIPTION Break input string into words called tokens The main functions of lexical analyzer are : Stripping out comments and white spaces Correlating error messages with the source program

OBJECTIVE The main objective is to to write a program to implement a lexical analyzer which can read input characters and group them into tokens.

HOW IT IS BEING ACHIEVED? It is achieved by extracting character from the expression using built in functions in C, such as is alpha(),isalnum(), etc.

SYNTAXES & KEYWORDS

isalpha()-Returns True if all characters in s are alphabetic, False otherwise isalnum ()-Returns True if all characters in s are alphanumeric, False otherwise.
HOW TO EXECUTE THE PROGRAM [cs303@localhost cs303]$ cc lexical

[cs303@localhost cs303]$ ./a.out


REQUIREMENTS FOR EXECUTION

S.No. 1 2 3

Facilities required System O/S S/W name 1

Quantity

Turbo C Compiler

10

SEC
EXPECTED OUTPUT AND ITS FORM

CSE/VI

PCD LAB MANUAL

This program takes a source program as input, and it is expected to produce a stream of tokens as output.
LIMITATIONS

The lexical analyzer reads source text and produces tokens, which arethe basic lexical units of the language. The limitations are some trailing context patterns cannot be properly matched and generate warning messages (dangerous trailing context).
APPLICATIONS

Lex, a programming tool for the Unix system, is a successful solution to the general problem of lexical analysis. Lex is targeted only C. It also places artificial limits on the size of strings that can be recognized. This feature is typically used to handle quoted strings with escapes to denote special characters. ALGORITHM
1.Start the program.

2.Extract first character from the expression using get char () function. 3.Check the character i) ii) iii) iv) v) vi) If it is a digit then print the token as number. If it is a +,-,*, or / then print the token as OPERATOR. If it is a <,>,<=,>=,/-,--, then print the token as RELATIONAL OPERATOR. If it is a (,), then print the token as PARANTHESIS. If it is an int,float,if,while, and etc then print the token as KEYWORD. If it is a Single letter on a letter followed by a digit or number, and then prints the token as IDENTIFIER. 4.Token is obtained using Step 3. 5. Go to Step3 otherwise proceed.

11

SEC PROGRAM PROGRAM NAME:lexical.c


#include<stdio.h> #include<string.h> #include<ctype.h> Void main() { charstr[MAX]; int state=0; inti=0,j,started=0,endid,startcon,endcon; for(j=0;j<MAX;j++) str[j]=NULL; printf(\nProgram on lexical analyzer\n); printf(\nEnter the string); gets(str); str[strlen(str)]=; printf(\nAnalysis); while(str[i]!=NULL) { while(str[i]==) i++; switch(state) { case 0: if(str[i]==i)state=1; else if(str[i]==w)state=3; else if(str[i]==d)state=8; else if(str[i]==e)state=10; else if(str[i]==f)state=14; else if(isalpha(str[i])||str[i]==_) { state=17; stated=I; } else if(str[i]==<)state=19; else if(str[i]==>)state=21; else if(str[i]===)state=23; { state=25; startcon=I; } else if(str[i]==()state=26;

CSE/VI

PCD LAB MANUAL

12

SEC
else if(str[i]==))state=27; else if(str[i]==;)state=28; else if(str[i]==+)state=29; else if(str[i]==-)state=30; break; case 1: if(str[i]==f)state=2; else { state=17; Startid=i-1; i--; } break; case 2: if(str[i]==c||str[i]==NULL) { printf(\nif:Keyword); state=0; i--; } else { state=17; started=i-2; i--; } break; case 3: if(str[i]==h)state=4; else { state=17; start=i-1; i--; } break; case 4: if(str[i]==I)state=5; else { state=17; state=i-2; i--;

CSE/VI

PCD LAB MANUAL

13

SEC
} break; case 5: if(str[i]==e)state=6; else { state=17; started=i-3; i--; } break; case 6: if(str[i]==e)state=7; else { state=17; state=i-4; i--; } break; case 7: if(str[i]==(||str[i]==NULL) { printf(\nWhile:Keyword); state=0; i--; } else { state=17; started=i-5; i--; } break; case 8: if(str[i]==0)state=9; else { state=17; startid=i-1; i--; } break;

CSE/VI

PCD LAB MANUAL

14

SEC
case 9: if(str[i]=={||str[i]==||str[i]==NULL||str==() { printf(\nDo:Keyword); state=0; i--; } break; case 10: if(str[i]==l'state=11; else { state=17; startid=i-1; i--; } break; case 11: if(str[i]==s)state=12; else { state=17; startid=i-2; i--; } break; case 12: if(str[i]==e)state=13; else { state=17; startid=i-3; i--; } break; case 13: if(str[i]=={||str[i]==||str[i]==NULL) { printf(\nElse:Keyword); state=0; i--; } else { state=17; startid=i-4;

CSE/VI

PCD LAB MANUAL

15

SEC

CSE/VI

PCD LAB MANUAL

i--; } break; case 14: if(str[i]==o)state=15; else { state=17; startid=i-1; i--; } break; case 15: if(str[i]==r)state=16; else { state=17; startid=i-2; i--; } break; case 16: if(str[i]==(||str[i]==NULL) { printf(\nFor:Keyword); state=0; i--; } else { state=17; startid=i-3; i--; } break; case 17: if(isalnum(str[i])||str[i]==-) { state=18; i++; } else if(str[i]==NULL||str[i]==<||str[i]==>||str[i]==(||str[i]==)||str[i]==;||str[i]==+||str[i]= state=18; i--;

=-)

16

SEC
break;

CSE/VI

PCD LAB MANUAL

case 18: if(str[i]==NULL||str[i]==<||str[i]==>||str[i]==(||str[i]==)||str[i]==;||str[i]==+||str[i]= =-) { endid=i-1; printf(\n); for(j=startid;j<endid;j++) printf(\n%c,str[i]); printf(\nn:Identifier); state=0; i--; } break; case 19: if(str[i]===) state=20; i--; else if(isalnum(str[i])||str[i]==-) { printf(\n<:Relational Operator); state=0; } break; case 20: if(isalnum(str[i]||str[i]==-) { printf(\n<=Relaitonal Operator); i--; state=0; } break; case 21: if(isalnum(str[i]||str[i]===) state=22; else if(isalnum(str[i])||str[i]==-) { printf(\n>:Relaitonal Operator); i--; state=0;

17

SEC

CSE/VI

PCD LAB MANUAL

} break; case 22: if(isalnum(str[i])||str[i]==-) { printf(\n>:Relaitonal Operator); i--; state=0; } break; case 23: if(str[i]===) state=24; else { printf(\n=:Assignment Operator); i--; state=0; } case 24: if(isalnum(str[i]) { printf(\n==:Relational Operator); state=0; } break; case 25: if(isalpha(str[i]) { printf(\n**ERROR**); puts(str); for(j=0;j<=I;j++) printf(\n); printf(\n); printf(\nError at position %d alphabet cannot follow digit,i); state=99; } else if(str[i]==(||str[i]==)||str[i]==<||str[i]==>||str[i]==NULL||str[i]==;||str[i]===) { endcon=i-1; printf(); for(j=startcon;j<=endcon;j++) printf(\n%c,str[i]); printf(\n:Constant);

18

SEC
state=0; i--; } break; case 26: printf(\n(:Special Character); startid=I; state=0; i--; break; case 27: printf(\n):Special Character); state=0; i--; break; case 28: printf(\n;:Special Character); state=0; i--; break; case 29: printf(\n+:Operator); state=0; i--; break; case 30: printf(\n-:Operator); state=0; i--; break; case 99: goto END; } i==; } printf(\nEnd of program); END: }

CSE/VI

PCD LAB MANUAL

19

SEC
OUTPUT

CSE/VI

PCD LAB MANUAL

[cs303@localhost cs303]$ cc lexical.c [cs303@localhost cs303]$ ./a.out Enter the string: (a+b) ),( b,a h+ : : : Special Character Identifier Arithmetic Operator

RESULT

The program has been executed to the lexical analyzer in C was implemented and the above output is obtained
VIVA QUESTIONS 1. List the various phases of a compiler. (Nov/Dec 2008) The following are the various phases of a compiler: Lexical Analyzer Syntax Analyzer Semantic Analyzer Intermediate code generator Code optimizer Code generator

2. Describe Lexical analyzer.

It converts a text representation of the program (sequence of characters) into a sequence of lexical unit for a particular language (tokens). A program which performs lexical analysis is called a lexical analyzer, lexer or scanner.
3.What is a symbol table? A symbol table is a data structure containing a record for each identifier, with fieldsfor the attributes of the identifier. The data structure allows us to find the record for eachidentifier quickly and to store or retrieve data from that record quickly. Whenever an identifier is detected by a lexical analyzer, it is entered into the symbol table. The attributes of an identifier cannot be determined by the lexical analyzer.

20

SEC
4.What is a compiler?

CSE/VI

PCD LAB MANUAL

A compiler is a program that reads a program written in one languagethe source language and translates it into an equivalent program in another language-the target language.The compiler reports to its user the presence of errors in the source program. POSSILBLE QUESTIONS 1. Implement a lexical analyzer Input string - a C program with Comments 2.Implement a lexical analyzer Input string - a C program to calculate leading of non terminal. 3.Implement a lexical analyzer Input string - a C program without Comments

21

SEC

CSE/VI

PCD LAB MANUAL

EX NO: 3 IMPLEMENTATION OF A LEXICAL ANALYZER USING LEXTOOL DATE :

DESCRIPTION

Lex is a tool for generating scanners. Scanners are programs thatrecognize lexical patterns in text. These lexical patterns (or regularexpressions) are defined in a particular syntax.
OBJECTIVE The main objective is lex is a tool that converts input information into a series of tokens. SYNTAXES & KEYWORDS

identifier [a-zA-Z][a-zA-Z0-9]* The shorthand character range construction [x-y] matches any of the characters

between (and including) x and y. For example, [a-c] means the same as a|b|c, and [a-cA-C] means the same as a|b|c|A|B|C. # assigns PREPROCESSOR DIRECTIVE If the statement begins with \* and ends with */ the Assign as COMMENT.

LEX SOURCE PROGRAM LEX SPECIFICATION The lex program consists of three parts %{ declarations %} %% translation rules %% auxillary procedures The declarations section includes declarations of variables,constants and regular definitions. The regular definitions are statements and are used as components of the regular expressions,appearing in the translation rules.

22

SEC

CSE/VI

PCD LAB MANUAL

The translation rules of a lex program are of the form P1{action 1} P2{action 2} . . . pn{action n} The simple lex example %{ #include <stdio.h> %} %% beginprintf("Started\n"); helloprintf("Hello yourself!\n"); thanksprintf("Your welcome\n"); endprintf("Stopped\n"); %% The first block, defined by the %{...%}, defines the text that will be inserted into thegenerated C source. In this case, because the examples later use the printf()function, you ensure that the stdio.h header is included. The second block, identified by the %% sequence, contains the definitions of theidentified string input and the result. In these cases, for a simple word, anappropriate response is printed. fopen() is used to open the source for scanning. identifier [a-zA-Z][a-zA-Z0-9]* is used to separate tokens that matches any of the characters between (and including) [].

HOW TO EXECUTE THE PROGRAM

[CS228@localhost cs228]$ vilexprogram.c [CS228@localhost cs228]$ lexprogram.l {cs228@localhost cs228]$cc lexprogram.yy.c [cs228@localhost cs228]$./a.outarea.c 23

SEC
REQUIREMENTS FOR EXECUTION

CSE/VI

PCD LAB MANUAL

S.No. 1 2 3

Facilities required System O/S S/W name 1 Linux Compiler

Quantity

EXPECTED OUTPUT AND ITS FORM: Lex tool inthis program is expected to produce tokens.
ADVANTAGES

It quickly generates solutions to problems that involve lexical analysis, that is, the recognition of strings of characters that satisfy certain characteristics. This enables to solve a wide class of problems drawn from text processing, code enciphering, compiler writing, and other areas.
LIMITATIONS

We can easily understand some of lexs limitations. For example, lex cannot be used to recognize nested structures such as parentheses. Nested structures are handled by incorporating a stack. Whenever we encounter a ( we push it on the stack. When a ) is encountered we match it with the top of the stack and pop the stack. However lex only has states and transitions between states.
APPLICATIONS

Lex is used to create a sample lexical analyzer for c programming language;it can recognize the valid symbols in c programming language including valid programming constructs.

24

SEC ALGORITHM 1. Initialize COMMENT=0

CSE/VI

PCD LAB MANUAL

Declare the Statement rule for the identifier. 2. If the statement begins with # then assignPREPROCESSOR DIRECTIVE. If the variables are declared as Int, Float, Char, Double, Long, Struct and Words are like whilefor break and Then assign as KEYWORDS. 3. If the statement begins with # then assignPREPROCESSOR DIRECTIVE. If the variables are declared as Int, Float, Char, Double, Long, Struct and Words are like while for break and Then assign as KEYWORDS. If it is a not a COMMENT, then mark as FUNCTION. 4. If the word enclosed with then print it as string and if it has [0-9] then Print as NUMBER 5. It contains operators like <, >, <=, >=, := then assign as Relational Operators. PROGRAM
%{ Int COMMENT=0; %} Identifier[a-zA-Z][a-zA-Z0-9]* %% #.*{print{\n %s is a PREPROCESSOR DIRECTIVE,yynext);} Int| Float| Char| double| while| do| for| if|break| continue| void| switch| case| long| struct| const| tupedef|

25

SEC

CSE/VI

PCD LAB MANUAL

return| else| goto { print(\n|n\t %s is a KEYWORD,YYTEXT);} /*{COMMWNR=1;} */{COMME[NT=0;} {identifier}\( {if(!COMMENT)printf(\n\nFUNCTION \n\t%s,yytext);} \{ {if(!COMMENT)printf(\n BLOCK BEGINS);} \} {if(!COMMENT)printf(\n BLOCK ENDS);} {identifier}(\[0-9]*\])? {if(!COMMENT)printf(\n %s IDENTIFIER,yytext);} \.*\ {if(!COMMENT)printf(\n\t%s is a string,yytext);} [0-9)+ {if(!COMMENT)printf(\n\t %s is a NUMBER,yytext);} \)(\;)? {if(!COMMENT)printf(\n\t);ECHO;printf(\n);} \(ECHO; ={if(COMMENT)printf(\n\t%s is anASSIGNMENTOPERATOR,yytext);] \<=| \>=| \<| ==| \>{if(!COMMENT)printf(\n\t is a RELATIONAL OPERATOR,yytext);} .|\n %% int main(intargc,char**argv) { if(argc>1) { FILE *file; file=fopen(argv[1],r); if(!file) { printf(could not open %s \n,argv[1]); exit(0); } yyin=file; } yylex(); printf(\n\n); return(); }

//input program saved in .c


#include<stdio.h> #include<stdlib.h> double area of circle(double v); int main(intargc,char *argv[]) {

26

SEC
if(argc<2) { printf(usage;%s radius\n,argv[0]); exit(1);

CSE/VI

PCD LAB MANUAL

} else { double radius =atof(argv[1]); double area=area_of_circle_radius); printf(area of circle with radius %f-%f\n,radius,area); } return0; }

OUTPUT [CS228@localhost cs228]$lexprogram.l {cs228@localhost cs228]$cc lexprogram.yy.c [cs228@localhost cs228]$./a.outarea.c RESULT The program has been executed to the lexical analyzerusing lex tool in C was implemented and the above output is obtained
VIVA QUESTIONS

1. List the various error recovery strategies for a lexical analysis. Possible error recovery actions are: Panic mode recovery Deleting an extraneous character Inserting a missing character Replacing an incorrect character by a correct character Transposing two adjacent characters

2.Define back patching. Back patching is the activity of filling up unspecified information of labels usingappropriate semantic actions in during the code generation process.In the semanticactions the functions used are mklist(i),merge_list(p1,p2) and back patch(p,i)

27

SEC

CSE/VI

PCD LAB MANUAL

3.Mention the functions that are used in back patching. (i)creates the new list. The index iis passed as an argument to this function Where I is an index to the array of quadruple. (ii)merge_list (p1,p2) this function concatenates two lists pointed by p1 and p2. It returns the pointer to the concatenated list. (iii)backpatch (p,i) inserts i as target label for the statement pointed by pointer p.

POSSIBLE QUESTIONS 1. 2. 3. Write a Program to insert a character string into a file from the console.
Write a Program to recognize white space, count no. of identifiers, characters, tabs & the length of the input strings Write a Program to generate a token from input string

28

SEC
EX NO: 4 DATE :

CSE/VI

PCD LAB MANUAL

IMPLEMENTATION OF A RECURSIVEDESCENTPARSER

DESCRIPTION

A recursive-descent parser is simple to construct from a classical context-free grammar if the grammar has the so-called LL(1) property; it means that the parser can always decide what to do by looking at the next input character.
OBJECTIVE

To develop a recursive descendent parserthat attempts to verify that the syntax of the input stream is correct as it is read from left to right.
SYNTAXES & KEYWORDS

Parsing Expression Grammar is a list of one or more rules of the form: name= expr; where expris a parsing expression, and name is a name given to it. The name is a string of one or moreletters (a-z, A-Z) and/or digits, starting with a letter. White space is allowed everywhere except insidenames. A recursive descent parser traverses the tree by first calling a procedure to recognize an E.

This procedure reads an 'x' and a '+' and then calls a procedure to recognize a T. This would look like the following routine.

HOW TO EXECUTE THE PROGRAM The program can be executed using a Turbo c compiler. EXPECTED OUTPUT AND ITS FORM

This program is expected to verify that the syntax of the input stream by reading it left to write for its correctness.

29

SEC
ADVANTAGES

CSE/VI

PCD LAB MANUAL

A recursive descent parser is clearly much simpler than an LL parser to implement, however (just as an LL one is simply than an LR one). Recursive-descent can handle any grammar which is LL(*) (that is, unlimitedlookahead) as well as a small set of ambiguous grammars. Recursive descent parsers tend to be faster but can be more complicated. Consequently, recursive descent parsers are generally only used for simple tasks and grammar based parsers are used to parse more sophisticated languages.
LIMITATIONS

The main limitation of recursive descent parsing (and all top-down parsing algorithms in general) is that they only work on grammars with certain properties. For example, if a grammar contains any left recursion, recursive descent parsing doesn't work. Recursive descent parsing are its exponential time complexity and non-termination in the face of left recursion.
APPLICATIONS

A Recursive Descent Parser supports the flexibility of an external DSL without requiring parser generator. The Recursive Descent Parser can be implemented in whatever general-purpose language one chooses. It uses control flow operators to implement the various grammar operators. Individual methods or functions implement the parsing rules for the different nonterminal symbols in the grammar. ALGORITHM 1) Start the program. 2) Declare variables for buffer size and length. 3) Get the expansion for which recursive descent parsing to be implemented. 4) Recursive descent function is called. 5) If given expression is not regular error message is displayed. 6) Recursive Descent parser is implemented and expression is displayed. 7) Stop the program.

30

SEC PROGRAM #include<stdio.h> #include<string.h> void A(void); char string[10]; intip_ptr = 0; inti; int main() { int length = 0; printf("Recursive Decent parser\n"); printf("\n\nProduction is:\n"); printf("\n\ts->cAb\n\tA->ad/a\n"); printf("\nEnter string: "); scanf("%s", string); length = strlen(string); if (string[i] == 'c') { ip_ptr++; i++; A(); if (string[i] == 'b') { ip_ptr++; i++; } } if (length == ip_ptr&& length > 2) printf("\nValid String\n"); else printf("\nInvalid String\n"); return 0; } void A() { if (string[i] == 'a') { i++; ip_ptr++; if (string[i] == 'd') { i++; ip_ptr++; } } }

CSE/VI

PCD LAB MANUAL

31

SEC
OUTPUT

CSE/VI

PCD LAB MANUAL

Recursive Decent parser Production is: s->cAb A->ad/a Enter string: ab Invalid String RESULT The program has been executed to the implementarecursivedescentparserforanexpressiongrammar w a s implemented and the above output is obtained VIVA QUESTIONS 1. Define recursivedescentparser. A basic operation necessary for this involves reading characters from the input stream and matching then with terminals from the grammar that describes the syntax of the input. Our recursive descent parsers will look ahead one character and advance the input stream reading pointer when proper matches occur. 2. Define chain rule. Ridding a grammar of chain rules merely involves substitution and makes the resultant parser more efficient. 3. Define Top-down parsing. Start at the root of the parse tree and grow toward leaves Pick a production & try to match the input Bad pick may need to backtrack POSSIBLE QUESTIONS: 1.Write a Program to read a text file. 2.Write a program to eliminate left recursion 3.Write a program which reads a left-recursive regular grammar, and removes left recursion from the grammar.

32

SEC
EX NO:5 DATE :

CSE/VI

PCD LAB MANUAL

IMPLEMENTATION OF PARSER USING YACC AND LEX

DESCRIPTION YACC and LEX tool are used for analyzing the subset of CProgram thatrecognize a valid arithmetic
expression.

OBJECTIVE Be proficient on writing grammars to specify syntax Understand the theories behind different parsing strategies-their strengths and limitations Understand how the generation of parser can be automated Be able to use YACC to generate parsers

SYNTAXES & KEYWORDS

o The symbols have higher precedence than symbols declared before in a %left, %right or %nonassoc line. o They have lower precedence than symbols declared after in a %left, %right or %nonassoc line. The symbols are declared to associate to the left (%left), to the right (%right), or to be non-associative (%nonassoc).

REQUIREMENTS FOR EXECUTION S.No. 1 2 3 Facilities required System O/S S/W name 1 LINUX Compiler Quantity

33

SEC
HOW TO EXECUTE THE PROGRAM ?

CSE/VI

PCD LAB MANUAL

The program can be executed C compiler in Linux OS. $ lex prog1.l $ yacc -d prog1.y $ cc -c lex.yy.cy.tab.c $ cc -o a.outlex.yy.oy.tab.o -lfl $ ./a.out
EXPECTED OUTPUT AND ITS FORM

This program is expected to verify the given arithmetic operation for its correctness.

ADVANTAGES Parser using YACC AND LEX tool can be used to check arithmetic expression for its correctness.

ALGORITHM

1. Include the necessary header files. 2. Declare the semantic rule for the identifier and number. 3. If the statement begins with main (), if else * while, the return as MAIN, IF ELSE and WHILE. If the variables are declared as int, float and char then return as VAR and NUM 4. Include the necessary header files. Initialize the err no=0 and declare like no as integer. 5. Declare the necessary tokens for the grammar.

34

SEC PROGRAM
LEX PART

CSE/VI

PCD LAB MANUAL

%{#include "y.tab.h" %} %% [a-zA-Z] {return ALPHA;} [0-9]+ {return NUMBER;}[\t\n]+ ; . {returnyytext[0];}%% YACC PART %{#include<stdio.h> %} %token NUMBER ALPHA %left '+''-' %left '*''/' %left '('')' %% expr:'+'expr |'-'expr |expr'+'expr |expr'-'expr |expr'*'expr |expr'/'expr |'('expr')'|NUMBER |ALPHA; %% int main() { printf("enter an arithematic expression\n"); yyparse(); printf("arithematic expression is valid\n"); return 0; } intyyerror(char *msg) { printf("\n%s",msg); printf("\narithematic expression is invalid"); exit(0); }

35

SEC
OUTPUT $ lex prog1.l $ yacc -d prog1.y $ cc -c lex.yy.cy.tab.c $ cc -o a.outlex.yy.oy.tab.o -lfl $ ./a.out enter an arithmetic expression(a+d)*(c-e) Arithmetic expression is valid

CSE/VI

PCD LAB MANUAL

RESULT The program has been executed to the parser using YACC and LEX TOOL was implementedand the above output is obtained. VIVA QUESTIONS 1. Explain yacc and lex tool Lex and Yacc can generate program fragments that solve the first task. The task of discovering the source structure again is decomposed into subtasks: 1. Split the source file into tokens (Lex). 2. Find the hierarchical structure of the program (Yacc) 2. Defineflex, a fast scanner generator Flex is a tool for generating scanners: programs which recognized lexical patterns in text. flex reads the given input files, or its standard input if no file names are given, for a description of a scanner to generate 3. Define Bison, The YACC-compatible Parser Generator Bison is a general-purpose parser generator that converts a grammar description for an LALR(1) context-free grammar into a C program to parse that grammar POSSIBLE QUESTIONS 1. Convertthe BNF rules into Yacc form and write codetogenerate abstract syntax tree 2. Write a Program to read a text file and copy it into the other text file.
3.Implement parser using yacc

36

SEC
EX NO: 6 DATE :

CSE/VI

PCD LAB MANUAL

IMPLEMENTATION OF CALCULATOR USING YACC

DESCRIPTION

In this programs two classical tools for compilers, Lex and Yacc are used to create a simple, desk-calculator program that performs addition, subtraction, multiplication, and division operations. OBJECTIVE To write semantic rules to the YACC program and implement a calculator that takes an expression with digits + and * and computes and prints its values.

SYNTAXES & KEYWORDS

LEX tool Input to Lex is divided into three sections with %% dividing the sections. This is best illustrated by example. .definitions.. %% rules. %% subroutines. YACC tool %token symbol...symbol Declare the given symbols as tokens (terminal symbols). These symbols are added as constant constructors for the token concrete type. %token <type>symbol...symbol Declare the given symbols as tokens with an attached attribute of the given type. %start symbol...symbol Declare the given symbols as entry points for the grammar. %type <type>symbol...symbol 37

SEC %left symbol...symbol %right symbol...symbol %nonassocsymbol...symbol

CSE/VI

PCD LAB MANUAL

Associate precedences and associativities to the given symbols. All symbols on the same line are given the same precedence. They have higher precedence than symbols declared before in a %left, %right or %nonassoc line. They have lower precedence than symbols declared after in a %left, %right or %nonassoc line. The symbols are declared to associate to the left (%left), to the right (%right), or to be non-associative (%nonassoc). %% . %%
Indicates the rules section of the YACC program

In this program two classical tools for compilers are user, that are o Lex: A Lexical Analyzer Generator o Yacc: Yet Another Compiler Compiler (Parser Generator) Lex creates programs that scan tokens one by one. Yacc takes a grammar (sentence structure) and generates a parser. In the first part of the program contains source code for Lex tool and the second part of the program contains YACC tool which groups the tokens logically.

The program can be executed by using c compiler with linux utility.

38

SEC

CSE/VI

PCD LAB MANUAL

To create the desk calculator example program, do the following: 1. Process the yacc grammar file using the -d optional flag (which informs the yacc command to create a file that defines the tokens used in addition to the C language source code): yacc -d desk.yacc 2. Use the ls command to verify that the following files were created: y.tab.c The C language source file that the yacc command created for the parser y.tab.h A header file containing define statements for the tokens used by the parser 3. Process the lex specification file: lexdesk.lex 4. Use the ls command to verify that the following file was created: lex.yy.c The C language source file that the lex command created for the lexical analyzer 5. Compile and link the two C language source files: cc y.tab.clex.yy.c 6. Use the ls command to verify that the following files were created: y.tab.o The object file for the y.tab.c source file lex.yy.o The object file for the lex.yy.c source file a.out The executable program file To run the program directly from the a.out file, type: $ a.out OR To move the program to a file with a more descriptive name, as in the following example, and run it, type: $ mva.out calculate $ calculate

39

SEC HOW TO EXECUTE THE PROGRAM [root@localhost ~] # lexdesk.l [root@localhost ~] # yacc d desk.y [root@localhost ~] # lex.yy.cy.tab.c ll [root@localhost ~] #./a.out REQUIREMENTS FOR EXECUTION

CSE/VI

PCD LAB MANUAL

S.No. 1 2 3

Facilities required System O/S S/W name 1 Linux Compiler

Quantity

EXPECTED OUTPUT Resultant value for the arithmetic operations. USE OF THIS OUTPUT The output can be used to decompose the compiler into two parts read the source program and

discover its structure(to solve this LEX and YACC used) from which target program can be generated.
ADVANTAGES In this program, single back end is developed for single source language.

It also has the advantage of allowing the use of a single back end for multiple source languages, and similarly allows the use of different back ends for different targets.
APPLICATIONS This program can be used to develop lexical analyzer and parser for a compiler using C programming language.

40

SEC

CSE/VI

PCD LAB MANUAL

ALGORITHM

1 . Start the program. 2.Perform the calculation using both the lex and yacc. 3.In the lex tool, if the given expression contains numbers and letters then they are displayed. 4.In the same way, the digits, letters and uminus are identified and displayed using yacctool. 5.The calculation is performed and the result is displayed. 6.Stop the program. PROGRAM LEX TOOL %{ #include "y.tab.h" #include<math.h> %} %% [0-9]+ { yylval.dval = atof(yytext); return NUMBER; } [0-9]+\.[0-9]+ { yylval.dval = atof(yytext); return NUMBER; } [a-z] { yylval.vblname = yytext[0]; return NAME; } [ \t] { } \n { return 0; } . { returnyytext[0]; } %% YACC TOOL %{ #include<math.h> #include<stdio.h> %} %union { 41

SEC

CSE/VI

PCD LAB MANUAL

doubledval; charvblname; } %token <vblname> NAME %token <dval> NUMBER %left '+' '-' %left '*' '/' %nonassoc UMINUS %type <dval> expression %% statement: NAME '=' expression { printf("%c = %g \n",$1,$3); } | expression { printf("= %g \n",$1); } ; expression: expression '+' expression { $$ = $1 + $3; } | expression '-' expression { $$ = $1 - $3; } | expression '*' expression { $$ = $1 * $3; } | expression '/' expression { if($3 == 0.0) { yyerror("Divide by zero"); } else $$ = $1 / $3; } | '(' expression ')' { $$ = $2; } | '-' expression %prec UMINUS { $$ = -$2; } | NUMBER { $$ = $1; } ; %% main() { yyparse(); } intyyerror (char *s) { printf("%s\n",s); exit(0); }

42

SEC

CSE/VI

PCD LAB MANUAL

OUTPUT [root@localhost ~] # lexdesk.l [root@localhost ~] # yacc d desk.y [root@localhost ~] # lex.yy.cy.tab.c ll [root@localhost ~] #./a.out ((2+3) + (4+5)) = 26 RESULT The program has been executed to lex and yacc tool is developed and a calculator operation is achieved. VIVA QUESTION AND ANSWERS 1. What is LEX? Lex is a computer program that generates lexical analysis ("scanners" or "lexers"). 2. Write the Structure of a Lex file. The structure of a Lex file is divided into three sections, separated by lines that contain only two percent signs, as follows: Definition section %% Rules section %% C code section 3. What is YACC? The Yacc is a computer program used to generate parser.

POSSIBLE QUESTIONS

1. Develop a parser which accepts a string and reports whether it is a valid SQL query statement or not. 2. Implementa Lexical analyzer for identifying different types of token usingyacc and lex tool 3.Design a parser which accepts a string and tells whether the string is accepted by above grammar or not using lex and yacc tool 43

SEC

CSE/VI

PCD LAB MANUAL

EX NO: 7& 8

IMPLEMENTATION OF FRONT END OF COMPILER


DATE :

DESCRIPTION The front end analyzes the source code to build an internal representation of the program, called the intermediate representation of source code. OBJECTIVE To implement the front end of a compiler that generators the three address code for a simple language with one data type integer arithmetic operator relational operators variable declaration statement one conditional construct one iterative construct and assignment statements. Front end of the compiler can be developed by using the features of C programming language. IMPORTANT SYNTAXES & KEYWORDS scanf("%s",&pg[i]); if((strcmp(pg[i],"getch();"))==0) if(pg[j][1]=='=') while((strcmp(pg[j],"}"))!=0) if((strcmp(pg[j],"}"))==0) DESCRIPTION In this program first the program from the user is received an input. The operators are compared to perform its relative operations for which assembly code will be created. HOW TO EXECUTE THE PROGRAM The program can be executed by using turbo Ccompiler(Alt+F9 Ctrl+F9 to Run) for compilation,

44

SEC REQUIREMENTS FOR EXECUTION S.No. 1 2 3 Facilities required System O/S S/W name 1

CSE/VI

PCD LAB MANUAL

Quantity

Windows XP Compiler

EXPECTED OUTPUT AND ITS FORM Front end of the compiler is expected to generate intermediate representation of source code. USE OF THIS OUTPUT This program, the front end translates the source language into an intermediate representation. This output can be given as the input to the second stage(or pass/ the back end) of the compiler. ADVANTAGES Developing front end of the compiler resolves the disadvantage of compiling in a single pass where it is not possible to perform many of the sophisticated compiler optimization needed to generate high quality code. LIMITATIONS It can be difficult to count exactly how many passes an optimizing compiler makes. For instance, different phases of optimization may analyse one expression many times but only analyse another expression once. APPLICATIONS This program can be used to develop a front end of a compiler using c programming language. ALGORITHM 1) Start the program. 2) Get the coding from the user. 3) Find the operators, arguments and results from the coding. 4) Display the value in the table. 5) Stop the program. PROGRAM 45

SEC

CSE/VI

PCD LAB MANUAL

#include<stdio.h> #include<conio.h> #include<string.h> void main() { char pg[100][100],str1[24];int tem=-1,ct=0,i=-1,j=0,j1,pos=-1,t=-1,flag,flag1,tt=0,fg=0; printf("Enter the codings \n"); while(i>-2) { i++;lab1:t++; scanf("%s",&pg[i]); if((strcmp(pg[i],"getch();"))==0) { i=-2;goto lab1; } } printf("\n pos \t oper \t arg1 \t arg2 \tresult \n"); while(j<t) { lop:ct=0; if(pg[j][1]=='=') { pos++; tem++; printf("%d\t%c\t%c\t%c\tt%d\n",pos,pg[j][3],pg[j][2],pg[j][4],tem); pos++; printf("%d\t:=\tt%d\t\t%c\n",pos,tem,pg[j][0]); } else if(((strcmp(pg[j],"if"))==0)||((strcmp(pg[j],"while"))==0)) { if((strcmp(pg[j],"if"))==0) strcpy(str1,"if"); if((strcmp(pg[j],"while"))==0) strcpy(str1,"ehile"); j++; j1=j; tem++; pos++; if(pg[j][3]=='=') 46

SEC

CSE/VI

PCD LAB MANUAL

printf("%d\t%c\t%c\t%c\tt%%d\n",pos,pg[j][2],pg[j][1],pg[j][4],tem); else printf("%d\t%c\t%c\t%c\tt%d\n",pos,pg[j][2],pg[j][1],pg[j][3],tem); j1+=2; pos++; while((strcmp(pg[j],"}"))!=0) { j++; if(pg[j][1]=='=') { tt=j; fg=1; } ct++; } ct=ct+pos+1; printf("%d\t==\tt%d\tFALSE\t%d\n",pos,tem,ct); if(fg==1) { j=tt;goto lop; } while((strcmp(pg[j],"}"))!=0) { pos++; tem++; printf("%d\t%c\t%c\t%c\tt%d\n",pos,pg[j][3],pg[j][2],pg[j][4],tem); pos++; printf("%d\t:=\tt%d\t\t%c\n",pos,tem,pg[j][0]); j++; } if((strcmp(pg[j+1],"else"))==0) { ct=0; j++; j1=j; j1+=2; pos++; while((strcmp(pg[j],"}"))!=0) { j1++;ct++; } ct=ct*2; 47

SEC

CSE/VI

PCD LAB MANUAL

ct++;ct+=(pos+1); j+=2; printf("%d\tGOTO\t\t\\t%d\n",pos,ct); while((strcmp(pg[j],"}"))!=0) { pos++; tem++; printf("%d\t%c\t%c\t%c\tt%d\n",pos,pg[j][3],pg[j][2],pg[j][4],tem); pos++; printf("%t:=\tt%d\t\t%c\n",pos,tem,pg[j][0]); j++; } pos++; printf("%d\tGOTO\t\t\t\%d\n",pos,ct); } } if((strcmp(pg[j],"}"))==0) { pos++; printf("%d\tGOTO\t\t\t%d\n",pos,pos+1); } j++; } getch(); }

48

SEC OUTPUT Enter the codings void main() { if(a<b) { a=a+b; } while(b<c) { b=b+c; } getch(); } posoper arg1 arg2 result 0 + a b t0 1 := t0 a 2 GOTO 3 3 + b c t1 4 := t1 b 5 GOTO 6 RESULT

CSE/VI

PCD LAB MANUAL

The programhas been executed to implement the front end of the compiler and the above output is obtained. VIVA QUESTIONS 1. Describe about front end and its phases. The front end consists of those phases or parts of phases that depend primarily on the source languageand are largely independent of the target machine. These include Lexical and Syntactic analysis The creation of symbol table Semantic analysis Generation of intermediate code A certain amount of code optimization can be done by the front end as well. Also includes error handlingthat goes along with each of these phases.

2. What is Intermediate Code Generation? 49

SEC

CSE/VI

PCD LAB MANUAL

After syntax and semantic analysis, some compilers generate an explicit intermediate representationof the source program. This intermediate representation can have a variety of forms. In three-address code, the source program might look like this, temp1: = inttoreal (10) temp2: = id3 * temp1 temp3: = id2 + temp2 id1: = temp3 3. What is meant by Code Optimisation? The code optimization phase attempts to improve the intermediate code, so that faster runningmachine codes will result. Some optimizations are trivial. There is a great variation in the amount of code optimization different compilers perform. In those that do the most, called optimising compilers, a significant fraction of the time of the compiler is spent on this phase. 4. What is meant by Code Generation The final phase of the compiler is the generation of target code, consisting normally of relocatable machine code or assembly code. Memory locations are selected for each of the variablesused by the program. Then, intermediate instructions are each translated into a sequence of machineinstructions that perform the same task. A crucial aspect is the assignment of variables to registers. 5. What are the advantages of using an intermediate language? Retargeting - Build a compiler for a new machine by attaching a new code generator to anexisting front-end. Optimization - reuse intermediate code optimizers in compilers for different languages anddifferent machines. Note: the terms intermediate code, intermediate language, and intermediaterepresentation are all used interchangeabl

POSSIBLE QUESTIONS 1. .Develop an interpreter which understands the assembly language instructions for Intel-8085 microprocessor, and executes the instructions correctly. User should be able to see the values in all relevant registers (registers will be implemented through variables) at any time 2.Implement a C program for front end of compiler

50

SEC
EX NO: 9, 10 DATE :

CSE/VI

PCD LAB MANUAL

IMPLEMENTATION OF BACK END OF COMPILER

DESCRIPTION The back end is responsible for translating the intermediate representation of the source code from the middle-end into assembly code. OBJECTIVE To implement the back end of the compiler which takes the three address code and produces the 8086 assembly language instructions that can be assembled and run using a 8086 assembly.
SYNTAXES & KEYWORDS

fopen(kk,"r"); this statement opens a file(kk) in read mode. printf("\t\tMOV %c,R%d\n\t",ip[i+k],j); if(ip[i+1]=='+') printf("\t\tADD"); // If the operator is an addition (+) then display the assembly code MOV ADD and store the result to the corresponding R elseprintf("\t\tSUB"); // else display the assembly code MOV SUB and store the result to the corresponding R In the first part of the program Open a file with read mode and read the content of the file one by one and get the first three address code. Check the arithmetic operator If the operator is an addition (+) then display the assembly code ADD and store the result to the corresponding R and if the operator is a subtraction (-) then display the assembly code SUB and store the result to the corresponding register

HOW TO EXECUTE THE PROGRAM The program can be executed by using turbo C compiler. The program can be executed by using turbo Ccompiler(Alt+F9 Ctrl+F9 to Run) for compilation,

51

SEC REQUIREMENTS FOR EXECUTION S.No. 1 2 3 Facilities required System O/S S/W name 1

CSE/VI

PCD LAB MANUAL

Quantity

Windows XP Compiler

EXPECTED OUTPUT AND ITS FORM Back end of the compiler is expected to generate assembly language as the output from the intermediate code. USE OF THIS OUTPUT Many modern compilers share a common 'two stage' design. The front end translates the source language into an intermediate representation. The second stage is the back end, which works with the internal representation to produce code in the output language. The front end and back end may operate as separate passes, or the front end may call the back end as a subroutine, passing it the intermediate representation. This approach mitigates complexity separating the concerns of the front end, which typically revolve around language semantics, error checking, and the like, from the concerns of the back end, which concentrates on producing output that is both efficient and correct. ADVANTAGES AND LIMITATIONS In this program, single back end is developed for single source language. It also has the advantage of allowing the use of a single back end for multiple source languages, and similarly allows the use of different back ends for different targets. APPLICATIONS This program can be used to develop a back end of a compiler using C programming language.

52

SEC ALGORITHM 1)Start the program.

CSE/VI

PCD LAB MANUAL

2)Get the three variables from statements and stored in the text file k.txt. 3)Compile the program and give the path of the source file. 4) Execute the program. 5)Target code for the given statement was produced. 6)Stop the program.

PROGRAM #include<stdio.h> #include<conio.h> #include<ctype.h> #include<stdlib.h> void main() { inti=2,j=0,k=2,k1=0; charip[10],kk[10]; FILE *fp; clrscr(); printf("\nEnter the filename of the intermediate code"); scanf("%s",&kk); fp=fopen(kk,"r"); if(fp==NULL) { printf("\nError in Opening the file"); getch(); } clrscr(); while(!feof(fp)) { fscanf(fp,"%s\n",ip); printf("\t\t%s\n",ip); } rewind(fp); printf("\n------------------------------\n"); printf("\tStatement \t\t target code\n"); printf("\n------------------------------\n"); 53

SEC while(!feof(fp)) { fscanf(fp,"%s",ip); printf("\t%s",ip); printf("\t\tMOV %c,R%d\n\t",ip[i+k],j); if(ip[i+1]=='+') printf("\t\tADD"); elseprintf("\t\tSUB"); if(islower(ip[i])) printf("%c,R%d\n\n",ip[i+k1],j); else printf("%c,%c\n",ip[i],ip[i+2]); j++;k1=2;k=0; } printf("\n-------------------------------\n"); getch(); fclose(fp); } OUTPUT

CSE/VI

PCD LAB MANUAL

Enter the filename of the intermediate code k.txt X=a-b Y=a-c z=a+b C=a-b C=a-b -----------------------------Statement target code -----------------------------X=a-b MOV b,R0 SUBa,R0 Y=a-c MOV a,R1 SUBc,R1 MOV a,R2 ADDb,R2 MOV a,R3 SUBb,R3 54

z=a+b

C=a-b

SEC

CSE/VI

PCD LAB MANUAL

C=a-b

MOV a,R4 SUBb,R4

------------------------------RESULT Thus the above the program is executed and the required output is obtained.

VIVA QUESTIONS 1. Define three address code. Three address code is a sequence of statements of the general form x : = y op z where x,y,z are operand and op is operator. The back end of compiler includes those portions that depend on the target machine and generallythose portions do not depend on the source language, just the intermediate language. These include Code optimization Code generation, along with error handling and symbol- table operations. 2. Write short notes on YACC. YACC is an automatic tool for generating the parser program. YACC stands for Yet Another Compiler Compiler which is basically the utility available from UNIX.Basically YACC is LALR parser generator. It can report conflict or ambiguities in the form of error messages.

POSSIBLE QUESTIONS 1. Write a Program to implement Predictive parsing table. 2.Implement the back end of the compiler which takes the three addresscode generated in problems 7 and 8, and produces the 8086 assemblylanguage instructions that can be assembled and run using a 8086assembler.

55

SEC

CSE/VI

PCD LAB MANUAL

EXPERIMENTS BEYOND THE SYLLABUS SYMBOL TABLE MANIPULATION


OBJECTIVE To write a program in C for manipulating Symbol Table.

ALGORITHM 1. Start the program. 2. Declare the variables. Get the character and check it using while Loop. 3. If n value is less than or equal to I then print the symbol address and type. Again check the n value with j. 4. The C value is changed to ASCII and check it Using if Store the C value in P and print the Identifier. Else check the character is equal to +, -, *, /, (, ) using if statement. statement.

5. Using for loop check I value with n and using if statement check the CH is equal to d[i] and print the address. Indicate flag as 0 and check the flag value is equal to zero using symbol table not found 6. Stop the program.

56

SEC PROGRAM

CSE/VI

PCD LAB MANUAL

#include<stdio.h> #include<conio.h. #include<alloc.h> void main() { int j=0,i=0,x=0,n; int flag=0; int *p,*add[10]; charc,ch,srch,b[10],d[10]; clrscr(); printf(\nEnter an expression and it is terminated by $); while((c=getchar())!=$) { b[i]=c; i++; } n=i-1; i=0; printf(Symbol Table\n); printf(\nSymbol\t\tAddress\t\ttype); while(j<=n) { c=b[j]; if(isalpha(toascii))) if(j==0) { p=malloc(c); add|x|=p; printf(\n\t%c\t\t%d\t\tidentifier,c,p); } clscr(); { ch=b[j|l]; if(ch==+||ch==*||ch==/||ch==||ch=)); { p=malloc(c); add|x|=p; 57

SEC

CSE/VI d|x|=c; printf(\n\t%c\t\t%d\t\tidentifier,c,p); x++; j++; } printf(\nEnter the symbol to search); fflush(stdin); srch=getchar(); for(i=0;i<=n;i++) { if(srch==d[i]) { printf(\nSymbol found\t); printf(%c\t%s%d\n,srch,@address,add[i]); break; flag=1; if(flag==1) { printf(Symbol not found); getch(); }

PCD LAB MANUAL

OUTPUT Enter an expression & it is terminated by $:a+b+$ Symbol Table Symbol Address a 1910 identifier b 2012 identifier Enter the symbol to search: a Symbol found a @address1910 Enter the symbol to search: b Symbol found b @address2012 Symbol not found

Type

58

SEC

CSE/VI E-CLOSURE

PCD LAB MANUAL

OBJECTIVE To write a program to implement an E-CLOSURE in c.

ALGORITHM

1. Start the program 2. Declare the required variables. 3. Declare file pointers as F1. Enter the input program. 4. Using while loop check the E-move of each state. 5. Print E-Closure of each State. 6. Stop the program. PROGRAM #include<stdio.h> #include<conio.h> #include<ctype.h> void main() { char a[20][20]; intc,k,count,j,I=1; FILE *f1; clrscr(); f1=fopen(input.txt,r); while((c=fgetc(f1))!=EOF) { k=1; while(c!=\n $$ c!=EOF) { a[i][k]=c; k++; c-iqctc(i1); i=2; for(i=2;i<count;i++) 59

SEC { printf(E-Closure(%c)=(%c,a[I][l],a[I][l]); j=3; if(a[I][j]!= ) printf(,); while(a[I][j]!=\t) { if(a[I][j]== ) break; clrscr(); printf()); printf(\n); } getch(); }

CSE/VI

PCD LAB MANUAL

OUTPUT E 0 1,2 1 2 2 3,1 3 2,1

b -

E-closure(0)={0,1,2} E-closure(1)={1,2} E-closure(2)={2,a 3,1} E-closure(3)={3,2,1}

60

SEC

CSE/VI OPERATOR PRECEDENCE PARSING OBJECTIVE

PCD LAB MANUAL

To write a C program for implementin operator precedence parsing for given grammar ALGORITHM 1. Start the program. 2.Declare all necessary header files and variables 3.Assign all the precedence relation for given grammar in a 2 d array 4.Get input from user in an array 5.Append $ symbol at the end of the input 6.Create stack using array and put $ as first element 7.Display the contents of stack and input array 8.If last element of stack and input is $ then the parsing is completed 9.Compare top of stack and top of input use find function to get element 10.If the symbol is space print parser error 11.If the symbol is < then shift top element of input to stack. 12.Display the contents of stack and input array 13.If the symbol is > then remove all elements from stack 14.Display the contents of stack and input array 15.End of the program.

PROGRAM #include<stdio.h> #include<conio.h> int find(char a) { switch(a) { case 'a': return 0; case '+': return 1; case '*': 61

SEC

CSE/VI return 2; case '$': return 3;} } void display(char a[],int top1,char b[],int top2) { inti;for(i=0;i<=top1;i++) printf("%c",a[i]); printf("\t"); for(i=top2;i<strlen(b);i++) printf("%c",b[i]);printf("\n"); } int main() { char table[][4]= {' ','>','>','>','<','<','<','>','<','>','<','>','<','<','<',' '}; char input[10]; char stack[10]={'$'}; inttop_stack=0,top_input=0,i,j; clrscr(); printf("operator precedence parsing for E->E+E/E*E/a\n"); printf("enter the string\n"); scanf("%s",input); strcat(input,"$"); printf("stack\tinput\n"); display(stack,top_stack,input,top_input); while(1) { if((stack[top_stack]=='$')&&(input[top_input]=='$')) {printf("string accepted"); break; } if(table[find(stack[top_stack])][find(input[top_input])]==' ') { printf("parse error"); getch(); exit(0); } if(table[find(stack[top_stack])][find(input[top_input])]=='<') { stack[++top_stack]='<';

PCD LAB MANUAL

62

SEC

CSE/VI stack[++top_stack]=input[top_input]; top_input++; display(stack,top_stack,input,top_input); continue; } if(table[find(stack[top_stack])][find(input[top_input])]=='>') { stack[++top_stack]='>'; display(stack,top_stack,input,top_input); top_stack-=3; display(stack,top_stack,input,top_input); } } getch(); } OUTPUT Operator precedence parsing for E->E+E/E*E/a Enter the string a+a*a stack input $ a+a*a$ $<a +a*a$ $<a> +a*a$ $ +a*a$ $<+ a*a$ $<+<a *a$ $<+<a> *a$ $<+ *a$ $<+<* a$ $<+<*<a $ $<+<*<a> $ $<+<* $ $<+<*> $ $<+ $ $<+> $ $ $ string accepted

PCD LAB MANUAL

63

SEC

CSE/VI

PCD LAB MANUAL

REFERENCES 1. A V Aho, R. Sethi, .J D Ullman, "Compilers: Principles,Techniques, and Tools", Pearson Education, ISBN 81 - 7758 - 5902. J. R. Levine, T. Mason, D. Brown, "Lex&Yacc", O'Reilly, 2000,ISBN 81-7366 -061-X. 83.K. Louden, "Compiler Construction: Principles and Practice",Thomson Brookes/Cole (ISE), 2003, ISBN 981 - 243 - 694-4 2. Reference book: John R. Levine, lex&yacc 3. Reference ppt: Lecture 2: Lexical Analysis, CS 440/540, George Mason university 4. Reference URL: http://dinosaur.compilertools.net/ 5. Online manual: http://dinosaur.compilertools.net/flex/index.html
FUTURE DEVELOPMENTS Prolog language,adopting the theoretical principle of first order predicate, is a popular and preferred logic programming language amid fields of study. At present noted deviations of prolog language, Turbo prolog, visual prolog etc . The future development targets the functional characteristics and working mechanism of the compiler through the analysis of prolog language organizing and translating procedure in conjunction with logical attribute of the language.

64

You might also like