Upcoming SlideShare
×

# Backus Naur and Chomsky Normal Forms

4,704 views

Published on

Published in: Technology
7 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total views
4,704
On SlideShare
0
From Embeds
0
Number of Embeds
66
Actions
Shares
0
0
0
Likes
7
Embeds 0
No embeds

No notes for slide

### Backus Naur and Chomsky Normal Forms

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