# Compiler Construction

Size: px
Start display at page:

Transcription

1 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 = w n for some non-negative integers m, n. (b) Characterize all pairs of words x, y, z satisfying the equality x 2 y 2 = z 2. (c) Characterize all triples of words x, y, z satisfying the equality xyz = yzx. 2. Let Σ = {a, b,..., z, 0, 1,..., 9, }. Let L be the language consisting of all non-empty words over Σ which (i) do not start with a digit, (ii) do not contain two consecutive occurrences of, and (iii) do not end with. (a) Construct a regular expression r such that L(r) = L. (b) Construct a DFA accepting L. (c) How many words of each length n does L include? 3. Show that the following languages over Σ are regular: (a) The collection of all words whose length is congruent to k modulo m (where 0 k m 1). (b) The collection of all words having the property that, for some fixed positive integer k and all pairs of letters σ 1, σ 2, the difference between the number of occurrences of σ 1 and of σ 2 in every prefix does not exceed k. 1

2 4. How many words of length n do the languages, corresponding to the following regular expressions, contain? (a) σ 1σ 2... σ k (where the σ i s are all distinct). (b) ( ). 5. Show that, if L is a regular language, then so are the following languages: (a) The language obtained by replacing, in each word of L, each occurrence of aa by b. (The replacement is done consecutively; thus, the block a 2k is replaced by b k and the block a 2k+1 by b k a.) (b) The language obtained from L by deleting the second last letter in every word of length 2 or more: 6. Let L 1, L 2 be two languages over Σ. Show that the equation L 1 L L 2 = L has a solution. Moreover, if L 1, L 2 are both regular (or both contextfree), then there exists a solution L with the same property. 7. Let Σ = {a, b, c}. Construct DFA s accepting the following languages: (a) All words containing neither aaa nor aca as a subword. (b) All words containing either ababa or abcba as a subword. 8. Construct NFA s accepting the languages corresponding to the following regular expressions: (a) bab(bba abb) bab. (b) ab(ab bba) a(ba φ )bba. 9. Present an algorithm which, given a DFA, returns all words of minimal length accepted by it (or an error if it accepts the empty language). 10. (a) Show that an infinite regular language may be written as an infinite disjoint union of infinite regular languages. (b) Does an infinite context-free language necessarily contain an infinite regular language? 2

3 11. Show that the following languages are not regular: (a) {a m b n c m+n : m, n 0}. (b) {a k b l c m d n : k, l, m, n 0, {k, l, m, n} 2}. (c) {0 m3 +n 3 : m, n 0}. 12. Given a set of non-negative integers, the set of their expansions in base 10 forms a partial language of {0, 1,..., 9}. For each of the following sets show that the corresponding language is regular or not (as indicated): (a) All powers of 1000 (regular). (b) All powers of 7 (not regular). (c) All perfect cubes (not regular). 13. Find the language accepted by the grammar: S AB BA, A aab ε, B bba ε. 14. Let a 1, a 2,..., a r be positive integers and b 1, b 2,..., b r nonnegative integers. Consider the language {σ a 1n+b 1 1 σ a 2n+b σ arn+br n 0}, where σ 1, σ 2,..., σ r are any letters, not necessarily distinct. Specify the conditions under which this language is context-free. r : 15. Construct a DFA accepting the same language as the grammar: S abs bas bca, A babb, B bcbac, C abac bc ε. 16. Construct a pushdown automaton accepting the same language as the grammar: S ε SbS AbS SaB, A Bb a 2, B b 2 S baa b 2. 3

4 17. Consider the grammar G defined by: S S + S S S a b c. (a) Is the language L(G) regular? If yes find a DFA accepting the same language as G and a regular grammar G equivalent to G. If not prove it. (b) How many words of each length does the language L(G) include? (c) How many derivation trees yield each word in L(G)? (Hint: Find a recurrence for the sequence which expresses the required number as a function of the word s length.) 2 Lexical Analysis 18. (a) Write a regular expression r such that L(r) consists of all identifiers (i.e., all strings of letters and digits, starting with a letter), with the exception of the three strings if, int and integer. (b) Construct a DFA which recognizes the string if as a token of type IF, the strings int and integer as tokens of type NUM, and all other identifiers as tokens of type ID. 19. (a) Given an arbitrary fixed integer d 2, construct a regular expression r such that L(r) consists of all base d expansions of even (positive) integers. (Thus, the alphabet consists of all digits in base d.) (b) Construct a DFA which recognizes every non-negative number, expanded in base d, as EVEN or as ODD. 20. (a) Given a regular expression r, define a regular expression r for vectors (of any non-negative length) of elements of type r. The entries of a vector are separated by commas and grouped by parentheses. (b) Given a DFA recognizing elements of type r, construct a DFA which recognizes vectors of such elements. 4

5 (c) Can you design your DFA so as to recognize vectors whose length is (i) 3 modulo 10? (ii) a prime? (d) The regular expression r represents vectors of vectors of elements of type r. Can you construct a regular expression [r] for matrices (i.e., vectors whose entries are vectors of the same length) of elements of type r? 21. (a) For any n 0, write a regular definition for the language of balanced parentheses of nesting level up to n. (b) Write a computer program which, for given n, will output a DFA for this language. The DFA should inform of the nesting level. (Represent the DFA in any way you like by a transition table, a graph, etc.) 22. Write a regular definition for the language of all strings over {a, b,..., z}, not containing if as a substring. 23. In Java, the command a = b+++--c; passes compilation, whereas the command a = b+++++c; does not. Why? 24. In a certain computer language, identifiers are strings of letters and digits, starting with a letter, with the additional constraint that a character should not appear more than once in the name. (A lowercase letter and the corresponding upper-case letter are considered as distinct.) Construct a DFA, with a minimal possible number of states, recognizing identifiers. How many states does this DFA consist of? 25. Consider the class of special NFAs discussed in class (with an initial state having no incoming transitions, a single accepting states with no outgoing transitions, and either a single σ-transition or up to two ε-transitions from any of the non-accepting states). Denoting the class by S, consider the following problem: Given M in S, does there exist an equivalent M in S having less states than M? Is the problem decidable? 26. A special NFA (see Question 25), accepting the language L(w 1 w 2... w n ), where w 1, w 2,..., w n are any words, is constructed according to the algorithm discussed in class. (Here, the construction is according to the order of the characters in the expression.) 5

6 (a) How many states does the NFA consist of? (b) Suppose the words w i are distinct from each other and none of them is empty. Does the NFA above consist of the minimal possible number of states (within the class of such NFAs)? 27. Consider the NFA where M = ({q ij : 1 i m, 1 j n}, {a, b},, q 11, q mn ), {q i,j+1, q i+1,j }, 1 i m 1, 1 j n 1, {q (q ij, ε) = m,j+1 }, i = m, 1 j n 1, {q i+1,n }, 1 i m 1, j = n,, i = m, j = n, { {qi,j+1 }, 1 i m, 1 j n 1, (q ij, a) =, 1 i m, j = n, { {qi+1,j }, 1 i m 1, 1 j n, (q ij, b) =, i = m, 1 j n. How many states are there in the equivalent DFA, constructed using the algorithm discussed in class? 3 Syntactic Analysis 28. Consider the grammar G 1 given by: S ises is ε, and the grammar G 2 given by: S M U, M imem ε, U imeu is. (Intuitively, you should think of these grammars as the two grammars presented in class for conditional statements. Here we deal only with occurrences of the words if and else, represented by i and e, respectively. M and U stand for matched and unmatched, respectively.) (a) Prove that L(G 1 ) = L(G 2 ). 6

7 (b) Which words are obtained by a unique derivation tree in G 1? (c) Write a program that, given a word in {i, e}, finds the number of derivation trees (if any) over G 1 producing this word. Prove that your algorithm works in polynomial time in the length of the input. (d) Prove that G 2 is unambiguous. (e) Write a program that, given a word in {i, e}, finds the unique derivation sequence over G 2 producing this word (and gives an error message if the word does not belong to L(G 2 )). 29. Consider the following grammar, designed to solve the ambiguity problem of the if-then-else grammar presented in class: stmt if cond then stmt matched, matched if cond then matched else stmt unconditionalstmt. Show that the grammar is still ambiguous. 30. Consider the grammar G given by: S SSσ 1 SSσ 2... SSσ r σ r+1, where σ 1, σ 2,..., σ r+1 are distinct terminals. Is the grammar unambiguous? If yes prove your claim, if not produce a word in L(G) with two distinct derivation trees. 31. Eliminate the left recursion (direct and indirect) in the following grammars: (a) (b) (c) S SaS Ab ab, A SA AB ba, B ABa Bb bab. S SAB ba, A SBS ABA a, B Sa AS bab. S Sb a, A AbS ba, B SABA AA b. 7

8 32. Transfer the following grammars into Chomsky Normal Form (omitting the word ε from the language if it accepted by the grammar): (a) (b) (c) S as bscsd e f. S asb A, A SS ε. S BCA c, A bac b. B cbcc ε, C acaa ε. 33. Suppose G is a grammar almost in Chomsky Normal Form, namely all rules are either of the form A a or of the form A BC or of the form A BCD. We use the idea of the CYK algorithm on this grammar directly. What will the runtime of the algorithm be? 34. Let G be a grammar in Chomsky Normal Form. (a) The way the CYK algorithm was presented in class, it just finds, for any w T, whether S = w or not. Change the algorithm so that, in case S = w, it will construct a corresponding parse tree. By how much does this addition change the runtime? (b) Change the CYK algorithm so that it will return not a boolean value, indicating whether S = w or not, but an integer value, indicating the number of parse trees producing w. Make sure your algorithm still works in polynomial time. (c) Consider again the previous part. Can you construct all these parse trees in polynomial time? (d) Design an algorithm that, for any strings α, β (N T ), decides whether α = β. What is the runtime of your algorithm? 35. Find the FIRST sets of all non-terminals and right-hand sides of all rules for the following grammars: (a) 8

9 S as AS BAb, A Aab AB ε, B Aa BbB ε. (b) S ABCS SS aba, A ACB cb ε, B BCB A bc, C AS c. (c) S SS AB c, A Aa Aab a ε, B bc bb Sb b, C ca SC c. 36. Find the FOLLOW sets of all non-terminals for the grammars in Question 35. 9

### 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

### 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

### 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

### 1. Which of the following regular expressions over {0, 1} denotes the set of all strings not containing 100 as a sub-string?

Multiple choice type questions. Which of the following regular expressions over {, } denotes the set of all strings not containing as a sub-string? 2. DFA has a) *(*)* b) ** c) ** d) *(+)* a) single final

### 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

### QUESTION BANK. Unit 1. Introduction to Finite Automata

QUESTION BANK Unit 1 Introduction to Finite Automata 1. Obtain DFAs to accept strings of a s and b s having exactly one a.(5m )(Jun-Jul 10) 2. Obtain a DFA to accept strings of a s and b s having even

### 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:

### 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

### CS 403 Compiler Construction Lecture 3 Lexical Analysis [Based on Chapter 1, 2, 3 of Aho2]

CS 403 Compiler Construction Lecture 3 Lexical Analysis [Based on Chapter 1, 2, 3 of Aho2] 1 What is Lexical Analysis? First step of a compiler. Reads/scans/identify the characters in the program and groups

### 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

### Skyup's Media. PART-B 2) Construct a Mealy machine which is equivalent to the Moore machine given in table.

Code No: XXXXX JAWAHARLAL NEHRU TECHNOLOGICAL UNIVERSITY, HYDERABAD B.Tech II Year I Semester Examinations (Common to CSE and IT) Note: This question paper contains two parts A and B. Part A is compulsory

Automata Theory TEST 1 Answers Max points: 156 Grade basis: 150 Median grade: 81% 1. (2 pts) See text. You can t be sloppy defining terms like this. You must show a bijection between the natural numbers

### 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

### 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

### QUESTION BANK. Formal Languages and Automata Theory(10CS56)

QUESTION BANK Formal Languages and Automata Theory(10CS56) Chapter 1 1. Define the following terms & explain with examples. i) Grammar ii) Language 2. Mention the difference between DFA, NFA and εnfa.

### Lexical Analysis. Dragon Book Chapter 3 Formal Languages Regular Expressions Finite Automata Theory Lexical Analysis using Automata

Lexical Analysis Dragon Book Chapter 3 Formal Languages Regular Expressions Finite Automata Theory Lexical Analysis using Automata Phase Ordering of Front-Ends Lexical analysis (lexer) Break input string

### AUBER (Models of Computation, Languages and Automata) EXERCISES

AUBER (Models of Computation, Languages and Automata) EXERCISES Xavier Vera, 2002 Languages and alphabets 1.1 Let be an alphabet, and λ the empty string over. (i) Is λ in? (ii) Is it true that λλλ=λ? Is

### 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

### 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

### ITEC2620 Introduction to Data Structures

ITEC2620 Introduction to Data Structures Lecture 9b Grammars I Overview How can a computer do Natural Language Processing? Grammar checking? Artificial Intelligence Represent knowledge so that brute force

### 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

### MIT Specifying Languages with Regular Expressions and Context-Free Grammars

MIT 6.035 Specifying Languages with Regular essions and Context-Free Grammars Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology Language Definition Problem How to precisely

### 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

### CHAPTER TWO LANGUAGES. Dr Zalmiyah Zakaria

CHAPTER TWO LANGUAGES By Dr Zalmiyah Zakaria Languages Contents: 1. Strings and Languages 2. Finite Specification of Languages 3. Regular Sets and Expressions Sept2011 Theory of Computer Science 2 Strings

### 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

### Glynda, the good witch of the North

Strings and Languages It is always best to start at the beginning -- Glynda, the good witch of the North What is a Language? A language is a set of strings made of of symbols from a given alphabet. An

### MIT Specifying Languages with Regular Expressions and Context-Free Grammars. Martin Rinard Massachusetts Institute of Technology

MIT 6.035 Specifying Languages with Regular essions and Context-Free Grammars Martin Rinard Massachusetts Institute of Technology Language Definition Problem How to precisely define language Layered structure

### DVA337 HT17 - LECTURE 4. Languages and regular expressions

DVA337 HT17 - LECTURE 4 Languages and regular expressions 1 SO FAR 2 TODAY Formal definition of languages in terms of strings Operations on strings and languages Definition of regular expressions Meaning

### 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

### Front End: Lexical Analysis. The Structure of a Compiler

Front End: Lexical Analysis The Structure of a Compiler Constructing a Lexical Analyser By hand: Identify lexemes in input and return tokens Automatically: Lexical-Analyser generator We will learn about

### 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

### 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)

### Context-Free Languages. Wen-Guey Tzeng Department of Computer Science National Chiao Tung University

Context-Free Languages Wen-Guey Tzeng Department of Computer Science National Chiao Tung University Context-Free Grammars A grammar G=(V, T, S, P) is context-free if all productions in P are of form A

### Context-Free Languages. Wen-Guey Tzeng Department of Computer Science National Chiao Tung University

Context-Free Languages Wen-Guey Tzeng Department of Computer Science National Chiao Tung University Context-Free Grammars A grammar G=(V, T, S, P) is context-free if all productions in P are of form A

### 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?

### CMPSCI 250: Introduction to Computation. Lecture #28: Regular Expressions and Languages David Mix Barrington 2 April 2014

CMPSCI 250: Introduction to Computation Lecture #28: Regular Expressions and Languages David Mix Barrington 2 April 2014 Regular Expressions and Languages Regular Expressions The Formal Inductive Definition

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

### CMPSCI 250: Introduction to Computation. Lecture #36: State Elimination David Mix Barrington 23 April 2014

CMPSCI 250: Introduction to Computation Lecture #36: State Elimination David Mix Barrington 23 April 2014 State Elimination Kleene s Theorem Overview Another New Model: The r.e.-nfa Overview of the Construction

### Chapter Seven: Regular Expressions

Chapter Seven: Regular Expressions Regular Expressions We have seen that DFAs and NFAs have equal definitional power. It turns out that regular expressions also have exactly that same definitional power:

### Zhizheng Zhang. Southeast University

Zhizheng Zhang Southeast University 2016/10/5 Lexical Analysis 1 1. The Role of Lexical Analyzer 2016/10/5 Lexical Analysis 2 2016/10/5 Lexical Analysis 3 Example. position = initial + rate * 60 2016/10/5

### Lexical Analysis (ASU Ch 3, Fig 3.1)

Lexical Analysis (ASU Ch 3, Fig 3.1) Implementation by hand automatically ((F)Lex) Lex generates a finite automaton recogniser uses regular expressions Tasks remove white space (ws) display source program

### 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

### Lec-5-HW-1, TM basics

Lec-5-HW-1, TM basics (Problem 0)-------------------- Design a Turing Machine (TM), T_sub, that does unary decrement by one. Assume a legal, initial tape consists of a contiguous set of cells, each containing

### 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.

### Concepts Introduced in Chapter 3. Lexical Analysis. Lexical Analysis Terms. Attributes for Tokens

Concepts Introduced in Chapter 3 Lexical Analysis Regular Expressions (REs) Nondeterministic Finite Automata (NFA) Converting an RE to an NFA Deterministic Finite Automatic (DFA) Lexical Analysis Why separate

### PART 3 - SYNTAX ANALYSIS. F. Wotawa TU Graz) Compiler Construction Summer term / 309

PART 3 - SYNTAX ANALYSIS F. Wotawa (IST @ TU Graz) Compiler Construction Summer term 2016 64 / 309 Goals Definition of the syntax of a programming language using context free grammars Methods for parsing

### COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

COMP-421 Compiler Design Presented by Dr Ioanna Dionysiou Administrative! [ALSU03] Chapter 3 - Lexical Analysis Sections 3.1-3.4, 3.6-3.7! Reading for next time [ALSU03] Chapter 3 Copyright (c) 2010 Ioanna

### 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

### Gujarat Technological University Sankalchand Patel College of Engineering, Visnagar B.E. Semester VII (CE) July-Nov Compiler Design (170701)

Gujarat Technological University Sankalchand Patel College of Engineering, Visnagar B.E. Semester VII (CE) July-Nov 2014 Compiler Design (170701) Question Bank / Assignment Unit 1: INTRODUCTION TO COMPILING

### Week 2: Syntax Specification, Grammars

CS320 Principles of Programming Languages Week 2: Syntax Specification, Grammars Jingke Li Portland State University Fall 2017 PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 1/ 62 Words and Sentences

### 2.2 Syntax Definition

42 CHAPTER 2. A SIMPLE SYNTAX-DIRECTED TRANSLATOR sequence of "three-address" instructions; a more complete example appears in Fig. 2.2. This form of intermediate code takes its name from instructions

### 2. Lexical Analysis! Prof. O. Nierstrasz!

2. Lexical Analysis! Prof. O. Nierstrasz! Thanks to Jens Palsberg and Tony Hosking for their kind permission to reuse and adapt the CS132 and CS502 lecture notes.! http://www.cs.ucla.edu/~palsberg/! http://www.cs.purdue.edu/homes/hosking/!

### Context Free Grammars. CS154 Chris Pollett Mar 1, 2006.

Context Free Grammars CS154 Chris Pollett Mar 1, 2006. Outline Formal Definition Ambiguity Chomsky Normal Form Formal Definitions A context free grammar is a 4-tuple (V, Σ, R, S) where 1. V is a finite

### UNION-FREE DECOMPOSITION OF REGULAR LANGUAGES

UNION-FREE DECOMPOSITION OF REGULAR LANGUAGES Thesis submitted in partial fulfillment of the requirements for the award of degree of Master of Engineering in Computer Science and Engineering Submitted

### Solutions to Homework 10

CS/Math 240: Intro to Discrete Math 5/3/20 Instructor: Dieter van Melkebeek Solutions to Homework 0 Problem There were five different languages in Problem 4 of Homework 9. The Language D 0 Recall that

### 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

### 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

### Automata Theory CS S-FR Final Review

Automata Theory CS411-2015S-FR Final Review David Galles Department of Computer Science University of San Francisco FR-0: Sets & Functions Sets Membership: a?{a,b,c} a?{b,c} a?{b,{a,b,c},d} {a,b,c}?{b,{a,b,c},d}

### 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

### General Overview of Compiler

General Overview of Compiler Compiler: - It is a complex program by which we convert any high level programming language (source code) into machine readable code. Interpreter: - It performs the same task

### 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

### Lexical Analysis. COMP 524, Spring 2014 Bryan Ward

Lexical Analysis COMP 524, Spring 2014 Bryan Ward Based in part on slides and notes by J. Erickson, S. Krishnan, B. Brandenburg, S. Olivier, A. Block and others The Big Picture Character Stream Scanner

### 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,

### Context-Free Languages. Wen-Guey Tzeng Department of Computer Science National Chiao Tung University

Context-Free Languages Wen-Guey Tzeng Department of Computer Science National Chiao Tung University Context-Free Grammars A grammar G=(V, T, S, P) is context-free if all productions in P are of form A

### 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

### Context-Free Languages. Wen-Guey Tzeng Department of Computer Science National Chiao Tung University

Context-Free Languages Wen-Guey Tzeng Department of Computer Science National Chiao Tung University 1 Context-Free Grammars Some languages are not regular. Eg. L={a n b n : n 0} A grammar G=(V, T, S, P)

### 2010: Compilers REVIEW: REGULAR EXPRESSIONS HOW TO USE REGULAR EXPRESSIONS

2010: Compilers Lexical Analysis: Finite State Automata Dr. Licia Capra UCL/CS REVIEW: REGULAR EXPRESSIONS a Character in A Empty string R S Alternation (either R or S) RS Concatenation (R followed by

### Formal Languages and Grammars. Chapter 2: Sections 2.1 and 2.2

Formal Languages and Grammars Chapter 2: Sections 2.1 and 2.2 Formal Languages Basis for the design and implementation of programming languages Alphabet: finite set Σ of symbols String: finite sequence

### Formal Languages and Compilers Lecture VI: Lexical Analysis

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

### COMP 330 Autumn 2018 McGill University

COMP 330 Autumn 2018 McGill University Assignment 4 Solutions and Grading Guide Remarks for the graders appear in sans serif font. Question 1[25 points] A sequence of parentheses is a sequence of ( and

### 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/

### 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.

### CS412/413. Introduction to Compilers Tim Teitelbaum. Lecture 2: Lexical Analysis 23 Jan 08

CS412/413 Introduction to Compilers Tim Teitelbaum Lecture 2: Lexical Analysis 23 Jan 08 Outline Review compiler structure What is lexical analysis? Writing a lexer Specifying tokens: regular expressions

### Regular Languages and Regular Expressions

Regular Languages and Regular Expressions According to our definition, a language is regular if there exists a finite state automaton that accepts it. Therefore every regular language can be described

### Context Free Languages

Context Free Languages COMP2600 Formal Methods for Software Engineering Katya Lebedeva Australian National University Semester 2, 2016 Slides by Katya Lebedeva and Ranald Clouston. COMP 2600 Context Free

### 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

### Dr. D.M. Akbar Hussain

1 2 Compiler Construction F6S Lecture - 2 1 3 4 Compiler Construction F6S Lecture - 2 2 5 #include.. #include main() { char in; in = getch ( ); if ( isalpha (in) ) in = getch ( ); else error (); while

### CSCE 314 Programming Languages

CSCE 314 Programming Languages Syntactic Analysis Dr. Hyunyoung Lee 1 What Is a Programming Language? Language = syntax + semantics The syntax of a language is concerned with the form of a program: how

### 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

### 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

### 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

6. Syntax Learning objectives: syntax and semantics syntax diagrams and EBNF describe context-free grammars terminal and nonterminal symbols productions definition of EBNF by itself parse tree grammars

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

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

### 1. (a) What are the closure properties of Regular sets? Explain. (b) Briefly explain the logical phases of a compiler model. [8+8]

Code No: R05311201 Set No. 1 1. (a) What are the closure properties of Regular sets? Explain. (b) Briefly explain the logical phases of a compiler model. [8+8] 2. Compute the FIRST and FOLLOW sets of each

### CMSC 330 Practice Problem 4 Solutions

CMC 330 Practice Problem 4 olutions 1. Context Free Grammars a. List the 4 components of a context free grammar. Terminals, non-terminals, productions, start symbol b. Describe the relationship between

### Midterm Exam. CSCI 3136: Principles of Programming Languages. February 20, Group 2

Banner number: Name: Midterm Exam CSCI 336: Principles of Programming Languages February 2, 23 Group Group 2 Group 3 Question. Question 2. Question 3. Question.2 Question 2.2 Question 3.2 Question.3 Question

### Chapter 4. Lexical and Syntax Analysis. Topics. Compilation. Language Implementation. Issues in Lexical and Syntax Analysis.

Topics Chapter 4 Lexical and Syntax Analysis Introduction Lexical Analysis Syntax Analysis Recursive -Descent Parsing Bottom-Up parsing 2 Language Implementation Compilation There are three possible approaches

### SWEN 224 Formal Foundations of Programming

T E W H A R E W Ā N A N G A O T E Ū P O K O O T E I K A A M Ā U I VUW V I C T O R I A UNIVERSITY OF WELLINGTON EXAMINATIONS 2011 END-OF-YEAR SWEN 224 Formal Foundations of Programming Time Allowed: 3 Hours

### 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

### Midterm I - Solution CS164, Spring 2014

164sp14 Midterm 1 - Solution Midterm I - Solution CS164, Spring 2014 March 3, 2014 Please read all instructions (including these) carefully. This is a closed-book exam. You are allowed a one-page handwritten

### 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

### Proof Techniques Alphabets, Strings, and Languages. Foundations of Computer Science Theory

Proof Techniques Alphabets, Strings, and Languages Foundations of Computer Science Theory Proof By Case Enumeration Sometimes the most straightforward way to prove that a property holds for all elements

### Parsers. Chapitre Language

Chapitre 2 Parsers 2.1 Language Analphabet,inthecontextofformallanguagescanbeanysetΣ.TheelementsofanalphabetΣarecalled letters.aword overanalphabetcanbeanyfinitesequenceofletters.thesetofallwordsoveranalphabet

### CMSC 330, Fall 2009, Practice Problem 3 Solutions

CMC 330, Fall 2009, Practice Problem 3 olutions 1. Context Free Grammars a. List the 4 components of a context free grammar. Terminals, non-terminals, productions, start symbol b. Describe the relationship

### Chapter 3: Describing Syntax and Semantics. Introduction Formal methods of describing syntax (BNF)

Chapter 3: Describing Syntax and Semantics Introduction Formal methods of describing syntax (BNF) We can analyze syntax of a computer program on two levels: 1. Lexical level 2. Syntactic level Lexical

### Chapter 4. Lexical analysis. Concepts. Lexical scanning Regular expressions DFAs and FSAs Lex. Lexical analysis in perspective

Chapter 4 Lexical analysis Lexical scanning Regular expressions DFAs and FSAs Lex Concepts CMSC 331, Some material 1998 by Addison Wesley Longman, Inc. 1 CMSC 331, Some material 1998 by Addison Wesley

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

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018 Lecture 11 Ana Bove April 26th 2018 Recap: Regular Languages Decision properties of RL: Is it empty? Does it contain this word? Contains

### CS402 - Theory of Automata FAQs By

CS402 - Theory of Automata FAQs By Define the main formula of Regular expressions? Define the back ground of regular expression? Regular expressions are a notation that you can think of similar to a programming