Teori Bahasa dan Automata Lecture 9: Contex-Free Grammars

Slides:



Advertisements
Similar presentations
C O N T E X T - F R E E LANGUAGES ( use a grammar to describe a language) 1.
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.
1 Introduction to Computability Theory Lecture12: Decidable Languages Prof. Amos Israeli.
1 Introduction to Computability Theory Lecture5: Context Free Languages Prof. Amos Israeli.
CS5371 Theory of Computation
Applied Computer Science II Chapter 2 : Context-free languages Prof. Dr. Luc De Raedt Institut für Informatik Albert-Ludwigs Universität Freiburg Germany.
Foundations of (Theoretical) Computer Science Chapter 2 Lecture Notes (Section 2.1: Context-Free Grammars) David Martin With some.
1 Foundations of Software Design Lecture 23: Finite Automata and Context-Free Grammars Marti Hearst Fall 2002.
1 Introduction to Parsing Lecture 5. 2 Outline Regular languages revisited Parser overview Context-free grammars (CFG’s) Derivations.
Lecture 16 Oct 18 Context-Free Languages (CFL) - basic definitions Examples.
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.
Grammars CPSC 5135.
PART I: overview material
Parsing Introduction Syntactic Analysis I. Parsing Introduction 2 The Role of the Parser The Syntactic Analyzer, or Parser, is the heart of the front.
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.
CSC312 Automata Theory Lecture # 26 Chapter # 12 by Cohen Context Free Grammars.
Donghyun (David) Kim Department of Mathematics and Physics North Carolina Central University 1 Chapter 2 Context-Free Languages Some slides are in courtesy.
CSE 311 Foundations of Computing I Lecture 20 Context-Free Grammars and Languages Autumn 2012 CSE
Theory of Computation Automata Theory Dr. Ayman Srour.
Chapter 3 – Describing Syntax CSCE 343. Syntax vs. Semantics Syntax: The form or structure of the expressions, statements, and program units. Semantics:
Lecture 17: Theory of Automata:2014 Context Free Grammars.
5. Context-Free Grammars and Languages
Chapter 3 – Describing Syntax
Computability Joke. Context-free grammars Parsing. Chomsky
LESSON 16.
Context-Free Grammars: an overview
CS 404 Introduction to Compiler Design
CS510 Compiler Lecture 4.
Chapter 2 :: Programming Language Syntax
Introduction to Parsing (adapted from CS 164 at Berkeley)
Syntax Specification and Analysis
Automata and Languages What do these have in common?
Syntax Analysis Chapter 4.
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
Simplifications of Context-Free Grammars
Context free grammar.
CSE 105 theory of computation
Formal Language Theory
Context Free Languages
CSE 3302 Programming Languages
Course 2 Introduction to Formal Languages and Automata Theory (part 2)
Context-Free Languages
Compiler Design 4. Language Grammars
Lexical and Syntax Analysis
Programming Language Syntax 2
5. Context-Free Grammars and Languages
Chapter 2: A Simple One Pass Compiler
Lecture 7: Introduction to Parsing (Syntax Analysis)
CHAPTER 2 Context-Free Languages
Context-Free Grammars
Context-Free Languages
CSE 311: Foundations of Computing
فصل دوم Context-Free Languages
CS 3304 Comparative Languages
CS 3304 Comparative Languages
Context-Free Languages
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Chapter 2 :: Programming Language Syntax
Chapter 2 :: Programming Language Syntax
CSE 105 theory of computation
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Discrete Maths 13. Grammars Objectives
Context-Free Grammars
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
Teori Bahasa dan Automata Lecture 6: Regular Expression
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
COMPILER CONSTRUCTION
Faculty of Computer Science and Information System
CSE 105 theory of computation
Presentation transcript:

Teori Bahasa dan Automata Lecture 9: Contex-Free Grammars By: Nur Uddin, Ph.D

Motivation In previous lectures, we introduced two different, though equivalent, methods of describing languages: finite automata and regular expressions. We showed that many languages can be described in this way but that some simple languages, such as 0 𝑛 1 𝑛 | 𝑛≥0 , cannot. Now, we present context-free grammars, a more powerful method of describing languages. Such grammars can describe certain features that have a recursive structure, whichmakes themuseful in a variety of applications.

Introduction Context-free grammars were first used in the study of human languages. One way of understanding the relationship of terms such as noun, verb, and preposition and their respective phrases leads to a natural recursion because noun phrases may appear inside verb phrases and vice versa. Context-free grammars help us organize and understand these relationships. An important application of context-free grammars occurs in the specification and compilation of programming languages. A grammar for a programming language often appears as a reference for people trying to learn the language syntax.

Introduction (Cont’d) Designers of compilers and interpreters for programming languages often start by obtaining a grammar for the language. Most compilers and interpreters contain a component called a parser that extracts the meaning of a program prior to generating the compiled code or performing the interpreted execution. A number of methodologies facilitate the construction of a parser once a context-free grammar is available. Some tools even automatically generate the parser from the grammar.

Context-Free Grammars A grammar consists of a collection of substitution rules, also called productions. Each rule appears as a line in the grammar, comprising a symbol and a string separated by an arrow. The symbol is called a variable. The string consists of variables and other symbols called terminals. The variable symbols often are represented by capital letters. The terminals are analogous to the input alphabet and often are represented by lowercase letters, numbers, or special symbols. One variable is designated as the start variable. It usually occurs on the left-hand side of the topmost rule.

Process Write down the start variable. It is the variable on the left-hand side of the top rule, unless specified otherwise. Find a variable that is written down and a rule that starts with that variable. Replace the written down variable with the right- hand side of that rule. Repeat step 2 until no variables remain.

Example 1 Grammar 𝐺 1 contains three rules. 𝐺 1 ‘s variables are A and B, where A is the start variable. Its terminals are 0, 1, and #.

Parse tree Grammar:

Language of the grammar

Formal Definition of Context-Free Grammar

Formal Definition of Context-Free Grammar (cont’d)

Example 2

Design Context-Free Grammars Context-free grammars are even trickier to construct than finite automata because we are more accustomed to programming a machine for specific tasks than we are to describing languages with grammars. The following techniques are helpful to construct a CFG: Many CFLs are the union of simpler CFLs. Constructing a CFG for a language that happens to be regular is easy if you can first construct a DFA for that language. Certain context-free languages contain strings with two substrings that are “linked” in the sense that a machine for such a language would need to remember an unbounded amount of information about one of the substrings to verify that it corresponds properly to the other substring. In more complex languages, the strings may contain certain structures that appear recursively as part of other (or the same) structures

Example 3

Ambiguity Sometimes a grammar can generate the same string in several different ways. Such a string will have several different parse trees and thus several different meanings. This result may be undesirable for certain applications, such as programming languages, where a program should have a unique interpretation. If a grammar generates the same string in several different ways, we say that the string is derived ambiguously in that grammar. If a grammar generates some string ambiguously, we say that the grammar is ambiguous.

Example of Ambiguously

Chomsky Normal Form When working with context-free grammars, it is often convenient to have them in simplified form. One of the simplest and most useful forms is called the Chomsky normal form.

Example 4

Example 4 (cont’d)

Example 4 (cont’d)