Context Free Languages

Similar documents
Context Free Languages and Pushdown Automata

Formal Languages and Compilers Lecture V: Parse Trees and Ambiguous Gr

Models of Computation II: Grammars and Pushdown Automata

Context-Free Grammars

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

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

Compilers and computer architecture From strings to ASTs (2): context free grammars

Introduction to Parsing. Lecture 5

Introduction to Parsing. Lecture 5

CMSC 330: Organization of Programming Languages. Context Free Grammars

Compiler Construction

Introduction to Parsing Ambiguity and Syntax Errors

Introduction to Parsing. Lecture 5. Professor Alex Aiken Lecture #5 (Modified by Professor Vijay Ganesh)

Introduction to Parsing Ambiguity and Syntax Errors

Outline. Regular languages revisited. Introduction to Parsing. Parser overview. Context-free grammars (CFG s) Lecture 5. Derivations.

Lecture 4: Syntax Specification

EECS 6083 Intro to Parsing Context Free Grammars

Parsing: Derivations, Ambiguity, Precedence, Associativity. Lecture 8. Professor Alex Aiken Lecture #5 (Modified by Professor Vijay Ganesh)

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

CS 314 Principles of Programming Languages

Syntax Analysis Check syntax and construct abstract syntax tree

Intro To Parsing. Step By Step

MIT Specifying Languages with Regular Expressions and Context-Free Grammars

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

Parsing II Top-down parsing. Comp 412

( ) i 0. Outline. Regular languages revisited. Introduction to Parsing. Parser overview. Context-free grammars (CFG s) Lecture 5.

Introduction to Parsing. Lecture 8

CMPT 755 Compilers. Anoop Sarkar.

Introduction to Parsing

CMSC 330: Organization of Programming Languages. Context Free Grammars

Principles of Programming Languages COMP251: Syntax and Grammars

Parsing III. CS434 Lecture 8 Spring 2005 Department of Computer Science University of Alabama Joel Jones

Programming Languages and Compilers (CS 421)

Programming Languages & Compilers. Programming Languages and Compilers (CS 421) I. Major Phases of a Compiler. Programming Languages & Compilers

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

Midterm I - Solution CS164, Spring 2014

3. Context-free grammars & parsing

Principles of Programming Languages COMP251: Syntax and Grammars

CMSC 330: Organization of Programming Languages

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

CMSC 330: Organization of Programming Languages. Context Free Grammars

Definition: two derivations are similar if one of them precedes the other.

Programming Languages and Compilers (CS 421)

CMSC 330: Organization of Programming Languages

n (0 1)*1 n a*b(a*) n ((01) (10))* n You tell me n Regular expressions (equivalently, regular 10/20/ /20/16 4

Dr. D.M. Akbar Hussain

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

COL728 Minor1 Exam Compiler Design Sem II, Answer all 5 questions Max. Marks: 20

LL parsing Nullable, FIRST, and FOLLOW

CMSC 330: Organization of Programming Languages

afewadminnotes CSC324 Formal Language Theory Dealing with Ambiguity: Precedence Example Office Hours: (in BA 4237) Monday 3 4pm Wednesdays 1 2pm

CIT Lecture 5 Context-Free Grammars and Parsing 4/2/2003 1

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

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

Lexical Analysis. COMP 524, Spring 2014 Bryan Ward

A Simple Syntax-Directed Translator

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

Non-deterministic Finite Automata (NFA)

([1-9] 1[0-2]):[0-5][0-9](AM PM)? What does the above match? Matches clock time, may or may not be told if it is AM or PM.

ECE251 Midterm practice questions, Fall 2010

Announcements. Written Assignment 1 out, due Friday, July 6th at 5PM.

Compiler Design Concepts. Syntax Analysis

Chapter 3: CONTEXT-FREE GRAMMARS AND PARSING Part2 3.3 Parse Trees and Abstract Syntax Trees

Hoare Logic. COMP2600 Formal Methods for Software Engineering. Rajeev Goré

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

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

SOFTWARE ARCHITECTURE 5. COMPILER

CSCI312 Principles of Programming Languages!

Abstract Syntax Trees L3 24

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Defining syntax using CFGs

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

Fall Compiler Principles Context-free Grammars Refresher. Roman Manevich Ben-Gurion University of the Negev

programming languages need to be precise a regular expression is one of the following: tokens are the building blocks of programs

Compilers Course Lecture 4: Context Free Grammars

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

Programming Languages & Compilers. Programming Languages and Compilers (CS 421) Programming Languages & Compilers. Major Phases of a Compiler

EDAN65: Compilers, Lecture 04 Grammar transformations: Eliminating ambiguities, adapting to LL parsing. Görel Hedin Revised:

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

Chapter 3. Describing Syntax and Semantics

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

R13 SET Discuss how producer-consumer problem and Dining philosopher s problem are solved using concurrency in ADA.

CS152: Programming Languages. Lecture 2 Syntax. Dan Grossman Spring 2011

2.2 Syntax Definition

CS 44 Exam #2 February 14, 2001

Outline. Parser overview Context-free grammars (CFG s) Derivations Syntax-Directed Translation

Derivations vs Parses. Example. Parse Tree. Ambiguity. Different Parse Trees. Context Free Grammars 9/18/2012

CSCE 314 Programming Languages

Context-Free Languages and Parse Trees

Ambiguous Grammars and Compactification

Languages and Compilers

Topic 3: Syntax Analysis I

1. Draw the state graphs for the finite automata which accept sets of strings composed of zeros and ones which:

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

LL(k) Parsing. Predictive Parsers. LL(k) Parser Structure. Sample Parse Table. LL(1) Parsing Algorithm. Push RHS in Reverse Order 10/17/2012

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

CSE 3302 Programming Languages Lecture 2: Syntax

CSE 130 Programming Language Principles & Paradigms Lecture # 5. Chapter 4 Lexical and Syntax Analysis

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

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

Transcription:

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 Languages 1

Ambiguity The definition of CF grammars allow for the possibility of having more than one structure for a given sentence. This ambiguity may make the meaning of a sentence unclear. A context-free grammar G is unambiguous iff every string can be derived by at most one parse tree. G is ambiguous iff there exists any word w L(G) derivable by more than one parse trees. COMP 2600 Context Free Languages 2

Dangling else Take the code if e1 then if e2 then s1 else s2 where e1, e2 are boolean expressions and s1, s2 are subprograms. Does this mean or if e1 then ( if e2 then s1 else s2 ) if e1 then ( if e2 then s1 ) else s2 The dangling else is a problem in computer programming in which an optional else clause in an ifthen(else) statement results in nested conditionals being ambiguous. COMP 2600 Context Free Languages 3

This is a problem that often comes up in compiler construction, especially parsing. COMP 2600 Context Free Languages 4

Inherently Ambiguous Languages Not all context-free languages can be given unambiguous grammars some are inherently ambiguous. Consider the language L = {a i b j c k i = j or j = k} How do we know that this is context-free? First, notice that L = {a i b i c k } {a i b j c j } We then combine CFGs for each side of this union (a standard trick): S T W T UV U aub ε V cv ε W XY X ax ε Y by c ε COMP 2600 Context Free Languages 5

The problem with L is that its sub-languages {a i b i c k } and {a i b j c j } have a non-empty intersection. If i = j = k in a string w, the number of occurrences of a s, b s, and c s is the same in w, and we cannot say by which rules of the grammar w was generated. Consider, for example, w = abc. a U S S T U b c V V X a X W Y b Y ε ε ε ε c Language L is inherently ambiguous: every grammar that generates L is ambiguous. COMP 2600 Context Free Languages 6

The Problem of Incomputability To verify that a grammar is unambiguous, there must be a procedure to turn an ambiguous grammar into equivalent unambiguous ones (i.e. the procedure of disambiguation). However this is an incomputable problem. There is no general algorithm that can disambiguate a given ambiguous grammar. Moreover, determining whether a grammar is ambiguous or not in the first place is also incomputable! The existing algorithms for ambiguity detection can only determine whether a given string (or a finite set of strings) is ambiguous wrt a given grammar. COMP 2600 Context Free Languages 7

Approaches for disambiguating a grammar A Question of (Non-)associativity Consider the grammar S S S int where int could be any integer and is interpreted as subtraction. This grammar is ambiguous: S S S 1 5 S 5 3 3 1 COMP 2600 Context Free Languages 8

The left tree evaluates 5 3 1 to 1. The right tree evaluates the same string to 3. We would like to be left-associative. I.e. we want 5 3 1 be parsed as the left tree and interpreted as 1. To achieve this we force the operator it associate to the left: S S int int If we wanted our grammar to be right associative, we would disambiguate the production rules as follows: S int S int Idea: Break the symmetry. COMP 2600 Context Free Languages 9

A Question of Precedence Consider the grammar S S S S + S int where is to be interpreted as multiplication and + as addition. The grammar is ambiguous: 1 + 2 3 could evaluate to 7 or 9. We want to have higher precedence than +. Thus we redefine our grammar as follows: S S + T T T T int int COMP 2600 Context Free Languages 10

Given a string 1+2 3, or 2 3+1, we have no choice but to expand to S+T first, so that (thinking bottom-up in the tree) + will be last command to be executed. Suppose we tried to derive 1 + 2 3 by first doing S T T 3. We are then stuck because we cannot substitute T with 1 + 2! As with associativity this trick consists of breaking symmetry. COMP 2600 Context Free Languages 11

Just an Example S S + T S T T T T U T /U U U (S) int This grammar provides brackets. Note that the language defined by this grammar is context-free, because of the need to keep track of bracket balancing to an arbitrary depth. COMP 2600 Context Free Languages 12