Topics: Ambiguous Grammar, DFA and SLR grammar.
Ismail Mohamed mohamud (maahir)
Abdiwahid farah isse
Abdifitah Awil Ahmed
Abdinor mohaedAhmed
Abdinasir nor kadie
Group five
Ambiguous Grammar
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.
Ambiguous Grammar
Some programming languages have ambiguous grammars; in this case, semantic
information is needed to select the intended parse tree of an ambiguous construct.
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 type def
name that is visible at this point.
Ambiguous Grammar
A CFG is said to be ambiguous if and only if it contains more than one derivation
trees for same string.
Definition of Ambiguous Grammar: Let G = (N,T, P, S ) be a CFG. A string w
∈ L(G) is said to be “ambiguously derivable “if there are two or more different
derivation trees for that string in G.
Definition of Ambiguous Grammar: A CFG given by G = (N, T, P, S) is said to
be “ambiguous” if there exists at least one string in L(G) which is ambiguously
derivable. Otherwise it is unambiguous.
Ambiguous Grammar
Ambiguity is a property of a grammar, and it is usually, but not always possible to find
an equivalent unambiguous grammar.
An “ inherantly ambiguous language” is a language for which no unambiguous
grammar exists.
Solved Example:
Show that the grammar G with production
S → a | aAb | abSb
A → aAAb | bS
is ambiguous.
Ambiguous Grammar
Solution:
S ⟹ abSb ( ∵ S → abSb)
⟹ abab (∵ S → a)
Similarly,
S ⟹ aAb ( ∵ S → aAb)
⟹ abSb (∵ A → bS)
⟹ abab (∵ S → a)
Since ‘abab’ has two different derivations, the grammar G is ambiguous.
Proof of CFG is ambiguous or not by using parse tree solved example:
Consider the grammar G with production:
S → aSS
S → b
Ambiguous Grammar
• The parse trees are as follows.
• Top down Parsing: Sequence of rules are applied in a leftmost derivation in Top
down parsing.
•
Bottom-up Parsing: Sequence of rules are applied in a rightmost derivation in
Bottom-up parsing.
Deterministic finite Automata
DFArefers to deterministic finite automata. Deterministicrefers to theuniquenessof the
computation.Thefinite automata are called deterministic finite automata if themachineis read an
input string onesymbolat a time.
InDFA,there isonly onepath for specific input from thecurrentstate to thenext state.
DFAdoesnotaccept thenullmove,i.e., theDFAcannotchangestate without any input character
DFAcancontain multiple final states.It isusedinLexicalAnalysisinCompiler.
Deterministic finite Automata
A DFAcanberepresentedby digraphs calledstatediagram.In which:
Thestate isrepresented byvertices.
Thearc labeled with aninput character showthe transitions.
Theinitial state ismarked with anarrow.
Thefinal state isdenoted by a doublecircle.
SIMPLE LR PARSING SLR
SLR refers to simpleLRParsing.It issameasLR(0)parsing. Theonly difference isintheparsing
table. T
oconstructSLR(1) parsing table, weusecanonical collection of LR(0) item.
IntheSLR(1) parsing, weplace thereducemoveonly inthefollow of left hand side.
Various stepsinvolved in theSLR(1) Parsing:
Forthegiven input string write a context free grammar
Checktheambiguity of the grammar
AddAugmentproductioninthegiven grammar
Create Canonicalcollection of LR(0) items
Draw a data flow diagram (DFA)
Constructa SLR(1) parsing table
Constructing SLR Parsing Tables – LR(0) Item
An LR(0) item of a grammar G is a production of G a dot at the
some position of the right side.
Ex:
A → aBb
Possible LR(0) Items:
A → ∙ aBb
(four different possibility)
A → a ∙ Bb
A → aB ∙ b
A → aBb
Constructing SLR Parsing Tables – LR(0) Item
Sets of LR(0) items will be the states of action and goto table of
the SLR parser.
States represent sets of “items”
LR parser makes shift-reduce decision by maintaining states to
keep track of where we are in a parsing process
SLR grammars are a superset of all LR(0) grammars
In the LR parsing,
"L" stands for left-to-right scanning of the input.
"R" stands for constructing a right most derivation in reverse.
Constructing SLR Parsing Tables
A collection of sets of LR(0) items (the canonical LR(0)
collection) is the basis for constructing SLR parsers.
Canonical LR(0) collection provides the basis of constructing a
DFA called LR(0) automaton
This DFA is used to make parsing decisions
Each state of LR(0) automaton represents a set of items in the
canonical LR(0) collection

Ambiguous grammar, dfa and slr grammar.

  • 1.
    Topics: Ambiguous Grammar,DFA and SLR grammar. Ismail Mohamed mohamud (maahir) Abdiwahid farah isse Abdifitah Awil Ahmed Abdinor mohaedAhmed Abdinasir nor kadie Group five
  • 2.
    Ambiguous Grammar In computerscience, 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.
  • 3.
    Ambiguous Grammar Some programminglanguages have ambiguous grammars; in this case, semantic information is needed to select the intended parse tree of an ambiguous construct. 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 type def name that is visible at this point.
  • 4.
    Ambiguous Grammar A CFGis said to be ambiguous if and only if it contains more than one derivation trees for same string. Definition of Ambiguous Grammar: Let G = (N,T, P, S ) be a CFG. A string w ∈ L(G) is said to be “ambiguously derivable “if there are two or more different derivation trees for that string in G. Definition of Ambiguous Grammar: A CFG given by G = (N, T, P, S) is said to be “ambiguous” if there exists at least one string in L(G) which is ambiguously derivable. Otherwise it is unambiguous.
  • 5.
    Ambiguous Grammar Ambiguity isa property of a grammar, and it is usually, but not always possible to find an equivalent unambiguous grammar. An “ inherantly ambiguous language” is a language for which no unambiguous grammar exists. Solved Example: Show that the grammar G with production S → a | aAb | abSb A → aAAb | bS is ambiguous.
  • 6.
    Ambiguous Grammar Solution: S ⟹abSb ( ∵ S → abSb) ⟹ abab (∵ S → a) Similarly, S ⟹ aAb ( ∵ S → aAb) ⟹ abSb (∵ A → bS) ⟹ abab (∵ S → a) Since ‘abab’ has two different derivations, the grammar G is ambiguous. Proof of CFG is ambiguous or not by using parse tree solved example: Consider the grammar G with production: S → aSS S → b
  • 7.
    Ambiguous Grammar • Theparse trees are as follows. • Top down Parsing: Sequence of rules are applied in a leftmost derivation in Top down parsing. • Bottom-up Parsing: Sequence of rules are applied in a rightmost derivation in Bottom-up parsing.
  • 8.
    Deterministic finite Automata DFArefersto deterministic finite automata. Deterministicrefers to theuniquenessof the computation.Thefinite automata are called deterministic finite automata if themachineis read an input string onesymbolat a time. InDFA,there isonly onepath for specific input from thecurrentstate to thenext state. DFAdoesnotaccept thenullmove,i.e., theDFAcannotchangestate without any input character DFAcancontain multiple final states.It isusedinLexicalAnalysisinCompiler.
  • 9.
    Deterministic finite Automata ADFAcanberepresentedby digraphs calledstatediagram.In which: Thestate isrepresented byvertices. Thearc labeled with aninput character showthe transitions. Theinitial state ismarked with anarrow. Thefinal state isdenoted by a doublecircle.
  • 10.
    SIMPLE LR PARSINGSLR SLR refers to simpleLRParsing.It issameasLR(0)parsing. Theonly difference isintheparsing table. T oconstructSLR(1) parsing table, weusecanonical collection of LR(0) item. IntheSLR(1) parsing, weplace thereducemoveonly inthefollow of left hand side. Various stepsinvolved in theSLR(1) Parsing: Forthegiven input string write a context free grammar Checktheambiguity of the grammar AddAugmentproductioninthegiven grammar Create Canonicalcollection of LR(0) items Draw a data flow diagram (DFA) Constructa SLR(1) parsing table
  • 11.
    Constructing SLR ParsingTables – LR(0) Item An LR(0) item of a grammar G is a production of G a dot at the some position of the right side. Ex: A → aBb Possible LR(0) Items: A → ∙ aBb (four different possibility) A → a ∙ Bb A → aB ∙ b A → aBb
  • 12.
    Constructing SLR ParsingTables – LR(0) Item Sets of LR(0) items will be the states of action and goto table of the SLR parser. States represent sets of “items” LR parser makes shift-reduce decision by maintaining states to keep track of where we are in a parsing process SLR grammars are a superset of all LR(0) grammars In the LR parsing, "L" stands for left-to-right scanning of the input. "R" stands for constructing a right most derivation in reverse.
  • 13.
    Constructing SLR ParsingTables A collection of sets of LR(0) items (the canonical LR(0) collection) is the basis for constructing SLR parsers. Canonical LR(0) collection provides the basis of constructing a DFA called LR(0) automaton This DFA is used to make parsing decisions Each state of LR(0) automaton represents a set of items in the canonical LR(0) collection