Context-free grammars, derivation trees, and ambiguity

Slides:



Advertisements
Similar presentations
AST Generation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture 9.
Advertisements

Lecture # 8 Chapter # 4: Syntax Analysis. Practice Context Free Grammars a) CFG generating alternating sequence of 0’s and 1’s b) CFG in which no consecutive.
Translator Architecture Code Generator ParserTokenizer string of characters (source code) string of tokens abstract program string of integers (object.
1 Parsing The scanner recognizes words The parser recognizes syntactic units Parser operations: Check and verify syntax based on specified syntax rules.
FACS Week 14 Grammars and their properties; Introduction to Parsing Lee McCluskey, room 2/07
CS 330 Programming Languages 09 / 13 / 2007 Instructor: Michael Eckmann.
Prof. Bodik CS 164 Lecture 81 Grammars and ambiguity CS164 3:30-5:00 TT 10 Evans.
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
Lecture 9UofH - COSC Dr. Verma 1 COSC 3340: Introduction to Theory of Computation University of Houston Dr. Verma Lecture 9.
COP4020 Programming Languages
Context-free Grammars
(2.1) Grammars  Definitions  Grammars  Backus-Naur Form  Derivation – terminology – trees  Grammars and ambiguity  Simple example  Grammar hierarchies.
Lecture 13 Parsing and Ambiguity. Given a string x and a CFG G = (V, Σ, R, S), determine whether x L(G) and if x L(G), find a derivation S * x. This problem.
1 Introduction to Parsing Lecture 5. 2 Outline Regular languages revisited Parser overview Context-free grammars (CFG’s) Derivations.
BİL 744 Derleyici Gerçekleştirimi (Compiler Design)1 Syntax Analyzer Syntax Analyzer creates the syntactic structure of the given source program. This.
1 Chapter 3 Describing Syntax and Semantics. 3.1 Introduction Providing a concise yet understandable description of a programming language is difficult.
Classification of grammars Definition: A grammar G is said to be 1)Right-linear if each production in P is of the form A  xB or A  x where A and B are.
Grammars CPSC 5135.
Lecture # 9 Chap 4: Ambiguous Grammar. 2 Chomsky Hierarchy: Language Classification A grammar G is said to be – Regular if it is right linear where each.
CS 3240: Languages and Computation Context-Free Languages.
Parsing Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 3.
Parsing Lecture 5 Fri, Jan 28, Syntax Analysis The syntax of a language is described by a context-free grammar. Each grammar rule has the form A.
Context Free Grammars CFGs –Add recursion to regular expressions Nested constructions –Notation expression  identifier | number | - expression | ( expression.
Transforming Grammars. CF Grammar Terms Parse trees. – Graphical representations of derivations. – The leaves of a parse tree for a fully filled out tree.
Syntax Analyzer (Parser)
CSC312 Automata Theory Lecture # 26 Chapter # 12 by Cohen Context Free Grammars.
Context Free Grammars and Regular Grammars Needs for CFG Grammars and Production Rules Context Free Grammars (CFG) Regular Grammars (RG)
1 Topic #4: Syntactic Analysis (Parsing) CSC 338 – Compiler Design and implementation Dr. Mohamed Ben Othman ( )
Mid-Terms Exam Scope and Introduction. Format Grades: 100 points -> 20% in the final grade Multiple Choice Questions –8 questions, 7 points each Short.
WELCOME TO A JOURNEY TO CS419 Dr. Hussien Sharaf Dr. Mohammad Nassef Department of Computer Science, Faculty of Computers and Information, Cairo University.
Describing Syntax and Semantics Chapter 3: Describing Syntax and Semantics Lectures # 6.
5. Context-Free Grammars and Languages
Chapter 3: Describing Syntax and Semantics
Context-free grammars
lec02-parserCFG May 8, 2018 Syntax Analyzer
CS510 Compiler Lecture 4.
Parsing and Parser Parsing methods: top-down & bottom-up
Fall Compiler Principles Context-free Grammars Refresher
LL(1) grammars Module 07.1 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez.
Syntax Specification and Analysis
COP4620 – Programming Language Translators Dr. Manuel E. Bermudez
The chomsky hierarchy Module 03.3 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez.
Compiler Construction
Finite-state automata
Regular grammars Module 04.1 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez.
Syntax Analysis Sections :.
Compiler Design 4. Language Grammars
Programming Language Syntax 2
Top-down derivation tree generation
Top-down parsing Module 06.3 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez.
Bottom-up derivation tree, original grammar
Bottom-up derivation tree, original grammar
TaBle-driven LL(1) Parsing
5. Context-Free Grammars and Languages
Chapter 2: A Simple One Pass Compiler
TaBle-driven LL(1) Parsing
R.Rajkumar Asst.Professor CSE
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Regular Expression to NFA
Theory of Computation Lecture #
Fall Compiler Principles Context-free Grammars Refresher
BNF 9-Apr-19.
Operator precedence and AST’s
COP4620 – Programming Language Translators Dr. Manuel E. Bermudez
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Operator Precedence and Associativity
lec02-parserCFG May 27, 2019 Syntax Analyzer
Context-Free Grammars
Answer Questions about Exam2 problems
Parsing CSCI 432 Computer Science Theory
Presentation transcript:

Context-free grammars, derivation trees, and ambiguity Module 06.1 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez

Context-free Grammars Derivation Trees Ambiguity Topics Context-free Grammars Derivation Trees Ambiguity

Context-free grammars Definition: A context-free grammar (CFG) is a quadruple G = (, , P, S), where all productions are of the form A → , for A ∊  and  ∊ (∪ )*. Re-writing using grammar rules: βAγ ⇒ βγ if A →  (derivation). Left-most derivation: At each step, the left-most nonterminal is re-written. Right-most derivation: At each step, the right-most nonterminal is re-written.

Sample grammar and derivations

Derivation trees Describe re-writes, independently of the order (left-most or right-most). Each tree branch matches a production rule in the grammar. Leaves are terminals. Bottom contour is the sentence. Left recursion causes left branching. Right recursion causes right branching.

Ambiguous grammars Examine input string, determine whether it's legal. Goal of parsing: Examine input string, determine whether it's legal. Same as: try to build derivation tree. Therefore, tree should be unique. Definition: A CFG is ambiguous if there exist two different right-most (or left-most, but not both) derivations for some sentence z. (Equivalent) Definition: A CFG is ambiguous if there exist two different derivation trees for some sentence z.

Classic ambiguities Simultaneous left/right recursion: E → E + E → i Dangling else problem: S → if E then S → if E then S else S → … Ambiguity is undecidable: no algorithm exists.

summary Context-free Grammars Derivation Trees Ambiguity