Compiler Design Lab Manual(Archana)Without Source Program-16.12.2013

16
Compiler Design Lab Session Plan Class: III/II CSE-A Class: III/II CSE-B S.No Name of the program Page No Date Planned Date Conducted Part-B: 1 Design a lexical analyzer for given language .the lexical analyzer should ignore redundant spaces, tabs and new lines. 3 2 Implement the lexical analyzer using JLex, flex or other lexical analyzer generating tools. 6 3 a. Design Predictive Parser of the given language 8 4 Design a SLR bottom up parser for the given language 12 5 Convert the BNF rules into Yacc form and write code to generate abstract syntax tree. 17 6 A program to generate machine code 19 1

description

Compiler Design Lab

Transcript of Compiler Design Lab Manual(Archana)Without Source Program-16.12.2013

Page 1: Compiler Design Lab Manual(Archana)Without Source Program-16.12.2013

Compiler DesignLab Session Plan

Class IIIII CSE-A

Class IIIII CSE-B

PART B

COMPILER DESIGN

SNo Name of the programPage No Date

Planned

Date Conducte

d

Part-B1

Design a lexical analyzer for given language the lexical analyzer should ignore redundant spaces tabs and new lines

3

2 Implement the lexical analyzer using JLex flex or other lexical analyzer generating tools

6

3a Design Predictive Parser of the given language

8

4 Design a SLR bottom up parser for the given language

12

5Convert the BNF rules into Yacc form and write code to generate abstract syntax tree

17

6A program to generate machine code 19

SNo Name of the programPage No Date

PlannedDate

Conducted

Part-B1

Design a lexical analyzer for given language the lexical analyzer should ignore redundant spaces tabs and new lines

3

2 Implement the lexical analyzer using JLex flex or other lexical analyzer generating tools

6

3Design Predictive Parser of the RECURSIVE DESCENT PARSER for the given language

8

4 Design a SLR bottom up parser for the given language

12

5Convert the BNF rules into Yacc form and write code to generate abstract syntax tree

17

6 A program to generate machine code 19

1

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

EXPERIMENT 1

NAME OF THE EXPERIMENT Design A Lexical Analyzer

2

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 ij

step3 Enter the enter expression

step4 Read the expression

step5 Initialize i=0

step6 Repeat step 5 until iltstrlen(exp) upto step 25

step7 Assign ch=exp[i]

step8 Declare j=0

step9 Repeat step 8 (chgt=aampampchlt=z) upto step14

step10 Assign id[j++]=ch

step11 Increment i =i+1

step12 Repeat step 11 while((exp[i]gt=aampampexp[i]lt=z)||(exp[i]gt=0ampampexp[i]lt=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(chgt=0ampampchlt=9)upto step24

step20 Assign dig[j++]=ch

step21 Increment i=i+1

step22 Repeat while(exp[i]gt=0ampampexp[i]lt=9)

step23 Assign dig[j++]=exp[i++]

step24 Declare dig[j]=0

step25 Display constant

step26 Stop

OUTPUT

3

VIVA QUESTIONS

1 What is lexical analyzer2 Which compiler is used for lexical analyzer3 What is pattern4 What is the output of Lexical analyzer5 What is the lexeme 6 what is the token

EXPERIMENT 2

NAME OF THE EXPERIMENT Implement lexical analyzer using lexical analyzer generating tools

4

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 if-else and while are implemented

ALGORITHM

Step1 StartStep2 Declare the declarations for the given language tokens like digit alphabet white space delimiters etc

digit[0-9]letter[A-Z a-z]delim[tn]W$delim+ID(letter)(letterdigit)+Integer digit+ ws print (ldquoSpecialCharactersrdquo)ID print(ldquoIdentifiersrdquo)digit print(ldquon Integer)if printf(ldquokeywordrdquo)else print(keyword)ldquoamp amprdquo print(logoical operators)ldquogtrdquoprint(logoical operators)ldquoltrdquoprint(logoical operators)ldquolt=rdquoprint(logoical operators)ldquogt=rdquoprint(logoical operators)ldquo=rdquo printf(ldquon nrdquo)ldquordquoprintf(ldquon nrdquo)ldquo+rdquo printf(ldquoarithmetic operatorrdquo)ldquo-ldquo printf (ldquoarithmeticrdquo)ldquordquo printf(ldquoarithmeticrdquo)ldquordquo printf(arithmeticrdquo) printf(ldquoarithmeticrdquo)

Step3 Write the auxillary procedure in main() functionStep4 endStep5 Stop

OUTPUT[rootlocalhost] lex lexprogl[rootlocalhost] cc lexyyc[rootlocalhost] aout lexprog

5

VIVA QUESTIONS

1 What is Parsing2 What is Token3 How many section in LEX program4 What is meant by extension of dot l(l) in LEX5 What is the syntax of LEX compiler program to save it6 what is the output command of LEX

6

EXPERIMENT 3

NAME OF THE EXPERIMENT Predictive Parser of the RECURSIVE DESCENT PARSER

AIM Design Predictive Parser of the RECURSIVE DESCENT PARSER given language

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 beparsed

ALGORITHM

Step1 StartStep2 declare w[10] as char and Z as an arrayStep3 enter the string with $ at the endStep4 if (A(w[z]) then increment z and check for (B(w[z])) and if satisfies increment z and check for lsquodrsquo if d

is present then increment and check for (D(w[z]))Step5 if step 4 is satisfied then the string is accepted

Else string is not Step6 give for the grammar A bcab in the loop A(int k)Step7 Describe the grammar b cd in the loop B (int k)Step8 Similarly describe the grammar D dabcdStep9 if steps789 are satisfied accordingly string is accepted

Else string is not acceptedStep10 Stop

OUTPUT

7

VIVA QUESTIONS

1 What is Predictive parser2 How many types of analysis can we do using Parser3 What is Recursive Decent Parser4 What is LR Parser5 What is back Tracking6 What are the problem in Top-Down parsing

8

EXPERIMENT 4

NAME OF THE EXPERIMENT Design of SLR

AIM Design SLR bottom up parser for the above language

THEORYThe 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 -gt α B β in closure(I) for all productions B -gt γ add B -gt γ to

closure(I) if not already there Keep applying this rule until no more items canbe added

Initial elements (I) are often referred to as the kernel elements of closure(I)

Defn Goto(IX) where I is a set of items X is a terminal or non-terminal is the closure(A 1048774 a X b) where A 1048774 a X b is in I

Algorithmbull C =closure(Srsquo1048774 S) where Srsquo 1048774 S is the production added for augmentationbull Repeat

ndash For each item I in C and grammar symbol X such that Goto(IX) is not empty and not in already an element of Cbull Add Goto(IX) to C

ALGORITHM

Step1 StartStep2 Initially the parser has s0 on the stack where s0 is the initial state and w$ is in bufferStep3 Set ip point to the first symbol of w$Step4 repeat forever beginStep5 Let S be the state on top of the stack and a symbol pointed to by ipStep6 If action [S a] =shift S then begin

Push S1 on to the top of the stackAdvance ip to next input symbol

Step7 Else if action [S a] reduce A-gtB then beginPop 2 |B| symbols of the stackLet S1 be the state now on the top of the stack

Step8 Output the production ABEnd

Step9 else if action [S a]=accepted then returnElseError()End

Step10 Stop

9

OUTPUT

Id(id+id)$Grammer accepted

VIVA QUESTIONS

1 What is SLR parsing2 What is Shift reduced parser3 What are the operations of Parser4 What is the use of parsing table5 What is bottom up parsing

10

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

THEORYYacc 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

ALGORITHMStep1 StartStep2 declare the declarations as a header file

includeltctypehgtStep3 token digitStep4 define the translations rules like line expr term factor

Lineexp lsquonrsquo print(ldquon d nrdquo$1)Exprexprrsquo+rsquo term ($$=$1=$3Termterm lsquo+rsquo factor($$ =$1$3FactorFactorrsquo(lsquoenterrsquo) lsquo$$ =$2)

Step5 define the supporting C routinesStep6 Stop

OUTPUT

[rootlocalhost] lex codegenl[rootlocalhost] yacc ndashd codegeny[rootlocalhost] cc lexyyc ytabc ndashll -lm[rootlocalhost] aout

INPUT OUTPUT

2+3

4-2

5

2

VIVA QUESTIONS

1 What is Abstract Syntax tree2 What are BNF Rules3 What is DAG representation4 How LALR(1) states are generates5 In which condition the user has to supply more information to YACC

11

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

THEORYMachine code also known as machine language is the elemental language of computers comprising a long sequence of binary digital zeros and ones

ALGORITHMStep1 StartStep2 for every three ndashaddress statement of the form x=y op zStep3 beginStep4 Call getreg() to obtain the location L which the computation y op z should be performedStep5 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 registerIf the value of y is not currently available in 1then generate an instruction MOV yl

Step6 Generate the instruction OP Zl 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

Step7 If the current values of y ador z are in register and we have no further use for themand they are live at the end of the blockthen after the register descriptor to indicate that after the execution of the statement x=y op zthose registers will no longer contain y and or z

Step8 store all resultsStep9 Stop

OUTPUT

VIVA QUESTIONS

1 What is target code2 What is machine code3 What is Cross compiler

12

4 Give the example for cross compiler5 What is the difference between syntax amp Semantics

13

Page 2: Compiler Design Lab Manual(Archana)Without Source Program-16.12.2013

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

EXPERIMENT 1

NAME OF THE EXPERIMENT Design A Lexical Analyzer

2

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 ij

step3 Enter the enter expression

step4 Read the expression

step5 Initialize i=0

step6 Repeat step 5 until iltstrlen(exp) upto step 25

step7 Assign ch=exp[i]

step8 Declare j=0

step9 Repeat step 8 (chgt=aampampchlt=z) upto step14

step10 Assign id[j++]=ch

step11 Increment i =i+1

step12 Repeat step 11 while((exp[i]gt=aampampexp[i]lt=z)||(exp[i]gt=0ampampexp[i]lt=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(chgt=0ampampchlt=9)upto step24

step20 Assign dig[j++]=ch

step21 Increment i=i+1

step22 Repeat while(exp[i]gt=0ampampexp[i]lt=9)

step23 Assign dig[j++]=exp[i++]

step24 Declare dig[j]=0

step25 Display constant

step26 Stop

OUTPUT

3

VIVA QUESTIONS

1 What is lexical analyzer2 Which compiler is used for lexical analyzer3 What is pattern4 What is the output of Lexical analyzer5 What is the lexeme 6 what is the token

EXPERIMENT 2

NAME OF THE EXPERIMENT Implement lexical analyzer using lexical analyzer generating tools

4

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 if-else and while are implemented

ALGORITHM

Step1 StartStep2 Declare the declarations for the given language tokens like digit alphabet white space delimiters etc

digit[0-9]letter[A-Z a-z]delim[tn]W$delim+ID(letter)(letterdigit)+Integer digit+ ws print (ldquoSpecialCharactersrdquo)ID print(ldquoIdentifiersrdquo)digit print(ldquon Integer)if printf(ldquokeywordrdquo)else print(keyword)ldquoamp amprdquo print(logoical operators)ldquogtrdquoprint(logoical operators)ldquoltrdquoprint(logoical operators)ldquolt=rdquoprint(logoical operators)ldquogt=rdquoprint(logoical operators)ldquo=rdquo printf(ldquon nrdquo)ldquordquoprintf(ldquon nrdquo)ldquo+rdquo printf(ldquoarithmetic operatorrdquo)ldquo-ldquo printf (ldquoarithmeticrdquo)ldquordquo printf(ldquoarithmeticrdquo)ldquordquo printf(arithmeticrdquo) printf(ldquoarithmeticrdquo)

Step3 Write the auxillary procedure in main() functionStep4 endStep5 Stop

OUTPUT[rootlocalhost] lex lexprogl[rootlocalhost] cc lexyyc[rootlocalhost] aout lexprog

5

VIVA QUESTIONS

1 What is Parsing2 What is Token3 How many section in LEX program4 What is meant by extension of dot l(l) in LEX5 What is the syntax of LEX compiler program to save it6 what is the output command of LEX

6

EXPERIMENT 3

NAME OF THE EXPERIMENT Predictive Parser of the RECURSIVE DESCENT PARSER

AIM Design Predictive Parser of the RECURSIVE DESCENT PARSER given language

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 beparsed

ALGORITHM

Step1 StartStep2 declare w[10] as char and Z as an arrayStep3 enter the string with $ at the endStep4 if (A(w[z]) then increment z and check for (B(w[z])) and if satisfies increment z and check for lsquodrsquo if d

is present then increment and check for (D(w[z]))Step5 if step 4 is satisfied then the string is accepted

Else string is not Step6 give for the grammar A bcab in the loop A(int k)Step7 Describe the grammar b cd in the loop B (int k)Step8 Similarly describe the grammar D dabcdStep9 if steps789 are satisfied accordingly string is accepted

Else string is not acceptedStep10 Stop

OUTPUT

7

VIVA QUESTIONS

1 What is Predictive parser2 How many types of analysis can we do using Parser3 What is Recursive Decent Parser4 What is LR Parser5 What is back Tracking6 What are the problem in Top-Down parsing

8

EXPERIMENT 4

NAME OF THE EXPERIMENT Design of SLR

AIM Design SLR bottom up parser for the above language

THEORYThe 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 -gt α B β in closure(I) for all productions B -gt γ add B -gt γ to

closure(I) if not already there Keep applying this rule until no more items canbe added

Initial elements (I) are often referred to as the kernel elements of closure(I)

Defn Goto(IX) where I is a set of items X is a terminal or non-terminal is the closure(A 1048774 a X b) where A 1048774 a X b is in I

Algorithmbull C =closure(Srsquo1048774 S) where Srsquo 1048774 S is the production added for augmentationbull Repeat

ndash For each item I in C and grammar symbol X such that Goto(IX) is not empty and not in already an element of Cbull Add Goto(IX) to C

ALGORITHM

Step1 StartStep2 Initially the parser has s0 on the stack where s0 is the initial state and w$ is in bufferStep3 Set ip point to the first symbol of w$Step4 repeat forever beginStep5 Let S be the state on top of the stack and a symbol pointed to by ipStep6 If action [S a] =shift S then begin

Push S1 on to the top of the stackAdvance ip to next input symbol

Step7 Else if action [S a] reduce A-gtB then beginPop 2 |B| symbols of the stackLet S1 be the state now on the top of the stack

Step8 Output the production ABEnd

Step9 else if action [S a]=accepted then returnElseError()End

Step10 Stop

9

OUTPUT

Id(id+id)$Grammer accepted

VIVA QUESTIONS

1 What is SLR parsing2 What is Shift reduced parser3 What are the operations of Parser4 What is the use of parsing table5 What is bottom up parsing

10

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

THEORYYacc 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

ALGORITHMStep1 StartStep2 declare the declarations as a header file

includeltctypehgtStep3 token digitStep4 define the translations rules like line expr term factor

Lineexp lsquonrsquo print(ldquon d nrdquo$1)Exprexprrsquo+rsquo term ($$=$1=$3Termterm lsquo+rsquo factor($$ =$1$3FactorFactorrsquo(lsquoenterrsquo) lsquo$$ =$2)

Step5 define the supporting C routinesStep6 Stop

OUTPUT

[rootlocalhost] lex codegenl[rootlocalhost] yacc ndashd codegeny[rootlocalhost] cc lexyyc ytabc ndashll -lm[rootlocalhost] aout

INPUT OUTPUT

2+3

4-2

5

2

VIVA QUESTIONS

1 What is Abstract Syntax tree2 What are BNF Rules3 What is DAG representation4 How LALR(1) states are generates5 In which condition the user has to supply more information to YACC

11

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

THEORYMachine code also known as machine language is the elemental language of computers comprising a long sequence of binary digital zeros and ones

ALGORITHMStep1 StartStep2 for every three ndashaddress statement of the form x=y op zStep3 beginStep4 Call getreg() to obtain the location L which the computation y op z should be performedStep5 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 registerIf the value of y is not currently available in 1then generate an instruction MOV yl

Step6 Generate the instruction OP Zl 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

Step7 If the current values of y ador z are in register and we have no further use for themand they are live at the end of the blockthen after the register descriptor to indicate that after the execution of the statement x=y op zthose registers will no longer contain y and or z

Step8 store all resultsStep9 Stop

OUTPUT

VIVA QUESTIONS

1 What is target code2 What is machine code3 What is Cross compiler

12

4 Give the example for cross compiler5 What is the difference between syntax amp Semantics

13

Page 3: Compiler Design Lab Manual(Archana)Without Source Program-16.12.2013

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 ij

step3 Enter the enter expression

step4 Read the expression

step5 Initialize i=0

step6 Repeat step 5 until iltstrlen(exp) upto step 25

step7 Assign ch=exp[i]

step8 Declare j=0

step9 Repeat step 8 (chgt=aampampchlt=z) upto step14

step10 Assign id[j++]=ch

step11 Increment i =i+1

step12 Repeat step 11 while((exp[i]gt=aampampexp[i]lt=z)||(exp[i]gt=0ampampexp[i]lt=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(chgt=0ampampchlt=9)upto step24

step20 Assign dig[j++]=ch

step21 Increment i=i+1

step22 Repeat while(exp[i]gt=0ampampexp[i]lt=9)

step23 Assign dig[j++]=exp[i++]

step24 Declare dig[j]=0

step25 Display constant

step26 Stop

OUTPUT

3

VIVA QUESTIONS

1 What is lexical analyzer2 Which compiler is used for lexical analyzer3 What is pattern4 What is the output of Lexical analyzer5 What is the lexeme 6 what is the token

EXPERIMENT 2

NAME OF THE EXPERIMENT Implement lexical analyzer using lexical analyzer generating tools

4

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 if-else and while are implemented

ALGORITHM

Step1 StartStep2 Declare the declarations for the given language tokens like digit alphabet white space delimiters etc

digit[0-9]letter[A-Z a-z]delim[tn]W$delim+ID(letter)(letterdigit)+Integer digit+ ws print (ldquoSpecialCharactersrdquo)ID print(ldquoIdentifiersrdquo)digit print(ldquon Integer)if printf(ldquokeywordrdquo)else print(keyword)ldquoamp amprdquo print(logoical operators)ldquogtrdquoprint(logoical operators)ldquoltrdquoprint(logoical operators)ldquolt=rdquoprint(logoical operators)ldquogt=rdquoprint(logoical operators)ldquo=rdquo printf(ldquon nrdquo)ldquordquoprintf(ldquon nrdquo)ldquo+rdquo printf(ldquoarithmetic operatorrdquo)ldquo-ldquo printf (ldquoarithmeticrdquo)ldquordquo printf(ldquoarithmeticrdquo)ldquordquo printf(arithmeticrdquo) printf(ldquoarithmeticrdquo)

Step3 Write the auxillary procedure in main() functionStep4 endStep5 Stop

OUTPUT[rootlocalhost] lex lexprogl[rootlocalhost] cc lexyyc[rootlocalhost] aout lexprog

5

VIVA QUESTIONS

1 What is Parsing2 What is Token3 How many section in LEX program4 What is meant by extension of dot l(l) in LEX5 What is the syntax of LEX compiler program to save it6 what is the output command of LEX

6

EXPERIMENT 3

NAME OF THE EXPERIMENT Predictive Parser of the RECURSIVE DESCENT PARSER

AIM Design Predictive Parser of the RECURSIVE DESCENT PARSER given language

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 beparsed

ALGORITHM

Step1 StartStep2 declare w[10] as char and Z as an arrayStep3 enter the string with $ at the endStep4 if (A(w[z]) then increment z and check for (B(w[z])) and if satisfies increment z and check for lsquodrsquo if d

is present then increment and check for (D(w[z]))Step5 if step 4 is satisfied then the string is accepted

Else string is not Step6 give for the grammar A bcab in the loop A(int k)Step7 Describe the grammar b cd in the loop B (int k)Step8 Similarly describe the grammar D dabcdStep9 if steps789 are satisfied accordingly string is accepted

Else string is not acceptedStep10 Stop

OUTPUT

7

VIVA QUESTIONS

1 What is Predictive parser2 How many types of analysis can we do using Parser3 What is Recursive Decent Parser4 What is LR Parser5 What is back Tracking6 What are the problem in Top-Down parsing

8

EXPERIMENT 4

NAME OF THE EXPERIMENT Design of SLR

AIM Design SLR bottom up parser for the above language

THEORYThe 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 -gt α B β in closure(I) for all productions B -gt γ add B -gt γ to

closure(I) if not already there Keep applying this rule until no more items canbe added

Initial elements (I) are often referred to as the kernel elements of closure(I)

Defn Goto(IX) where I is a set of items X is a terminal or non-terminal is the closure(A 1048774 a X b) where A 1048774 a X b is in I

Algorithmbull C =closure(Srsquo1048774 S) where Srsquo 1048774 S is the production added for augmentationbull Repeat

ndash For each item I in C and grammar symbol X such that Goto(IX) is not empty and not in already an element of Cbull Add Goto(IX) to C

ALGORITHM

Step1 StartStep2 Initially the parser has s0 on the stack where s0 is the initial state and w$ is in bufferStep3 Set ip point to the first symbol of w$Step4 repeat forever beginStep5 Let S be the state on top of the stack and a symbol pointed to by ipStep6 If action [S a] =shift S then begin

Push S1 on to the top of the stackAdvance ip to next input symbol

Step7 Else if action [S a] reduce A-gtB then beginPop 2 |B| symbols of the stackLet S1 be the state now on the top of the stack

Step8 Output the production ABEnd

Step9 else if action [S a]=accepted then returnElseError()End

Step10 Stop

9

OUTPUT

Id(id+id)$Grammer accepted

VIVA QUESTIONS

1 What is SLR parsing2 What is Shift reduced parser3 What are the operations of Parser4 What is the use of parsing table5 What is bottom up parsing

10

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

THEORYYacc 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

ALGORITHMStep1 StartStep2 declare the declarations as a header file

includeltctypehgtStep3 token digitStep4 define the translations rules like line expr term factor

Lineexp lsquonrsquo print(ldquon d nrdquo$1)Exprexprrsquo+rsquo term ($$=$1=$3Termterm lsquo+rsquo factor($$ =$1$3FactorFactorrsquo(lsquoenterrsquo) lsquo$$ =$2)

Step5 define the supporting C routinesStep6 Stop

OUTPUT

[rootlocalhost] lex codegenl[rootlocalhost] yacc ndashd codegeny[rootlocalhost] cc lexyyc ytabc ndashll -lm[rootlocalhost] aout

INPUT OUTPUT

2+3

4-2

5

2

VIVA QUESTIONS

1 What is Abstract Syntax tree2 What are BNF Rules3 What is DAG representation4 How LALR(1) states are generates5 In which condition the user has to supply more information to YACC

11

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

THEORYMachine code also known as machine language is the elemental language of computers comprising a long sequence of binary digital zeros and ones

ALGORITHMStep1 StartStep2 for every three ndashaddress statement of the form x=y op zStep3 beginStep4 Call getreg() to obtain the location L which the computation y op z should be performedStep5 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 registerIf the value of y is not currently available in 1then generate an instruction MOV yl

Step6 Generate the instruction OP Zl 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

Step7 If the current values of y ador z are in register and we have no further use for themand they are live at the end of the blockthen after the register descriptor to indicate that after the execution of the statement x=y op zthose registers will no longer contain y and or z

Step8 store all resultsStep9 Stop

OUTPUT

VIVA QUESTIONS

1 What is target code2 What is machine code3 What is Cross compiler

12

4 Give the example for cross compiler5 What is the difference between syntax amp Semantics

13

Page 4: Compiler Design Lab Manual(Archana)Without Source Program-16.12.2013

VIVA QUESTIONS

1 What is lexical analyzer2 Which compiler is used for lexical analyzer3 What is pattern4 What is the output of Lexical analyzer5 What is the lexeme 6 what is the token

EXPERIMENT 2

NAME OF THE EXPERIMENT Implement lexical analyzer using lexical analyzer generating tools

4

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 if-else and while are implemented

ALGORITHM

Step1 StartStep2 Declare the declarations for the given language tokens like digit alphabet white space delimiters etc

digit[0-9]letter[A-Z a-z]delim[tn]W$delim+ID(letter)(letterdigit)+Integer digit+ ws print (ldquoSpecialCharactersrdquo)ID print(ldquoIdentifiersrdquo)digit print(ldquon Integer)if printf(ldquokeywordrdquo)else print(keyword)ldquoamp amprdquo print(logoical operators)ldquogtrdquoprint(logoical operators)ldquoltrdquoprint(logoical operators)ldquolt=rdquoprint(logoical operators)ldquogt=rdquoprint(logoical operators)ldquo=rdquo printf(ldquon nrdquo)ldquordquoprintf(ldquon nrdquo)ldquo+rdquo printf(ldquoarithmetic operatorrdquo)ldquo-ldquo printf (ldquoarithmeticrdquo)ldquordquo printf(ldquoarithmeticrdquo)ldquordquo printf(arithmeticrdquo) printf(ldquoarithmeticrdquo)

Step3 Write the auxillary procedure in main() functionStep4 endStep5 Stop

OUTPUT[rootlocalhost] lex lexprogl[rootlocalhost] cc lexyyc[rootlocalhost] aout lexprog

5

VIVA QUESTIONS

1 What is Parsing2 What is Token3 How many section in LEX program4 What is meant by extension of dot l(l) in LEX5 What is the syntax of LEX compiler program to save it6 what is the output command of LEX

6

EXPERIMENT 3

NAME OF THE EXPERIMENT Predictive Parser of the RECURSIVE DESCENT PARSER

AIM Design Predictive Parser of the RECURSIVE DESCENT PARSER given language

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 beparsed

ALGORITHM

Step1 StartStep2 declare w[10] as char and Z as an arrayStep3 enter the string with $ at the endStep4 if (A(w[z]) then increment z and check for (B(w[z])) and if satisfies increment z and check for lsquodrsquo if d

is present then increment and check for (D(w[z]))Step5 if step 4 is satisfied then the string is accepted

Else string is not Step6 give for the grammar A bcab in the loop A(int k)Step7 Describe the grammar b cd in the loop B (int k)Step8 Similarly describe the grammar D dabcdStep9 if steps789 are satisfied accordingly string is accepted

Else string is not acceptedStep10 Stop

OUTPUT

7

VIVA QUESTIONS

1 What is Predictive parser2 How many types of analysis can we do using Parser3 What is Recursive Decent Parser4 What is LR Parser5 What is back Tracking6 What are the problem in Top-Down parsing

8

EXPERIMENT 4

NAME OF THE EXPERIMENT Design of SLR

AIM Design SLR bottom up parser for the above language

THEORYThe 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 -gt α B β in closure(I) for all productions B -gt γ add B -gt γ to

closure(I) if not already there Keep applying this rule until no more items canbe added

Initial elements (I) are often referred to as the kernel elements of closure(I)

Defn Goto(IX) where I is a set of items X is a terminal or non-terminal is the closure(A 1048774 a X b) where A 1048774 a X b is in I

Algorithmbull C =closure(Srsquo1048774 S) where Srsquo 1048774 S is the production added for augmentationbull Repeat

ndash For each item I in C and grammar symbol X such that Goto(IX) is not empty and not in already an element of Cbull Add Goto(IX) to C

ALGORITHM

Step1 StartStep2 Initially the parser has s0 on the stack where s0 is the initial state and w$ is in bufferStep3 Set ip point to the first symbol of w$Step4 repeat forever beginStep5 Let S be the state on top of the stack and a symbol pointed to by ipStep6 If action [S a] =shift S then begin

Push S1 on to the top of the stackAdvance ip to next input symbol

Step7 Else if action [S a] reduce A-gtB then beginPop 2 |B| symbols of the stackLet S1 be the state now on the top of the stack

Step8 Output the production ABEnd

Step9 else if action [S a]=accepted then returnElseError()End

Step10 Stop

9

OUTPUT

Id(id+id)$Grammer accepted

VIVA QUESTIONS

1 What is SLR parsing2 What is Shift reduced parser3 What are the operations of Parser4 What is the use of parsing table5 What is bottom up parsing

10

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

THEORYYacc 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

ALGORITHMStep1 StartStep2 declare the declarations as a header file

includeltctypehgtStep3 token digitStep4 define the translations rules like line expr term factor

Lineexp lsquonrsquo print(ldquon d nrdquo$1)Exprexprrsquo+rsquo term ($$=$1=$3Termterm lsquo+rsquo factor($$ =$1$3FactorFactorrsquo(lsquoenterrsquo) lsquo$$ =$2)

Step5 define the supporting C routinesStep6 Stop

OUTPUT

[rootlocalhost] lex codegenl[rootlocalhost] yacc ndashd codegeny[rootlocalhost] cc lexyyc ytabc ndashll -lm[rootlocalhost] aout

INPUT OUTPUT

2+3

4-2

5

2

VIVA QUESTIONS

1 What is Abstract Syntax tree2 What are BNF Rules3 What is DAG representation4 How LALR(1) states are generates5 In which condition the user has to supply more information to YACC

11

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

THEORYMachine code also known as machine language is the elemental language of computers comprising a long sequence of binary digital zeros and ones

ALGORITHMStep1 StartStep2 for every three ndashaddress statement of the form x=y op zStep3 beginStep4 Call getreg() to obtain the location L which the computation y op z should be performedStep5 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 registerIf the value of y is not currently available in 1then generate an instruction MOV yl

Step6 Generate the instruction OP Zl 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

Step7 If the current values of y ador z are in register and we have no further use for themand they are live at the end of the blockthen after the register descriptor to indicate that after the execution of the statement x=y op zthose registers will no longer contain y and or z

Step8 store all resultsStep9 Stop

OUTPUT

VIVA QUESTIONS

1 What is target code2 What is machine code3 What is Cross compiler

12

4 Give the example for cross compiler5 What is the difference between syntax amp Semantics

13

Page 5: Compiler Design Lab Manual(Archana)Without Source Program-16.12.2013

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 if-else and while are implemented

ALGORITHM

Step1 StartStep2 Declare the declarations for the given language tokens like digit alphabet white space delimiters etc

digit[0-9]letter[A-Z a-z]delim[tn]W$delim+ID(letter)(letterdigit)+Integer digit+ ws print (ldquoSpecialCharactersrdquo)ID print(ldquoIdentifiersrdquo)digit print(ldquon Integer)if printf(ldquokeywordrdquo)else print(keyword)ldquoamp amprdquo print(logoical operators)ldquogtrdquoprint(logoical operators)ldquoltrdquoprint(logoical operators)ldquolt=rdquoprint(logoical operators)ldquogt=rdquoprint(logoical operators)ldquo=rdquo printf(ldquon nrdquo)ldquordquoprintf(ldquon nrdquo)ldquo+rdquo printf(ldquoarithmetic operatorrdquo)ldquo-ldquo printf (ldquoarithmeticrdquo)ldquordquo printf(ldquoarithmeticrdquo)ldquordquo printf(arithmeticrdquo) printf(ldquoarithmeticrdquo)

Step3 Write the auxillary procedure in main() functionStep4 endStep5 Stop

OUTPUT[rootlocalhost] lex lexprogl[rootlocalhost] cc lexyyc[rootlocalhost] aout lexprog

5

VIVA QUESTIONS

1 What is Parsing2 What is Token3 How many section in LEX program4 What is meant by extension of dot l(l) in LEX5 What is the syntax of LEX compiler program to save it6 what is the output command of LEX

6

EXPERIMENT 3

NAME OF THE EXPERIMENT Predictive Parser of the RECURSIVE DESCENT PARSER

AIM Design Predictive Parser of the RECURSIVE DESCENT PARSER given language

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 beparsed

ALGORITHM

Step1 StartStep2 declare w[10] as char and Z as an arrayStep3 enter the string with $ at the endStep4 if (A(w[z]) then increment z and check for (B(w[z])) and if satisfies increment z and check for lsquodrsquo if d

is present then increment and check for (D(w[z]))Step5 if step 4 is satisfied then the string is accepted

Else string is not Step6 give for the grammar A bcab in the loop A(int k)Step7 Describe the grammar b cd in the loop B (int k)Step8 Similarly describe the grammar D dabcdStep9 if steps789 are satisfied accordingly string is accepted

Else string is not acceptedStep10 Stop

OUTPUT

7

VIVA QUESTIONS

1 What is Predictive parser2 How many types of analysis can we do using Parser3 What is Recursive Decent Parser4 What is LR Parser5 What is back Tracking6 What are the problem in Top-Down parsing

8

EXPERIMENT 4

NAME OF THE EXPERIMENT Design of SLR

AIM Design SLR bottom up parser for the above language

THEORYThe 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 -gt α B β in closure(I) for all productions B -gt γ add B -gt γ to

closure(I) if not already there Keep applying this rule until no more items canbe added

Initial elements (I) are often referred to as the kernel elements of closure(I)

Defn Goto(IX) where I is a set of items X is a terminal or non-terminal is the closure(A 1048774 a X b) where A 1048774 a X b is in I

Algorithmbull C =closure(Srsquo1048774 S) where Srsquo 1048774 S is the production added for augmentationbull Repeat

ndash For each item I in C and grammar symbol X such that Goto(IX) is not empty and not in already an element of Cbull Add Goto(IX) to C

ALGORITHM

Step1 StartStep2 Initially the parser has s0 on the stack where s0 is the initial state and w$ is in bufferStep3 Set ip point to the first symbol of w$Step4 repeat forever beginStep5 Let S be the state on top of the stack and a symbol pointed to by ipStep6 If action [S a] =shift S then begin

Push S1 on to the top of the stackAdvance ip to next input symbol

Step7 Else if action [S a] reduce A-gtB then beginPop 2 |B| symbols of the stackLet S1 be the state now on the top of the stack

Step8 Output the production ABEnd

Step9 else if action [S a]=accepted then returnElseError()End

Step10 Stop

9

OUTPUT

Id(id+id)$Grammer accepted

VIVA QUESTIONS

1 What is SLR parsing2 What is Shift reduced parser3 What are the operations of Parser4 What is the use of parsing table5 What is bottom up parsing

10

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

THEORYYacc 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

ALGORITHMStep1 StartStep2 declare the declarations as a header file

includeltctypehgtStep3 token digitStep4 define the translations rules like line expr term factor

Lineexp lsquonrsquo print(ldquon d nrdquo$1)Exprexprrsquo+rsquo term ($$=$1=$3Termterm lsquo+rsquo factor($$ =$1$3FactorFactorrsquo(lsquoenterrsquo) lsquo$$ =$2)

Step5 define the supporting C routinesStep6 Stop

OUTPUT

[rootlocalhost] lex codegenl[rootlocalhost] yacc ndashd codegeny[rootlocalhost] cc lexyyc ytabc ndashll -lm[rootlocalhost] aout

INPUT OUTPUT

2+3

4-2

5

2

VIVA QUESTIONS

1 What is Abstract Syntax tree2 What are BNF Rules3 What is DAG representation4 How LALR(1) states are generates5 In which condition the user has to supply more information to YACC

11

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

THEORYMachine code also known as machine language is the elemental language of computers comprising a long sequence of binary digital zeros and ones

ALGORITHMStep1 StartStep2 for every three ndashaddress statement of the form x=y op zStep3 beginStep4 Call getreg() to obtain the location L which the computation y op z should be performedStep5 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 registerIf the value of y is not currently available in 1then generate an instruction MOV yl

Step6 Generate the instruction OP Zl 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

Step7 If the current values of y ador z are in register and we have no further use for themand they are live at the end of the blockthen after the register descriptor to indicate that after the execution of the statement x=y op zthose registers will no longer contain y and or z

Step8 store all resultsStep9 Stop

OUTPUT

VIVA QUESTIONS

1 What is target code2 What is machine code3 What is Cross compiler

12

4 Give the example for cross compiler5 What is the difference between syntax amp Semantics

13

Page 6: Compiler Design Lab Manual(Archana)Without Source Program-16.12.2013

VIVA QUESTIONS

1 What is Parsing2 What is Token3 How many section in LEX program4 What is meant by extension of dot l(l) in LEX5 What is the syntax of LEX compiler program to save it6 what is the output command of LEX

6

EXPERIMENT 3

NAME OF THE EXPERIMENT Predictive Parser of the RECURSIVE DESCENT PARSER

AIM Design Predictive Parser of the RECURSIVE DESCENT PARSER given language

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 beparsed

ALGORITHM

Step1 StartStep2 declare w[10] as char and Z as an arrayStep3 enter the string with $ at the endStep4 if (A(w[z]) then increment z and check for (B(w[z])) and if satisfies increment z and check for lsquodrsquo if d

is present then increment and check for (D(w[z]))Step5 if step 4 is satisfied then the string is accepted

Else string is not Step6 give for the grammar A bcab in the loop A(int k)Step7 Describe the grammar b cd in the loop B (int k)Step8 Similarly describe the grammar D dabcdStep9 if steps789 are satisfied accordingly string is accepted

Else string is not acceptedStep10 Stop

OUTPUT

7

VIVA QUESTIONS

1 What is Predictive parser2 How many types of analysis can we do using Parser3 What is Recursive Decent Parser4 What is LR Parser5 What is back Tracking6 What are the problem in Top-Down parsing

8

EXPERIMENT 4

NAME OF THE EXPERIMENT Design of SLR

AIM Design SLR bottom up parser for the above language

THEORYThe 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 -gt α B β in closure(I) for all productions B -gt γ add B -gt γ to

closure(I) if not already there Keep applying this rule until no more items canbe added

Initial elements (I) are often referred to as the kernel elements of closure(I)

Defn Goto(IX) where I is a set of items X is a terminal or non-terminal is the closure(A 1048774 a X b) where A 1048774 a X b is in I

Algorithmbull C =closure(Srsquo1048774 S) where Srsquo 1048774 S is the production added for augmentationbull Repeat

ndash For each item I in C and grammar symbol X such that Goto(IX) is not empty and not in already an element of Cbull Add Goto(IX) to C

ALGORITHM

Step1 StartStep2 Initially the parser has s0 on the stack where s0 is the initial state and w$ is in bufferStep3 Set ip point to the first symbol of w$Step4 repeat forever beginStep5 Let S be the state on top of the stack and a symbol pointed to by ipStep6 If action [S a] =shift S then begin

Push S1 on to the top of the stackAdvance ip to next input symbol

Step7 Else if action [S a] reduce A-gtB then beginPop 2 |B| symbols of the stackLet S1 be the state now on the top of the stack

Step8 Output the production ABEnd

Step9 else if action [S a]=accepted then returnElseError()End

Step10 Stop

9

OUTPUT

Id(id+id)$Grammer accepted

VIVA QUESTIONS

1 What is SLR parsing2 What is Shift reduced parser3 What are the operations of Parser4 What is the use of parsing table5 What is bottom up parsing

10

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

THEORYYacc 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

ALGORITHMStep1 StartStep2 declare the declarations as a header file

includeltctypehgtStep3 token digitStep4 define the translations rules like line expr term factor

Lineexp lsquonrsquo print(ldquon d nrdquo$1)Exprexprrsquo+rsquo term ($$=$1=$3Termterm lsquo+rsquo factor($$ =$1$3FactorFactorrsquo(lsquoenterrsquo) lsquo$$ =$2)

Step5 define the supporting C routinesStep6 Stop

OUTPUT

[rootlocalhost] lex codegenl[rootlocalhost] yacc ndashd codegeny[rootlocalhost] cc lexyyc ytabc ndashll -lm[rootlocalhost] aout

INPUT OUTPUT

2+3

4-2

5

2

VIVA QUESTIONS

1 What is Abstract Syntax tree2 What are BNF Rules3 What is DAG representation4 How LALR(1) states are generates5 In which condition the user has to supply more information to YACC

11

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

THEORYMachine code also known as machine language is the elemental language of computers comprising a long sequence of binary digital zeros and ones

ALGORITHMStep1 StartStep2 for every three ndashaddress statement of the form x=y op zStep3 beginStep4 Call getreg() to obtain the location L which the computation y op z should be performedStep5 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 registerIf the value of y is not currently available in 1then generate an instruction MOV yl

Step6 Generate the instruction OP Zl 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

Step7 If the current values of y ador z are in register and we have no further use for themand they are live at the end of the blockthen after the register descriptor to indicate that after the execution of the statement x=y op zthose registers will no longer contain y and or z

Step8 store all resultsStep9 Stop

OUTPUT

VIVA QUESTIONS

1 What is target code2 What is machine code3 What is Cross compiler

12

4 Give the example for cross compiler5 What is the difference between syntax amp Semantics

13

Page 7: Compiler Design Lab Manual(Archana)Without Source Program-16.12.2013

EXPERIMENT 3

NAME OF THE EXPERIMENT Predictive Parser of the RECURSIVE DESCENT PARSER

AIM Design Predictive Parser of the RECURSIVE DESCENT PARSER given language

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 beparsed

ALGORITHM

Step1 StartStep2 declare w[10] as char and Z as an arrayStep3 enter the string with $ at the endStep4 if (A(w[z]) then increment z and check for (B(w[z])) and if satisfies increment z and check for lsquodrsquo if d

is present then increment and check for (D(w[z]))Step5 if step 4 is satisfied then the string is accepted

Else string is not Step6 give for the grammar A bcab in the loop A(int k)Step7 Describe the grammar b cd in the loop B (int k)Step8 Similarly describe the grammar D dabcdStep9 if steps789 are satisfied accordingly string is accepted

Else string is not acceptedStep10 Stop

OUTPUT

7

VIVA QUESTIONS

1 What is Predictive parser2 How many types of analysis can we do using Parser3 What is Recursive Decent Parser4 What is LR Parser5 What is back Tracking6 What are the problem in Top-Down parsing

8

EXPERIMENT 4

NAME OF THE EXPERIMENT Design of SLR

AIM Design SLR bottom up parser for the above language

THEORYThe 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 -gt α B β in closure(I) for all productions B -gt γ add B -gt γ to

closure(I) if not already there Keep applying this rule until no more items canbe added

Initial elements (I) are often referred to as the kernel elements of closure(I)

Defn Goto(IX) where I is a set of items X is a terminal or non-terminal is the closure(A 1048774 a X b) where A 1048774 a X b is in I

Algorithmbull C =closure(Srsquo1048774 S) where Srsquo 1048774 S is the production added for augmentationbull Repeat

ndash For each item I in C and grammar symbol X such that Goto(IX) is not empty and not in already an element of Cbull Add Goto(IX) to C

ALGORITHM

Step1 StartStep2 Initially the parser has s0 on the stack where s0 is the initial state and w$ is in bufferStep3 Set ip point to the first symbol of w$Step4 repeat forever beginStep5 Let S be the state on top of the stack and a symbol pointed to by ipStep6 If action [S a] =shift S then begin

Push S1 on to the top of the stackAdvance ip to next input symbol

Step7 Else if action [S a] reduce A-gtB then beginPop 2 |B| symbols of the stackLet S1 be the state now on the top of the stack

Step8 Output the production ABEnd

Step9 else if action [S a]=accepted then returnElseError()End

Step10 Stop

9

OUTPUT

Id(id+id)$Grammer accepted

VIVA QUESTIONS

1 What is SLR parsing2 What is Shift reduced parser3 What are the operations of Parser4 What is the use of parsing table5 What is bottom up parsing

10

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

THEORYYacc 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

ALGORITHMStep1 StartStep2 declare the declarations as a header file

includeltctypehgtStep3 token digitStep4 define the translations rules like line expr term factor

Lineexp lsquonrsquo print(ldquon d nrdquo$1)Exprexprrsquo+rsquo term ($$=$1=$3Termterm lsquo+rsquo factor($$ =$1$3FactorFactorrsquo(lsquoenterrsquo) lsquo$$ =$2)

Step5 define the supporting C routinesStep6 Stop

OUTPUT

[rootlocalhost] lex codegenl[rootlocalhost] yacc ndashd codegeny[rootlocalhost] cc lexyyc ytabc ndashll -lm[rootlocalhost] aout

INPUT OUTPUT

2+3

4-2

5

2

VIVA QUESTIONS

1 What is Abstract Syntax tree2 What are BNF Rules3 What is DAG representation4 How LALR(1) states are generates5 In which condition the user has to supply more information to YACC

11

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

THEORYMachine code also known as machine language is the elemental language of computers comprising a long sequence of binary digital zeros and ones

ALGORITHMStep1 StartStep2 for every three ndashaddress statement of the form x=y op zStep3 beginStep4 Call getreg() to obtain the location L which the computation y op z should be performedStep5 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 registerIf the value of y is not currently available in 1then generate an instruction MOV yl

Step6 Generate the instruction OP Zl 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

Step7 If the current values of y ador z are in register and we have no further use for themand they are live at the end of the blockthen after the register descriptor to indicate that after the execution of the statement x=y op zthose registers will no longer contain y and or z

Step8 store all resultsStep9 Stop

OUTPUT

VIVA QUESTIONS

1 What is target code2 What is machine code3 What is Cross compiler

12

4 Give the example for cross compiler5 What is the difference between syntax amp Semantics

13

Page 8: Compiler Design Lab Manual(Archana)Without Source Program-16.12.2013

VIVA QUESTIONS

1 What is Predictive parser2 How many types of analysis can we do using Parser3 What is Recursive Decent Parser4 What is LR Parser5 What is back Tracking6 What are the problem in Top-Down parsing

8

EXPERIMENT 4

NAME OF THE EXPERIMENT Design of SLR

AIM Design SLR bottom up parser for the above language

THEORYThe 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 -gt α B β in closure(I) for all productions B -gt γ add B -gt γ to

closure(I) if not already there Keep applying this rule until no more items canbe added

Initial elements (I) are often referred to as the kernel elements of closure(I)

Defn Goto(IX) where I is a set of items X is a terminal or non-terminal is the closure(A 1048774 a X b) where A 1048774 a X b is in I

Algorithmbull C =closure(Srsquo1048774 S) where Srsquo 1048774 S is the production added for augmentationbull Repeat

ndash For each item I in C and grammar symbol X such that Goto(IX) is not empty and not in already an element of Cbull Add Goto(IX) to C

ALGORITHM

Step1 StartStep2 Initially the parser has s0 on the stack where s0 is the initial state and w$ is in bufferStep3 Set ip point to the first symbol of w$Step4 repeat forever beginStep5 Let S be the state on top of the stack and a symbol pointed to by ipStep6 If action [S a] =shift S then begin

Push S1 on to the top of the stackAdvance ip to next input symbol

Step7 Else if action [S a] reduce A-gtB then beginPop 2 |B| symbols of the stackLet S1 be the state now on the top of the stack

Step8 Output the production ABEnd

Step9 else if action [S a]=accepted then returnElseError()End

Step10 Stop

9

OUTPUT

Id(id+id)$Grammer accepted

VIVA QUESTIONS

1 What is SLR parsing2 What is Shift reduced parser3 What are the operations of Parser4 What is the use of parsing table5 What is bottom up parsing

10

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

THEORYYacc 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

ALGORITHMStep1 StartStep2 declare the declarations as a header file

includeltctypehgtStep3 token digitStep4 define the translations rules like line expr term factor

Lineexp lsquonrsquo print(ldquon d nrdquo$1)Exprexprrsquo+rsquo term ($$=$1=$3Termterm lsquo+rsquo factor($$ =$1$3FactorFactorrsquo(lsquoenterrsquo) lsquo$$ =$2)

Step5 define the supporting C routinesStep6 Stop

OUTPUT

[rootlocalhost] lex codegenl[rootlocalhost] yacc ndashd codegeny[rootlocalhost] cc lexyyc ytabc ndashll -lm[rootlocalhost] aout

INPUT OUTPUT

2+3

4-2

5

2

VIVA QUESTIONS

1 What is Abstract Syntax tree2 What are BNF Rules3 What is DAG representation4 How LALR(1) states are generates5 In which condition the user has to supply more information to YACC

11

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

THEORYMachine code also known as machine language is the elemental language of computers comprising a long sequence of binary digital zeros and ones

ALGORITHMStep1 StartStep2 for every three ndashaddress statement of the form x=y op zStep3 beginStep4 Call getreg() to obtain the location L which the computation y op z should be performedStep5 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 registerIf the value of y is not currently available in 1then generate an instruction MOV yl

Step6 Generate the instruction OP Zl 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

Step7 If the current values of y ador z are in register and we have no further use for themand they are live at the end of the blockthen after the register descriptor to indicate that after the execution of the statement x=y op zthose registers will no longer contain y and or z

Step8 store all resultsStep9 Stop

OUTPUT

VIVA QUESTIONS

1 What is target code2 What is machine code3 What is Cross compiler

12

4 Give the example for cross compiler5 What is the difference between syntax amp Semantics

13

Page 9: Compiler Design Lab Manual(Archana)Without Source Program-16.12.2013

EXPERIMENT 4

NAME OF THE EXPERIMENT Design of SLR

AIM Design SLR bottom up parser for the above language

THEORYThe 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 -gt α B β in closure(I) for all productions B -gt γ add B -gt γ to

closure(I) if not already there Keep applying this rule until no more items canbe added

Initial elements (I) are often referred to as the kernel elements of closure(I)

Defn Goto(IX) where I is a set of items X is a terminal or non-terminal is the closure(A 1048774 a X b) where A 1048774 a X b is in I

Algorithmbull C =closure(Srsquo1048774 S) where Srsquo 1048774 S is the production added for augmentationbull Repeat

ndash For each item I in C and grammar symbol X such that Goto(IX) is not empty and not in already an element of Cbull Add Goto(IX) to C

ALGORITHM

Step1 StartStep2 Initially the parser has s0 on the stack where s0 is the initial state and w$ is in bufferStep3 Set ip point to the first symbol of w$Step4 repeat forever beginStep5 Let S be the state on top of the stack and a symbol pointed to by ipStep6 If action [S a] =shift S then begin

Push S1 on to the top of the stackAdvance ip to next input symbol

Step7 Else if action [S a] reduce A-gtB then beginPop 2 |B| symbols of the stackLet S1 be the state now on the top of the stack

Step8 Output the production ABEnd

Step9 else if action [S a]=accepted then returnElseError()End

Step10 Stop

9

OUTPUT

Id(id+id)$Grammer accepted

VIVA QUESTIONS

1 What is SLR parsing2 What is Shift reduced parser3 What are the operations of Parser4 What is the use of parsing table5 What is bottom up parsing

10

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

THEORYYacc 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

ALGORITHMStep1 StartStep2 declare the declarations as a header file

includeltctypehgtStep3 token digitStep4 define the translations rules like line expr term factor

Lineexp lsquonrsquo print(ldquon d nrdquo$1)Exprexprrsquo+rsquo term ($$=$1=$3Termterm lsquo+rsquo factor($$ =$1$3FactorFactorrsquo(lsquoenterrsquo) lsquo$$ =$2)

Step5 define the supporting C routinesStep6 Stop

OUTPUT

[rootlocalhost] lex codegenl[rootlocalhost] yacc ndashd codegeny[rootlocalhost] cc lexyyc ytabc ndashll -lm[rootlocalhost] aout

INPUT OUTPUT

2+3

4-2

5

2

VIVA QUESTIONS

1 What is Abstract Syntax tree2 What are BNF Rules3 What is DAG representation4 How LALR(1) states are generates5 In which condition the user has to supply more information to YACC

11

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

THEORYMachine code also known as machine language is the elemental language of computers comprising a long sequence of binary digital zeros and ones

ALGORITHMStep1 StartStep2 for every three ndashaddress statement of the form x=y op zStep3 beginStep4 Call getreg() to obtain the location L which the computation y op z should be performedStep5 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 registerIf the value of y is not currently available in 1then generate an instruction MOV yl

Step6 Generate the instruction OP Zl 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

Step7 If the current values of y ador z are in register and we have no further use for themand they are live at the end of the blockthen after the register descriptor to indicate that after the execution of the statement x=y op zthose registers will no longer contain y and or z

Step8 store all resultsStep9 Stop

OUTPUT

VIVA QUESTIONS

1 What is target code2 What is machine code3 What is Cross compiler

12

4 Give the example for cross compiler5 What is the difference between syntax amp Semantics

13

Page 10: Compiler Design Lab Manual(Archana)Without Source Program-16.12.2013

OUTPUT

Id(id+id)$Grammer accepted

VIVA QUESTIONS

1 What is SLR parsing2 What is Shift reduced parser3 What are the operations of Parser4 What is the use of parsing table5 What is bottom up parsing

10

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

THEORYYacc 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

ALGORITHMStep1 StartStep2 declare the declarations as a header file

includeltctypehgtStep3 token digitStep4 define the translations rules like line expr term factor

Lineexp lsquonrsquo print(ldquon d nrdquo$1)Exprexprrsquo+rsquo term ($$=$1=$3Termterm lsquo+rsquo factor($$ =$1$3FactorFactorrsquo(lsquoenterrsquo) lsquo$$ =$2)

Step5 define the supporting C routinesStep6 Stop

OUTPUT

[rootlocalhost] lex codegenl[rootlocalhost] yacc ndashd codegeny[rootlocalhost] cc lexyyc ytabc ndashll -lm[rootlocalhost] aout

INPUT OUTPUT

2+3

4-2

5

2

VIVA QUESTIONS

1 What is Abstract Syntax tree2 What are BNF Rules3 What is DAG representation4 How LALR(1) states are generates5 In which condition the user has to supply more information to YACC

11

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

THEORYMachine code also known as machine language is the elemental language of computers comprising a long sequence of binary digital zeros and ones

ALGORITHMStep1 StartStep2 for every three ndashaddress statement of the form x=y op zStep3 beginStep4 Call getreg() to obtain the location L which the computation y op z should be performedStep5 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 registerIf the value of y is not currently available in 1then generate an instruction MOV yl

Step6 Generate the instruction OP Zl 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

Step7 If the current values of y ador z are in register and we have no further use for themand they are live at the end of the blockthen after the register descriptor to indicate that after the execution of the statement x=y op zthose registers will no longer contain y and or z

Step8 store all resultsStep9 Stop

OUTPUT

VIVA QUESTIONS

1 What is target code2 What is machine code3 What is Cross compiler

12

4 Give the example for cross compiler5 What is the difference between syntax amp Semantics

13

Page 11: Compiler Design Lab Manual(Archana)Without Source Program-16.12.2013

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

THEORYYacc 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

ALGORITHMStep1 StartStep2 declare the declarations as a header file

includeltctypehgtStep3 token digitStep4 define the translations rules like line expr term factor

Lineexp lsquonrsquo print(ldquon d nrdquo$1)Exprexprrsquo+rsquo term ($$=$1=$3Termterm lsquo+rsquo factor($$ =$1$3FactorFactorrsquo(lsquoenterrsquo) lsquo$$ =$2)

Step5 define the supporting C routinesStep6 Stop

OUTPUT

[rootlocalhost] lex codegenl[rootlocalhost] yacc ndashd codegeny[rootlocalhost] cc lexyyc ytabc ndashll -lm[rootlocalhost] aout

INPUT OUTPUT

2+3

4-2

5

2

VIVA QUESTIONS

1 What is Abstract Syntax tree2 What are BNF Rules3 What is DAG representation4 How LALR(1) states are generates5 In which condition the user has to supply more information to YACC

11

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

THEORYMachine code also known as machine language is the elemental language of computers comprising a long sequence of binary digital zeros and ones

ALGORITHMStep1 StartStep2 for every three ndashaddress statement of the form x=y op zStep3 beginStep4 Call getreg() to obtain the location L which the computation y op z should be performedStep5 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 registerIf the value of y is not currently available in 1then generate an instruction MOV yl

Step6 Generate the instruction OP Zl 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

Step7 If the current values of y ador z are in register and we have no further use for themand they are live at the end of the blockthen after the register descriptor to indicate that after the execution of the statement x=y op zthose registers will no longer contain y and or z

Step8 store all resultsStep9 Stop

OUTPUT

VIVA QUESTIONS

1 What is target code2 What is machine code3 What is Cross compiler

12

4 Give the example for cross compiler5 What is the difference between syntax amp Semantics

13

Page 12: Compiler Design Lab Manual(Archana)Without Source Program-16.12.2013

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

THEORYMachine code also known as machine language is the elemental language of computers comprising a long sequence of binary digital zeros and ones

ALGORITHMStep1 StartStep2 for every three ndashaddress statement of the form x=y op zStep3 beginStep4 Call getreg() to obtain the location L which the computation y op z should be performedStep5 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 registerIf the value of y is not currently available in 1then generate an instruction MOV yl

Step6 Generate the instruction OP Zl 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

Step7 If the current values of y ador z are in register and we have no further use for themand they are live at the end of the blockthen after the register descriptor to indicate that after the execution of the statement x=y op zthose registers will no longer contain y and or z

Step8 store all resultsStep9 Stop

OUTPUT

VIVA QUESTIONS

1 What is target code2 What is machine code3 What is Cross compiler

12

4 Give the example for cross compiler5 What is the difference between syntax amp Semantics

13

Page 13: Compiler Design Lab Manual(Archana)Without Source Program-16.12.2013

4 Give the example for cross compiler5 What is the difference between syntax amp Semantics

13