0% found this document useful (0 votes)
34 views14 pages

Lec 01. Grammar, Derivations, Parse Tree

Uploaded by

zainfayyaz498
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views14 pages

Lec 01. Grammar, Derivations, Parse Tree

Uploaded by

zainfayyaz498
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Grammar

 Why we are interested in grammar in because syntax analysis or


parsing phase of the compiler which looks for syntax errors, use CFG
or grammar in general.
 The syntax of grammar language constructs can be specified by CFG.
 A grammar give a precise syntactic specification of a
programming language.
 Universal Parsing Methods can parse any grammar. These methods
are, however, too insufficient to use in production compiler.
Grammar
Introduction:
 Language usually contain infinite number of strings, we cannot
tabulate each and every string to represent the language. Therefor like
automata.
 Grammar is also a mathematical model for representation of
language, using which we can generate the entire language.
 Therefor, a grammar is usually thought of as a language generator.
 E.g., L = {a, aa, ab, aaa, aab, aba, …}
 Grammar L Automata
Define a Language by Grammar
 To generate a string in the language, one begins with a string consisting
of only a single start symbol.
 The production rules are then applied in any order, until a string that
contains neither the start symbol nor designated nonterminal symbol is
produced.
 A sequence of rule applications is called a derivations.
 All the intermediate stages (strings) is called sentential forms. The
language formed by the grammar consists of all distinct strings that can

 L(G) = {w | w ϵ ∑*, s ⇨* W}
be generated in this manner.

 ⇨*(reflexive, transitive closure)


Formal Grammar
 A Formal Grammar is define by 4-tuple ( Σ, P, S)
 is finite nonempty set whose elements are called variables/ nonterminal.
 Σ is finite nonempty set whose elements are called terminals, Ո Σ = Ф.
 S is a special variable (i.e., any element of (S ϵ ) ) called the start symbol. Like every automaton
has exactly one initial state, similarly every grammar has exactly one start symbol.
 P is a finite set whose element are α → β. Where α and β are strings on Ս Σ.
 α has at least one symbol from , the element of P are called productions or production rules
or rewriting rules.
 For a formal valid productions,
 α→β
 α ϵ {Σ Ս }* {Σ Ս }*
 β ϵ {Σ Ս }*
Equivalence of Grammar
 and are equivalent if:
L () = L ()
Derivation
 Derivation: The process of Deriving a string is known as Derivation.
 Derivation/ Syntax/ Parse Tree: The graphical representation of
derivation is known as derivation of tree.
 E→ E + E / E * E / E = E / I’d
Derivation
 Sentential Form: Intermediate step involve in the derivation is known as
Sentential Form.
 E→ E + E / E * E / E = E / I’d Sentential Form
E
E*E
E+E *E
I’D + E * E
I’D + I’D * E
I’D + I’D * I’D
Left Most Derivation
 Left Most Derivation: the process of construction of parse tree by
expanding the left most non terminal is known as LMD and the
graphical representation of LMD in known as LMDT.
LMD
E→E+E E
E→E*E E*E
E→E=E E+E *E
E → I’D I’D + E * E
E → I’D I’D + I’D * E
E → I’D I’D + I’D * I’D
Right Most Derivation
 Right Most Derivation: the process of construction of parse tree by
expanding the right most non terminal is known as LMD and the
graphical representation of LMD in known as LMDT.

RMD
E→E+E E
E→E*E E*E
E→E=E E+E *E
E → I’D I’D + E * E
E → I’D I’D + I’D * E
E → I’D I’D + I’D * I’D
Parse Tree
 A parse Tree is a graphical representation of a derivation that filters
out the order in which productions are applied to replace non
terminals.
 Each interior nod of a parse tree represents the application of a
production.
 The interior node is labeled with the non terminal A in the head of
the productions.
 The children of the node are labeled, from left to right, by the
symbol in the body of the production by which this A was replaced
during the derivations.
Parse Tree
 Example: -(I’d + I’d )
E

- E

( + )

I’d I’d
The leaves of a parse tree are labeled by non terminal or terminal, and read from
left to right constitute a sectional form, called the yield or frontier of the tree.
Any Question?

You might also like