Derivations and Languages

Slides:



Advertisements
Similar presentations
Lecture # 8 Chapter # 4: Syntax Analysis. Practice Context Free Grammars a) CFG generating alternating sequence of 0’s and 1’s b) CFG in which no consecutive.
Advertisements

1 Parsing The scanner recognizes words The parser recognizes syntactic units Parser operations: Check and verify syntax based on specified syntax rules.
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
Context-Free Grammars Chapter 3. 2 Context-Free Grammars and Languages n Defn A context-free grammar is a quadruple (V, , P, S), where  V is.
Syntactic Pattern Recognition Statistical PR:Find a feature vector x Train a system using a set of labeled patterns Classify unknown patterns Ignores relational.
Lecture 13 Parsing and Ambiguity. Given a string x and a CFG G = (V, Σ, R, S), determine whether x L(G) and if x L(G), find a derivation S * x. This problem.
EECS 6083 Intro to Parsing Context Free Grammars
Problem of the DAY Create a regular context-free grammar that generates L= {w  {a,b}* : the number of a’s in w is not divisible by 3} Hint: start by designing.
Chapter 4 Context-Free Languages Copyright © 2011 The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 1.
Context-free Grammars Example : S   Shortened notation : S  aSaS   | aSa | bSb S  bSb Which strings can be generated from S ? [Section 6.1]
Normal Forms for Context-Free Grammars Definition: A symbol X in V  T is useless in a CFG G=(V, T, P, S) if there does not exist a derivation of the form.
Design contex-free grammars that generate: L 1 = { u v : u ∈ {a,b}*, v ∈ {a, c}*, and |u| ≤ |v| ≤ 3 |u| }. L 2 = { a p b q c p a r b 2r : p, q, r ≥ 0 }
1 Context-Free Languages Not all languages are regular. L 1 = {a n b n | n  0} is not regular. L 2 = {(), (()), ((())),...} is not regular.  some properties.
Classification of grammars Definition: A grammar G is said to be 1)Right-linear if each production in P is of the form A  xB or A  x where A and B are.
Context Free Grammars CIS 361. Introduction Finite Automata accept all regular languages and only regular languages Many simple languages are non regular:
Lecture # 19. Example Consider the following CFG ∑ = {a, b} Consider the following CFG ∑ = {a, b} 1. S  aSa | bSb | a | b | Λ The above CFG generates.
Chapter 5 Context-Free Grammars
Languages & Grammars. Grammars  A set of rules which govern the structure of a language Fritz Fritz The dog The dog ate ate left left.
Lecture # 9 Chap 4: Ambiguous Grammar. 2 Chomsky Hierarchy: Language Classification A grammar G is said to be – Regular if it is right linear where each.
Dept. of Computer Science & IT, FUUAST Automata Theory 2 Automata Theory V Context-Free Grammars andLanguages.
CS 3240: Languages and Computation Context-Free Languages.
Context Free Grammars. Context Free Languages (CFL) The pumping lemma showed there are languages that are not regular –There are many classes “larger”
Grammars Hopcroft, Motawi, Ullman, Chap 5. Grammars Describes underlying rules (syntax) of programming languages Compilers (parsers) are based on such.
Grammars CS 130: Theory of Computation HMU textbook, Chap 5.
1 Chapter 6 Simplification of CFGs and Normal Forms.
Introduction Finite Automata accept all regular languages and only regular languages Even very simple languages are non regular (  = {a,b}): - {a n b.
Overview of Previous Lesson(s) Over View 3 Model of a Compiler Front End.
Transparency No. 1 Formal Language and Automata Theory Homework 5.
Compiler Construction Lecture Five: Parsing - Part Two CSC 2103: Compiler Construction Lecture Five: Parsing - Part Two Joyce Nakatumba-Nabende 1.
AUTOMATA THEORY. Chapter 05 CONTEX-FREE GRAMMERS AND LANGUAGES.
Syntax Analysis By Noor Dhia Syntax analysis:- Syntax analysis or parsing is the most important phase of a compiler. The syntax analyzer considers.
Recap lecture 31 Context Free Grammar, Terminals, non- terminals, productions, CFG, context Free language, examples.
1 Context-Free Languages & Grammars (CFLs & CFGs) Reading: Chapter 5.
WELCOME TO A JOURNEY TO CS419 Dr. Hussien Sharaf Dr. Mohammad Nassef Department of Computer Science, Faculty of Computers and Information, Cairo University.
Context-Free Languages & Grammars (CFLs & CFGs) (part 2)
5. Context-Free Grammars and Languages
CONTEXT-FREE LANGUAGES
lec02-parserCFG May 8, 2018 Syntax Analyzer
BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS
Context-Free Languages & Grammars (CFLs & CFGs)
Formal Language & Automata Theory
CS 404 Introduction to Compiler Design
Programming Languages Translator
CS510 Compiler Lecture 4.
Fall Compiler Principles Context-free Grammars Refresher
Chapter 3 Context-Free Grammar and Parsing
Recap lecture 33 Example of trees, Polish Notation, examples, Ambiguous CFG, example,
Syntax Specification and Analysis
Table-driven parsing Parsing performed by a finite state machine.
Compiler Construction
Theory Of Automata By Dr. MM Alam
Compiler Construction
PARSE TREES.
Pushdown Automata Reading: Chapter 6.
Parsing Techniques.
Context-Free Languages
Context-Free Grammars and Languages
Relationship to Left- and Rightmost Derivations
5. Context-Free Grammars and Languages
Lecture 7: Introduction to Parsing (Syntax Analysis)
Finite Automata and Formal Languages
Cpt S 317: Spring 2009 Sampath Kumar S, AP/CSE, SECE
Cpt S 317: Spring 2009 Sampath Kumar S, AP/CSE, SECE
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Deterministic Finite Automaton (DFA)
Relationship to Left- and Rightmost Derivations
Theory of Computation Lecture #
CFGs: Formal Definition
Fall Compiler Principles Context-free Grammars Refresher
lec02-parserCFG May 27, 2019 Syntax Analyzer
Presentation transcript:

Derivations and Languages Cpt S 317: Spring 2009 Derivations and Languages Sampath Kumar S, AP/CSE, SECE School of EECS, WSU

Derivations and Languages 2/22/2019 Derivations and Languages While inferring whether the given input strings belongs to the given CFG, we can have 2 approaches: Using the rules from body to head (Recursive Inference) Using the rules from head to body (Derivation) Derivation using Grammar Recursive Inference Derivation Left Most Derivation Right Most Derivation Sampath Kumar S, AP/CSE, SECE

2/22/2019 Recursive Inference: Here we take string from each variables, concatenate them in proper order and infer that the resulting string is in the language of the variable in the head Sampath Kumar S, AP/CSE, SECE

2/22/2019 Derivation: Here we use the production from head to body i.e., from start symbol expanding till reaches the given string. This use of grammar is called derivation. Sampath Kumar S, AP/CSE, SECE

2/22/2019 Problems to discuss: For the language L = {wcwR | w (0+1)*} check whether the string 01c10 belongs to the language L or not. Solution: Step 1: For the language L, the productions are S → 0S0 | 1S1 | C Step 2: Derivation S → 0S0 [∵ S → 0S0] → 01E10 [∵ S → 1S1] → 01c10 [∵ S → c] Sampath Kumar S, AP/CSE, SECE

Understanding the Language defined by CFG: 2/22/2019 Understanding the Language defined by CFG: The only way to recognize the language, is to try out various strings from the given production rules. Then by observing the derived strings, one can find out the language generated from the given CFG. Sampath Kumar S, AP/CSE, SECE

2/22/2019 Problems to discuss: Derive ‘a4’ from the grammar G=({S}, {a},P,S) where P: S → aS|ε. Find L(G) and derive the string ‘abbab’ for the grammar G=({S}, {a},P,S) where P: S → aS|bS|a|b. Find L(G) and derive the string ‘abbaaba’ for the grammar G=({S,X}, {a},P,S) where P: S → XaaX, X → aX|bX|ε. Find L(G) for the grammar G=({S,C}, {a,b},P,S) where P is given by S → aCa, C → aCa|b. Sampath Kumar S, AP/CSE, SECE

Leftmost and Rightmost Derivation: 2/22/2019 Leftmost and Rightmost Derivation: If a word w is generated by a CFG by a certain derivation and at each step in the derivation, a rule of production is applied to the leftmost non terminal in the working string, then this derivation is called leftmost derivation (LMD). If a word w is generated by a CFG by a certain derivation and at each step in the derivation, a rule of production is applied to the rightmost non terminal in the working string, then this derivation is called rightmost derivation (RMD). Sampath Kumar S, AP/CSE, SECE

2/22/2019 Problems to discuss: Consider the CFG ({S,X}, {a,b}, P, S} where productions are S → baXaS|ab, X →Xab|aa. Find the LMD and RMD for the string w=baaaababaab. Consider the CFG S → aB|bA A → a | aS | bAA B → b | bS | aBB Find the LMD and RMD for the string W=aabbabba. Sampath Kumar S, AP/CSE, SECE

2/22/2019 Derivation Tree: A derivation tree or parse tree is an ordered rooted tree that graphically represents the semantic information a string derived from a Context - Free Grammar. Sampath Kumar S, AP/CSE, SECE

Representation Technique 2/22/2019 Representation Technique Root vertex − Must be labeled by the start symbol. Vertex − Labeled by a non-terminal symbol. Leaves − Labeled by a terminal symbol or ε. If S → x1x2 …… xn is a production rule in a CFG, then the parse tree / derivation tree will be as follows − Sampath Kumar S, AP/CSE, SECE

Approaches for constructing Derivation Tree: 2/22/2019 Approaches for constructing Derivation Tree: There are two different approaches to draw a derivation tree − Top-down Approach − Starts with the starting symbol S Goes down to tree leaves using productions Bottom-up Approach − Starts from tree leaves Proceeds upward to the root which is the starting symbol S Sampath Kumar S, AP/CSE, SECE

Derivation or Yield of a Tree: 2/22/2019 Derivation or Yield of a Tree: The derivation or the yield of a parse tree is the final string obtained by concatenating the labels of the leaves of the tree from left to right, ignoring the Nulls. However, if all the leaves are Null, derivation is Null. Sampath Kumar S, AP/CSE, SECE

Sentential Form and Partial Derivation Tree: 2/22/2019 Sentential Form and Partial Derivation Tree: A partial derivation tree is a sub-tree of a derivation tree/parse tree such that either all of its children are in the sub-tree or none of them are in the sub-tree. Sampath Kumar S, AP/CSE, SECE

Leftmost and Rightmost Derivation of a String 2/22/2019 Leftmost and Rightmost Derivation of a String Leftmost derivation − A leftmost derivation is obtained by applying production to the leftmost variable in each step. Rightmost derivation − A rightmost derivation is obtained by applying production to the rightmost variable in each step. Sampath Kumar S, AP/CSE, SECE

Left and Right Recursive Grammars 2/22/2019 Left and Right Recursive Grammars In a context-free grammar G, if there is a production in the form X → Xa, where X is a non-terminal and ‘a’ is a string of terminals, it is called a left recursive production. The grammar having a left recursive production is called a left recursive grammar. If in a context-free grammar G, if there is a production is in the form X → aX where X is a non-terminal and ‘a’ is a string of terminals, it is called a right recursive production. The grammar having a right recursive production is called a right recursive grammar. Sampath Kumar S, AP/CSE, SECE

Problems to discuss: 68. Let a CFG G= { {S}, {a, b}, P, S} Where 2/22/2019 Problems to discuss: 68. Let a CFG G= { {S}, {a, b}, P, S} Where P = S → SS | aSb | ε. Draw derivation tree for the string “abaabb” One derivation form of the above CFG is S → SS → aSbS → abS → abaSb → abaaSbb → abaabb Sampath Kumar S, AP/CSE, SECE

2/22/2019 Problems to discuss: 69. Let any set of production rules in a CFG be X → X+X | X*X |X| a over an alphabet {a}. Find LMD and RMD for string "a+a*a“. 70. Construct Parse tree for the string a*(a+b00) for the following CFG using left and right most derivation. E → A | E + E | E * E | (E) A → a | b |Aa | Ab | A0 | A1 71. Construct Parse tree for the string a*(a+a) for the following CFG using left and right most derivation. E → E + E | E * E | (E) | a Sampath Kumar S, AP/CSE, SECE

2/22/2019 Problems to discuss: 72. Generate parse tree for the string 00101 using left and right most derivation for following CFG S → A1B, A → 0A|ε | B → 0B | 1B | ε 73. Generate parse tree for the string babbab using left and right most derivation for following CFG S → aSa | bSb | a |b | ε Sampath Kumar S, AP/CSE, SECE

2/22/2019 Sampath Kumar S, AP/CSE, SECE

2/22/2019 நன்றி  Sampath Kumar S, AP/CSE, SECE