Grammars CS 130: Theory of Computation HMU textbook, Chap 5.

Slides:



Advertisements
Similar presentations
CFGs and PDAs Sipser 2 (pages ). Long long ago…
Advertisements

Grammars, constituency and order A grammar describes the legal strings of a language in terms of constituency and order. For example, a grammar for a fragment.
About Grammars CS 130 Theory of Computation HMU Textbook: Sec 7.1, 6.3, 5.4.
CFGs and PDAs Sipser 2 (pages ). Last time…
CS5371 Theory of Computation
CSC 3130: Automata theory and formal languages Andrej Bogdanov The Chinese University of Hong Kong Context-free.
Applied Computer Science II Chapter 2 : Context-free languages Prof. Dr. Luc De Raedt Institut für Informatik Albert-Ludwigs Universität Freiburg Germany.
Context-Free Grammars Lecture 7
January 14, 2015CS21 Lecture 51 CS21 Decidability and Tractability Lecture 5 January 14, 2015.
Courtesy Costas Busch - RPI1 Context-Free Languages.
Chapter 3: Formal Translation Models
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
Lecture 9UofH - COSC Dr. Verma 1 COSC 3340: Introduction to Theory of Computation University of Houston Dr. Verma Lecture 9.
COP4020 Programming Languages
Prof. Busch - LSU1 Context-Free Languages. Prof. Busch - LSU2 Regular Languages Context-Free Languages.
FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
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.
INHERENT LIMITATIONS OF COMPUTER PROGRAMS CSci 4011.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 7 Mälardalen University 2010.
1 Introduction to Parsing Lecture 5. 2 Outline Regular languages revisited Parser overview Context-free grammars (CFG’s) Derivations.
CPSC 388 – Compiler Design and Construction Parsers – Context Free Grammars.
Context-free Grammars Example : S   Shortened notation : S  aSaS   | aSa | bSb S  bSb Which strings can be generated from S ? [Section 6.1]
CSCI 2670 Introduction to Theory of Computing September 21, 2004.
Pushdown Automata CS 130: Theory of Computation HMU textbook, Chap 6.
A sentence (S) is composed of a noun phrase (NP) and a verb phrase (VP). A noun phrase may be composed of a determiner (D/DET) and a noun (N). A noun phrase.
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:
Context-Free Grammars
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
Grammars CPSC 5135.
Languages & Grammars. Grammars  A set of rules which govern the structure of a language Fritz Fritz The dog The dog ate ate left left.
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
Dept. of Computer Science & IT, FUUAST Automata Theory 2 Automata Theory V Context-Free Grammars andLanguages.
Context Free Grammars. Context Free Languages (CFL) The pumping lemma showed there are languages that are not regular –There are many classes “larger”
CMSC 330: Organization of Programming Languages Context-Free Grammars.
Parsing Introduction Syntactic Analysis I. Parsing Introduction 2 The Role of the Parser The Syntactic Analyzer, or Parser, is the heart of the front.
CFG1 CSC 4181Compiler Construction Context-Free Grammars Using grammars in parsers.
Midterm 1 Breakdown >79 3 >29 7 >69 5 >19 5 >59 7 >49 9 >39 7.
Introduction to Parsing
1 A well-parenthesized string is a string with the same number of (‘s as )’s which has the property that every prefix of the string has at least as many.
Context Free Grammars 1. Context Free Languages (CFL) The pumping lemma showed there are languages that are not regular –There are many classes “larger”
CS 3240 – Chapter 5. LanguageMachineGrammar RegularFinite AutomatonRegular Expression, Regular Grammar Context-FreePushdown AutomatonContext-Free Grammar.
Chapter 3 Context-Free Grammars and Parsing. The Parsing Process sequence of tokens syntax tree parser Duties of parser: Determine correct syntax Build.
CS 208: Computing Theory Assoc. Prof. Dr. Brahim Hnich Faculty of Computer Sciences Izmir University of Economics.
1Computer Sciences Department. Book: INTRODUCTION TO THE THEORY OF COMPUTATION, SECOND EDITION, by: MICHAEL SIPSER Reference 3Computer Sciences Department.
Grammars Hopcroft, Motawi, Ullman, Chap 5. Grammars Describes underlying rules (syntax) of programming languages Compilers (parsers) are based on such.
11 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 7 School of Innovation, Design and Engineering Mälardalen University 2012.
Introduction Finite Automata accept all regular languages and only regular languages Even very simple languages are non regular (  = {a,b}): - {a n b.
Chapter 5 Context-free Languages
Context-Free Languages
CSC312 Automata Theory Lecture # 26 Chapter # 12 by Cohen Context Free Grammars.
Chapter 5 Context-Free Languages Regular language are effective in describing certain simple patterns. But it is not enough to describe programming languages.
Overview of Previous Lesson(s) Over View 3 Model of a Compiler Front End.
Chapter 4: Syntax analysis Syntax analysis is done by the parser. –Detects whether the program is written following the grammar rules and reports syntax.
CSCI 4325 / 6339 Theory of Computation Zhixiang Chen Department of Computer Science University of Texas-Pan American.
Grammar Set of variables Set of terminal symbols Start variable Set of Production rules.
1 A well-parenthesized string is a string with the same number of (‘s as )’s which has the property that every prefix of the string has at least as many.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 3.
5. Context-Free Grammars and Languages
CONTEXT-FREE LANGUAGES
Formal Language & Automata Theory
G. Pullaiah College of Engineering and Technology
Context free grammar.
PARSE TREES.
Finite Automata and Formal Languages
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Presentation transcript:

Grammars CS 130: Theory of Computation HMU textbook, Chap 5

Grammars Describes underlying rules (syntax) of programming languages Compilers (parsers) are based on such descriptions More expressive than regular expressions/finite automata Context-free grammar (CFG) or just grammar

Definition A grammar is a 4-tuple G = (V,T,P,S) V: set of variables or nonterminals T: set of terminal symbols (terminals) P: set of productions Each production: head  body, where head is a variable, and body is a string of zero or more terminals and variables S: a start symbol from V

Example 1: Assignment statements V = { S, E }, T = { i, =, +, *, n } Productions: S  i = E E  n E  i E  E + E E  E * E This grammar represents strings such as: i = n + n i = n * i + i + n * n

Example 2: Palindromes V = { S }, T = { a, b } Productions: S   S  a S  b S  aSa S  bSb This grammar represents strings such as: a baab babab abaaabaaaba 

Example 3: 0 n 1 n V = { S }, T = { 0, 1 } Productions: S   S  0S1 This grammar represents strings such as: 

Derivation of strings Applying productions: replace variable (head) with corresponding string (body) Example: to derive i = n + n in for the assignment statement grammar: S  i = E  i = E + E  i = E + n  i = n + n Above derivation was carried out through four applications of the productions: S  i = E, E  E + E, E  n, E  n

L(G): Language of a grammar Definition: Given a grammar G, and a string w over the alphabet T, S  * G w if there is a sequence of productions that derive w L(G) = { w in T* | S  * G w }, the language of the grammar G

Leftmost vs rightmost derivations Leftmost derivation: the leftmost variable is always the one replaced when applying a production Example: S  i = E  i = E + E  i = n + E  i = n + n Rightmost derivation: rightmost variable is replaced Example: S  i = E  i = E + E  i = E + n  i = n + n

Sentential forms In a derivation, assuming it begins with S, all intermediate strings are called sentential forms of the grammar G Example: i = E and i = E + n are sentential forms of the assignment statement grammar The sentential forms are called leftmost (rightmost) sentential forms if they are a result of leftmost (rightmost) derivations

Parse trees Recall that a tree in graph theory is a set of nodes such that There is a special node called the root Nodes can have zero or more child nodes Nodes without children are called leaves Interior nodes: nodes that are not leaves A parse tree for a grammar G is a tree such that the interior nodes are non-terminals in G and children of a non-terminal correspond to the body of a production in G

Yield of a parse tree Yield: concatenation of leaves from left to right If the root of the tree is the start symbol, and all leaves are terminal symbols, then the yield is a string in L(G) Note: a derivation always corresponds to some parse tree

Context-free languages A language generated by a grammar is called a context-free language The set of regular languages is a subset of the set of context-free languages Proof? Some languages are context-free, but not regular e.g., palindromes (proven not regular through the pumping lemma)

What’s next Applications of CFGs, particularly in parsing Expressive capability of CFGs Corresponding machine-based model equivalent to CFGs called PDAs (Pushdown Automata)