Context-free grammars

Slides:



Advertisements
Similar presentations
lec02-parserCFG March 27, 2017 Syntax Analyzer
Advertisements

AST Generation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Concepts Lecture 9.
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.
SIMPLIFYING GRAMMARS Definition: A useless symbol of a context-free
Context-Free Grammars Lecture 7
Chapter 3: Formal Translation Models
COP4020 Programming Languages
Context-Free Grammars Chapter 3. 2 Context-Free Grammars and Languages n Defn A context-free grammar is a quadruple (V, , P, S), where  V is.
1 Introduction to Parsing Lecture 5. 2 Outline Regular languages revisited Parser overview Context-free grammars (CFG’s) Derivations.
Syntax Directed Definitions Synthesized Attributes
BİL 744 Derleyici Gerçekleştirimi (Compiler Design)1 Syntax Analyzer Syntax Analyzer creates the syntactic structure of the given source program. This.
LANGUAGE TRANSLATORS: WEEK 3 LECTURE: Grammar Theory Introduction to Parsing Parser - Generators TUTORIAL: Questions on grammar theory WEEKLY WORK: Read.
Normal Forms for Context-Free Grammars Definition: A symbol X in V  T is useless in a CFG G=(V, T, P, S) if there does not exist a derivation of the form.
Grammars CPSC 5135.
PART I: overview material
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.
Unit-3 Parsing Theory (Syntax Analyzer) PREPARED BY: PROF. HARISH I RATHOD COMPUTER ENGINEERING DEPARTMENT GUJARAT POWER ENGINEERING & RESEARCH INSTITUTE.
Syntax Analyzer (Parser)
1 Pertemuan 7 & 8 Syntax Analysis (Parsing) Matakuliah: T0174 / Teknik Kompilasi Tahun: 2005 Versi: 1/6.
Overview of Previous Lesson(s) Over View 3 Model of a Compiler Front End.
1 Topic #4: Syntactic Analysis (Parsing) CSC 338 – Compiler Design and implementation Dr. Mohamed Ben Othman ( )
Syntax Analysis By Noor Dhia Syntax analysis:- Syntax analysis or parsing is the most important phase of a compiler. The syntax analyzer considers.
Syntax analysis Jianguo Lu School of Computer Science University of Windsor.
Describing Syntax and Semantics Chapter 3: Describing Syntax and Semantics Lectures # 6.
COP4620 – Programming Language Translators Dr. Manuel E. Bermudez
lec02-parserCFG May 8, 2018 Syntax Analyzer
COP4620 – Programming Language Translators Dr. Manuel E. Bermudez
Introduction to Parsing
Formal Language & Automata Theory
CS510 Compiler Lecture 4.
Context-free grammars, derivation trees, and ambiguity
Fall Compiler Principles Context-free Grammars Refresher
Chapter 3 Context-Free Grammar and Parsing
Introduction to Parsing (adapted from CS 164 at Berkeley)
Grammars Module 03.2 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez.
Syntax Specification and Analysis
COP4620 – Programming Language Translators Dr. Manuel E. Bermudez
Even-Even Devise a grammar that generates strings with even number of a’s and even number of b’s.
Theory Of Automata By Dr. MM Alam
Compiler Construction
COP4620 – Programming Language Translators Dr. Manuel E. Bermudez
Top-down derivation tree generation
Regular grammars Module 04.1 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez.
Context-Free Languages
Compiler Design 4. Language Grammars
Bottom-up AST, original grammar
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
5. Context-Free Grammars and Languages
Decidablity Following are the decidable problems w.r.t. CFG
R.Rajkumar Asst.Professor CSE
Finite Automata and Formal Languages
COP4620 – Programming Language Translators Dr. Manuel E. Bermudez
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
First, Follow and Select sets
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Theory of Computation Lecture #
Fall Compiler Principles Context-free Grammars Refresher
Operator precedence and AST’s
Operator Precedence and Associativity
lec02-parserCFG May 27, 2019 Syntax Analyzer
Context Free Grammars-II
Answer Questions about Exam2 problems
COMPILER CONSTRUCTION
Presentation transcript:

Context-free grammars COP4620 – Programming Language Translators Dr. Manuel E. Bermudez

topics Context-free Grammars Derivation Trees Transduction Grammars Ambiguity Grammar Reduction.

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.

Transduction Grammars Definition: A transduction grammar (a.k.a. syntax-directed translation scheme) is like a CFG, except for the following generalization: Each production is a triple (A, β, ω)  Ф x V* x V*, called a translation rule, denoted A → β => ω, where A is the left part, β is the right part, and ω is the translation part.

Transduction Grammars Translation of infix to postfix expressions. E → E + T => E T + → T => T T → P * T => P T * → P => P P → (E) => E → i => i The translation part describes how the output is generated, as the input is derived. Derivation: ( E, E ) => ( E + T, E T + ) => ( T + T, T T + ) => ( P + T, P T + ) => ( i + T, i T + ) => ( i + P * T, i P T * + ) => ( i + i * T, i i T * + ) => ( i + i * i, i i i * + )

Transduction Grammars Derivation: (E, E) => (E+T, <+E T>) => (T+T, <+T T>) => (P+T, <+P T>) => (i+T, <+i T>) => (i+P*T, <+i<*P T>>) => (i+i*T, <+i<*i T>>) => (i+i*P, <+i<*i P>>) => (i+i*i, <+i<*i i>>) Transduction to Abstract Syntax Trees Notation: < N t1 … tn > denotes String-to-tree transduction grammar: E → E + T => < + E T > → T => T T → P * T => < * P T > → P => P P → (E) => E → i => i t1 … tn N i + *

Transduction Grammars Definition: A transduction grammar is simple if for every rule A →  => β, the sequence of nonterminals in  is identical to the sequence in β. E → E + T => < + E T > → T => T T → P * T => < * P T > → P => P P → (E) => E → i => i Notation: dispense with nonterminals and tree notation: in the translation parts, leaving: E → E + T => + → T T → P * T => * → P P → (E) → i => i Look familiar ? AST !!

Grammar ambiguity 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.

Grammar reduction What language does this grammar generate? S → a D → EDBC A → BCDEF E → CBA B → ASDFA F → S C → DDCF L(G) = {a} Problem: Many nonterminals (and productions) cannot be used in the generation of any sentence.

Grammar reduction Definition: A CFG is reduced iff for all A  Ф, a) S =>* αAβ, for some α, β  V*, (we say A is generable), b) A =>* z, for some z  Σ* (we say A is terminable). G is reduced iff every nonterminal A is both generable and terminable. Example: S → BB A → aA B → bB → a B is not terminable, since B =>* z, for any z  Σ*. A is not generable, since S =>* αAβ, for any α,βV*.

Grammar reduction To find out which nonterminals are generable: Build the graph (Ф, δ), where (A, B)  δ iff A → αBβ is a production. Check that all nodes are reachable from S. Example: S → BB A → aA B → bB → a A is not reachable from S, so A is not generable. S B A

Grammar reduction Algorithmically, while(Generable changes) do Generable := {S} while(Generable changes) do for each A → Bβ do if A  Generable then Generable := Generable U {B} od { Now, Generable contains the nonterminals that are generable }

Grammar reduction To find out which nonterminals are terminable: Build the graph (2Ф, δ), where (N, N U {A})  δ iff A → X1 … Xn is a production, and for all i, Xi  Σ or Xi  N. Check that the node Ф (set of all nonterminals) is reachable from node ø (empty set). Example: S → BB A → aA B → bB → a {A, S, B} not reachable from ø ! Only {A} is reachable from ø. Thus S and B are not terminable. {A,B} {B} {B,S} {S} ø {A} {A,S} {A,S,B}

Grammar reduction Algorithmically, while (Terminable changes) do for each A → X1…Xn do if every nonterminal among the X’s is in Terminable then Terminable := Terminable U {A} od { Now, Terminable contains the nonterminals that are terminable. }

Grammar reduction Reducing a grammar: Find all terminable nonterminals. Remove any production A→X1…Xn if any Xi is not terminable. Find all generable nonterminals. Remove any production A → X1 … Xn if A is not generable.

Grammar reduction Example: E → E + T F → not F → T Q → P / Q T → F * T P → (E) → P → i Terminable: {P, T, E}, not Terminable: {F, Q}. So, eliminate every production whose right-part contains either F or Q. E → E + T T → P → T P → (E) → i Generable: {E, T, P}, not Generable: { }. Grammar is reduced.

summary Context-free Grammars Derivation Trees Transduction Grammars Ambiguity Grammar Reduction.