This document discusses grammars for compilers, including Backus-Naur Form (BNF) and Chomsky Normal Form (CNF). It defines BNF as a notation for describing language grammars using production rules. It provides an example of BNF for C++. It also discusses variants of BNF and advantages such as readability. Finally, it defines CNF as grammars with binary productions and compares BNF and CNF, noting BNF is more human-readable while CNF enables efficient parsing algorithms.
Apidays New York 2024 - The value of a flexible API Management solution for O...
Backus Naur and Chomsky Normal Forms
1. NOTES Title: Grammars for Compilers Intro to BNF Definition of BNF Example : http://otal.umd.edu/drweb/c++tutorial/lessons/BNF.HTM Variants of BNF: http://en.wikipedia.org/wiki/Backus–Naur_Form Why BNF? CNF Definition Characteristics BNF vs. CNF 10/26/2009 1 PESIT
2. Backus Naur and Chomsky Normal Forms Ashutosh Pandey 10/26/2009 2 PESIT
3. Backus-Naur Form Created by John Warner Backus (1927-2007) to describe the grammar of ALGOL, the first, widely used high-level language. The Backus–Naur Form or BNF grammars have significant similarities to Panini's Sanskrit grammar rules, and the notation is sometimes also referred to as Panini–Backus Form. 10/26/2009 3 PESIT
4. Definition of BNF A BNF specification consists of a set of production rules written as:- <variable> ::= <expression> where <variable> is a non-terminal symbol <expression> consists of one or more sequences of terminal and non-terminal symbol and each sequence is separated by the ‘ | ‘ symbol. Terminal symbols cannot appear on the left side of a production 10/26/2009 4 PESIT
5. Example of BNF BNF Specification of the C++ grammar:- <c++prog> ::= <sysdirectives> <decdefs> main () {<body>} <sysdirectives> ::= ‘ ‘| #<includedir> <includedir> ::= include '<'<filename>'>‘ . . . <body> ::= | <statement> | <decdef> |<body> <statement> | <body> <decdef> <statement> ::= <assignment>; | <inputstatement>; | <outputstatement>; | <condstatement>; | <iterativestatement>; <assignment> ::= <id>++ | ++<id> | <id>-- | --<id> | <equalassign> … 10/26/2009 5 PESIT
6. Variants of BNF Many variations of the BNF exist, for the sake of simplicity, and readability Extended BNF (EBNF) is a popular variant, which uses regular expression repetition operators such as ‘+’ or ‘*’ Reduced BNF (RBNF) is used to encode rules of various routing protocols. 10/26/2009 6 PESIT
7. Advantages of BNF High human readability: BNF is informal in nature. Ease in Parsing: simple LL parsers are sufficient. Ease of adaptability: Can be customized for various application due to it’s informal nature 10/26/2009 7 PESIT
8. Who uses BNF? YACC Definitions of markup languages such as XML (HTML does not have a grammar). GOLD: A freeware parsing system. Other parser generators for JAVA(Parser Objects), Python (Wormhole). 10/26/2009 8 PESIT
9. Chomsky Normal Form A grammar is in the Chomsky normal form if all it’s productions are of the form :- S AB A a Where S is the start variable, AB are any two non – terminal symbol and a is a terminal symbol. 10/26/2009 9 PESIT
10. Characteristics of CNF Every parse tree is a binary tree with 2n – 1 nodes, where n is the length of the string to be parsed. No Ambiguity. Uses the CYK Algorithm whose time complexity is Θ(2n- 1), therefore efficient parsing is another characteristic Every context-free grammar can be efficiently converted in to CNF. 10/26/2009 10 PESIT
11. Uses of CNF Required for some efficient parsing algorithms such as Cocke-Younger-Kasami (CYK) algorithm Used for proofs in the field of languages and computability because of its linear order of growth. 10/26/2009 11 PESIT
12. BNF vs. CNF BNF Easily understood by humans. Useful for Parser Generators. Uses LL, LR, LALR Parsing algorithms. CNF Often difficult to understand. Useful for theorem proving. Uses the CYK Parsing algorithm 10/26/2009 12 PESIT