CFGs: Formal Definition

Slides:



Advertisements
Similar presentations
Parsing V: Bottom-up Parsing
Advertisements

CPSC Compiler Tutorial 4 Midterm Review. Deterministic Finite Automata (DFA) Q: finite set of states Σ: finite set of “letters” (input alphabet)
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.
About Grammars CS 130 Theory of Computation HMU Textbook: Sec 7.1, 6.3, 5.4.
Parsing — Part II (Ambiguity, Top-down parsing, Left-recursion Removal)
1 Module 31 Closure Properties for CFL’s –Kleene Closure construction examples proof of correctness –Others covered less thoroughly in lecture union, concatenation.
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 Chapter 3 Context-Free Grammars and Parsing. 2 Parsing: Syntax Analysis decides which part of the incoming token stream should be grouped together.
BİL 744 Derleyici Gerçekleştirimi (Compiler Design)1 Syntax Analyzer Syntax Analyzer creates the syntactic structure of the given source program. This.
10/13/2015IT 3271 Tow kinds of predictive parsers: Bottom-Up: The syntax tree is built up from the leaves Example: LR(1) parser Top-Down The syntax tree.
1 Context-Free Languages Not all languages are regular. L 1 = {a n b n | n  0} is not regular. L 2 = {(), (()), ((())),...} is not regular.  some properties.
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.
Context Free Grammars CIS 361. Introduction Finite Automata accept all regular languages and only regular languages Many simple languages are non regular:
Context-Free Grammars
Grammars CPSC 5135.
Languages & Grammars. Grammars  A set of rules which govern the structure of a language Fritz Fritz The dog The dog ate ate left left.
Profs. Necula CS 164 Lecture Top-Down Parsing ICOM 4036 Lecture 5.
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.
Context Free Grammars. Context Free Languages (CFL) The pumping lemma showed there are languages that are not regular –There are many classes “larger”
CFG1 CSC 4181Compiler Construction Context-Free Grammars Using grammars in parsers.
1 Efficient Parsing and Ambiguity. Ambiguity A grammar G is ambiguous if – There exists w ∊ L(G) such that – There are two parse trees for w Allows multiple.
Grammars Hopcroft, Motawi, Ullman, Chap 5. Grammars Describes underlying rules (syntax) of programming languages Compilers (parsers) are based on such.
Grammars CS 130: Theory of Computation HMU textbook, Chap 5.
Introduction Finite Automata accept all regular languages and only regular languages Even very simple languages are non regular (  = {a,b}): - {a n b.
Top-Down Parsing.
Mid-Terms Exam Scope and Introduction. Format Grades: 100 points -> 20% in the final grade Multiple Choice Questions –8 questions, 7 points each Short.
Compiler Construction Lecture Five: Parsing - Part Two CSC 2103: Compiler Construction Lecture Five: Parsing - Part Two Joyce Nakatumba-Nabende 1.
1 Context-Free Languages & Grammars (CFLs & CFGs) Reading: Chapter 5.
Comp 411 Principles of Programming Languages Lecture 3 Parsing
Context-Free Languages & Grammars (CFLs & CFGs) (part 2)
CONTEXT-FREE LANGUAGES
lec02-parserCFG May 8, 2018 Syntax Analyzer
Context-Free Languages & Grammars (CFLs & CFGs)
Chapter 4 - Parsing CSCE 343.
Context-Free Grammars: an overview
Formal Language & Automata Theory
CS 404 Introduction to Compiler Design
Programming Languages Translator
CS510 Compiler Lecture 4.
LR(k) grammars The Chinese University of Hong Kong Fall 2009
Lecture #12 Parsing Types.
Chapter 3 Context-Free Grammar and Parsing
Table-driven parsing Parsing performed by a finite state machine.
Context-Free Grammars
PARSE TREES.
CS 363 Comparative Programming Languages
4 (c) parsing.
Context-Free Languages
Context-Free Grammars
REGULAR LANGUAGES AND REGULAR GRAMMARS
Relationship to Left- and Rightmost Derivations
Syntax Analysis source program lexical analyzer tokens syntax analyzer
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
CHAPTER 2 Context-Free Languages
CSC 4181Compiler Construction Context-Free Grammars
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Relationship to Left- and Rightmost Derivations
CSC 4181 Compiler Construction Context-Free Grammars
Theory of Computation Lecture #
Chapter Fifteen: Stack Machine Applications
Context-Free Grammars
BNF 9-Apr-19.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
LR(k) grammars The Chinese University of Hong Kong Fall 2008
lec02-parserCFG May 27, 2019 Syntax Analyzer
Context-Free Grammars
COMPILER CONSTRUCTION
Lexical Analysis Uses formalism of Regular Languages
Parsing CSCI 432 Computer Science Theory
Presentation transcript:

CFGs: Formal Definition G = (V, S, P, S) V = variables a finite set S = alphabet or terminals a finite set P = productions a finite set S = start variable SV Productions’ form, where AV, a(VS)*: A  a

CFGs: Derivations Derivations in one step: bAg G bag  Aa  P  xS*, a,b,g(VS)* Can choose any variable for use for derivation step. Derivations in zero-or-more steps: G* is the reflexive and transitive closure of G . Language of a grammar: L(G) = {xS* | S G* x}

Parse Trees Sample derivations: S  A | A B A  e | a | A b | A A B  b | b c | B c | b B Sample derivations: S  AB  AAB  aAB  aaB  aabB  aabb S  AB  AbB  Abb  AAbb  Aabb  aabb These two derivations use same productions, but in different orders. S A B b a Root label = start node. Each interior label = variable. Each parent/child relation = derivation step. Each leaf label = terminal or e. All leaf labels together = derived string = yield.

Left- & Rightmost Derivations S  A | A B A  e | a | A b | A A B  b | b c | B c | b B Sample derivations: S  AB  AAB  aAB  aaB  aabB  aabb S  AB  AbB  Abb  AAbb  Aabb  aabb S A B b a These two derivations are special. 1st derivation is leftmost. Always picks leftmost variable. 2nd derivation is rightmost. Always picks rightmost variable.

Left / Rightmost Derivations In proofs… Restrict attention to left- or rightmost derivations. In parsing algorithms… E.g., recursive descent uses leftmost; yacc uses rightmost.

Derivation Trees Other derivation trees for this string? S  A | A B A  e | a | A b | A A B  b | b c | B c | b B w = aabb Other derivation trees for this string? S A B b a S A B b a S A b a e Infinitely many others possible.

Defining ambiguity of grammar, not language. CFG ambiguous  any of following equivalent statements:  string w with multiple derivation trees.  string w with multiple leftmost derivations.  string w with multiple rightmost derivations. Defining ambiguity of grammar, not language.

Ambiguity & Disambiguation Given ambiguous grammar, would like an equivalent unambiguous grammar. Allows more knowledge about structure of derivation. Simplifies inductive proofs on derivations. Can lead to more efficient parsing algorithms. In programming languages, want to impose a canonical structure on derivations. E.g., for 1+23. Strategy: Force an ordering on all derivations.

Disambiguation Example Exp  n | Exp + Exp | Exp  Exp What is an equivalent unambiguous grammar? Exp  Term | Term + Exp Term  n | n  Term Uses operator precedence left-associativity

Parsing Designations Major parsing algorithm classes are LL and LR The first letter indicates what order the input is read – L means left to right Second letter is direction in the “parsing tree” the derivation goes, L = top down, R = bottom up K of LL(k) or LR(k) is number of symbols lookahead in input during parsing Power of parsing techniques LL(k) < LR(k) LL(n) < LL(n+1), LR(n) < LR(n+1) Choice of LL or LR largely religious