BNF & EBNF

AshaniDickowita 7,946 views 21 slides Sep 19, 2017
Slide 1
Slide 1 of 21
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21

About This Presentation

This presentation includes all the details regarding the Backus Naur Form and the Extended Backus Naur Form.For more information visit : https://www.youtube.com/watch?v=hl2NLbIaU7U&t=255s


Slide Content

BNF EBNF

What is BNF ? Why need BNF ? What does BNF look like ? Examples of BNF Parse trees Examples of parse trees Origin of EBNF Examples

What is BNF? BNF = Backus-Naur Form It is a formal, mathematical way to specify context-free grammars BNF is Meta language (Meta language - a language that describes a language, which can describe a programming language). It is precise and unambiguous

Why need BNF? Before BNF, people specified programming languages ambiguously, i.e., with English “He fed her cat food .” Can have different meanings, He fed a woman’s cat some food. He fed a woman some food that was intended for cats. He somehow encouraged some cat food to eat something.

Peter Naur John Backus

What does BNF look like? BNF grammar consists of 4 parts , The set of tokens The set of non terminal symbols The start symbol The set of productions LHS = RHS Non-terminals = Terminals/non-terminals

“or” “ is defined as” Assign -> id := expr id -> A|B|C expr -> id+ expr | id * expr |(expr)|id Terminal Nonterminal What does BNF look like?

Example 1 A := B*(A+C ) Solution --> Assign -> id := expr A := expr A := id*expr A := B*(expr) A := B*(id + expr) A := B*(A + expr) A := B*(A + id) A := B*(A + C)

Example 2 A := (B * A) + C Solution --> Assign -> id := expr A := ( expr) A := ( id*expr) A := ( B*expr) A := ( B*(expr)) This cannot be derived.

Example 3 A:=A+B*C Solution --> Assign -> id := expr A :=id+expr A :=A+expr A :=A+id*expr A :=A+B*expr A :=A+B*id A :=A+B*C

Parse Trees A Parse tree is a hierarchical representation of a derivation.

A grammar is ambiguous if it generates a sentential form that has two or more distinct parse trees .

Example 1 A := B*(A+C) <assign> id := <expr> A id * <expr> B ( expr ) id + expr A id C

Example 3 A:= A+B*C <assign> Id := <expr> A id + <expr> A id * <expr> B id C

<assign> Id := < expr > A <expr> + <expr> id < expr > * < expr > A id id B C

Origin of EBNF Stands for “Extended Backus-Naur Form”. Increase readability and write ability.

Optional [ ] < if_cond > if <logic> then <stmt> Repetition { } < stmts > <stmt> { ; <stmt> } * 0 or more + 1 or more eg :- digit { digit } digit can be 1 or more Group ( ) value + integer | - integer value ( + | - )integer +

Syntax Graphs <stmt> < var > = <expr> var = expr < var > A | B | C A B C < var > <stmt>

< stmt> <stmt> | ; <stmt > < var > a | ab <stmt> ; a b

Example 1 < expr > <term> + <term> | <term> - <term> Example 2 <program> begin < stmt_list > end <term> <term> - + < expr > begin end < stmt_list >

Example 3 <stmt> < var > = <expression> < var > = <expression> <stmt>