Presentation is loading. Please wait.

Presentation is loading. Please wait.

BNF A CFL Metalanguage Some Variations Particular View to SLK Copyright © 2015 – Curt Hill.

Similar presentations


Presentation on theme: "BNF A CFL Metalanguage Some Variations Particular View to SLK Copyright © 2015 – Curt Hill."— Presentation transcript:

1 BNF A CFL Metalanguage Some Variations Particular View to SLK Copyright © 2015 – Curt Hill

2 Metalanguage A language that describes languages English is both a language and a metalanguage –It is able to describe itself BNF is a metalanguage for describing Context Free Languages –Most programming languages are Context Free

3 Origins Name is acronym for Backus Normal Form Developed in late 1950s to describe FORTRAN Used to describe ALGOL 60 by Peter Naur –BNF is also Backus Naur Form Many variations, but we have nothing that is substantially better Copyright © 2015 – Curt Hill

4 Purpose BNF describe productions Recall that a production is a part of a Chomsky grammar A production is a rewrite rule The left hand symbol may be rewritten as the items on the right The left hand item must be a non- terminal –A construction from the language The right hand side is zero or more terminals and non-terminals Copyright © 2015 – Curt Hill

5 Form Productions in BNF take the form: NT ::= S 1 S 2 … S n –Where: NT is a non-terminal S k is either a non-terminal or terminal symbol A terminal is lexeme that will be seen –Such as punctuation, reserved words, identifiers A non-terminal is a construction –Such as for-loop, expression, function Copyright © 2015 – Curt Hill

6 Non-terminals Some systems prefer to see a non- terminal marked in some way –Such as enclosed in angle brackets Others do not – they think the only difference between a terminal and non-terminal is that the terminal never appears on the left hand side of the production Copyright © 2015 – Curt Hill

7 Alternation Many non-terminals may be rewritten in multiple ways This may be handled in a variety of ways –Duplicated productions –Alternation symbol – often | Lets consider an example Copyright © 2015 – Curt Hill

8 C Statement types Duplicated ::= ::= ::= ::= Alternation symbol ::= | | | Both of these use angle brackets to highlight non-terminals Copyright © 2015 – Curt Hill

9 Repetition BNF has historically used recursion to express repetition –Particularly odd in that recursion was unavailable in FORTRAN and difficult in assembly A non-terminal is then defined in terms of itself Consider lists Copyright © 2015 – Curt Hill

10 Lists in C Compound statement: ::= | Function parameters: ::= identifier ( ) ::= |, | _empty_ Copyright © 2015 – Curt Hill

11 Empty It is possible for a production to be empty –Replaced by nothing This is the usual way to specify an optional item It is also the way to end recursion Sometimes it has its own symbol like _empty_ Other times it is just empty on RHS or after a | Consider C types Copyright © 2015 – Curt Hill

12 C Types ::= ::= short | long | signed | unsigned | _empty_ ::= bool | char | int | float Copyright © 2015 – Curt Hill

13 Statements Most modern languages have a recursive statement construction ::= | | | … ::= if ( ) ::= { } ::= | | _empty_ What statements are not recursive? Copyright © 2015 – Curt Hill

14 EBNF Extended BNF Add something that makes the specification easier Things enclosed in [ and ] are optional, they may be present zero or one times Some times things enclosed in { and } may be present 0 or more times Each flavor of EBNF may choose a slightly different notation Copyright © 2015 – Curt Hill

15 Syntax Graph More graphic and easier to read than regular BNF Same expressive power May be programmatically generated Cannot use these to drive parser generators Copyright © 2015 – Curt Hill term + -

16 What Can BNF Not Do? Cannot consider the source in any other place Thus it cannot check: –Variables are defined –Expressions are of the correct type In a compiler this is usually done with extra checking –Referencing the symbol table BNF knows nothing about meaning –That is semantics Copyright © 2015 – Curt Hill

17 Derivations We derive strings by starting with the start symbol, and repeatedly replacing the left hand side by the right side of one of the productions If we can derive a string it is a legal part of the language Consider the following example Copyright © 2015 – Curt Hill

18 Example 1 Copyright © 2015 – Curt Hill Productions: ::= begin end ::= | ; ::= ident = ::= + | - | ::= ident | number

19 Example 2 Is the following a sentence in the above language? begin x = 3; y = x end If it is we should be able to find a derivation Copyright © 2015 – Curt Hill

20 Derivation ::= begin end ::= ; ::= ident = ::= ::= number ::= ident = ::= ::= ident Copyright © 2015 – Curt Hill

21 Parse Trees A parse tree is a derivation Each interior node represents a production The leaves are the actual input string Copyright © 2015 – Curt Hill

22 Derivation Copyright © 2015 – Curt Hill pgm beginend S_list stmt S_list ; Ident(x) = var Number(3) stmt Ident(y) = var Ident(x)

23 Exercise Generate the BNF for a C family switch case Assume is already defined Assume that the switch expression is only an identifier Assume that the case selector is a number Every case has a break There must be one or more case The last case must be a default Copyright © 2015 – Curt Hill

24 SLK This parser generator takes its own variation of BNF The LHS is on first line May be followed by several symbols –::= –-> –Among others Each subsequent line is an alternative No identification of non-terminals Blank line terminates Copyright © 2015 – Curt Hill

25 Example This production given earlier ::= | ; Would be in SLK s_list ::= stmt stmt ; s_list Copyright © 2015 – Curt Hill

26 Repetition SLK does accept a few EBNF constructs Braces enclose something repeated zero or more times –Thus braces, such as C compound statements must be escaped –Following the braces with + makes it one or more times Brackets enclose something optional –Zero or one times Copyright © 2015 – Curt Hill

27 Other Things SLK accepts both C++ comment –// and /* */ The reserved symbol _epsilon_ is the empty symbol Copyright © 2015 – Curt Hill

28 Conclusion BNF and its variations are the standard way to define a Context Free Language Using BNF and a parser generator like SLK and a lexical analyzer generator like Flex++ is the fastest way to construct a working recognizer or language processor Copyright © 2015 – Curt Hill


Download ppt "BNF A CFL Metalanguage Some Variations Particular View to SLK Copyright © 2015 – Curt Hill."

Similar presentations


Ads by Google