G. Pullaiah College of Engineering and Technology

Slides:



Advertisements
Similar presentations
Simplifications of Context-Free Grammars
Advertisements

CPSC Compiler Tutorial 4 Midterm Review. Deterministic Finite Automata (DFA) Q: finite set of states Σ: finite set of “letters” (input alphabet)
C O N T E X T - F R E E LANGUAGES ( use a grammar to describe a language) 1.
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
Applied Computer Science II Chapter 2 : Context-free languages Prof. Dr. Luc De Raedt Institut für Informatik Albert-Ludwigs Universität Freiburg Germany.
Courtesy Costas Busch - RPI1 Pushdown Automata PDAs.
Courtesy Costas Buch - RPI1 Simplifications of Context-Free Grammars.
Costas Busch - RPI1 NPDAs Accept Context-Free Languages.
Courtesy Costas Busch - RPI1 NPDAs Accept Context-Free Languages.
Costas Busch - RPI1 Context-Free Languages. Costas Busch - RPI2 Regular Languages.
Courtesy Costas Busch - RPI1 Context-Free Languages.
1 Simplifications of Context-Free Grammars. 2 A Substitution Rule Substitute Equivalent grammar.
1 Compilers. 2 Compiler Program v = 5; if (v>5) x = 12 + v; while (x !=3) { x = x - 3; v = 10; } Add v,v,0 cmp v,5 jmplt ELSE THEN: add x, 12,v.
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
Fall 2004COMP 3351 Context-Free Languages. Fall 2004COMP 3352 Regular Languages.
Prof. Busch - LSU1 Context-Free Languages. Prof. Busch - LSU2 Regular Languages Context-Free Languages.
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
Fall 2005Costas Busch - RPI1 Context-Free Languages.
Context-Free Languages Hinrich Schütze CIS, LMU, Slides based on RPI CSCI 2400 Thanks to Costas Busch.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 7 Mälardalen University 2010.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 5 Mälardalen University 2005.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 8 Mälardalen University 2010.
Pushdown Automata (PDAs)
1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 5 Mälardalen University 2010.
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
1 Lex. 2 Lex is a lexical analyzer Var = ; if (test > 20) temp = 0; else while (a < 20) temp++; Lex Ident: Var Integer: 12 Oper: + Integer: 9 Semicolumn:
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 11 Midterm Exam 2 -Context-Free Languages Mälardalen University 2005.
1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University 2010.
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 CS 130: Theory of Computation HMU textbook, Chap 5.
Foundations of (Theoretical) Computer Science Chapter 2 Lecture Notes (Section 2.2: Pushdown Automata) Prof. Karen Daniels, Fall 2010 with acknowledgement.
11 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 7 School of Innovation, Design and Engineering Mälardalen University 2012.
Context-Free Languages. Regular Languages Context-Free Languages.
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
Formal Languages, Automata and Models of Computation
Costas Busch - LSU1 Parsing. Costas Busch - LSU2 Compiler Program File v = 5; if (v>5) x = 12 + v; while (x !=3) { x = x - 3; v = 10; } Add v,v,5.
Donghyun (David) Kim Department of Mathematics and Physics North Carolina Central University 1 Chapter 2 Context-Free Languages Some slides are in courtesy.
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 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 8 Mälardalen University 2011.
Theory of Languages and Automata By: Mojtaba Khezrian.
5. Context-Free Grammars and Languages
Formal Languages, Automata and Models of Computation
Closed book, closed notes
Context-Free Languages
Formal Language & Automata Theory
CS510 Compiler Lecture 4.
Chapter 3 – Describing Syntax
PDAs Accept Context-Free Languages
Table-driven parsing Parsing performed by a finite state machine.
NPDAs Accept Context-Free Languages
Simplifications of Context-Free Grammars
CSE 105 theory of computation
Pushdown Automata PDAs
Pushdown Automata PDAs
PDAs Accept Context-Free Languages
NPDAs Accept Context-Free Languages
Context-free Languages
CHAPTER 2 Context-Free Languages
Context-Free Languages
فصل دوم Context-Free Languages
Parsing Costas Busch - LSU.
Context-Free Languages
Chapter 2 Context-Free Language - 01
… NPDAs continued.
CSE 105 theory of computation
Normal Forms for Context-free Grammars
CSE 105 theory of computation
Presentation transcript:

G. Pullaiah College of Engineering and Technology Formal Languages & Automata Theory Department of Computer Science & Engineering

Content Midterm results Regular vs Content Midterm results Regular vs. Non-regular Languages Context-Free Languages Context-Free Grammars Derivation Trees. Ambiguity Applications Push-Down Automata, PDA

Midterm 1 Solution http://www.idt.mdh.se/kurser/cd5560/12_11/examination/Duggor/MIDTERM1-20121127-Solution.pdf

A comment on the MIDTERM 1 The Pumping Lemma for Regular Languages Pumping Lemma cannot be used to prove that a language is regular! An example: If something is a square it always has four edges (a property of square) But: having proved that something has four edges does not necessarily mean that the object is a square. http://www2.mat.ua.pt/rosalia/cadeiras/TC/pump.pdf

Time to take the next step: beyond Regular Languages Non-regular languages Context-Free Languages Regular Languages

Automata theory: formal languages and formal grammars

Context-Free Languages Based on C Busch, RPI, Models of Computation

Context-Free Languages Grammars Pushdown Automata

Context-Free Grammars

Formal Definition Grammar Set of variables Set of terminal symbols Start variable Set of production rules

Repetition: Regular Grammars Variables Terminal symbols Start variables Right or Left Linear Grammars. Productions of the form: or is string of terminals

Definition: Context-Free Grammars Variables Terminal symbols Start variables Productions of the form: is string of variables and terminals

Regular vs. Context-free Grammar A regular grammar is either right or left linear, whereas context free* grammar is any combination of terminals and non-terminals. Hence regular grammars are a subset of context-free grammars. Grammar generating palindromes is not regular: *The name context-free grammar is explained by the property of productions that are independent of the surrounding symbols. There are also context-sensitive grammars where productions depend on the context (symbols that surround variables).

Example 1: A context-free grammar A derivation

A context-free grammar Another derivation

Example 2: A context-free grammar A derivation

A context-free grammar Another derivation

Example 3: A context-free grammar A derivation

A context-free grammar A derivation

Example 4:

Definition: Context-Free Grammars Variables Terminal symbols Start variables Productions of the form: is string of variables and terminals

Definition: Context-Free Languages A language is context-free if and only if there is a grammar with

Derivation Order Leftmost derivation

Derivation Order Rightmost derivation

Leftmost derivation

Rightmost derivation

Derivation Trees

Derivation can be represented in a tree form

Derivation Tree

Derivation Tree yield

Partial Derivation Trees

Partial derivation tree

sentential form Partial derivation tree yield

Sometimes, derivation order doesn’t matter Leftmost: Rightmost: The same derivation tree

Ambiguity

derivation (* denotes multiplication) leftmost derivation

derivation leftmost derivation

Two derivation trees

The grammar is ambiguous! String has two derivation trees

The grammar is ambiguous as the string has two leftmost derivations:

Definition A context-free grammar is ambiguous if some string has two or more derivation trees (two or more leftmost/rightmost derivations).

Why do we care about ambiguity?

Why do we care about ambiguity?

Why do we care about ambiguity?

Correct result:

for programming languages We want to remove ambiguity! Ambiguity is bad for programming languages We want to remove ambiguity!

We fix the ambiguous grammar… …by introducing parentheses () to indicate grouping, (precedence) Non-ambiguous grammar

Unique derivation tree

The grammar : is non-ambiguous. Every string has a unique derivation tree.

Inherent Ambiguity Example: Some context free languages have only ambiguous grammars! Example:

The string has two derivation trees

Non-regular languages Context-Free Languages Regular Languages

Applications: Compilers

Machine Code Program Add v,v,0 cmp v,5 jmplt ELSE THEN: add x, 12,v ELSE: WHILE: cmp x,3 ... v = 5; if (v>5) x = 12 + v; while (x !=3) { x = x - 3; v = 10; } ...... Compiler

Compiler Lexical analyzer parser input output machine code program

A parser “knows” the grammar of the programming language

Parser PROGRAM STMT_LIST STMT_LIST STMT; STMT_LIST | STMT; STMT EXPR | IF_STMT | WHILE_STMT | { STMT_LIST } EXPR EXPR + EXPR | EXPR - EXPR | ID IF_STMT if (EXPR) then STMT | if (EXPR) then STMT else STMT WHILE_STMT while (EXPR) do STMT

The parser finds the derivation of a particular input derivation Parser input E  E + E  E + E * E  10 + E*E  10 + 2 * E  10 + 2 * 5 E  E + E | E * E | INT 10 + 2 * 5

derivation derivation tree 10 E 2 5 + * E  E + E  E + E * E  10 + E*E  10 + 2 * E  10 + 2 * 5

derivation tree 10 E 2 5 + * machine code mult a, 2, 5 add b, 10, a

Parsing examples

Parser input string derivation grammar

Example: Parser derivation input ?

Exhaustive Search Phase 1: Find derivation of All possible derivations of length 1

Phase 2 Phase 1

Phase 2 Phase 3

Final result of exhaustive search (top-down parsing) Parser input derivation

Another use of context free grammars: Context Free Art http://www

Context Free Art

Context-Free Languages Grammars Pushdown Automata stack automaton

Pushdown Automata PDAs

Pushdown Automaton - PDA Input String Stack States

A PDA can write symbols on a stack and read them later on. The Stack A PDA can write symbols on a stack and read them later on. POP reading symbol PUSH writing symbol All access to the stack only on the top! (Stack top is written leftmost in the string, e.g. yxz) A stack is valuable as it can hold an unlimited amount of information. The stack allows pushdown automata to recognize some non-regular languages.

The States Pop old reading Push new Input stack symbol writing symbol

input stack top Replace (An alternative is to start and finish with empty stack)

input Push top stack

input Pop top stack

input No Change top stack

Formal Definition Pushdown Automaton is defined as 7-tuple Final States Input alphabet Stack Transition function Final states start symbol start state

Example 3.7 Salling: Input Time 0 A PDA for simple nested parenthesis strings Time 0 Input Stack

Example 3.7 Time 1 Input Stack

Example 3.7 Time 2 Input Stack

Example 3.7 Time 3 Input Stack

Example 3.7 Time 4 Input Stack

Example 3.7 Time 5 Input Stack

Example 3.7 Time 6 Input Stack

Example 3.7 Time 7 Input Stack