Chapter 4: Context-Free Grammars

Size: px
Start display at page:

Download "Chapter 4: Context-Free Grammars"

Transcription

1 Chapter 4: Context-Free Grammars 4.1 Basics of Context-Free Grammars Definition A context-free grammars, or CFG, G is specified by a quadruple (N, Σ, P, S), where N is the nonterminal or variable alphabet; Σ is the terminal alphabet and N and Σ are disjoint; P N (N Σ) is a finite set of productions or rules; and S in N is the sentence symbol. 1

2 A production is usually written as A α where A is in N and α is in (N Σ). It is said to be an A-production of production for A, and α is the right-hand side of the production. It is convenient to speck of the vocabulary of G which is denoted by V and defined as N Σ. In examples we typically use the BNF symbol to group together all productions for a single nonterminal. 2

3 Notational Conventions Notterminals are represented by uppercase letters A, B, C,..., S, except when stated otherwise. In particular, S denotes the sentence symbol. Terminals are represented by early lowercase letters a, b,.... Late uppercase letters,..., X, Y, Z are used to mean either a nonterminal or a terminal. Terminal words are represented by late lowercase letters..., x, y, z, while lowercase Greek4 letters are used to represent arbitrary words of terminals and nonterminals. In all cases these symbols can appear with subscripts and/or superscripts. 3

4 A CFG is used to generate a language by rewriting, which we now define. Definition Let G = (N, Σ, P, S) be a CFG and β a word in V. If β can be decomposed into β 1 Aβ 2 for some A in N and for some β 1 and β 2 in V, and there is an A-production A α in P, then β can be rewritten as β 1 αβ 2 or β 1 αβ 2 is said to be derived from β. We have replaced A by α or substituted α for A. In this case we write or simply β β 1 αβ 2 in G β β 1 αβ 2 if G is understood. 4

5 Observe that for β to be rewritten it must contain at least one nonterminal, because only nonterminal have productions associated with them. However, there may be many nonterminals in β and/or many appearances of one nonterminal. In this case β can be rewritten in many different ways. Similarly, when a specific appearance of a nonterminal in β is chosen to be rewritten there may be more than one production available for it and, hence, once more, more than one way of rewriting it, In other words, rewriting is a nondeterministic action. The notion of rewriting is comparable to that of a move in an FA, and in the same way that we obtained a configuration or move sequence for FAs we obtain a rewriting or derivation sequence. 5

6 Definition Let G = (N, Σ, P, S) be a CFG and α be a word in V. If there are words α 0,..., α n 1, β 0,..., β n 1, β in V and nonterminals A 0,..., A n 1, for some n 1, such that α 0 A 0 β 0 α 1 A 1 β 1... α n 1 A n 1 β n 1 β where α = α 0 A 0 β 0, then we write α n β We say that β can be derived from α in n rewriting steps, that is, α n is an n-step derivation or, more simply, a deriavtion. Also, the sequence α 0 A 0 β 0,..., α n 1 A n 1 β n 1, β is a derivation sequence, it details how the derivation occurs, the productions that are used, and to which appearances of symbols they are applied. We write α + β if there is an n 1 and an n-step derivatiopn starting from α and leading to β. Also, we write α β if either α = β or α + β, and in the former case we may also write α 0 β, the 0-step derivation. 6

7 Definition Let G = (N, Σ, P, S) be a CFG. A word α in V is a sentential form if there exists a derivation S α. A derivation S α is called a sentential derivation. A sentential form α is terminal if α is in Σ. A terminal sentential form is also called a sentence. The language of G, denoted by L(G), is the set of all sentences, namely, L(G) = {x : S + x and x is in Σ }. A language L Σ is said to be a context-free language, or CFL, if there is a CFG, G, with L = L(G). The family of CFLs, denoted by L CF, is defined by L CF = {L : L is a CFL}. Finally, we say that two CFGs, G 1 and G 2, are equivalent if L(G 1 ) = L(G 2 ), that is, they secify the same language. 7

8 Can we really be sure we have explored all possible derivation sequences, when attempting to generate a proposed sentence. We can avoid this problem by, first, using the syntax or derivation tree and, second, using cannonical derivations. 8

9 Definition Let G = (N, Σ, P, S) be a CFG and let T be an ordered, directed tree whose nodes are labeled with either symbols from V or the empty word. Then T is a syntax tree (with respect to G) if every node u in T satisfies the following condition: If u is an external node, then it is labeled with a nonterminal symbol, a terminal symbol, or with λ, denoting the empty word, and in this latter case it is the only child of its parent, otherwise u is an internal node labeled with a nonterminal symbol A, and if it has k children, k 1, labeled with X 1,..., X k, from left to right, then the production A X 1... X k is in P. 9

10 The frontier of a syntax tree read in left to right order gives a word, the yield of the tree, denoted by y(t ). Lemma Let G(N, Σ, P, S) be a CFG and T be a syntex tree, with respect to G, having A in N as its root label and α = y(t ). Then A α in G with a derivation sequence that uses the same productions applied to the same appearances of the same symbols that appear in T. Lemma demonstrates that to every syntex tree there corresponds a derivation sequence. In other words, derivation sequences giv rise to all syntex trees; none are omitted. 10

11 Given a syntax tree any traversal of it produces a derivation sequence. However, there are two specific traversals that are of particular interest: preorder and reverse preorder traversals. These correspond, as we shall see, to the following canonical derivation sequences. Definition Let G(N, Σ, P, S) be a CFG. Let β and γ be in V such that β = β 1 Aβ 2, γ = β 1 αβ 2 and A α is in P, that is, β 1 Aβ 2 β 1 αβ 2 in G. If β 1 is in Σ we write β L γ; β is rewritten in the leftmost position to give γ. Similarly, if β 2 is in Σ we write β R γ; β is rewritten in the rightmost position to give γ. 11

12 We extend L and R to L n and R n L, + and R +, and L and R, respectively. We speck of leftmost and rightmost derivations, respectively. We will to prove that whenever there is a sentential derivation of some terminal word in a CFG, then there is always a leftmost and rightmost sentential derivation of the same terminal word. 12

13 Definition Let G(N, Σ, P, S) be a CFG and let β and γ be in V such that D : β = β 0... β n = γ is a derivation in G for some n 0. A nonterminal in γ is clearly not rewritten in D we say it is passive. A nonterminal which is rewritten in D is said to be active. To generalize the notion of a leftmost derivation we require that the derivation is leftmost with respect to the active nonterminals. 13

14 We say that D is a pre-leftmost derivation if at each derivation step the leftmost active nonterminal is rewritten. In this case we write β l n γ, β l + γ, or β l γ. Pre-rightmost derivations are defined similarly, giving rise to the notation β r n γ, β r + γ, or β r γ. A pre-leftmost derivation is a leftmost derivation if the derived word is terminal, since in this case all nonterminals are written, that is, they are all active. Lemma Let G(N, Σ, P, S) be a CFG and T be a syntex tree, with respect to G, having A in N as its root label and α = y(t ). Then A l α in G (A r α in G) with a pre-leftmost (pre-rightmost) derivation that uses the same productions applied to the same appearances of the same symbols that appear in T. 14

15 Theorem Let G(N, Σ, P, S) be a CFG. Then for all x in Σ, the following three conditions are equivalent 1. S + x in G; 2. S L + x in G; 3. S R + x in G. This theorem means that we need consider either only leftmost derivations or only rightmost derivations when generating sentences. 15

16 Definition Let G = (N, Σ, P, S) be a CFG. Let x be a sentence with two distinct syntax trees (leftmost derivations or rightmost derivations). Then x is said to be ambiguous with respect to G. G is ambiguous if there is at least one ambiguous sentence derived from S. We say a CFL, L, is (inherently) ambiguous if for all CFGs, G, with L(G) = L, G is ambiguous. Ambiguity is important, in practice, because it indicates that a sentence may have more than one meaning. In natural languages this causes difficulty in communication, and in the legal arena causes lawsuits. In programming languages ambiguity indicates the possibility that a program which is compiled with different compilers can then be excuted to give different results with the same data. 16

17 CFG AMBIGUITY INSTANCE: A CFG, G = (N, Σ, P, S). QUESTION: Is G ambiguous? UNAMBIGUOUS CFG CONSTRUCTION INSTANCE: An ambiguous CFG. G = (N, Σ, P, S). QUESTION: Can an equivalent unambiguous CFG be constructed? CFL AMBIGUITY INSTANCE: An ambigous CFG, G = (N, Σ, P, S). QUESTION: Does there exist an equivalent unambiguous CFG? 17

18 CFG MEMBERSHIP INSTANCE: A CFG, G = (N, Σ, P, S), and a word x in Σ. QUESTION: Is x in L(G)? CFG PARSING INSTANCE: A CFG, G = (N, Σ, P, S), and a word x in Σ. QUESTION: What are the syntax trees of x respect to G? 18

19 4.2 Simplications Redundant Symbols Symbols in a CFG can be redundant because either they do not appear in any sentential form or, even if they do appear in a sentential form, they are unable to derive any terminal words. In the former case such symbols are said to be unreachable and in the latter they are said to be nonterminating. Correspondingly, a symbol that appears in a sentential form is said to be reachable, and one that derives a terminal word is said to be terminating. 19

20 A CFG with neither nonterminating nor unreachable symbols is said to be reduced. We will give two algorithms, one to detect terminating symbols and the second to detect reachable symbols. These form the basis for eliminating redundant symbols from a CFG. The algorithm Terminating Symbols is based on the following idea If a nonterminal A has a production consisting solely of terminating symbols, then A is itself terminating. 20

21 In particular, if G generates a nonempty language, then there must be at least one nonterminal having a production with a terminal right-hand side. Terminating Symbols identifies these nonterminals, treats them as new terminals, and it then repeats the process until no new terminating symbols are found. marking is used to identify the new terminating symbols. 21

22 Algorithm Terminating Symbols. On entry: An arbitrary CFG, G = (N, Σ, P, S). On exit: The set T of terminating symbols. begin {A marking algorithm is used} T := Σ; Mark, in some way, all appearances of terminal symbols in P ; repeat Let Q be the set of unmarked nonterminals which have at least one production whose right-hand side consists solely of marked symbols. (Note that the right-hand side of a λ-production right-hand side of a λ-production consists solely of marked symbols); Mark all appearances in P of the symbols in Q; T := T Q until Q = ; end of Algorithm. 22

23 Algorithm Reachable Symbols. On entry: An arbitrary CFG, G = (N, Σ, P, S). On exit: The set R of reachable symbols. begin {A marking algorithm is used} R := {S}; Q := {S}; {The set of newly marked symbols} repeat Mark all symbols on the right-hand sides of all productions which have left-hand sides in Q; Let Q be the set of newly marked symbols, which are not already in R; R := R Q until Q = ; end of Algorithm. 23

24 Theorem Let G = (N, Σ, P, S) be an arbitrary CFG. Then there is an algorithm which constructs an equivalent CFG, G = (N, Σ, P, S), from G, where N N, P P, and N Σ consists solely of terminating symbols, with the possible exception of S. Theorem Let G = (N, Σ, P, S) be an arbitrary CFG. Then there is an algorithm which constructs an equivalent CFG, G = (N, Σ, P, S), from G, where N N, P P, and N Σ consists solely of reachable symbols. Theorem Let G = (N, Σ, P, S) be an arbitrary CFG. Then there is an algorithm which constructs an equivalent, reduced CFG, G = (N, Σ, P, S), where N N, Σ Σ, and P P. 24

25 4.2.2 Empty Productions An empty production (λ-production or null production) is a production whose right-hand side is the empty word, that is, a production of the form A λ Given a CFG, G = (N, Σ, P, S), if λ is in L(G), then empty productions are necessary; otherwise λ cannot be generated at all. However, when λ is not in L(G) any empty productions in G can be eliminated in such a way that L(G) is unaffected. The elimination procedure only affects productions which contain, on right-hand side, nonterminals that can derive the empty word. We call such nonterminals λ-nonterminals. 25

26 A nonterminal A is said a λ-nonterminal if there is a derivation A + λ in G. Note that λ is in L(G) if and only if S is a λ-nonterminal. If G has no λ-nonterminal it is said to be λ-free. 26

27 If G has λ-productions, then in a sentential derivation S α 1 α 2... α n in Σ The lengths of α i may vary tremendously with respect to one another. This is because many nonterminals may be introduced which only contribute the empty word to α n. However, if G has no λ-productions, then we know that S = 1 α 1 α 2... α n The lengths are monotonically increasing. This property is useful when testing whether or not a given words is generated by a CFG. If A + λ, then A α in G with α + λ. 27

28 Algorithm λ-nonterminals. On entry: An arbitrary CFG, G = (N, Σ, P, S). On exit: The set A of λ-nonterminals. begin {A marking algorithm is used} A := ; Mark the right-hand sides of all empty productions in P ; repeat Let Q be the set of unmarked nonterminals which have at least one production whose right-hand side consists solely of marked symbols. Mark all appearances in P of the symbols in Q; A := A Q until Q = ; end of Algorithm. 28

29 To eliminate λ-nonterminals from a CFG we use the following algorithm, which eliminates λ-nonterminals without introducing new ones. The strategy is based on the following idea Let A be a λ-nonterminal in G. Then conceptually we split it into two nonterminals A and A such that A generates all the nonempty terminal words that A does, and A only generates λ. Now in the right-hand side of a production in which A appears once, B αaβ say, we replace it with two productions B αa β and B αa β. Since A only generates λ we replace it by λ giving B αβ and use A in place of A. If B αaβaγ, then we obtain all four combinations of replacements, and so on. 29

30 Let G 1 and G 2 be two CFGs; we say they are λ-equivalent if they generate the same language when the empty word is ignored. In other words, G 1 and G 2 are λ-equivalent if L(G 1 ) {λ} = L(G 2 ) {λ}. Clearly, equivalence always implies λ-equivalence, but the converse implication is not valid. 30

31 Algorithm Eliminate λ-nonterminals. On entry: G = (N, Σ, P, S) is an arbitrary reduced CFG with at least one λ-nonterminal.. On exit: A reduced CFG, G = (N, Σ, P, S), λ-equivalent to G having no λ-nonterminals. begin For all λ-nonterminals A in N, For all productions B β in P with β A = m 1 Let β = β 0 Aβ 1... Aβ m where β 0... β m A = 0; Replace B β in P with 2 m productions {B β 0 X 1 β 1... X m β m : X i is in {λ, A}, 1 i m} Finally, remove all λ-productions from P giving P end of Algorithm. 31

32 Thoerem Let G = (N, Σ, P, S) be an arbitrary reduced CFG. Then there is an algorithm which constructs a reduced, λ-free CFG, G, λ-equivalent to G. In other words, for every CFL, L, there is a λ-free CFG, G with L(G) = L {λ}. 32

33 4.2.3 Unit Productions A production A α in a CFG, G = (N, Σ, P, S), is said to be a unit production if α is in N. A special case of a unit production is the production A A, a circular production. Such a production is similar to the transition (q, λ, q) in a λ-nfa, in that it can be removed immediately without affecting the language of the grammar. A CFG with no unit productions is said to be unit-free. 33

34 Algorithm Eliminate One Unit Production. On entry: G = (N, Σ, P, S) is an arbitrary reduced CFG with no circular productions and at least one unit production. On exit: G is a reduced equivalent CFG with one fewer unit production than G. begin Choose an arbitrary unit production B C in G; there must be at least one; Replace B C in P with B α, for all α in V N, satisfy C C 1... C r α, for some nonterminals C 1,..., C r. end of Algorithm. Theorem Let G = (N, Σ, P, S) be an arbitrary reduced CFG. Then there is an algorithm which constructs an equivalent, unit-free, reduced CFG, G, from G. 34

35 4.2.4 Binary Form and Chomsky Normal Form A CFG, G = (N, Σ, P, S), is said to be in binary form if for all A α in P, α 2. It is in Chomsky normal form, of CNF, if it is in binary form and there are only two kinds of productions A a; a in Σ, and A BC; B, C in N. 35

36 We prove that for every CFG, G, a λ-equivalent CFG, G, in CNF can be effectively constructed. We do this by first obtaining a binary form equivalent CFG. Letting G = (N, Σ, P, S) be a CFG we define Maxrhs(G) = max{ α : A α is in P }. Clearly, G is in binary form if and only if Maxrhs(G) 2. If Maxrhs(G) > 2, each right-hand side α of length Maxrhs(G) is split into two halves. The second half is replaced by a new nonterminal giving a shorter right-hand side. 36

37 Algorithm Reduce Largest Productions. On entry: An arbitrary reduced CFG, G = (N, Σ, P, S) with Maxrhs(G) 3. On exit: An equivalent reduced CFG, G, with Maxrhs(G ) < Maxrhs(G). begin For all productions A α in P with α = Maxrhs(G) Letting α = α 1 α 2, where α 1 = α /2 1 and α 2 = α /2 2, replace A α in P by A α 1 [Aα] and[aα] α 2, where [Aα] is a new unique nonterminal. {Then 2 α 1 [Aα] ( α + 1)/2 < α } end of Algorithm. 37

38 Theorem Let G = (N, Σ, P, S) be an arbitrary reduced CFG. Then there is an algorithm that constructs an equivalent reduced CFG, G, in binary form from G. Theorem Chomsky Normal Form Theorem Let G = (N, Σ, P, S) be an arbitrary reduced CFG. Then there is an algorithm that constructs a λ-equivalent reduced CFG, G, in CNF form from G. Thus, for every CFL, L, there exists a CFG, G, in CNF with L(G) = L {λ}. 38

39 4.2.5 Greibach Normal Form and Two-Standard Normal Form Let G = (N, Σ, P, S) be a CFG. Then G is said to be in Greibach normal form, or GNF, if each production A α in P satisfies α is in ΣN, that is, its right-hand side begins with a terminal and is followed by a, possibly empty, sequence of nonterminals. 39

40 For m 1, G is said to be in m-standard normal form, or m-snf, if each production A α in P satisfies α is in m i=0 ΣN i, that is, it is in GNF, but there are never more than m nonterminals on the right-hand sides of productions. We are particularly interested in 2-standard normal form. To obtain a Greibach normal form grammar from a given CFG we first remove the left-recursion. 40

41 Let G = (N, Σ, P, S) be a CFG. We say that G is left-recursive if there is a nonterminal A in G and a derivation A L + Aα, for some α in V. We also say that such a nonterminal is left-recursive. If there is a derivation A R + αa we say A and G are right-recursive. If A Aα is in P we say A is directly left-recursive and if A αa is in P we say A is directly right-recursive. Finally, G is non-left- (non-right-) recursive if it is not left- (right-) recursive. 41

42 Theorem Let G = (N, Σ, P, S) be a reduced, λ-free, unit-free CFG. Then there is an algorithm which constructs an equivalent non-left-recursive CFG, G, from G. In other words, for every CFL, L, there exists a non-left-recursive CFG, G, with L(G) = L {λ}. Clearly, similar result holds for non-right-recursive CFGs too. 42

43 For the construction of CFGs in GNF there is a curcial observation, as follows. Observation Let G = (N, Σ, P, S) be a reduced, non-left-recursive CFG in CNF. Then there is a nonterminal A in N whose productions all begin with a terminal, that is A only has terminal left appearances. 43

44 Given a λ-free, non-left-recursive CFG we begin to obtain an equivalent GNF grammar by carrying out the following substitution rule For each nonterminal A having only terminal left appearances, replace each occurrence of A in the leftmost position of a production with its right-hand sides. So if A α 1 α 2 α 3 D and B AC, then B α 1 C α 2 C α 3 DC. After this has been carried out either all nonterminals have only terminal left appearances or there is some nonterminal B that has a production starting with a nonterminal. 44

45 Observation Let G = (N, Σ, P, S) be a reduced, non-left-recursive CFG. Then there is a nonterminal B with B-productions having left appearances of either terminals or nonterminals which have only terminal left appearances. Observation and are both special cases of a more general result, which we prefer to prove. Observation shows the existence of nonterminals which in one leftmost derivation step produce a terminal on the left, while Observation captures those that require two leftmost derivation steps. 45

46 Definition Let G = (N, Σ, P, S) be a non-left-recursive CFG. For all A in N, define int(a) by int(a) is the largest integer k such that A L k 1 Bα L aβ, for some a in Σ, B in N, and α, β in (N Σ). Let intmax(g) = max({int(a) : A is in N}). 46

47 Lemma Let G = (N, Σ, P, S) be a reduced, non-left-recursive CFG. Then 1. int is well-defined with 1 intmax #N; 2. for all i, 1 i intmax, there is an A with int(a) = i; and 3. if A Bα is in P then int(a) > int(b), that is int provides a partial ordering of N. Theorem Greibach Normal Form Theorem Given an arbitrary CFG, G = (N, Σ, P, S), there is an algorithm that constructs a λ-equivalent CFG, G, in CNF from it. In other words, for every CFL, L, there exists a CFG, G, in CNF with L(G) = L {λ}. 47

48 Algorithm GNF Conversion. On entry: A reduced, λ-free, non-left-recursive CFG, G = (N, Σ, P, S). On exit: A reduced, equivalent CFG, G = (N, Σ, P, S) in Greibach normal form. begin T := {A : A is in N and int(a) = 1}; P := {A α : A α is in P and int(a) = 1}; P := P P ; for i = 2 to intmax do begin for all A in T do replace all left appearances of A in P by the right-hand sides of all A-productions in P ; T := {A : A is in N and int(a) = i}; P := P {A α : A α is in P and int(a) = i}; P := P P end; end of Algorithm. 48

49 Lemma The Nonterminal Substitution Lemma Let G = (N, Σ, P, S) be an arbitrary CFG, A α 1 Bα 2 be a production in P, and B β 1... β r be the B-productions in P. Let G = (N, Σ, P, S) be a new CFG in which P = P except that A α 1 Bα 2 is replaced by A α 1 β 1 α 2... α 1 β r α 2. Then L(G ) = L(G). 49

50 It is possible to prove a stronger version of the GNF theorem, namely, it is sufficient to allow only productions of the following types 1. A α; 2. A αb; 3. A αbc; where B and C are not necessarily distinct from A or each other. In other words, 2-SNF is sufficient to capture all CFLs. 50

51 Theorem Greibach 2-Standard Normal Form Theorem Let G be an arbitrary reduced CFG. Then there is an algorithm that constructs a reduced λ-equivalent CFG, G, in 2-SNF from G. In other words, for every CFL, L, there exists a 2-SNF CFG, G, with L(G) = L {λ}. 51

52 4.3 Linear and Regular Grammars We show that 1-standard normal form grammars generate L DF A and, hence, L REG. Theorem For every reduced CFG, G, in 1-SNF there exists an NFA, M, with L(M) {λ} = L(G), and conversely. 1-standard normal form grammars are sometimes called regular grammars, or REGG. 52

53 Let G = (N, Σ, P, S) be a reduced CFG; then it is said to be linear, or a LING, if all productions in P are either of forma x or A xby, for some terminal words x and y. G is said to be right linear, or a RLING, if its productions are either of the form A x or A xb. Left linear CFGs, or LLINGs, are defined similarly. Clearly, a CFG in 1-SNF is a RLING and a LING. A CFL, L, is said to be a linear language or LINL if L = L(G), for some LING, G. 53

54 Linear grammars, on the other hand, are more powerful than FAs and less powerful than unrestricted CFGs. The family of linear languages is denoted by L LIN, and defined by L LIN = {L : L = L(G) for some LING, G}, has attained a central place in the theory of CFLs. 4.4 Extended Context-Free Grammars 54

Parsing. Context-Free Grammars (CFG) Laura Kallmeyer. Winter 2017/18. Heinrich-Heine-Universität Düsseldorf 1 / 26

Parsing. Context-Free Grammars (CFG) Laura Kallmeyer. Winter 2017/18. Heinrich-Heine-Universität Düsseldorf 1 / 26 Parsing Context-Free Grammars (CFG) Laura Kallmeyer Heinrich-Heine-Universität Düsseldorf Winter 2017/18 1 / 26 Table of contents 1 Context-Free Grammars 2 Simplifying CFGs Removing useless symbols Eliminating

More information

Foundations of Informatics: a Bridging Course

Foundations of Informatics: a Bridging Course Foundations of Informatics: a Bridging Course Week 3: Formal Languages and Semantics Thomas Noll Lehrstuhl für Informatik 2 RWTH Aachen University noll@cs.rwth-aachen.de http://www.b-it-center.de/wob/en/view/class211_id948.html

More information

THEORY OF COMPUTATION (AUBER) EXAM CRIB SHEET

THEORY OF COMPUTATION (AUBER) EXAM CRIB SHEET THEORY OF COMPUTATION (AUBER) EXAM CRIB SHEET Regular Languages and FA A language is a set of strings over a finite alphabet Σ. All languages are finite or countably infinite. The set of all languages

More information

Languages. Languages. An Example Grammar. Grammars. Suppose we have an alphabet V. Then we can write:

Languages. Languages. An Example Grammar. Grammars. Suppose we have an alphabet V. Then we can write: Languages A language is a set (usually infinite) of strings, also known as sentences Each string consists of a sequence of symbols taken from some alphabet An alphabet, V, is a finite set of symbols, e.g.

More information

Introduction to Theory of Computing

Introduction to Theory of Computing CSCI 2670, Fall 2012 Introduction to Theory of Computing Department of Computer Science University of Georgia Athens, GA 30602 Instructor: Liming Cai www.cs.uga.edu/ cai 0 Lecture Note 3 Context-Free Languages

More information

Lecture 12 Simplification of Context-Free Grammars and Normal Forms

Lecture 12 Simplification of Context-Free Grammars and Normal Forms Lecture 12 Simplification of Context-Free Grammars and Normal Forms COT 4420 Theory of Computation Chapter 6 Normal Forms for CFGs 1. Chomsky Normal Form CNF Productions of form A BC A, B, C V A a a T

More information

Context-free Grammars and Languages

Context-free Grammars and Languages Context-free Grammars and Languages COMP 455 002, Spring 2019 Jim Anderson (modified by Nathan Otterness) 1 Context-free Grammars Context-free grammars provide another way to specify languages. Example:

More information

This lecture covers Chapter 5 of HMU: Context-free Grammars

This lecture covers Chapter 5 of HMU: Context-free Grammars This lecture covers Chapter 5 of HMU: Context-free rammars (Context-free) rammars (Leftmost and Rightmost) Derivations Parse Trees An quivalence between Derivations and Parse Trees Ambiguity in rammars

More information

Context Free Grammars: Introduction

Context Free Grammars: Introduction Context Free Grammars: Introduction Context free grammar (CFG) G = (V, Σ, R, S), where V is a set of grammar symbols Σ V is a set of terminal symbols R is a set of rules, where R (V Σ) V S is a distinguished

More information

Einführung in die Computerlinguistik

Einführung in die Computerlinguistik Einführung in die Computerlinguistik Context-Free Grammars (CFG) Laura Kallmeyer Heinrich-Heine-Universität Düsseldorf Summer 2016 1 / 22 CFG (1) Example: Grammar G telescope : Productions: S NP VP NP

More information

Context-Free Grammar

Context-Free Grammar Context-Free Grammar CFGs are more powerful than regular expressions. They are more powerful in the sense that whatever can be expressed using regular expressions can be expressed using context-free grammars,

More information

Context Free Grammars

Context Free Grammars Automata and Formal Languages Context Free Grammars Sipser pages 101-111 Lecture 11 Tim Sheard 1 Formal Languages 1. Context free languages provide a convenient notation for recursive description of languages.

More information

CS 373: Theory of Computation. Fall 2010

CS 373: Theory of Computation. Fall 2010 CS 373: Theory of Computation Gul Agha Mahesh Viswanathan Fall 2010 1 1 Normal Forms for CFG Normal Forms for Grammars It is typically easier to work with a context free language if given a CFG in a normal

More information

Context-Free Grammars and Languages. We have seen that many languages cannot be regular. Thus we need to consider larger classes of langs.

Context-Free Grammars and Languages. We have seen that many languages cannot be regular. Thus we need to consider larger classes of langs. Context-Free Grammars and Languages We have seen that many languages cannot be regular. Thus we need to consider larger classes of langs. Contex-Free Languages (CFL s) played a central role natural languages

More information

Einführung in die Computerlinguistik

Einführung in die Computerlinguistik Einführung in die Computerlinguistik Context-Free Grammars formal properties Laura Kallmeyer Heinrich-Heine-Universität Düsseldorf Summer 2018 1 / 20 Normal forms (1) Hopcroft and Ullman (1979) A normal

More information

Context Free Languages and Grammars

Context Free Languages and Grammars Algorithms & Models of Computation CS/ECE 374, Fall 2017 Context Free Languages and Grammars Lecture 7 Tuesday, September 19, 2017 Sariel Har-Peled (UIUC) CS374 1 Fall 2017 1 / 36 What stack got to do

More information

Context Free Grammars: Introduction. Context Free Grammars: Simplifying CFGs

Context Free Grammars: Introduction. Context Free Grammars: Simplifying CFGs Context Free Grammars: Introduction CFGs are more powerful than RGs because of the following 2 properties: 1. Recursion Rule is recursive if it is of the form X w 1 Y w 2, where Y w 3 Xw 4 and w 1, w 2,

More information

60-354, Theory of Computation Fall Asish Mukhopadhyay School of Computer Science University of Windsor

60-354, Theory of Computation Fall Asish Mukhopadhyay School of Computer Science University of Windsor 60-354, Theory of Computation Fall 2013 Asish Mukhopadhyay School of Computer Science University of Windsor Pushdown Automata (PDA) PDA = ε-nfa + stack Acceptance ε-nfa enters a final state or Stack is

More information

This lecture covers Chapter 7 of HMU: Properties of CFLs

This lecture covers Chapter 7 of HMU: Properties of CFLs This lecture covers Chapter 7 of HMU: Properties of CFLs Chomsky Normal Form Pumping Lemma for CFs Closure Properties of CFLs Decision Properties of CFLs Additional Reading: Chapter 7 of HMU. Chomsky Normal

More information

Syntactical analysis. Syntactical analysis. Syntactical analysis. Syntactical analysis

Syntactical analysis. Syntactical analysis. Syntactical analysis. Syntactical analysis Context-free grammars Derivations Parse Trees Left-recursive grammars Top-down parsing non-recursive predictive parsers construction of parse tables Bottom-up parsing shift/reduce parsers LR parsers GLR

More information

CS375: Logic and Theory of Computing

CS375: Logic and Theory of Computing CS375: Logic and Theory of Computing Fuhua (Frank) Cheng Department of Computer Science University of Kentucky 1 Table of Contents: Week 1: Preliminaries (set algebra, relations, functions) (read Chapters

More information

Functions on languages:

Functions on languages: MA/CSSE 474 Final Exam Notation and Formulas page Name (turn this in with your exam) Unless specified otherwise, r,s,t,u,v,w,x,y,z are strings over alphabet Σ; while a, b, c, d are individual alphabet

More information

TAFL 1 (ECS-403) Unit- III. 3.1 Definition of CFG (Context Free Grammar) and problems. 3.2 Derivation. 3.3 Ambiguity in Grammar

TAFL 1 (ECS-403) Unit- III. 3.1 Definition of CFG (Context Free Grammar) and problems. 3.2 Derivation. 3.3 Ambiguity in Grammar TAFL 1 (ECS-403) Unit- III 3.1 Definition of CFG (Context Free Grammar) and problems 3.2 Derivation 3.3 Ambiguity in Grammar 3.3.1 Inherent Ambiguity 3.3.2 Ambiguous to Unambiguous CFG 3.4 Simplification

More information

Computational Models - Lecture 5 1

Computational Models - Lecture 5 1 Computational Models - Lecture 5 1 Handout Mode Iftach Haitner. Tel Aviv University. November 28, 2016 1 Based on frames by Benny Chor, Tel Aviv University, modifying frames by Maurice Herlihy, Brown University.

More information

Chapter 6. Properties of Regular Languages

Chapter 6. Properties of Regular Languages Chapter 6 Properties of Regular Languages Regular Sets and Languages Claim(1). The family of languages accepted by FSAs consists of precisely the regular sets over a given alphabet. Every regular set is

More information

Computational Models - Lecture 4

Computational Models - Lecture 4 Computational Models - Lecture 4 Regular languages: The Myhill-Nerode Theorem Context-free Grammars Chomsky Normal Form Pumping Lemma for context free languages Non context-free languages: Examples Push

More information

FORMAL LANGUAGES, AUTOMATA AND COMPUTATION

FORMAL LANGUAGES, AUTOMATA AND COMPUTATION FORMAL LANGUAGES, AUTOMATA AND COMPUTATION DECIDABILITY ( LECTURE 15) SLIDES FOR 15-453 SPRING 2011 1 / 34 TURING MACHINES-SYNOPSIS The most general model of computation Computations of a TM are described

More information

SYLLABUS. Introduction to Finite Automata, Central Concepts of Automata Theory. CHAPTER - 3 : REGULAR EXPRESSIONS AND LANGUAGES

SYLLABUS. Introduction to Finite Automata, Central Concepts of Automata Theory. CHAPTER - 3 : REGULAR EXPRESSIONS AND LANGUAGES Contents i SYLLABUS UNIT - I CHAPTER - 1 : AUT UTOMA OMATA Introduction to Finite Automata, Central Concepts of Automata Theory. CHAPTER - 2 : FINITE AUT UTOMA OMATA An Informal Picture of Finite Automata,

More information

Properties of Context-free Languages. Reading: Chapter 7

Properties of Context-free Languages. Reading: Chapter 7 Properties of Context-free Languages Reading: Chapter 7 1 Topics 1) Simplifying CFGs, Normal forms 2) Pumping lemma for CFLs 3) Closure and decision properties of CFLs 2 How to simplify CFGs? 3 Three ways

More information

Pushdown Automata: Introduction (2)

Pushdown Automata: Introduction (2) Pushdown Automata: Introduction Pushdown automaton (PDA) M = (K, Σ, Γ,, s, A) where K is a set of states Σ is an input alphabet Γ is a set of stack symbols s K is the start state A K is a set of accepting

More information

CPS 220 Theory of Computation

CPS 220 Theory of Computation CPS 22 Theory of Computation Review - Regular Languages RL - a simple class of languages that can be represented in two ways: 1 Machine description: Finite Automata are machines with a finite number of

More information

UNIT-VI PUSHDOWN AUTOMATA

UNIT-VI PUSHDOWN AUTOMATA Syllabus R09 Regulation UNIT-VI PUSHDOWN AUTOMATA The context free languages have a type of automaton that defined them. This automaton, called a pushdown automaton, is an extension of the nondeterministic

More information

5 Context-Free Languages

5 Context-Free Languages CA320: COMPUTABILITY AND COMPLEXITY 1 5 Context-Free Languages 5.1 Context-Free Grammars Context-Free Grammars Context-free languages are specified with a context-free grammar (CFG). Formally, a CFG G

More information

Definition: A grammar G = (V, T, P,S) is a context free grammar (cfg) if all productions in P have the form A x where

Definition: A grammar G = (V, T, P,S) is a context free grammar (cfg) if all productions in P have the form A x where Recitation 11 Notes Context Free Grammars Definition: A grammar G = (V, T, P,S) is a context free grammar (cfg) if all productions in P have the form A x A V, and x (V T)*. Examples Problem 1. Given the

More information

Grammars and Context Free Languages

Grammars and Context Free Languages Grammars and Context Free Languages H. Geuvers and J. Rot Institute for Computing and Information Sciences Version: fall 2016 H. Geuvers & J. Rot Version: fall 2016 Talen en Automaten 1 / 24 Outline Grammars

More information

MA/CSSE 474 Theory of Computation

MA/CSSE 474 Theory of Computation MA/CSSE 474 Theory of Computation CFL Hierarchy CFL Decision Problems Your Questions? Previous class days' material Reading Assignments HW 12 or 13 problems Anything else I have included some slides online

More information

Properties of context-free Languages

Properties of context-free Languages Properties of context-free Languages We simplify CFL s. Greibach Normal Form Chomsky Normal Form We prove pumping lemma for CFL s. We study closure properties and decision properties. Some of them remain,

More information

Non-context-Free Languages. CS215, Lecture 5 c

Non-context-Free Languages. CS215, Lecture 5 c Non-context-Free Languages CS215, Lecture 5 c 2007 1 The Pumping Lemma Theorem. (Pumping Lemma) Let be context-free. There exists a positive integer divided into five pieces, Proof for for each, and..

More information

Context-Free Grammars: Normal Forms

Context-Free Grammars: Normal Forms Context-Free Grammars: Normal Forms Seungjin Choi Department of Computer Science and Engineering Pohang University of Science and Technology 77 Cheongam-ro, Nam-gu, Pohang 37673, Korea seungjin@postech.ac.kr

More information

(NB. Pages are intended for those who need repeated study in formal languages) Length of a string. Formal languages. Substrings: Prefix, suffix.

(NB. Pages are intended for those who need repeated study in formal languages) Length of a string. Formal languages. Substrings: Prefix, suffix. (NB. Pages 22-40 are intended for those who need repeated study in formal languages) Length of a string Number of symbols in the string. Formal languages Basic concepts for symbols, strings and languages:

More information

ECS 120: Theory of Computation UC Davis Phillip Rogaway February 16, Midterm Exam

ECS 120: Theory of Computation UC Davis Phillip Rogaway February 16, Midterm Exam ECS 120: Theory of Computation Handout MT UC Davis Phillip Rogaway February 16, 2012 Midterm Exam Instructions: The exam has six pages, including this cover page, printed out two-sided (no more wasted

More information

Einführung in die Computerlinguistik Kontextfreie Grammatiken - Formale Eigenschaften

Einführung in die Computerlinguistik Kontextfreie Grammatiken - Formale Eigenschaften Normal forms (1) Einführung in die Computerlinguistik Kontextfreie Grammatiken - Formale Eigenschaften Laura Heinrich-Heine-Universität Düsseldorf Sommersemester 2013 normal form of a grammar formalism

More information

FLAC Context-Free Grammars

FLAC Context-Free Grammars FLAC Context-Free Grammars Klaus Sutner Carnegie Mellon Universality Fall 2017 1 Generating Languages Properties of CFLs Generation vs. Recognition 3 Turing machines can be used to check membership in

More information

Context Free Languages (CFL) Language Recognizer A device that accepts valid strings. The FA are formalized types of language recognizer.

Context Free Languages (CFL) Language Recognizer A device that accepts valid strings. The FA are formalized types of language recognizer. Context Free Languages (CFL) Language Recognizer A device that accepts valid strings. The FA are formalized types of language recognizer. Language Generator: Context free grammars are language generators,

More information

Grammars and Context Free Languages

Grammars and Context Free Languages Grammars and Context Free Languages H. Geuvers and A. Kissinger Institute for Computing and Information Sciences Version: fall 2015 H. Geuvers & A. Kissinger Version: fall 2015 Talen en Automaten 1 / 23

More information

Context-Free Grammars and Languages. Reading: Chapter 5

Context-Free Grammars and Languages. Reading: Chapter 5 Context-Free Grammars and Languages Reading: Chapter 5 1 Context-Free Languages The class of context-free languages generalizes the class of regular languages, i.e., every regular language is a context-free

More information

Context-Free Grammars and Languages

Context-Free Grammars and Languages Context-Free Grammars and Languages Seungjin Choi Department of Computer Science and Engineering Pohang University of Science and Technology 77 Cheongam-ro, Nam-gu, Pohang 37673, Korea seungjin@postech.ac.kr

More information

Computational Models - Lecture 4 1

Computational Models - Lecture 4 1 Computational Models - Lecture 4 1 Handout Mode Iftach Haitner and Yishay Mansour. Tel Aviv University. April 3/8, 2013 1 Based on frames by Benny Chor, Tel Aviv University, modifying frames by Maurice

More information

Plan for 2 nd half. Just when you thought it was safe. Just when you thought it was safe. Theory Hall of Fame. Chomsky Normal Form

Plan for 2 nd half. Just when you thought it was safe. Just when you thought it was safe. Theory Hall of Fame. Chomsky Normal Form Plan for 2 nd half Pumping Lemma for CFLs The Return of the Pumping Lemma Just when you thought it was safe Return of the Pumping Lemma Recall: With Regular Languages The Pumping Lemma showed that if a

More information

The View Over The Horizon

The View Over The Horizon The View Over The Horizon enumerable decidable context free regular Context-Free Grammars An example of a context free grammar, G 1 : A 0A1 A B B # Terminology: Each line is a substitution rule or production.

More information

CISC4090: Theory of Computation

CISC4090: Theory of Computation CISC4090: Theory of Computation Chapter 2 Context-Free Languages Courtesy of Prof. Arthur G. Werschulz Fordham University Department of Computer and Information Sciences Spring, 2014 Overview In Chapter

More information

Theory of Computation - Module 3

Theory of Computation - Module 3 Theory of Computation - Module 3 Syllabus Context Free Grammar Simplification of CFG- Normal forms-chomsky Normal form and Greibach Normal formpumping lemma for Context free languages- Applications of

More information

Lecture 11 Context-Free Languages

Lecture 11 Context-Free Languages Lecture 11 Context-Free Languages COT 4420 Theory of Computation Chapter 5 Context-Free Languages n { a b : n n { ww } 0} R Regular Languages a *b* ( a + b) * Example 1 G = ({S}, {a, b}, S, P) Derivations:

More information

Section 1 (closed-book) Total points 30

Section 1 (closed-book) Total points 30 CS 454 Theory of Computation Fall 2011 Section 1 (closed-book) Total points 30 1. Which of the following are true? (a) a PDA can always be converted to an equivalent PDA that at each step pops or pushes

More information

CS481F01 Prelim 2 Solutions

CS481F01 Prelim 2 Solutions CS481F01 Prelim 2 Solutions A. Demers 7 Nov 2001 1 (30 pts = 4 pts each part + 2 free points). For this question we use the following notation: x y means x is a prefix of y m k n means m n k For each of

More information

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018 Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018 Lecture 14 Ana Bove May 14th 2018 Recap: Context-free Grammars Simplification of grammars: Elimination of ǫ-productions; Elimination of

More information

Chap. 7 Properties of Context-free Languages

Chap. 7 Properties of Context-free Languages Chap. 7 Properties of Context-free Languages 7.1 Normal Forms for Context-free Grammars Context-free grammars A where A N, (N T). 0. Chomsky Normal Form A BC or A a except S where A, B, C N, a T. 1. Eliminating

More information

Harvard CS 121 and CSCI E-207 Lecture 12: General Context-Free Recognition

Harvard CS 121 and CSCI E-207 Lecture 12: General Context-Free Recognition Harvard CS 121 and CSCI E-207 Lecture 12: General Context-Free Recognition Salil Vadhan October 11, 2012 Reading: Sipser, Section 2.3 and Section 2.1 (material on Chomsky Normal Form). Pumping Lemma for

More information

Simplification and Normalization of Context-Free Grammars

Simplification and Normalization of Context-Free Grammars implification and Normalization, of Context-Free Grammars 20100927 Slide 1 of 23 Simplification and Normalization of Context-Free Grammars 5DV037 Fundamentals of Computer Science Umeå University Department

More information

An Efficient Context-Free Parsing Algorithm. Speakers: Morad Ankri Yaniv Elia

An Efficient Context-Free Parsing Algorithm. Speakers: Morad Ankri Yaniv Elia An Efficient Context-Free Parsing Algorithm Speakers: Morad Ankri Yaniv Elia Yaniv: Introduction Terminology Informal Explanation The Recognizer Morad: Example Time and Space Bounds Empirical results Practical

More information

Even More on Dynamic Programming

Even More on Dynamic Programming Algorithms & Models of Computation CS/ECE 374, Fall 2017 Even More on Dynamic Programming Lecture 15 Thursday, October 19, 2017 Sariel Har-Peled (UIUC) CS374 1 Fall 2017 1 / 26 Part I Longest Common Subsequence

More information

Context-Free Grammars. 2IT70 Finite Automata and Process Theory

Context-Free Grammars. 2IT70 Finite Automata and Process Theory Context-Free Grammars 2IT70 Finite Automata and Process Theory Technische Universiteit Eindhoven May 18, 2016 Generating strings language L 1 = {a n b n n > 0} ab L 1 if w L 1 then awb L 1 production rules

More information

Chapter 2: Finite Automata

Chapter 2: Finite Automata Chapter 2: Finite Automata 2.1 States, State Diagrams, and Transitions Finite automaton is the simplest acceptor or recognizer for language specification. It is also the simplest model of a computer. A

More information

CS5371 Theory of Computation. Lecture 7: Automata Theory V (CFG, CFL, CNF)

CS5371 Theory of Computation. Lecture 7: Automata Theory V (CFG, CFL, CNF) CS5371 Theory of Computation Lecture 7: Automata Theory V (CFG, CFL, CNF) Announcement Homework 2 will be given soon (before Tue) Due date: Oct 31 (Tue), before class Midterm: Nov 3, (Fri), first hour

More information

Context-Free Grammars (and Languages) Lecture 7

Context-Free Grammars (and Languages) Lecture 7 Context-Free Grammars (and Languages) Lecture 7 1 Today Beyond regular expressions: Context-Free Grammars (CFGs) What is a CFG? What is the language associated with a CFG? Creating CFGs. Reasoning about

More information

Finite Automata and Formal Languages TMV026/DIT321 LP Useful, Useless, Generating and Reachable Symbols

Finite Automata and Formal Languages TMV026/DIT321 LP Useful, Useless, Generating and Reachable Symbols Finite Automata and Formal Languages TMV026/DIT321 LP4 2012 Lecture 13 Ana Bove May 7th 2012 Overview of today s lecture: Normal Forms for Context-Free Languages Pumping Lemma for Context-Free Languages

More information

6.1 The Pumping Lemma for CFLs 6.2 Intersections and Complements of CFLs

6.1 The Pumping Lemma for CFLs 6.2 Intersections and Complements of CFLs CSC4510/6510 AUTOMATA 6.1 The Pumping Lemma for CFLs 6.2 Intersections and Complements of CFLs The Pumping Lemma for Context Free Languages One way to prove AnBn is not regular is to use the pumping lemma

More information

Syntactic Analysis. Top-Down Parsing

Syntactic Analysis. Top-Down Parsing Syntactic Analysis Top-Down Parsing Copyright 2015, Pedro C. Diniz, all rights reserved. Students enrolled in Compilers class at University of Southern California (USC) have explicit permission to make

More information

Pushdown Automata. Reading: Chapter 6

Pushdown Automata. Reading: Chapter 6 Pushdown Automata Reading: Chapter 6 1 Pushdown Automata (PDA) Informally: A PDA is an NFA-ε with a infinite stack. Transitions are modified to accommodate stack operations. Questions: What is a stack?

More information

CPSC 421: Tutorial #1

CPSC 421: Tutorial #1 CPSC 421: Tutorial #1 October 14, 2016 Set Theory. 1. Let A be an arbitrary set, and let B = {x A : x / x}. That is, B contains all sets in A that do not contain themselves: For all y, ( ) y B if and only

More information

Chomsky and Greibach Normal Forms

Chomsky and Greibach Normal Forms Chomsky and Greibach Normal Forms Teodor Rus rus@cs.uiowa.edu The University of Iowa, Department of Computer Science Computation Theory p.1/25 Simplifying a CFG It is often convenient to simplify CFG One

More information

St.MARTIN S ENGINEERING COLLEGE Dhulapally, Secunderabad

St.MARTIN S ENGINEERING COLLEGE Dhulapally, Secunderabad St.MARTIN S ENGINEERING COLLEGE Dhulapally, Secunderabad-500 014 Subject: FORMAL LANGUAGES AND AUTOMATA THEORY Class : CSE II PART A (SHORT ANSWER QUESTIONS) UNIT- I 1 Explain transition diagram, transition

More information

Pushdown Automata. Notes on Automata and Theory of Computation. Chia-Ping Chen

Pushdown Automata. Notes on Automata and Theory of Computation. Chia-Ping Chen Pushdown Automata Notes on Automata and Theory of Computation Chia-Ping Chen Department of Computer Science and Engineering National Sun Yat-Sen University Kaohsiung, Taiwan ROC Pushdown Automata p. 1

More information

Theory of Computation

Theory of Computation Thomas Zeugmann Hokkaido University Laboratory for Algorithmics http://www-alg.ist.hokudai.ac.jp/ thomas/toc/ Lecture 3: Finite State Automata Motivation In the previous lecture we learned how to formalize

More information

Theory Of Computation UNIT-II

Theory Of Computation UNIT-II Regular Expressions and Context Free Grammars: Regular expression formalism- equivalence with finite automata-regular sets and closure properties- pumping lemma for regular languages- decision algorithms

More information

CSE302: Compiler Design

CSE302: Compiler Design CSE302: Compiler Design Instructor: Dr. Liang Cheng Department of Computer Science and Engineering P.C. Rossin College of Engineering & Applied Science Lehigh University February 27, 2007 Outline Recap

More information

CDM Parsing and Decidability

CDM Parsing and Decidability CDM Parsing and Decidability 1 Parsing Klaus Sutner Carnegie Mellon Universality 65-parsing 2017/12/15 23:17 CFGs and Decidability Pushdown Automata The Recognition Problem 3 What Could Go Wrong? 4 Problem:

More information

CSC 4181Compiler Construction. Context-Free Grammars Using grammars in parsers. Parsing Process. Context-Free Grammar

CSC 4181Compiler Construction. Context-Free Grammars Using grammars in parsers. Parsing Process. Context-Free Grammar CSC 4181Compiler Construction Context-ree Grammars Using grammars in parsers CG 1 Parsing Process Call the scanner to get tokens Build a parse tree from the stream of tokens A parse tree shows the syntactic

More information

Chomsky Normal Form for Context-Free Gramars

Chomsky Normal Form for Context-Free Gramars Chomsky Normal Form for Context-Free Gramars Deepak D Souza Department of Computer Science and Automation Indian Institute of Science, Bangalore. 17 September 2014 Outline 1 CNF 2 Converting to CNF 3 Correctness

More information

Decidability (What, stuff is unsolvable?)

Decidability (What, stuff is unsolvable?) University of Georgia Fall 2014 Outline Decidability Decidable Problems for Regular Languages Decidable Problems for Context Free Languages The Halting Problem Countable and Uncountable Sets Diagonalization

More information

Computational Models - Lecture 3

Computational Models - Lecture 3 Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. p. 1 Computational Models - Lecture 3 Equivalence of regular expressions and regular languages (lukewarm leftover

More information

5/10/16. Grammar. Automata and Languages. Today s Topics. Grammars Definition A grammar G is defined as G = (V, T, P, S) where:

5/10/16. Grammar. Automata and Languages. Today s Topics. Grammars Definition A grammar G is defined as G = (V, T, P, S) where: Grammar Automata and Languages Grammar Prof. Mohamed Hamada oftware Engineering Lab. The University of Aizu Japan Regular Grammar Context-free Grammar Context-sensitive Grammar Left-linear Grammar right-linear

More information

Context-free grammars and languages

Context-free grammars and languages Context-free grammars and languages The next class of languages we will study in the course is the class of context-free languages. They are defined by the notion of a context-free grammar, or a CFG for

More information

Introduction to Formal Languages, Automata and Computability p.1/42

Introduction to Formal Languages, Automata and Computability p.1/42 Introduction to Formal Languages, Automata and Computability Pushdown Automata K. Krithivasan and R. Rama Introduction to Formal Languages, Automata and Computability p.1/42 Introduction We have considered

More information

Grammar formalisms Tree Adjoining Grammar: Formal Properties, Parsing. Part I. Formal Properties of TAG. Outline: Formal Properties of TAG

Grammar formalisms Tree Adjoining Grammar: Formal Properties, Parsing. Part I. Formal Properties of TAG. Outline: Formal Properties of TAG Grammar formalisms Tree Adjoining Grammar: Formal Properties, Parsing Laura Kallmeyer, Timm Lichte, Wolfgang Maier Universität Tübingen Part I Formal Properties of TAG 16.05.2007 und 21.05.2007 TAG Parsing

More information

COMP-330 Theory of Computation. Fall Prof. Claude Crépeau. Lec. 10 : Context-Free Grammars

COMP-330 Theory of Computation. Fall Prof. Claude Crépeau. Lec. 10 : Context-Free Grammars COMP-330 Theory of Computation Fall 2017 -- Prof. Claude Crépeau Lec. 10 : Context-Free Grammars COMP 330 Fall 2017: Lectures Schedule 1-2. Introduction 1.5. Some basic mathematics 2-3. Deterministic finite

More information

Notes for Comp 497 (Comp 454) Week 10 4/5/05

Notes for Comp 497 (Comp 454) Week 10 4/5/05 Notes for Comp 497 (Comp 454) Week 10 4/5/05 Today look at the last two chapters in Part II. Cohen presents some results concerning context-free languages (CFL) and regular languages (RL) also some decidability

More information

Suppose h maps number and variables to ɛ, and opening parenthesis to 0 and closing parenthesis

Suppose h maps number and variables to ɛ, and opening parenthesis to 0 and closing parenthesis 1 Introduction Parenthesis Matching Problem Describe the set of arithmetic expressions with correctly matched parenthesis. Arithmetic expressions with correctly matched parenthesis cannot be described

More information

Introduction and Motivation. Introduction and Motivation. Introduction to Computability. Introduction and Motivation. Theory. Lecture5: Context Free

Introduction and Motivation. Introduction and Motivation. Introduction to Computability. Introduction and Motivation. Theory. Lecture5: Context Free ntroduction to Computability Theory Lecture5: Context Free Languages Prof. Amos sraeli 1 ntroduction and Motivation n our study of RL-s we Covered: 1. Motivation and definition of regular languages. 2.

More information

Left-Forbidding Cooperating Distributed Grammar Systems

Left-Forbidding Cooperating Distributed Grammar Systems Left-Forbidding Cooperating Distributed Grammar Systems Filip Goldefus a, Tomáš Masopust b,, Alexander Meduna a a Faculty of Information Technology, Brno University of Technology Božetěchova 2, Brno 61266,

More information

This lecture covers Chapter 6 of HMU: Pushdown Automata

This lecture covers Chapter 6 of HMU: Pushdown Automata This lecture covers Chapter 6 of HMU: ushdown Automata ushdown Automata (DA) Language accepted by a DA Equivalence of CFs and the languages accepted by DAs Deterministic DAs Additional Reading: Chapter

More information

Blackhole Pushdown Automata

Blackhole Pushdown Automata Fundamenta Informaticae XXI (2001) 1001 1020 1001 IOS Press Blackhole Pushdown Automata Erzsébet Csuhaj-Varjú Computer and Automation Research Institute, Hungarian Academy of Sciences Kende u. 13 17, 1111

More information

DD2371 Automata Theory

DD2371 Automata Theory KTH CSC VT 2008 DD2371 Automata Theory Dilian Gurov Lecture Outline 1. The lecturer 2. Introduction to automata theory 3. Course syllabus 4. Course objectives 5. Course organization 6. First definitions

More information

Context Free Languages: Decidability of a CFL

Context Free Languages: Decidability of a CFL Theorem 14.1 Context Free Languages: Decidability of a CFL Statement: Given a CFL L and string w, there is a decision procedure that determines whether w L. Proof: By construction. 1. Proof using a grammar

More information

Grammars (part II) Prof. Dan A. Simovici UMB

Grammars (part II) Prof. Dan A. Simovici UMB rammars (part II) Prof. Dan A. Simovici UMB 1 / 1 Outline 2 / 1 Length-Increasing vs. Context-Sensitive rammars Theorem The class L 1 equals the class of length-increasing languages. 3 / 1 Length-Increasing

More information

Theory Bridge Exam Example Questions

Theory Bridge Exam Example Questions Theory Bridge Exam Example Questions Annotated version with some (sometimes rather sketchy) answers and notes. This is a collection of sample theory bridge exam questions. This is just to get some idea

More information

Solution. S ABc Ab c Bc Ac b A ABa Ba Aa a B Bbc bc.

Solution. S ABc Ab c Bc Ac b A ABa Ba Aa a B Bbc bc. Section 12.4 Context-Free Language Topics Algorithm. Remove Λ-productions from grammars for langauges without Λ. 1. Find nonterminals that derive Λ. 2. For each production A w construct all productions

More information

Pushdown Automata (2015/11/23)

Pushdown Automata (2015/11/23) Chapter 6 Pushdown Automata (2015/11/23) Sagrada Familia, Barcelona, Spain Outline 6.0 Introduction 6.1 Definition of PDA 6.2 The Language of a PDA 6.3 Euivalence of PDA s and CFG s 6.4 Deterministic PDA

More information

NPDA, CFG equivalence

NPDA, CFG equivalence NPDA, CFG equivalence Theorem A language L is recognized by a NPDA iff L is described by a CFG. Must prove two directions: ( ) L is recognized by a NPDA implies L is described by a CFG. ( ) L is described

More information

Computational Models - Lecture 5 1

Computational Models - Lecture 5 1 Computational Models - Lecture 5 1 Handout Mode Iftach Haitner and Yishay Mansour. Tel Aviv University. April 10/22, 2013 1 Based on frames by Benny Chor, Tel Aviv University, modifying frames by Maurice

More information