O slideshow foi denunciado.
Seu SlideShare está sendo baixado. ×

Context Free Grammar

Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Próximos SlideShares
Lecture: Context-Free Grammars
Lecture: Context-Free Grammars
Carregando em…3
×

Confira estes a seguir

1 de 49 Anúncio

Mais Conteúdo rRelacionado

Diapositivos para si (19)

Semelhante a Context Free Grammar (20)

Anúncio

Mais de Akhil Kaushik (20)

Mais recentes (20)

Anúncio

Context Free Grammar

  1. 1. Akhil Kaushik Asstt. Prof., CE Deptt., TIT Bhiwani Context Free Grammar
  2. 2. Introduction • Syntax:- the form or structure of the expressions, statements, and program units. • Semantics:- the meaning of the expressions, statements, and program units. • Syntax and semantics provide a language’s definition.
  3. 3. Introduction • A language is a set of sentences. • A sentence is a string of characters over some alphabets. Ex: a = b + c; or z = (a + b) * c; • Syntax: <assign> →<id> = <expr> ; • <id> →a | b | c • <expr> →<id> + <expr> | <id> * <expr> | <id> • Semantics : a = b + c;
  4. 4. Introduction • Recognizers – A recognition device reads input strings of the language and decides whether the input strings belong to the language. • Ex: Syntax analysis part of a compiler. • Generators – A device that generates sentences of a language. • One can determine if the syntax of a particular sentence is correct by comparing it to the structure of the generator.
  5. 5. Introduction
  6. 6. The Chomsky Hierarchy • Noam Chomsky, - American linguist, philosopher, scientist and social activist gave Chomsky hierarchy of grammars in 1956. • Grammar Definition: It is defined by four tuples: G = {V,T,P,S} where – V = Non-terminals – T = Terminals – P = Production Rule – S = Start Symbol
  7. 7. The Chomsky Hierarchy
  8. 8. The Chomsky Hierarchy • Unrestricted grammar generates Recursively enumerable languages (Type-0 Languages) include all formal grammars. • They generate exactly all languages that can be recognized by a Turing machine. • This grammar has rules of the form α → β (where α contains non-terminals and β contains terminals or non terminals).
  9. 9. The Chomsky Hierarchy Examples:- • AB → A • AB → aB • S → ε • a → AB • ε → a
  10. 10. The Chomsky Hierarchy • Type-1 grammar generate the Context-sensitive languages. • These languages can be recognized by a linear bounded automaton. • These grammars have rules of the form α → β with a restriction that length of | α | ≤ | β | .
  11. 11. The Chomsky Hierarchy • Examples:- • aAb → bbb • aA → bbb • aAb → bb
  12. 12. The Chomsky Hierarchy • Type-2 grammar generate the Context-Free languages. • These languages are exactly all languages that can be recognized by a non-deterministic pushdown automaton. • Context-free languages are the theoretical basis for the syntax of most programming languages.
  13. 13. The Chomsky Hierarchy • These are defined by rules of the form A → α where A is a nonterminal and α is a string of terminals and non-terminal (there will be no context on the left and right of non-terminal ). • Examples: A → BCD • A →aBC • a →AbC
  14. 14. The Chomsky Hierarchy • Type-3 Languages generate the Regular languages. • These languages are exactly all languages that can be decided by a finite state automaton. • Regular languages are commonly used to define search patterns of programming languages.
  15. 15. The Chomsky Hierarchy • It can be classified into two types:- (1)Right Linear (2)Left Linear. • If we have repetition of non terminals on right side [ A → xB|x] then it is known as Right Linear. • If we have repetition of non terminals on left side [ A → Bx|x] then it is known as Left Linear. (A,B є non terminals and x є Σ*)
  16. 16. The Chomsky Hierarchy Examples:- • S → aS|b or S → aS|c • S → Sa|b or A → ba • S → aS|b or S → Sa|c
  17. 17. The Chomsky Hierarchy
  18. 18. Context- Free Grammars (CFG)
  19. 19. Introduction • A context-free grammar is a notation for describing languages. • It is more powerful than finite automata or RE’s, but still cannot define all possible languages. • Useful for nested structures. Ex. parentheses in programming languages.
  20. 20. Introduction • Basic idea is to use “variables” to stand for sets of strings (i.e., languages). • These variables are defined recursively, in terms of one another. • Recursive rules (“productions”) involve only concatenation. • Alternative rules for a variable allow union.
  21. 21. Introduction • CFGs are also known as:- – Phrase Structure Grammars – Backus-Naur Form (BNF) • It consists of:- – Rules (equations or productions) – Terminals (tokens) – Non-terminals (Language constituents like noun phrase, verb phrase, sentence, etc.)
  22. 22. Formal Definition • G (CFG) is defined by G = (V, Σ, P, S) – V = variables a finite set (non-terminals) – Σ = alphabet or terminals a finite set – P = productions a finite set – S = start variable S ϵ V • Productions’ form, where A ϵ V, α ϵ (V υ S)*: • A → a
  23. 23. Example • Here is a formal CFG for { 0n1n | n >= 1}. • Terminals = {0, 1}, Variables = {S}, Start symbol = S. • Productions = S -> 01, S -> 0S1 • We derive strings in the language of a CFG by starting with the start symbol, and repeatedly replacing some variable A by the right side of one of its productions.
  24. 24. RE to CFG
  25. 25. Derivation • A derivation is a sequence of rules applied to a string that accounts for that string. – Covers all the elements in the string. – Covers only the elements in the string. • Derivations allow us to replace any of the variables in a string. • Leads to many different derivations of the same string.
  26. 26. Derivation Tree A parse tree of a derivation is a tree in which:- • Each internal node is labeled with a non-terminal. • If a rule A →A1A2…An occurs in the derivation then is a parent node of nodes labeled A1, A2, …, An
  27. 27. Derivation Tree
  28. 28. Leftmost vs Rightmost Derivation A left-most derivation of a sentential form is one in which rules transforming the left- most non-terminal are always applied. A right-most derivation of a sentential form is one in which rules transforming the right-most non-terminal are always applied.
  29. 29. Leftmost vs Rightmost Derivation
  30. 30. Ambiguous Grammar • A grammar G is ambiguous if there is a word w ϵ L(G) having are least two different parse trees.
  31. 31. Ambiguous Grammar
  32. 32. Ambiguous Grammar • Given an ambiguous grammar, would like an equivalent unambiguous grammar. – Allows you to know more about structure of a given derivation. – Can lead to more efficient parsing algorithms. – In programming languages, want to impose a canonical structure on derivations. E.g., for 1+2´3. • Strategy: Force an ordering on all derivations.
  33. 33. Ambiguous Grammar
  34. 34. Ambiguous Grammar
  35. 35. Ambiguous Grammar
  36. 36. Ambiguous Grammar
  37. 37. Ambiguous Grammar
  38. 38. Ambiguous Grammar
  39. 39. Ambiguous Grammar
  40. 40. Ambiguous Grammar
  41. 41. Ambiguous Grammar
  42. 42. Ambiguous Grammar
  43. 43. Ambiguous Grammar
  44. 44. Ambiguous Grammar
  45. 45. Disambiguation
  46. 46. Disambiguation
  47. 47. Disambiguation
  48. 48. Disambiguation By forcing the leftmost variable (or alternatively, the rightmost variable) to be replaced, we avoid these “distinctions without a difference.”
  49. 49. Akhil Kaushik akhilkaushik05@gmail.com 9416910303 CONTACT ME AT: Akhil Kaushik akhilkaushik05@gmail.com 9416910303 THANK YOU !!!

×