Computational model language and grammar bnf

25
Computational model language &grammar BNF

description

computational model(BNF) with Discrete math Background!!

Transcript of Computational model language and grammar bnf

Page 1: Computational model language and grammar bnf

Computational model language &grammar

BNF

Page 2: Computational model language and grammar bnf

What is a Computational Model?

Computers can perform many tasks. Given a task, two questions

arise.The first is: Can it be carried out using a computer? Once we know that this first question has an affirmative

Answer,we can ask the second question: How can the task be carried out?

Models of computation are used to help answer these questions.

Page 3: Computational model language and grammar bnf

STRUCTURES IN COMPUTATIONAL MODEL

There are three types of structures used in models of computation, namely, grammars, finite-state machines, and Turing machines.

We will only focus on grammar structural because it will lead to us BNF

Grammars are used to generate the words of a language and to determine whether a word is in a language.

Formal languages, which are generated by grammars, provide models both for natural languages, such as English, and for programming languages, such as Pascal, Fortran, Prolog, C, and Java. In particular, grammars are extremely important in the construction and theory of compilers.

Page 4: Computational model language and grammar bnf

Computational Modeling Of BNF

Derivation of BNF: LANGUAGE AND GRAMMER Phrase-structure grammar Types of phrase-structure grammar Derivation Tree

Page 5: Computational model language and grammar bnf

Phrase structure grammar

G:{V,T,N,S,P} Phrase structure grammar consists of a

finite set V: consisting of the vocabulary A finite set of T: Terminal symbols(cannot

be replaced) V-T = N: A set of non-Terminal symbols. A start position set S: starting And a finite set of P: Production rules. V*: The superset of V, consisting of the

language generated by V.

Page 6: Computational model language and grammar bnf

Types of Phrase Structure Grammar

Page 7: Computational model language and grammar bnf

Context Free Grammars Consists of finite set of grammar rules. Ambiguity occurs in CFG ( Context free grammar ). BNF is a CFG( Context Free Grammar ). A context-free grammar (CFG) G is a quadruple (V,

Σ, R, S) where V: a set of non-terminal symbols Σ: a set of terminals (V ∩ Σ = Ǿ) R: a set of rules (R: V → (V U Σ)*); left side always

non-terminal S: a start symbol.

Page 8: Computational model language and grammar bnf

Example: Parse structure of a English Sentence

G=(V,T,N,S,P) V=(a, boy, sleeps, soundly, sentence, noun phrase,

verb phrase, article, noun, verb, adverb) T=(a, boy, sleeps, soundly) N=(sentence, noun phrase, verb phrase, noun,

verb, article, adverb) P=(<sentence> <noun phrase> <verb

phrase>, <noun phrase> <noun> <article>, <verb phrase> <verb> <adverb>, <article> a, <noun> boy,<verb> sleeps, <adverb> soundly).

Page 9: Computational model language and grammar bnf

The Language generated

<sentence> = <noun phrase> <verb phrase>

= <article> <noun> < verb >

<adverb>

= a <noun> <verb> <adverb>

= a boy <verb> <adverb>

= a boy sleeps < adverb>

= a boy sleeps soundly.

Page 10: Computational model language and grammar bnf

Graphical Representation Of Using Derivation Tree

Sentence

noun phrase verb phrase

article noun verb adverb

a boy sleeps soundly

Page 11: Computational model language and grammar bnf

Derivation/Parse Tree

It describes the syntactical hierarchy of a

grammars productions

If ambiguity exists their can be more then one

parse trees

Basically is a graphical representation.

Page 12: Computational model language and grammar bnf

What is BNF? Backus-Naur notation (more commonly known as BNF or Backus-Naur Form) is a

Meta Language used to describe a language, which was developed by John Backus (and possibly Peter Naur as well) to describe the syntax of the Algol60 programming language.

It was primarily developed by John Backus ,but adopted and slightly improved by Peter Naur for Algol60, which made it well-known. Because of this Naur calls BNF Backus Normal Form, while everyone else calls it Backus-Naur Form.)

It is used to formally define the grammar of a language, so that there is no disagreement or ambiguity as to what is allowed and what is not. In fact, BNF is so unambiguous that there is a lot of mathematical theory around these kinds of grammars, and one can actually mechanically construct a parser for a language given a BNF grammar for it. (There are some kinds of grammars for which this isn't possible, but they can usually be transformed manually into ones that can be used.)

Programs that do this are commonly called "compiler compilers". The most famous of these is YACC, but there are many more.

Page 13: Computational model language and grammar bnf

HOW IT WORKS ??The Principles

BNF is sort of like a mathematical game: you start with a

Symbol and are then given rules for what you

can replace this symbol with. The language defined by the

BNF grammar is just the set of all strings you can produce

by following these rules.

The rules are called production rules, and look like this:

symbol := alternative1 | alternative2 ...

Page 14: Computational model language and grammar bnf

Terminal Symbols Terminal symbols are literal symbols which may appear in the inputs to or outputs

from the production rules of a formal grammar and which cannot be changed

using the rules of the grammar (this is the reason for the name "terminal").

For concreteness, consider a grammar defined by two rules:

x can become xa

x can become a

Here a is a terminal symbol because no rule exists which would

change it in to something else. (On the other hand, x has two rules

that can change it, thus it is nonterminal.) A formal language defined

(or generated) by a particular grammar is the set of strings that can be

produced by the grammar and that consist only of terminal symbols.

Page 15: Computational model language and grammar bnf

Non-Terminal Symbols

Nonterminal symbols are those symbols which

can be replaced. A formal grammar

includes a start symbol, a designated member of

the set of nonterminals.

e.g:

digit = “0”|"1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" ;

So digit is non-terminal because it’s value lies at any

range in 0 - 9.

Page 16: Computational model language and grammar bnf

An Example

What is an integer? An integer can be a single digit and so:

<integer> ::= <digit>

However, what is a digit?

A digit is 0 or 1 or 2 or … or 9.

<digit> ::= 0|1|2|3|4|5|6|7|8|9

The vertical line is read as or.

Page 17: Computational model language and grammar bnf

How are we going to specify integers of any length?

All integers of more than one digit start with a single digit and are

followed by an integer and the final integer is a single digit

integer.

An indefinitely long integer is defined as

› <integer> ::= <digit><integer>

E.g. 147

› Is a single digit integer ( 1 ) followed by the integer 47.

› 47 is a single digit integer ( 4 ) followed by a single digit

integer ( 7 ).

Page 18: Computational model language and grammar bnf

BNF Code Conversion Of C# Code:

SAMPLE CODE OF BNF

Int A=1+2/3;

Console.writeLine(“{0}”,A); 

BNFC Code:

EAdd ( (Eint 1) (Ediv (Eint 2 ) (Eint 3) ) )

Echo (A)

EXPLANATION:

;EAdd == exp1 + exp2 (exp can be of any type of num)

;Eint == Exp is of type int

;Echo == prints the value

Page 19: Computational model language and grammar bnf

Table In BNF CodeC# code :Int a=5Int i=1;Int ans=0;While(i!=10){Ans=a*i;Console.writline(“{0}”,ans);i++;} BNF CODE:Eint A ::= 5EInt I ::= 1EInt ans ::= 0 While ::= <i != 11>< 

Eint ans ::= Emul (Eint A )(Eint i)Echo ansEint i ::= Eadd (Eint i + 1)

>

Page 20: Computational model language and grammar bnf

Bnf Flow Of Code

Note: In some cases a lexical analyzer with syntax and semantic analyzer make up a Parser!!

Page 21: Computational model language and grammar bnf

Working of Lexical Analyzer

Scans the Code, breaks it down into ‘Tokens’:

constants (integer, double, char, string, etc.)

Identifier(Variables) Operators Punctuation(separator) reserved words(keywords) Process is called

‘Tokenization’

Page 22: Computational model language and grammar bnf

Working of Parser(Syntax and Semantics)

Syntax tells of the valid form of the program

Described via CFG Determines

whether a statement is valid and can be produced via the production rules.

Semantic tell of the behavior of the program.

Two types: Static semantic

check(compile time error checking). Example: using undeclared variables, type checking.

Dynamic semantic check(run time error checking). Example: Array out of bounds, division by zero

Page 23: Computational model language and grammar bnf

BNFC = BNF Converter BNF = Backus-Naur Form (also known as Context-Free Grammars).

BNF is the standard format for

the specification and documentation of programming languages.

BNFC makes BNF usable for implementation as well.

BNFC is a high-level front end to traditional implementation formats

(in the style of Lex and YACC): "BNFC is a compiler compiler"

BNFC saves 90% of source code writing in a typical compiler front

end.

BNFC can be used for projects carried out in C, C++, C#, F#, Haskell,

Java, OCaml.

Page 24: Computational model language and grammar bnf

What BNFC can be used for

Strongest case: when designing and implementing a

new programming language

rapid development(can build on a previous language)

guaranteed consistency of components

freedom to change implementation of language(change

its grammar)

ANSI C and Java have been implemented

but some languages have rough corners (e.g. Haskell)

Page 25: Computational model language and grammar bnf

Question and Answers

Q) What is computational model? Q)Description of Language and Grammar? Q)What is Parse-Structure grammar? Q)What is Context free Grammar? Q)Define BNF; who were main contributors to its

development? Q)BNF Model: Compiler. Explain?