• Home
  • Queen's University
  • Software Specifications
  • Introduction to Context-Free Languages and Grammars

Introduction to Context-Free Languages and Grammars

CISC/CMPE 223, Winter 2023, Context-free languages 1 Context-Free Languages This material is described in Chapter 10 of the textbook. We have observed that regular languages have nice properties, however, the specification capabilities of regular expressions and state diagrams are limited. Next we consider a rewrit- ing mechanism, or grammar, that overcomes some of the limitations. Context-free grammars are more powerful than state diagrams, that is, they define a larger class of languages. Context-free grammars are the most widely used specification tool for the syntactic struc- ture of programming languages. The appendix of the textbook contains a grammar specifi- cation for a subset of the C language that covers features that are needed for the program verification part of the course. In the programming language community grammars were originally specified using the Backus-Naur formalism (BNF): · the nonterminals are indicated with angle brackets · the left and right sides of rules (productions) are separated by : : = · | indicates alternative definitions Example. < expr> : := < expr> + <expr> | <expr> > <expr> | << expr>) | a Note that the string a + a x a has two essentially different derivations: the derivation can begin either with the "plus" rule or the "product" rule. Which derivation should we use? Notational convention for grammars: In the following we will use -> instead of : : = for the productions (or rewriting rules). We normally denote nonterminals/variables by upper case letters and terminal symbols by lower case letters or digits. CISC/CMPE 223, Winter 2023, Context-free languages 2 Example. Design a context-free grammar for the language {0"1" | n ? 0} U {1"0" | n ? 0} Next we will go through a more formal definition of a context-free grammar. Also we define formally the derivations of the grammar and the set of terminal strings (language) generated by the grammar. Definition. A context-free grammar (CFG) is a 4-tuple (V, E, P, S) where 1. V is a finite set of nonterminals (also called variables) 2. 2 is a finite set of terminals; 2 and V are disjoint 3. P is a finite set of productions, the left side of each production is a nonterminal and the right side is a string of nonterminals and terminals 4. SE V is the start nonterminal The definition means that the set of productions P is a finite subset of V x (2 U V) *. The productions are applied only to variables, that is, the left side of a production is always a variable. The right side of a production is a string of variables and terminal symbols. A derivation begins with the start variable S. The productions of the grammar are applied to any variable occurring in the "current string" (a.k.a. sentential form) and the process is continued as long a we get a string consisting only of terminal symbols. The language generated by the grammar consists of all terminal strings that are obtained in this way. Next we define formally the