Backus Naur and Chomsky Normal Forms - Presentation Transcript
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
Backus Naur and Chomsky Normal Forms Ashutosh Pandey 10/26/2009 2 PESIT
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
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
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
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
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
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
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
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
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
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
0 comments
Post a comment