SlideShare una empresa de Scribd logo
1 de 19
Ambiguous grammar, LMD & RMD, Infix &
Postfix, Implementation Of 3 address Code
Compiler Design
Team Members
Group-I Assignment Topic : BRESENHARAM'S ALGORITHM (ELIPSE Drawing)
Group's representative: TANGUTURU SAI KRISHNA
S.No. BITS ID NAME Official Email ID Personal Email ID
1 2011HW69898
TANGUTURU SAI KRISHNA saikrishna.tanguturu@wipro.com sai.tsk2008@gmail.com
2 2011HW69900
RAYAPU MOSES rayapu.moses@wipro.com stalinkvd001@gmail.com
3 2011HW69932 SHENBAGAMOORTHY A
shenbagamoorthy.a83@wipro.com moorthy2626@gmail.com
4 2011HW69913
ANURUPA K C anurupa.c85@wipro.com anu.rupa30@gmail.com
5 2011HW69909
ARUNJUNAISELVAM P arunjunaiselvam.p95@wipro.com arunjunai.carrer@gmail.com
6 2011HW69569
PRANOB JYOTI KALITA pranob.kalita@wipro.com pranob.kalita90@gmail.com
7 2011HW69893
TINNALURI V N PRASANTH prasanth.tinnaluri@wipro.com naga.prasanth985@gmail.com
8 2011HW69904
KONDALA SUMATHI sumathi.kondala@wipro.com sumathi.kondala@gmail.com
9 2011HW69896
DASIKA KRISHNA dasika.krishna@wipro.com dasikakrishnas@gmail.com
10 2011HW69907
SHEIK SANAVULLA sheik.sanavulla@wipro.com sanavulla.sms@gmail.com
What is an ambiguous grammar? Give
an example.
 In computer science, a context-free grammar is said to be an ambiguous grammar if
there exists a string which can be generated by the grammar in more than one way
(i.e., the string admits more than one parse tree or, equivalently, more than one
leftmost derivation). A context-free language is inherently ambiguous if all context-
free grammars generating that language are ambiguous.
Some programming languages have ambiguous grammars; in this case, semantic
information is needed to select the intended parse tree of an ambiguous construct.
For example, in C the following:
x * y ;
can be interpreted as either:
* the declaration of an identifier named y of type pointer-to-x, or
* an expression in which x is multiplied by y and then the result is discarded.
To correctly choose between the two possible interpretations, a compiler must
consult its symbol table to find out whether x has been declared as a typedef name
that is visible at this point.
Leftmost and Rightmost Derivations
 At any stage during a parse, when we have derived some sentential form
(that is not yet a sentence) we will potentially have two choices to make:
 which non-terminal in the sentential form to apply a production rule to
 which production rule for that non-terminal to apply
 Eg. in the above example, when we derived , we could then have applied a
production rule to any of these three non-terminals, and would then have
had to choose among all the production rules for either or .
 The first decision here is relatively easy to solve: we will be reading the
input string from left to right, so it is our own interest to derive the
leftmost terminal of the resulting sentence as soon as possible. Thus, in a
top-down parse we always choose the leftmost non-terminal in a sentential
form to apply a production rule to - this is called a leftmost derivation.
 If we were doing a bottom-up parse then the situation would be reversed,
and we would want to do apply the production rules in reverse to the
leftmost symbols; thus we are performing a rightmost derivation in
reverse.
Cont., Leftmost Derivations
Cont., Rightmost Derivations
Infix & Postfix
 Infix, Postfix and Prefix notations are three different but equivalent ways of writing expressions. It is
easiest to demonstrate the differences by looking at examples of operators that take two operands.
 Infix notation: X + Y
 Operators are written in-between their operands. This is the usual way we write expressions. An
expression such as A * ( B + C ) / D is usually taken to mean something like: "First add B and C
together, then multiply the result by A, then divide by D to give the final answer."
 Infix notation needs extra information to make the order of evaluation of the operators clear: rules built
into the language about operator precedence and associativity, and brackets ( ) to allow users to
override these rules. For example, the usual rules for associativity say that we perform operations from
left to right, so the multiplication by A is assumed to come before the division by D. Similarly, the usual
rules for precedence say that we perform multiplication and division before we perform addition and
subtraction
 Postfix notation (also known as "Reverse Polish notation"): X Y +
 Operators are written after their operands. The infix expression given above is equivalent to
A B C + * D /
The order of evaluation of operators is always left-to-right, and brackets cannot be used to change this
order. Because the "+" is to the left of the "*" in the example above, the addition must be performed
before the multiplication.
Operators act on values immediately to the left of them. For example, the "+" above uses the "B" and "C".
We can add (totally unnecessary) brackets to make this explicit:
( (A (B C +) *) D /)
Thus, the "*" uses the two values immediately preceding: "A", and the result of the addition. Similarly, the
"/" uses the result of the multiplication and the "D".
Cont ., Infix & Postfix
Infix Postfix Notes
A * B + C / D A B * C D / +
multiply A and B,
divide C by D,
add the results
A * (B + C) / D A B C + * D /
add B and C,
multiply by A,
divide by D
A * (B + C / D) A B C D / + *
divide C by D,
add B,
multiply by A
Cont ., Infix & Postfix
 Converting between these notations
 The most straightforward method is to start by
inserting all the implicit brackets that show the
order of evaluation e.g.:
 Infix Postfix
( (A * B) + (C / D) ) ( (A B *) (C D /) +)
((A * (B + C) ) / D) ( (A (B C +) *) D /)
(A * (B + (C / D) ) ) (A (B (C D /) +) *)
Cont ., Infix & Postfix
 You can convert directly between these bracketed
forms simply by moving the operator within the
brackets e.g. (X + Y) or (X Y +) or (+ X Y). Repeat this
for all the operators in an expression, and finally
remove any superfluous brackets.
 You can use a similar trick to convert to and from
parse trees - each bracketed triplet of an operator
and its two operands (or sub-expressions)
corresponds to a node of the tree. The
corresponding parse trees are:
Cont ., Infix & Postfix
Implementation Of 3 address Code:
 A three-address statement is an abstract form of
intermediate code. In a compiler, these statements
can be implemented as records with fields for the
operator and the operands. Three such
representations are quadruples, triples, and
indirect triples.
 There are 3 methods in which 3 address code.
1. Quadrupules,
2. Triples,
3. Indirect Triples
Quadrupules,Triples & Indirect Triples
 Quadruples
 A quadruple is a record structure with four fields, which we
call op, arg l, arg 2, and result. The op field contains an
internal code for the operator. The three-address statement
x:= y op z is represented by placing y in arg 1. z in arg 2. and x
in result. Statements with unary operators like x: = – y or x: =
y do not use arg 2. Operators like param use neither arg2 nor
result. Conditional and unconditional jumps put the target
label in result. The quadruples in Fig. H.S(a) are for the
assignment a: = b+ – c + b i – c. They are obtained from the
three-address code in Fig. 8.5(a). The contents of fields arg 1,
arg 2, and result are normally pointers to the symbol-table
entries for the names represented by these fields. If so,
temporary names must be entered into the symbol table as
they are created.
 Eg. a := b * -c + b * -c
Quadruples:(easy to rearrange code for
global optimization, lots of temporaries)
Quadruples:(easy to rearrange code for global optimization, lots of temporaries)
# Op Arg1 Arg2 Res
(0) uminus c t1
(1) * b t1 t2
(2) uminus c t3
(3) * b t3 t4
(4) + t2 t4 t5
(5) := t5 a
 Triples
 To avoid entering temporary names into the symbol
table. we might refer to a temporary value bi the
position of the statement that computes it. If we do so,
three-address statements can be represented by records
with only three fields: op, arg 1 and arg2, as in Fig.
8.8(b). The fields arg l and arg2, for the arguments of op,
are either pointers to the symbol table (for
programmer- defined names or constants) or pointers
into the triple structure (for temporary values). Since
three fields are used, this intermediate code format is
known as triples.’ Except for the treatment of
programmer-defined names, triples correspond to the
representation of a syntax tree or dag by an array of
nodes, as below
Triples: (temporaries are implicit,
difficult to rearrange code)
# Op Arg1 Arg2
(0) uminus c
(1) * b (0)
(2) uminus c
(3) * b (2)
(4) + (1) (3)
(5) := a (4)
 Indirect Triples
 Another implementation of three-address code that
has been considered is that of listing pointers to
triples, rather than listing the triples themselves.
This implementation is naturally called indirect
triples. For example, let us use an
array statement to list pointers to triples in the
desired order.
Indirect Triples: (temporaries are implicit
& easier to rearrange code.
#(Program) Stmt #(Triple
Counter)
Op Arg1 Arg2
(0) (14)  (14) uminus c
(1) (15)  (15) * b (14)
(2) (16)  (16) uminus c
(3) (17)  (17) * b (16)
(4) (18)  (18) + (15) (17)
(5) (19)  (19) := a (18)
Thank You

Más contenido relacionado

La actualidad más candente

Code Optimization
Code OptimizationCode Optimization
Code Optimization
guest9f8315
 

La actualidad más candente (20)

Working principle of Turing machine
Working principle of Turing machineWorking principle of Turing machine
Working principle of Turing machine
 
Introduction to Compiler design
Introduction to Compiler design Introduction to Compiler design
Introduction to Compiler design
 
Code Optimization
Code OptimizationCode Optimization
Code Optimization
 
Syntax analysis
Syntax analysisSyntax analysis
Syntax analysis
 
Turing machine-TOC
Turing machine-TOCTuring machine-TOC
Turing machine-TOC
 
Lexical analysis - Compiler Design
Lexical analysis - Compiler DesignLexical analysis - Compiler Design
Lexical analysis - Compiler Design
 
Role-of-lexical-analysis
Role-of-lexical-analysisRole-of-lexical-analysis
Role-of-lexical-analysis
 
Code Optimization
Code OptimizationCode Optimization
Code Optimization
 
A Role of Lexical Analyzer
A Role of Lexical AnalyzerA Role of Lexical Analyzer
A Role of Lexical Analyzer
 
CONTEXT FREE GRAMMAR
CONTEXT FREE GRAMMAR CONTEXT FREE GRAMMAR
CONTEXT FREE GRAMMAR
 
Characteristics Schedule based on Recover-ability & Serial-ability
Characteristics Schedule based on Recover-ability & Serial-abilityCharacteristics Schedule based on Recover-ability & Serial-ability
Characteristics Schedule based on Recover-ability & Serial-ability
 
MACRO PROCESSOR
MACRO PROCESSORMACRO PROCESSOR
MACRO PROCESSOR
 
Context free grammar
Context free grammar Context free grammar
Context free grammar
 
1.Role lexical Analyzer
1.Role lexical Analyzer1.Role lexical Analyzer
1.Role lexical Analyzer
 
Push Down Automata (PDA) | TOC (Theory of Computation) | NPDA | DPDA
Push Down Automata (PDA) | TOC  (Theory of Computation) | NPDA | DPDAPush Down Automata (PDA) | TOC  (Theory of Computation) | NPDA | DPDA
Push Down Automata (PDA) | TOC (Theory of Computation) | NPDA | DPDA
 
Three address code In Compiler Design
Three address code In Compiler DesignThree address code In Compiler Design
Three address code In Compiler Design
 
Theory of automata and formal language
Theory of automata and formal languageTheory of automata and formal language
Theory of automata and formal language
 
Code generation
Code generationCode generation
Code generation
 
Phases of Compiler
Phases of CompilerPhases of Compiler
Phases of Compiler
 
Interrupts
InterruptsInterrupts
Interrupts
 

Destacado

Chapter Three(2)
Chapter Three(2)Chapter Three(2)
Chapter Three(2)
bolovv
 

Destacado (20)

Build a compiler using C#, Irony and RunSharp.
Build a compiler using C#, Irony and RunSharp.Build a compiler using C#, Irony and RunSharp.
Build a compiler using C#, Irony and RunSharp.
 
Grammar
GrammarGrammar
Grammar
 
Type checking in compiler design
Type checking in compiler designType checking in compiler design
Type checking in compiler design
 
Type Checking(Compiler Design) #ShareThisIfYouLike
Type Checking(Compiler Design) #ShareThisIfYouLikeType Checking(Compiler Design) #ShareThisIfYouLike
Type Checking(Compiler Design) #ShareThisIfYouLike
 
Infix prefix postfix expression -conversion
Infix  prefix postfix expression -conversionInfix  prefix postfix expression -conversion
Infix prefix postfix expression -conversion
 
About Tokens and Lexemes
About Tokens and LexemesAbout Tokens and Lexemes
About Tokens and Lexemes
 
Lecture 11 semantic analysis 2
Lecture 11 semantic analysis 2Lecture 11 semantic analysis 2
Lecture 11 semantic analysis 2
 
Syntax directed translation
Syntax directed translationSyntax directed translation
Syntax directed translation
 
Ambiguity
AmbiguityAmbiguity
Ambiguity
 
Structural ambiguity
Structural ambiguityStructural ambiguity
Structural ambiguity
 
5th Semester (June-2016) Computer Science and Information Science Engineering...
5th Semester (June-2016) Computer Science and Information Science Engineering...5th Semester (June-2016) Computer Science and Information Science Engineering...
5th Semester (June-2016) Computer Science and Information Science Engineering...
 
Compiler Questions
Compiler QuestionsCompiler Questions
Compiler Questions
 
Lecture 10 semantic analysis 01
Lecture 10 semantic analysis 01Lecture 10 semantic analysis 01
Lecture 10 semantic analysis 01
 
Bottomupparser
BottomupparserBottomupparser
Bottomupparser
 
Chapter Three(2)
Chapter Three(2)Chapter Three(2)
Chapter Three(2)
 
Compiler presention
Compiler presentionCompiler presention
Compiler presention
 
Compiler design
Compiler designCompiler design
Compiler design
 
Compilers Design
Compilers DesignCompilers Design
Compilers Design
 
Data Locality
Data LocalityData Locality
Data Locality
 
Lexicalanalyzer
LexicalanalyzerLexicalanalyzer
Lexicalanalyzer
 

Similar a Compiler Design - Ambiguous grammar, LMD & RMD, Infix & Postfix, Implementation Of 3 address Code

14-Intermediate code generation - Variants of Syntax trees - Three Address Co...
14-Intermediate code generation - Variants of Syntax trees - Three Address Co...14-Intermediate code generation - Variants of Syntax trees - Three Address Co...
14-Intermediate code generation - Variants of Syntax trees - Three Address Co...
venkatapranaykumarGa
 
C programming session 02
C programming session 02C programming session 02
C programming session 02
Dushmanta Nath
 
Intermediate code- generation
Intermediate code- generationIntermediate code- generation
Intermediate code- generation
rawan_z
 
Chapter 3.3
Chapter 3.3Chapter 3.3
Chapter 3.3
sotlsoc
 

Similar a Compiler Design - Ambiguous grammar, LMD & RMD, Infix & Postfix, Implementation Of 3 address Code (20)

Assignment12
Assignment12Assignment12
Assignment12
 
Conversion of in fix pre fix,infix by sarmad baloch
Conversion of in fix pre fix,infix by sarmad balochConversion of in fix pre fix,infix by sarmad baloch
Conversion of in fix pre fix,infix by sarmad baloch
 
Mycasestudy
MycasestudyMycasestudy
Mycasestudy
 
Linear Data Structures_SSD.pdf
Linear Data Structures_SSD.pdfLinear Data Structures_SSD.pdf
Linear Data Structures_SSD.pdf
 
14-Intermediate code generation - Variants of Syntax trees - Three Address Co...
14-Intermediate code generation - Variants of Syntax trees - Three Address Co...14-Intermediate code generation - Variants of Syntax trees - Three Address Co...
14-Intermediate code generation - Variants of Syntax trees - Three Address Co...
 
Intermediate code generation
Intermediate code generationIntermediate code generation
Intermediate code generation
 
Chapter 6 intermediate code generation
Chapter 6   intermediate code generationChapter 6   intermediate code generation
Chapter 6 intermediate code generation
 
C programming session 02
C programming session 02C programming session 02
C programming session 02
 
Lecture 12 intermediate code generation
Lecture 12 intermediate code generationLecture 12 intermediate code generation
Lecture 12 intermediate code generation
 
Chapter 6 Intermediate Code Generation
Chapter 6   Intermediate Code GenerationChapter 6   Intermediate Code Generation
Chapter 6 Intermediate Code Generation
 
C language basics
C language basicsC language basics
C language basics
 
[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence
[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence
[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence
 
C program
C programC program
C program
 
Topic 2_revised.pptx
Topic 2_revised.pptxTopic 2_revised.pptx
Topic 2_revised.pptx
 
Cd2 [autosaved]
Cd2 [autosaved]Cd2 [autosaved]
Cd2 [autosaved]
 
Intermediate code- generation
Intermediate code- generationIntermediate code- generation
Intermediate code- generation
 
Chapter 3.3
Chapter 3.3Chapter 3.3
Chapter 3.3
 
Compiler chapter six .ppt course material
Compiler chapter six .ppt course materialCompiler chapter six .ppt course material
Compiler chapter six .ppt course material
 
Compiler notes--unit-iii
Compiler notes--unit-iiiCompiler notes--unit-iii
Compiler notes--unit-iii
 
Intermediate code generator
Intermediate code generatorIntermediate code generator
Intermediate code generator
 

Último

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Último (20)

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 

Compiler Design - Ambiguous grammar, LMD & RMD, Infix & Postfix, Implementation Of 3 address Code

  • 1. Ambiguous grammar, LMD & RMD, Infix & Postfix, Implementation Of 3 address Code Compiler Design
  • 2. Team Members Group-I Assignment Topic : BRESENHARAM'S ALGORITHM (ELIPSE Drawing) Group's representative: TANGUTURU SAI KRISHNA S.No. BITS ID NAME Official Email ID Personal Email ID 1 2011HW69898 TANGUTURU SAI KRISHNA saikrishna.tanguturu@wipro.com sai.tsk2008@gmail.com 2 2011HW69900 RAYAPU MOSES rayapu.moses@wipro.com stalinkvd001@gmail.com 3 2011HW69932 SHENBAGAMOORTHY A shenbagamoorthy.a83@wipro.com moorthy2626@gmail.com 4 2011HW69913 ANURUPA K C anurupa.c85@wipro.com anu.rupa30@gmail.com 5 2011HW69909 ARUNJUNAISELVAM P arunjunaiselvam.p95@wipro.com arunjunai.carrer@gmail.com 6 2011HW69569 PRANOB JYOTI KALITA pranob.kalita@wipro.com pranob.kalita90@gmail.com 7 2011HW69893 TINNALURI V N PRASANTH prasanth.tinnaluri@wipro.com naga.prasanth985@gmail.com 8 2011HW69904 KONDALA SUMATHI sumathi.kondala@wipro.com sumathi.kondala@gmail.com 9 2011HW69896 DASIKA KRISHNA dasika.krishna@wipro.com dasikakrishnas@gmail.com 10 2011HW69907 SHEIK SANAVULLA sheik.sanavulla@wipro.com sanavulla.sms@gmail.com
  • 3. What is an ambiguous grammar? Give an example.  In computer science, a context-free grammar is said to be an ambiguous grammar if there exists a string which can be generated by the grammar in more than one way (i.e., the string admits more than one parse tree or, equivalently, more than one leftmost derivation). A context-free language is inherently ambiguous if all context- free grammars generating that language are ambiguous. Some programming languages have ambiguous grammars; in this case, semantic information is needed to select the intended parse tree of an ambiguous construct. For example, in C the following: x * y ; can be interpreted as either: * the declaration of an identifier named y of type pointer-to-x, or * an expression in which x is multiplied by y and then the result is discarded. To correctly choose between the two possible interpretations, a compiler must consult its symbol table to find out whether x has been declared as a typedef name that is visible at this point.
  • 4. Leftmost and Rightmost Derivations  At any stage during a parse, when we have derived some sentential form (that is not yet a sentence) we will potentially have two choices to make:  which non-terminal in the sentential form to apply a production rule to  which production rule for that non-terminal to apply  Eg. in the above example, when we derived , we could then have applied a production rule to any of these three non-terminals, and would then have had to choose among all the production rules for either or .  The first decision here is relatively easy to solve: we will be reading the input string from left to right, so it is our own interest to derive the leftmost terminal of the resulting sentence as soon as possible. Thus, in a top-down parse we always choose the leftmost non-terminal in a sentential form to apply a production rule to - this is called a leftmost derivation.  If we were doing a bottom-up parse then the situation would be reversed, and we would want to do apply the production rules in reverse to the leftmost symbols; thus we are performing a rightmost derivation in reverse.
  • 7. Infix & Postfix  Infix, Postfix and Prefix notations are three different but equivalent ways of writing expressions. It is easiest to demonstrate the differences by looking at examples of operators that take two operands.  Infix notation: X + Y  Operators are written in-between their operands. This is the usual way we write expressions. An expression such as A * ( B + C ) / D is usually taken to mean something like: "First add B and C together, then multiply the result by A, then divide by D to give the final answer."  Infix notation needs extra information to make the order of evaluation of the operators clear: rules built into the language about operator precedence and associativity, and brackets ( ) to allow users to override these rules. For example, the usual rules for associativity say that we perform operations from left to right, so the multiplication by A is assumed to come before the division by D. Similarly, the usual rules for precedence say that we perform multiplication and division before we perform addition and subtraction  Postfix notation (also known as "Reverse Polish notation"): X Y +  Operators are written after their operands. The infix expression given above is equivalent to A B C + * D / The order of evaluation of operators is always left-to-right, and brackets cannot be used to change this order. Because the "+" is to the left of the "*" in the example above, the addition must be performed before the multiplication. Operators act on values immediately to the left of them. For example, the "+" above uses the "B" and "C". We can add (totally unnecessary) brackets to make this explicit: ( (A (B C +) *) D /) Thus, the "*" uses the two values immediately preceding: "A", and the result of the addition. Similarly, the "/" uses the result of the multiplication and the "D".
  • 8. Cont ., Infix & Postfix Infix Postfix Notes A * B + C / D A B * C D / + multiply A and B, divide C by D, add the results A * (B + C) / D A B C + * D / add B and C, multiply by A, divide by D A * (B + C / D) A B C D / + * divide C by D, add B, multiply by A
  • 9. Cont ., Infix & Postfix  Converting between these notations  The most straightforward method is to start by inserting all the implicit brackets that show the order of evaluation e.g.:  Infix Postfix ( (A * B) + (C / D) ) ( (A B *) (C D /) +) ((A * (B + C) ) / D) ( (A (B C +) *) D /) (A * (B + (C / D) ) ) (A (B (C D /) +) *)
  • 10. Cont ., Infix & Postfix  You can convert directly between these bracketed forms simply by moving the operator within the brackets e.g. (X + Y) or (X Y +) or (+ X Y). Repeat this for all the operators in an expression, and finally remove any superfluous brackets.  You can use a similar trick to convert to and from parse trees - each bracketed triplet of an operator and its two operands (or sub-expressions) corresponds to a node of the tree. The corresponding parse trees are:
  • 11. Cont ., Infix & Postfix
  • 12. Implementation Of 3 address Code:  A three-address statement is an abstract form of intermediate code. In a compiler, these statements can be implemented as records with fields for the operator and the operands. Three such representations are quadruples, triples, and indirect triples.  There are 3 methods in which 3 address code. 1. Quadrupules, 2. Triples, 3. Indirect Triples
  • 13. Quadrupules,Triples & Indirect Triples  Quadruples  A quadruple is a record structure with four fields, which we call op, arg l, arg 2, and result. The op field contains an internal code for the operator. The three-address statement x:= y op z is represented by placing y in arg 1. z in arg 2. and x in result. Statements with unary operators like x: = – y or x: = y do not use arg 2. Operators like param use neither arg2 nor result. Conditional and unconditional jumps put the target label in result. The quadruples in Fig. H.S(a) are for the assignment a: = b+ – c + b i – c. They are obtained from the three-address code in Fig. 8.5(a). The contents of fields arg 1, arg 2, and result are normally pointers to the symbol-table entries for the names represented by these fields. If so, temporary names must be entered into the symbol table as they are created.  Eg. a := b * -c + b * -c
  • 14. Quadruples:(easy to rearrange code for global optimization, lots of temporaries) Quadruples:(easy to rearrange code for global optimization, lots of temporaries) # Op Arg1 Arg2 Res (0) uminus c t1 (1) * b t1 t2 (2) uminus c t3 (3) * b t3 t4 (4) + t2 t4 t5 (5) := t5 a
  • 15.  Triples  To avoid entering temporary names into the symbol table. we might refer to a temporary value bi the position of the statement that computes it. If we do so, three-address statements can be represented by records with only three fields: op, arg 1 and arg2, as in Fig. 8.8(b). The fields arg l and arg2, for the arguments of op, are either pointers to the symbol table (for programmer- defined names or constants) or pointers into the triple structure (for temporary values). Since three fields are used, this intermediate code format is known as triples.’ Except for the treatment of programmer-defined names, triples correspond to the representation of a syntax tree or dag by an array of nodes, as below
  • 16. Triples: (temporaries are implicit, difficult to rearrange code) # Op Arg1 Arg2 (0) uminus c (1) * b (0) (2) uminus c (3) * b (2) (4) + (1) (3) (5) := a (4)
  • 17.  Indirect Triples  Another implementation of three-address code that has been considered is that of listing pointers to triples, rather than listing the triples themselves. This implementation is naturally called indirect triples. For example, let us use an array statement to list pointers to triples in the desired order.
  • 18. Indirect Triples: (temporaries are implicit & easier to rearrange code. #(Program) Stmt #(Triple Counter) Op Arg1 Arg2 (0) (14)  (14) uminus c (1) (15)  (15) * b (14) (2) (16)  (16) uminus c (3) (17)  (17) * b (16) (4) (18)  (18) + (15) (17) (5) (19)  (19) := a (18)