# Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018

Size: px
Start display at page:

Transcription

1 Finite Automata Theory and Formal Languages TMV027/DIT321 LP Lecture 11 Ana Bove April 26th 2018 Recap: Regular Languages Decision properties of RL: Is it empty? Does it contain this word? Contains ɛ, contains at most ɛ, is infinite? Equivalence of languages using table-filling algorithm; Minimisation of DFA using table-filling algorithm. April 26th 2018, Lecture 11 TMV027/DIT321 1/30

2 Overview of Today s Lecture Context-free grammars; Derivations; Parse trees; Proofs in grammars. Contributes to the following learning outcome: Explain and manipulate the diff. concepts in automata theory and formal lang; Prove properties of languages, grammars and automata with rigorously formal mathematical methods; Design automata, regular expressions and context-free grammars accepting or generating a certain language; Describe the language accepted by an automata or generated by a regular expression or a context-free grammar; Determine if a certain word belongs to a language; Differentiate and manipulate formal descriptions of lang, automata and grammars. April 26th 2018, Lecture 11 TMV027/DIT321 2/30 Context-free Grammars We have seen that not all languages are regular. Context-free grammars (CFG) define the so called context-free languages. They have been developed in the mid-1950s by Noam Chomsky. We will see (next lecture) that regular languages context-free languages CFG play an important role in the description and design of programming languages and compilers, for example, for the implementation of parsers. April 26th 2018, Lecture 11 TMV027/DIT321 3/30

3 Example: Palindromes Let us consider the language P of palindromes over Σ = {0, 1}. That is, P = {w Σ w = rev(w)}. Example of palindromes over Σ are ɛ, 0110, , 010. We can use the Pumping Lemma for RL to show that P is not regular. How can P be defined? Consider the inductive definition of a (very similar) language L: ɛ, 0 and 1 are in L; if w L then 0w0 and 1w1 are also in L. April 26th 2018, Lecture 11 TMV027/DIT321 4/30 Example: CFG for Palindromes P ɛ P 0 P 1 P 0P0 P 1P1 The variable P represents the set of strings that are palindromes. The rules explain how to construct the strings in the language. April 26th 2018, Lecture 11 TMV027/DIT321 5/30

4 Example: CFG for Simple Expressions We can define several sets in a grammar. Example: Here we define 2 sets: those representing simple numerical expressions (denoted by E) and those representing Boolean expressions (denoted by B). Note that they depend on each other! E 0 E 1 E E + E E if B then E else E B True B False B E < E B E == E April 26th 2018, Lecture 11 TMV027/DIT321 6/30 Compact Notation We can have a more compact notation for the productions defining elements in a certain set. Example: Palindromes can be defined as P ɛ 0 1 0P0 1P1 Example: The expressions can be defined as E 0 1 E + E if B then E else E B True False E < E E == E April 26th 2018, Lecture 11 TMV027/DIT321 7/30

5 Context-Free Grammars Definition: A context-free grammar is a 4-tuple G = (V, T, R, S) where: V is a finite set of variables or non-terminals: each variable represents a language or set of strings; T is a finite set of terminals: think of T as the alphabet of the language we are defining; R is a finite set of rules or productions which recursively define the language. Each production consists of: A variable being defined in the production; The symbol ; A string of 0 or more terminals and variables called the body of the production; S is the start variable and represents the language we are defining; other variables define the auxiliary classes needed to define our language. April 26th 2018, Lecture 11 TMV027/DIT321 8/30 Example: C++ Compound Statements A context free grammar for statements: with R as follows: ({S, LC, C, E, Id, LLoD, L, D}, {a,..., A,..., 0,..., (, ),...}, R, S) S {LC} LC ɛ C LC C S if (E) C if (E) C else C while (E) C do C while (E) for (C E; E) C case E : C switch (E) C return E; goto Id; break; continue; E... Id L LLoD LLoD L LLoD D LLoD ɛ L A B... Z a b... z D April 26th 2018, Lecture 11 TMV027/DIT321 9/30

6 Notation for Context-Free Grammars We use the following convention when working with CFG: a, b, c,..., 0, 1,..., (, ), +, %,... are terminal symbols; A, B, C,... are variables (non-terminals); w, x, y, z,... are strings of terminals; X, Y,... are either a terminal or a variable; α, β, γ,... are strings of terminals and/or variables; In particular, they can also represent strings with only variables. April 26th 2018, Lecture 11 TMV027/DIT321 10/30 Working with Grammars We use the productions of a CFG to infer that a string w is in the language of a given variable. Example: Let us see if is a palindrome. We can do this in 2 (equivalent) ways: Recursive inference: From body to head. (Similar to how we would construct the element in the inductive set.) 1) 0 is palindrome by rule P 0 2) 101 is palindrome using 1) and rule P 1P1 3) is palindrome using 2) and rule P 1P1 4) is palindrome using 3) and rule P 0P0 Derivation: Notation: From head to body using the same rules in the opposite order. P 0P0 01P10 011P April 26th 2018, Lecture 11 TMV027/DIT321 11/30

7 Formal Definition of a Derivation Let G = (V, T, R, S) be a CFG. Definition: Let A V and α, β (V T ). Let A γ R. Then αaβ αγβ is one derivation step (alternative αaβ G αγβ). Example: B E == E 0 == E 0 == E + E 0 == E == April 26th 2018, Lecture 11 TMV027/DIT321 12/30 Reflexive-transitive Closure of a Derivation We can define a relation performing zero or more derivation steps. Definition: The reflexive-transitive closure of is the relation (alternative G ) defined as follows: α α α β β γ α γ Example: B B B 0 + E < E B 0 == Note: We denote A n α when α is derived from A in n steps. Example: B 0 B April 26th 2018, Lecture 11 TMV027/DIT321 13/30

8 Leftmost and Rightmost Derivations At every derivation step we can choose to replace any variable by the right-hand side of one of its productions. Two particular derivations are: Leftmost derivation: Notations: lm and lm. At each step we choose to replace the leftmost variable. Example: B lm 0 == B lm E == E lm 0 == E lm 0 == E + E lm 0 == 0 + E lm 0 == Rightmost derivation: Notations: rm and rm. At each step we choose to replace the rightmost variable. Example: B rm 0 == B rm E == E rm E == E + E rm E == E + 1 rm E == rm 0 == April 26th 2018, Lecture 11 TMV027/DIT321 14/30 Sentential Forms Derivations from the start variable have a special role. Definition: Let G = (V, T, R, S) be a CFG and let α (V T ). α is called a sentential form if S α. We say left sentential form if S lm α or right sentential form if S rm α. Example: 010P010 is a sentential form since P 0P0 01P10 010P010 April 26th 2018, Lecture 11 TMV027/DIT321 15/30

9 Language of a Grammar Definition: Let G = (V, T, R, S) be a CFG. The language of G, denoted L(G) is the set of terminal strings that can be derived from the start variable. L(G) = {w T S G w} Definition: If L is the language of a certain context-free grammar, then L is said to be a context-free language. April 26th 2018, Lecture 11 TMV027/DIT321 16/30 Examples of Context Free Grammars Example: Construct a CFG generating {0 i 1 j j i 0}. S ɛ 0S1 S1 Example: Construct a CFG generating {w {0, 1} # 0 (w) = # 1 (w)}. S ɛ 0S1 1S0 SS Example: Construct a grammar for the following language: {a n b n c m d m n, m 1} {a n b m c m d n n, m 1} S AB C A aab ab B cbd cd C acd add D bdc bc April 26th 2018, Lecture 11 TMV027/DIT321 17/30

10 Observations on Derivations Observe: If we have A γ then we also have αaβ αγβ. The same sequence of steps that took us from A to γ will also take us from αaβ to αγβ. Example: We have E since E E + E 0 + E This same derivation justifies E == E E == since E == E E == E + E E == 0 + E E == April 26th 2018, Lecture 11 TMV027/DIT321 18/30 Observations on Derivations Observe: If we have A X 1 X 2... X k w, then we can break w into pieces w 1, w 2,..., w k such that X i w i. If X i is a terminal then X i = w i. This can be showed by proving (by induction on the length of the derivation) that if X 1 X 2... X k α then all positions of α that come from expansion of X i are to the left of all positions that come from the expansion of X j if i < j. To obtain X i w i from A w we need to remove all positions of the sentential form (see next slide) that are to the left and to the right of all the positions derived from X i, and all steps not relevant for the derivation of w i from X i. Example: Let B E == E E == E + E E == E + 0 E == E + E + 0 E == 0 + E == 0 + E == The derivation from the middle E in the sentential form E == E + E is E E + E 0 + E April 26th 2018, Lecture 11 TMV027/DIT321 19/30

11 Parse Trees Parse trees are a way to represent derivations. A parse tree reflects the internal structure of a word of the language. Using parse trees it becomes very clear which is the variable that was replaced at each step. In addition, it becomes clear which terminal symbols where generated/derived form a particular variable. Parse trees are very important in compiler theory. In a compiler, a parser takes the source code into its abstract tree, which is an abstract version of the parse tree. This parse tree is the structure of the program. April 26th 2018, Lecture 11 TMV027/DIT321 20/30 Parse Trees Definition: Let G = (V, T, R, S) be a CFG. The parse trees for G are trees with the following conditions: Nodes are labelled with a variable; Leaves are either variables, terminals or ɛ; If a node is labelled A and it has children labelled X 1, X 2,..., X n respectively from left to right, then it must exist in R a production of the form A X 1 X 2... X n. Note: If a leaf is ɛ it should be the only child of its parent A and there should be a production A ɛ. Note: Of particular importance are the parse trees with root S. Exercise: Construct the parse trees for 0 == E + 1 and for April 26th 2018, Lecture 11 TMV027/DIT321 21/30

12 Height of a Parse Tree Definition: The height of a parse tree is the maximum length of a path from the root of the tree to one of its leaves. Observe: We count the edges in the tree, and not the number of nodes and the leaf in the path. Example: The height of the parse tree for 0 == E + 1 is 3 and the one for is 4. April 26th 2018, Lecture 11 TMV027/DIT321 22/30 Yield of a Parse Tree Definition: A yield of a parse tree is the string resulted from concatenating all the leaves of the tree from left to right. Observations: We will show than the yield of a tree is a string derived from the root of the tree; Of particular importance are the yields that consist of only terminals; that is, the leaves are either terminals or ɛ; When, in addition, the root is S then we have a parse tree for a string in the language of the grammar; We will see that yields can be used to describe the language of a grammar. April 26th 2018, Lecture 11 TMV027/DIT321 23/30

13 Context-Free Languages and Inductive Definitions For each CFG there is a corresponding inductive definition (see slide 4). Example: Consider the grammar for palindromes in slide 5 (and 7). Now consider the following inductive definition: Base Cases: The empty string is a palindrome; 0 and 1 are palindromes. Inductive Steps: If w is a palindrome, then so are 0w0 and 1w1. A natural way to do proofs on context-free languages defined with an inductive set is to follow this inductive structure. How to prove properties when the language is defined with a grammar? April 26th 2018, Lecture 11 TMV027/DIT321 24/30 Proofs About a Grammar When we want to prove something about a grammar we usually need to prove an statement for each of the variables in the grammar. See notes on induction for an example! (Compare this with proofs about FA where we needed statements for each state.) Proofs about grammars are in general done by: (course-of-value) induction on the length of a certain string of the language; (course-of-value) induction on the length (number of steps) of a derivation of a certain string; induction on the structure of the strings/words in the language; (Recall words are either empty or not empty!) (course-of-value) induction on the height of the parse tree. April 26th 2018, Lecture 11 TMV027/DIT321 25/30

14 Example: Proof About a Grammar Exercise: Consider the grammar S ɛ 0S1 1S0 SS. Prove that w. if S w then # 0 (w) = # 1 (w). Proof: Our P(n) : w. if S n w then # 0 (w) = # 1 (w). Proof by course-of-value induction on the length n of the derivation. Base cases: If length is 1 then we have S ɛ and # 0 (ɛ) = # 1 (ɛ) holds. Inductive Steps: Our IH is that i. 1 i n, w. if S i w then # 0 (w ) = # 1 (w ). Let S n+1 w with n > 0. Then we have 3 cases: S 0S1 n w: Here w = 0w 1 1 with S n w 1. By IH # 0 (w 1 ) = # 1 (w 1 ) hence # 0 (w) = # 0 (0w 1 1) = 1 + # 0 (w 1 ) = 1 + # 1 (w 1 ) = # 1 (w); S 1S0 n 1w 1 0: Similar... S SS n w: Here w = w 1 w 2 with S i w 1 and S j w 2 for i, j < n. By IH # 0 (w 1 ) = # 1 (w 1 ) and # 0 (w 2 ) = # 1 (w 2 ). Hence # 0 (w) = # 0 (w 1 ) + # 0 (w 2 ) = # 1 (w 1 ) + # 1 (w 2 ) = # 1 (w). April 26th 2018, Lecture 11 TMV027/DIT321 26/30 Example: Proof About a Grammar Lemma: Let G be the grammar presented on slide 5. Then L(G) is the set of palindromes over {0, 1}. Proof: We will prove that w. if w {0, 1}, then w L(G) iff w = rev(w). If) Our P(n) : w. if w is a palindrome and w = n then w L(G), that is, P w. Proof by course-of-value induction on n (that is, w ). Base cases: If w = 0 or w = 1 then w is ɛ, 0 or 1. We have productions P ɛ, P 0 and P 1. Then P w so w L(G). Inductive Steps: Assume w such that w > 1. Our IH is that the property holds for any w such that w < w. Since w = rev(w) then w = 0w 0 or w = 1w 1, and w = rev(w ). w < w so by IH then P w. If w = 0w 0 we have P 0P0 0w 0 = w so w L(G). Similarly if w = 1w 1. April 26th 2018, Lecture 11 TMV027/DIT321 27/30

15 Example: Proof About a Grammar (Cont.) Only-if) We prove that w. if w L(G) then w is a palindrome. Our P(n) : w. if P n w then w = rev(w). Proof by mathematical induction on n (length of the derivation of w). Base case: If the derivation is in one step then we should have P ɛ, P 0 and P 1. In all cases we have w = rev(w). Inductive Step: Our IH is that w. if P n w with n > 0 then w = rev(w ). Assume P n+1 w. The we have 2 cases: P 0P0 n 0w 0 = w; P 1P1 n 1w 1 = w. Observe that in both cases P n w with n > 0. Hence by IH w = rev(w ) so w = rev(w). April 26th 2018, Lecture 11 TMV027/DIT321 28/30 Overview of Next Lecture Sections , 5.4: Inference, derivations and parse trees; Ambiguity in grammars; More about proofs on grammars and languages. April 26th 2018, Lecture 11 TMV027/DIT321 29/30

16 Overview of next Week Mon 30 Tue 1 Wed 2 Thu 3 Fri EL41 Consultation Lec HB3 CFG. April 26th 2018, Lecture 11 TMV027/DIT321 30/30

### MA513: Formal Languages and Automata Theory Topic: Context-free Grammars (CFG) Lecture Number 18 Date: September 12, 2011

MA53: Formal Languages and Automata Theory Topic: Context-free Grammars (CFG) Lecture Number 8 Date: September 2, 20 xercise: Define a context-free grammar that represents (a simplification of) expressions

### Context-Free Languages and Parse Trees

Context-Free Languages and Parse Trees Mridul Aanjaneya Stanford University July 12, 2012 Mridul Aanjaneya Automata Theory 1/ 41 Context-Free Grammars A context-free grammar is a notation for describing

### Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2016

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2016 Lecture 15 Ana Bove May 23rd 2016 More on Turing machines; Summary of the course. Overview of today s lecture: Recap: PDA, TM Push-down

### Ambiguous Grammars and Compactification

Ambiguous Grammars and Compactification Mridul Aanjaneya Stanford University July 17, 2012 Mridul Aanjaneya Automata Theory 1/ 44 Midterm Review Mathematical Induction and Pigeonhole Principle Finite Automata

### Context-Free Languages & Grammars (CFLs & CFGs) Reading: Chapter 5

Context-Free Languages & Grammars (CFLs & CFGs) Reading: Chapter 5 1 Not all languages are regular So what happens to the languages which are not regular? Can we still come up with a language recognizer?

### Context-Free Grammars

Context-Free Grammars 1 Informal Comments 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.

### Homework. Context Free Languages. Before We Start. Announcements. Plan for today. Languages. Any questions? Recall. 1st half. 2nd half.

Homework Context Free Languages Homework #2 returned Homework #3 due today Homework #4 Pg 133 -- Exercise 1 (use structural induction) Pg 133 -- Exercise 3 Pg 134 -- Exercise 8b,c,d Pg 135 -- Exercise

### Syntax Analysis Part I

Syntax Analysis Part I Chapter 4: Context-Free Grammars Slides adapted from : Robert van Engelen, Florida State University Position of a Parser in the Compiler Model Source Program Lexical Analyzer Token,

### CT32 COMPUTER NETWORKS DEC 2015

Q.2 a. Using the principle of mathematical induction, prove that (10 (2n-1) +1) is divisible by 11 for all n N (8) Let P(n): (10 (2n-1) +1) is divisible by 11 For n = 1, the given expression becomes (10

### Syntax Analysis. Prof. James L. Frankel Harvard University. Version of 6:43 PM 6-Feb-2018 Copyright 2018, 2015 James L. Frankel. All rights reserved.

Syntax Analysis Prof. James L. Frankel Harvard University Version of 6:43 PM 6-Feb-2018 Copyright 2018, 2015 James L. Frankel. All rights reserved. Context-Free Grammar (CFG) terminals non-terminals start

### Context-Free Grammars and Languages (2015/11)

Chapter 5 Context-Free Grammars and Languages (2015/11) Adriatic Sea shore at Opatija, Croatia Outline 5.0 Introduction 5.1 Context-Free Grammars (CFG s) 5.2 Parse Trees 5.3 Applications of CFG s 5.4 Ambiguity

### Parsing. source code. while (k<=n) {sum = sum+k; k=k+1;}

Compiler Construction Grammars Parsing source code scanner tokens regular expressions lexical analysis Lennart Andersson parser context free grammar Revision 2012 01 23 2012 parse tree AST builder (implicit)

### Where We Are. CMSC 330: Organization of Programming Languages. This Lecture. Programming Languages. Motivation for Grammars

CMSC 330: Organization of Programming Languages Context Free Grammars Where We Are Programming languages Ruby OCaml Implementing programming languages Scanner Uses regular expressions Finite automata Parser

### 3. Syntax Analysis. Andrea Polini. Formal Languages and Compilers Master in Computer Science University of Camerino

3. Syntax Analysis Andrea Polini Formal Languages and Compilers Master in Computer Science University of Camerino (Formal Languages and Compilers) 3. Syntax Analysis CS@UNICAM 1 / 54 Syntax Analysis: the

### CMSC 330: Organization of Programming Languages. Architecture of Compilers, Interpreters

: Organization of Programming Languages Context Free Grammars 1 Architecture of Compilers, Interpreters Source Scanner Parser Static Analyzer Intermediate Representation Front End Back End Compiler / Interpreter

### Introduction to Syntax Analysis

Compiler Design 1 Introduction to Syntax Analysis Compiler Design 2 Syntax Analysis The syntactic or the structural correctness of a program is checked during the syntax analysis phase of compilation.

### CS525 Winter 2012 \ Class Assignment #2 Preparation

1 CS525 Winter 2012 \ Class Assignment #2 Preparation Ariel Stolerman 2.26) Let be a CFG in Chomsky Normal Form. Following is a proof that for any ( ) of length exactly steps are required for any derivation

### UNIT I PART A PART B

OXFORD ENGINEERING COLLEGE (NAAC ACCREDITED WITH B GRADE) DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING LIST OF QUESTIONS YEAR/SEM: III/V STAFF NAME: Dr. Sangeetha Senthilkumar SUB.CODE: CS6503 SUB.NAME:

### Derivations of a CFG. MACM 300 Formal Languages and Automata. Context-free Grammars. Derivations and parse trees

Derivations of a CFG MACM 300 Formal Languages and Automata Anoop Sarkar http://www.cs.sfu.ca/~anoop strings grow on trees strings grow on Noun strings grow Object strings Verb Object Noun Verb Object

### CS5371 Theory of Computation. Lecture 8: Automata Theory VI (PDA, PDA = CFG)

CS5371 Theory of Computation Lecture 8: Automata Theory VI (PDA, PDA = CFG) Objectives Introduce Pushdown Automaton (PDA) Show that PDA = CFG In terms of descriptive power Pushdown Automaton (PDA) Roughly

### R10 SET a) Construct a DFA that accepts an identifier of a C programming language. b) Differentiate between NFA and DFA?

R1 SET - 1 1. a) Construct a DFA that accepts an identifier of a C programming language. b) Differentiate between NFA and DFA? 2. a) Design a DFA that accepts the language over = {, 1} of all strings that

### Introduction to Syntax Analysis. The Second Phase of Front-End

Compiler Design IIIT Kalyani, WB 1 Introduction to Syntax Analysis The Second Phase of Front-End Compiler Design IIIT Kalyani, WB 2 Syntax Analysis The syntactic or the structural correctness of a program

### Optimizing Finite Automata

Optimizing Finite Automata We can improve the DFA created by MakeDeterministic. Sometimes a DFA will have more states than necessary. For every DFA there is a unique smallest equivalent DFA (fewest states

VALLIAMMAI ENGNIEERING COLLEGE SRM Nagar, Kattankulathur 603203. DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING Year & Semester : III Year, V Semester Section : CSE - 1 & 2 Subject Code : CS6503 Subject

### Models of Computation II: Grammars and Pushdown Automata

Models of Computation II: Grammars and Pushdown Automata COMP1600 / COMP6260 Dirk Pattinson Australian National University Semester 2, 2018 Catch Up / Drop in Lab Session 1 Monday 1100-1200 at Room 2.41

### CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages Context Free Grammars 1 Architecture of Compilers, Interpreters Source Analyzer Optimizer Code Generator Abstract Syntax Tree Front End Back End Compiler

### Normal Forms for CFG s. Eliminating Useless Variables Removing Epsilon Removing Unit Productions Chomsky Normal Form

Normal Forms for CFG s Eliminating Useless Variables Removing Epsilon Removing Unit Productions Chomsky Normal Form 1 Variables That Derive Nothing Consider: S -> AB, A -> aa a, B -> AB Although A derives

### JNTUWORLD. Code No: R

Code No: R09220504 R09 SET-1 B.Tech II Year - II Semester Examinations, April-May, 2012 FORMAL LANGUAGES AND AUTOMATA THEORY (Computer Science and Engineering) Time: 3 hours Max. Marks: 75 Answer any five

### CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages Context Free Grammars and Parsing 1 Recall: Architecture of Compilers, Interpreters Source Parser Static Analyzer Intermediate Representation Front End Back

### Closure Properties of CFLs; Introducing TMs. CS154 Chris Pollett Apr 9, 2007.

Closure Properties of CFLs; Introducing TMs CS154 Chris Pollett Apr 9, 2007. Outline Closure Properties of Context Free Languages Algorithms for CFLs Introducing Turing Machines Closure Properties of CFL

### Syntax Analysis Check syntax and construct abstract syntax tree

Syntax Analysis Check syntax and construct abstract syntax tree if == = ; b 0 a b Error reporting and recovery Model using context free grammars Recognize using Push down automata/table Driven Parsers

### CMSC 330: Organization of Programming Languages. Context Free Grammars

CMSC 330: Organization of Programming Languages Context Free Grammars 1 Architecture of Compilers, Interpreters Source Analyzer Optimizer Code Generator Abstract Syntax Tree Front End Back End Compiler

### Compiler Design Concepts. Syntax Analysis

Compiler Design Concepts Syntax Analysis Introduction First task is to break up the text into meaningful words called tokens. newval=oldval+12 id = id + num Token Stream Lexical Analysis Source Code (High

### Homework. Announcements. Before We Start. Languages. Plan for today. Chomsky Normal Form. Final Exam Dates have been announced

Homework Homework #3 returned Homework #4 due today Homework #5 Pg 169 -- Exercise 4 Pg 183 -- Exercise 4c,e,i Pg 184 -- Exercise 10 Pg 184 -- Exercise 12 Pg 185 -- Exercise 17 Due 10 / 17 Announcements

Oxford University Press 2013 Slides for Faculty Assistance Preliminaries Author: Vivek Kulkarni vivek_kulkarni@yahoo.com Outline Following topics are covered in the slides: Basic concepts, namely, symbols,

### Architecture of Compilers, Interpreters. CMSC 330: Organization of Programming Languages. Front End Scanner and Parser. Implementing the Front End

Architecture of Compilers, Interpreters : Organization of Programming Languages ource Analyzer Optimizer Code Generator Context Free Grammars Intermediate Representation Front End Back End Compiler / Interpreter

### Introduction to Parsing. Lecture 8

Introduction to Parsing Lecture 8 Adapted from slides by G. Necula Outline Limitations of regular languages Parser overview Context-free grammars (CFG s) Derivations Languages and Automata Formal languages

### Definition: A context-free grammar (CFG) is a 4- tuple. variables = nonterminals, terminals, rules = productions,,

CMPSCI 601: Recall From Last Time Lecture 5 Definition: A context-free grammar (CFG) is a 4- tuple, variables = nonterminals, terminals, rules = productions,,, are all finite. 1 ( ) \$ Pumping Lemma for

### CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages Context Free Grammars 1 Architecture of Compilers, Interpreters Source Analyzer Optimizer Code Generator Abstract Syntax Tree Front End Back End Compiler

### Lecture 8: Context Free Grammars

Lecture 8: Context Free s Dr Kieran T. Herley Department of Computer Science University College Cork 2017-2018 KH (12/10/17) Lecture 8: Context Free s 2017-2018 1 / 1 Specifying Non-Regular Languages Recall

### Syntax Analysis. Martin Sulzmann. Martin Sulzmann Syntax Analysis 1 / 38

Syntax Analysis Martin Sulzmann Martin Sulzmann Syntax Analysis 1 / 38 Syntax Analysis Objective Recognize individual tokens as sentences of a language (beyond regular languages). Example 1 (OK) Program

### CMSC 330: Organization of Programming Languages. Context Free Grammars

CMSC 330: Organization of Programming Languages Context Free Grammars 1 Architecture of Compilers, Interpreters Source Analyzer Optimizer Code Generator Abstract Syntax Tree Front End Back End Compiler

### CSE P 501 Compilers. Parsing & Context-Free Grammars Hal Perkins Winter /15/ Hal Perkins & UW CSE C-1

CSE P 501 Compilers Parsing & Context-Free Grammars Hal Perkins Winter 2008 1/15/2008 2002-08 Hal Perkins & UW CSE C-1 Agenda for Today Parsing overview Context free grammars Ambiguous grammars Reading:

### Compilation 2012 Context-Free Languages Parsers and Scanners. Jan Midtgaard Michael I. Schwartzbach Aarhus University

Compilation 2012 Parsers and Scanners Jan Midtgaard Michael I. Schwartzbach Aarhus University Context-Free Grammars Example: sentence subject verb object subject person person John Joe Zacharias verb asked

### Decision Properties for Context-free Languages

Previously: Decision Properties for Context-free Languages CMPU 240 Language Theory and Computation Fall 2018 Context-free languages Pumping Lemma for CFLs Closure properties for CFLs Today: Assignment

### Outline. Limitations of regular languages. Introduction to Parsing. Parser overview. Context-free grammars (CFG s)

Outline Limitations of regular languages Introduction to Parsing Parser overview Lecture 8 Adapted from slides by G. Necula Context-free grammars (CFG s) Derivations Languages and Automata Formal languages

### 1. [5 points each] True or False. If the question is currently open, write O or Open.

University of Nevada, Las Vegas Computer Science 456/656 Spring 2018 Practice for the Final on May 9, 2018 The entire examination is 775 points. The real final will be much shorter. Name: No books, notes,

### Compilers Course Lecture 4: Context Free Grammars

Compilers Course Lecture 4: Context Free Grammars Example: attempt to define simple arithmetic expressions using named regular expressions: num = [0-9]+ sum = expr "+" expr expr = "(" sum ")" num Appears

### Defining Languages GMU

Defining Languages CS463 @ GMU How do we discuss languages? We might focus on these qualities: readability: how well does a language explicitly and clearly describe its purpose? writability: how expressive

### Languages and Compilers

Principles of Software Engineering and Operational Systems Languages and Compilers SDAGE: Level I 2012-13 3. Formal Languages, Grammars and Automata Dr Valery Adzhiev vadzhiev@bournemouth.ac.uk Office:

### CS415 Compilers. Syntax Analysis. These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University

CS415 Compilers Syntax Analysis These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University Limits of Regular Languages Advantages of Regular Expressions

### Complexity Theory. Compiled By : Hari Prasad Pokhrel Page 1 of 20. ioenotes.edu.np

Chapter 1: Introduction Introduction Purpose of the Theory of Computation: Develop formal mathematical models of computation that reflect real-world computers. Nowadays, the Theory of Computation can be

### Last lecture CMSC330. This lecture. Finite Automata: States. Finite Automata. Implementing Regular Expressions. Languages. Regular expressions

Last lecture CMSC330 Finite Automata Languages Sets of strings Operations on languages Regular expressions Constants Operators Precedence 1 2 Finite automata States Transitions Examples Types This lecture

### 15 212: Principles of Programming. Some Notes on Grammars and Parsing

15 212: Principles of Programming Some Notes on Grammars and Parsing Michael Erdmann Spring 2011 1 Introduction These notes are intended as a rough and ready guide to grammars and parsing. The theoretical

### Syntax Analysis: Context-free Grammars, Pushdown Automata and Parsing Part - 4. Y.N. Srikant

Syntax Analysis: Context-free Grammars, Pushdown Automata and Part - 4 Department of Computer Science and Automation Indian Institute of Science Bangalore 560 012 NPTEL Course on Principles of Compiler

### LL(1) predictive parsing

LL(1) predictive parsing Informatics 2A: Lecture 11 Mary Cryan School of Informatics University of Edinburgh mcryan@staffmail.ed.ac.uk 10 October 2018 1 / 15 Recap of Lecture 10 A pushdown automaton (PDA)

### Actually talking about Turing machines this time

Actually talking about Turing machines this time 10/25/17 (Using slides adapted from the book) Administrivia HW due now (Pumping lemma for context-free languages) HW due Friday (Building TMs) Exam 2 out

### CS 315 Programming Languages Syntax. Parser. (Alternatively hand-built) (Alternatively hand-built)

Programming languages must be precise Remember instructions This is unlike natural languages CS 315 Programming Languages Syntax Precision is required for syntax think of this as the format of the language

### CS 2210 Sample Midterm. 1. Determine if each of the following claims is true (T) or false (F).

CS 2210 Sample Midterm 1. Determine if each of the following claims is true (T) or false (F). F A language consists of a set of strings, its grammar structure, and a set of operations. (Note: a language

### Formal Languages and Compilers Lecture VII Part 3: Syntactic A

Formal Languages and Compilers Lecture VII Part 3: Syntactic Analysis Free University of Bozen-Bolzano Faculty of Computer Science POS Building, Room: 2.03 artale@inf.unibz.it http://www.inf.unibz.it/

### CS2210: Compiler Construction Syntax Analysis Syntax Analysis

Comparison with Lexical Analysis The second phase of compilation Phase Input Output Lexer string of characters string of tokens Parser string of tokens Parse tree/ast What Parse Tree? CS2210: Compiler

### Context Free Languages and Pushdown Automata

Context Free Languages and Pushdown Automata COMP2600 Formal Methods for Software Engineering Ranald Clouston Australian National University Semester 2, 2013 COMP 2600 Context Free Languages and Pushdown

### University of Nevada, Las Vegas Computer Science 456/656 Fall 2016

University of Nevada, Las Vegas Computer Science 456/656 Fall 2016 The entire examination is 925 points. The real final will be much shorter. Name: No books, notes, scratch paper, or calculators. Use pen

### Context-Free Grammars

Context-Free Grammars Describing Languages We've seen two models for the regular languages: Finite automata accept precisely the strings in the language. Regular expressions describe precisely the strings

### CMSC 330: Organization of Programming Languages. Context-Free Grammars Ambiguity

CMSC 330: Organization of Programming Languages Context-Free Grammars Ambiguity Review Why should we study CFGs? What are the four parts of a CFG? How do we tell if a string is accepted by a CFG? What

### Syntax Analysis. Amitabha Sanyal. (www.cse.iitb.ac.in/ as) Department of Computer Science and Engineering, Indian Institute of Technology, Bombay

Syntax Analysis (www.cse.iitb.ac.in/ as) Department of Computer Science and Engineering, Indian Institute of Technology, Bombay September 2007 College of Engineering, Pune Syntax Analysis: 2/124 Syntax

### (a) R=01[((10)*+111)*+0]*1 (b) ((01+10)*00)*. [8+8] 4. (a) Find the left most and right most derivations for the word abba in the grammar

Code No: R05310501 Set No. 1 III B.Tech I Semester Regular Examinations, November 2008 FORMAL LANGUAGES AND AUTOMATA THEORY (Computer Science & Engineering) Time: 3 hours Max Marks: 80 Answer any FIVE

### ECS 120 Lesson 16 Turing Machines, Pt. 2

ECS 120 Lesson 16 Turing Machines, Pt. 2 Oliver Kreylos Friday, May 4th, 2001 In the last lesson, we looked at Turing Machines, their differences to finite state machines and pushdown automata, and their

### COP4020 Programming Languages. Syntax Prof. Robert van Engelen

COP4020 Programming Languages Syntax Prof. Robert van Engelen Overview n Tokens and regular expressions n Syntax and context-free grammars n Grammar derivations n More about parse trees n Top-down and

### (Refer Slide Time: 0:19)

Theory of Computation. Professor somenath Biswas. Department of Computer Science & Engineering. Indian Institute of Technology, Kanpur. Lecture-15. Decision Problems for Regular Languages. (Refer Slide

### Final Course Review. Reading: Chapters 1-9

Final Course Review Reading: Chapters 1-9 1 Objectives Introduce concepts in automata theory and theory of computation Identify different formal language classes and their relationships Design grammars

### Foundations of Computer Science Spring Mathematical Preliminaries

Foundations of Computer Science Spring 2017 Equivalence Relation, Recursive Definition, and Mathematical Induction Mathematical Preliminaries Mohammad Ashiqur Rahman Department of Computer Science College

### Programming Lecture 3

Programming Lecture 3 Expressions (Chapter 3) Primitive types Aside: Context Free Grammars Constants, variables Identifiers Variable declarations Arithmetic expressions Operator precedence Assignment statements

### CS 403: Scanning and Parsing

CS 403: Scanning and Parsing Stefan D. Bruda Fall 2017 THE COMPILATION PROCESS Character stream Scanner (lexical analysis) Token stream Parser (syntax analysis) Parse tree Semantic analysis Abstract syntax

### COP 3402 Systems Software Syntax Analysis (Parser)

COP 3402 Systems Software Syntax Analysis (Parser) Syntax Analysis 1 Outline 1. Definition of Parsing 2. Context Free Grammars 3. Ambiguous/Unambiguous Grammars Syntax Analysis 2 Lexical and Syntax Analysis

### Compiler Design 1. Bottom-UP Parsing. Goutam Biswas. Lect 6

Compiler Design 1 Bottom-UP Parsing Compiler Design 2 The Process The parse tree is built starting from the leaf nodes labeled by the terminals (tokens). The parser tries to discover appropriate reductions,

### Wednesday, September 9, 15. Parsers

Parsers What is a parser A parser has two jobs: 1) Determine whether a string (program) is valid (think: grammatically correct) 2) Determine the structure of a program (think: diagramming a sentence) Agenda

### Parsers. What is a parser. Languages. Agenda. Terminology. Languages. A parser has two jobs:

What is a parser Parsers A parser has two jobs: 1) Determine whether a string (program) is valid (think: grammatically correct) 2) Determine the structure of a program (think: diagramming a sentence) Agenda

### Introduction to Lexing and Parsing

Introduction to Lexing and Parsing ECE 351: Compilers Jon Eyolfson University of Waterloo June 18, 2012 1 Riddle Me This, Riddle Me That What is a compiler? 1 Riddle Me This, Riddle Me That What is a compiler?

### LL(1) predictive parsing

LL(1) predictive parsing Informatics 2A: Lecture 11 John Longley School of Informatics University of Edinburgh jrl@staffmail.ed.ac.uk 13 October, 2011 1 / 12 1 LL(1) grammars and parse tables 2 3 2 / 12

### Defining syntax using CFGs

Defining syntax using CFGs Roadmap Last time Defined context-free grammar This time CFGs for specifying a language s syntax Language membership List grammars Resolving ambiguity CFG Review G = (N,Σ,P,S)

### Multiple Choice Questions

Techno India Batanagar Computer Science and Engineering Model Questions Subject Name: Formal Language and Automata Theory Subject Code: CS 402 Multiple Choice Questions 1. The basic limitation of an FSM

### THE COMPILATION PROCESS EXAMPLE OF TOKENS AND ATTRIBUTES

THE COMPILATION PROCESS Character stream CS 403: Scanning and Parsing Stefan D. Bruda Fall 207 Token stream Parse tree Abstract syntax tree Modified intermediate form Target language Modified target language

### Decidable Problems. We examine the problems for which there is an algorithm.

Decidable Problems We examine the problems for which there is an algorithm. Decidable Problems A problem asks a yes/no question about some input. The problem is decidable if there is a program that always

### Theoretical Part. Chapter one:- - What are the Phases of compiler? Answer:

Theoretical Part Chapter one:- - What are the Phases of compiler? Six phases Scanner Parser Semantic Analyzer Source code optimizer Code generator Target Code Optimizer Three auxiliary components Literal

### Finite Automata. Dr. Nadeem Akhtar. Assistant Professor Department of Computer Science & IT The Islamia University of Bahawalpur

Finite Automata Dr. Nadeem Akhtar Assistant Professor Department of Computer Science & IT The Islamia University of Bahawalpur PhD Laboratory IRISA-UBS University of South Brittany European University

### CS 314 Principles of Programming Languages. Lecture 3

CS 314 Principles of Programming Languages Lecture 3 Zheng Zhang Department of Computer Science Rutgers University Wednesday 14 th September, 2016 Zheng Zhang 1 CS@Rutgers University Class Information

### Formal Languages and Compilers Lecture IV: Regular Languages and Finite. Finite Automata

Formal Languages and Compilers Lecture IV: Regular Languages and Finite Automata Free University of Bozen-Bolzano Faculty of Computer Science POS Building, Room: 2.03 artale@inf.unibz.it http://www.inf.unibz.it/

### CS 321 Programming Languages and Compilers. VI. Parsing

CS 321 Programming Languages and Compilers VI. Parsing Parsing Calculate grammatical structure of program, like diagramming sentences, where: Tokens = words Programs = sentences For further information,

### THEORY OF COMPUTATION

THEORY OF COMPUTATION UNIT-1 INTRODUCTION Overview This chapter begins with an overview of those areas in the theory of computation that are basic foundation of learning TOC. This unit covers the introduction

### TAFL 1 (ECS-403) Unit- V. 5.1 Turing Machine. 5.2 TM as computer of Integer Function

TAFL 1 (ECS-403) Unit- V 5.1 Turing Machine 5.2 TM as computer of Integer Function 5.2.1 Simulating Turing Machine by Computer 5.2.2 Simulating Computer by Turing Machine 5.3 Universal Turing Machine 5.4

### Formal Languages and Automata

Mobile Computing and Software Engineering p. 1/3 Formal Languages and Automata Chapter 3 Regular languages and Regular Grammars Chuan-Ming Liu cmliu@csie.ntut.edu.tw Department of Computer Science and

### Formal Languages. Grammar. Ryan Stansifer. Department of Computer Sciences Florida Institute of Technology Melbourne, Florida USA 32901

Formal Languages Grammar Ryan Stansifer Department of Computer Sciences Florida Institute of Technology Melbourne, Florida USA 32901 http://www.cs.fit.edu/~ryan/ March 15, 2018 A formal language is a set

### The Turing Machine. Unsolvable Problems. Undecidability. The Church-Turing Thesis (1936) Decision Problem. Decision Problems

The Turing Machine Unsolvable Problems Motivating idea Build a theoretical a human computer Likened to a human with a paper and pencil that can solve problems in an algorithmic way The theoretical machine

### CS154 Midterm Examination. May 4, 2010, 2:15-3:30PM

CS154 Midterm Examination May 4, 2010, 2:15-3:30PM Directions: Answer all 7 questions on this paper. The exam is open book and open notes. Any materials may be used. Name: I acknowledge and accept the

### LR Parsing Techniques

LR Parsing Techniques Introduction Bottom-Up Parsing LR Parsing as Handle Pruning Shift-Reduce Parser LR(k) Parsing Model Parsing Table Construction: SLR, LR, LALR 1 Bottom-UP Parsing A bottom-up parser

### CS402 Theory of Automata Solved Subjective From Midterm Papers. MIDTERM SPRING 2012 CS402 Theory of Automata

Solved Subjective From Midterm Papers Dec 07,2012 MC100401285 Moaaz.pk@gmail.com Mc100401285@gmail.com PSMD01 MIDTERM SPRING 2012 Q. Point of Kleen Theory. Answer:- (Page 25) 1. If a language can be accepted

### Compiler Construction

Compiler Construction Exercises 1 Review of some Topics in Formal Languages 1. (a) Prove that two words x, y commute (i.e., satisfy xy = yx) if and only if there exists a word w such that x = w m, y =