Presentation is loading. Please wait.

Presentation is loading. Please wait.

Syntax Specification and BNF © Allan C. Milne Abertay University v12.7.11.

Similar presentations


Presentation on theme: "Syntax Specification and BNF © Allan C. Milne Abertay University v12.7.11."— Presentation transcript:

1

2 Syntax Specification and BNF © Allan C. Milne Abertay University v12.7.11

3 Agenda. Language Sspecification. BNF. Derivation sequence. Examples.

4 Syntax and Semantics. A language must be defined in terms of both its syntax and semantics. Syntax defines the format of the programs, statements and structures. –What a program looks like. Semantics defines the meaning of language constructs. –What a program does.

5 Language Specifications. Syntax can be formally defined. –e.g. grammars, BNF, syntax diagrams. While semantics can also be formally defined these definitions are usually long-winded and inaccessible to programmers. –e.g. attribute grammars, denotational semantics.

6 Specifying Syntax. There are a number of approaches to defining the syntax of a language: –Informal (eg Basic manuals). An assignment statement has the name of an identifier followed by an equals sign and an arbitrary expression. –Syntax diagrams (eg Pascal). –BNF (eg Algol-60). ::= "=" id expression =

7 Backus Normal Form (BNF). Backus Naur Form or Backus Normal Form. First used for the specification of Algol-60. This is a formal specification method. Underlying formalism is grammar theory. –A grammar is a generative device that specifies how to generate valid sentences in the language. BNF is a metalanguage.

8 Naming of Parts. ::= "=" ; metasymbols a rule non-terminal symbols terminal symbol a production

9 The Parts. Metasymbols –part of the BNF language Not the language being specified – ::= " ; | ( ) ? + * Terminal Symbols –atomic components of the language being specified –may use "…" to remove ambiguity Non-Terminal Symbols –define syntactic components used in the specification –are part of the specification not the language –name surrounded by –are derived into smaller units

10 Productions. A rule in BNF can have 1 or more alternative productions: ::= true | false ; This rule is made up of the following two productions: ::= true ; ::= false ;

11 The Null Production. It is useful to have a notation for the null, or empty, production; –that is a production that derives to nothing. We use the notation <> to indicate the null production. Note this must be an entire production –it cannot be used with other terminal or non- terminal symbols in a production. Example ::= if "(" ")" ; ::= else | <> ;

12 Applying Productions. Productions of a rule are applied through substitution: … …  …  where the rule for has a production ::=  This is a derivation of. Example: assume a production ::= hello ; and a string a b c d apply the production to get a b c d  a b hello c d

13 Derivation Sequence. A derivation sequence is the sequence of single derivation steps starting from the distinguished symbol of the BNF and terminating in a string of terminal symbols. The distinguished or starter symbol is defined to be the non- terminal of the first rule of the BNF specification. The final string of terminals is our program, known formally as a sentence.

14 The Language. The language defined by a BNF specification is the set of all sentences that can be derived from the distinguished symbol of the specification. Putting it another way, a sentence (or program) of a language is syntactically valid if and only if a derivation sequence can be found.

15 An Example BNF Specification. A specification of the syntax of a parenthesised comma-separated list of animals: ::= "(" ")" | "(" ")" | "(", ")" ; ::= |, ; ::= ant | bat | cat | dog ;

16 Example Derivation Sequence. Here is one possible derivation sequence for the sentence “( dog,ant)” :  (, )  (, ant )  ( dog, ant )

17 Parsing. Parsing a program is finding a derivation sequence for the program. There are two general approaches to this: –Top-down; start from the distinguished symbol and find substitutions that result in the target program. –Bottom-up; start with the input programand try to reduce this back to the distinguished symbol. This latter is the approach used by Yacc.

18 Extended BNF. The standard BNF notation has been extended to include iterative constructs; –this is known as Extended BNF or EBNF notation; –it is a superset of standard BNF. For those interested refer to my version of this notation which includes a full specification of the EBNF notation in itself and supporting tools. Find this at –http://a510690.ces.abertay.ac.uk/EBNF/http://a510690.ces.abertay.ac.uk/EBNF/


Download ppt "Syntax Specification and BNF © Allan C. Milne Abertay University v12.7.11."

Similar presentations


Ads by Google