1 2 Introduction to Programming Lecture 9: Describing the Syntax Michela Pedroni 16 Novem ber 2003 Chair of Softw are Engineering I ntro – Lecture 9 Chair of Softw are Engineering I ntro – Lecture 9 Why describe the syntax formally? Syntax: Conditional 3 4 We know syntax descriptions for human languages: A conditional instruction consists, in order, of: � An “If part”, of the form if condition . � e.g. gramm ar for German, French, … � A “Then part” of the form then compound . � Zero or m ore “Else if parts”, each of the form elseif condition then com pound . � Expressed in natural language � Zero or one “Else part” of the form else compound � The keyword end . � Good enough for human use Here each condition is a boolean expression, and � Ambiguous, like human languages themselves each com pound is a compound instruction. Chair of Softw are Engineering I ntro – Lecture 9 Chair of Softw are Engineering I ntro – Lecture 9 Why describe the syntax formally? Why describe the syntax formally? 5 6 Programm ing languages need better descriptions: Compilers use algorithm s to � More precise: must tell us unambiguously � Determine if input is correct program text whether given program text is legal or not � Analyse program text to extract specimens � Use formalism sim ilar to mathematics � Translate program text to machine instructions � Can be fed into com pilers for automatic processing of programs Compilers need strict form al definition of programm ing language Chair of Softw are Engineering I ntro – Lecture 9 Chair of Softw are Engineering I ntro – Lecture 9 1
History Formal Description of Syntax 7 8 � 1954 FORTRAN: First widely recognized Use formal language to describe programm ing programm ing language (developed by John Backus languages. et Al.) � 1958 ALGOL 58: Joint work of European and American groups � 1960 ALGOL 60: Preparation showed a need for a Languages used to describe other languages are formal description � John Backus (member of called Meta-Languages ALGOL team) proposed Backus-Normal-Form (BNF) � 1964: Donald Knuth suggested acknowledging Peter Naur for his contribution � Backus-Naur- Meta-Language used to describe Eiffel: Form BNF-E (Variant of the Backus-Naur-Form, BNF) � Many variants since then, e.g. graphical variant by Niklaus Wirth Chair of Softw are Engineering I ntro – Lecture 9 Chair of Softw are Engineering I ntro – Lecture 9 Formal description of a language Formal Description of Syntax 9 10 A language is a set of phrases � BNF lets us describe syntactical properties of a language A phrase is a finite sequence of tokens from a certain “vocabulary” � Remember: Description of a programm ing language also includes lexical and semantic Not every possible sequence is a phrase of the properties � other tools language A gramm ar specifies which sequences are phrases and which are not BNF is used to define a gramm ar for a programming language Chair of Softw are Engineering I ntro – Lecture 9 Chair of Softw are Engineering I ntro – Lecture 9 Elements of a grammar: Terminals Grammar 11 12 Definition A Gramm ar for a language is a finite set of rules for producing phrases, such that: � Terminals 1. Any sequence obtained by a finite num ber of Tokens of the language that are not defined by applications of rules from the gramm ar is a phrase of the language. a production of the grammar. E.g. keywords from Eiffel such as if , then , end or symbols such as the semicolon “ ; ” or the 2. Any phrase of the language can be obtained by a assignment “ := ” finite num ber of applications of rules from the gramm ar. Chair of Softw are Engineering I ntro – Lecture 9 Chair of Softw are Engineering I ntro – Lecture 9 2
Elements of a grammar: Productions Elements of a grammar: Nonterminals 13 14 � Nonterm inals � Productions Rules that define nonterm inals of the grammar Names of syntactical structures or using a com bination of term inals and (other) substructures used to build phrases. nonterm inals Chair of Softw are Engineering I ntro – Lecture 9 Chair of Softw are Engineering I ntro – Lecture 9 An example production BNF Elements: Concatenation 15 16 Conditional: Graphical representation: Instruction else A B Condition then Instruction if end BNF: A B Terminal Meaning: A followed by B Nonterminal Production Chair of Softw are Engineering I ntro – Lecture 9 Chair of Softw are Engineering I ntro – Lecture 9 BNF Elements: Optional BNF Elements: Choice 17 18 Graphical representation: Graphical representation: A A B BNF: [ A ] BNF: A | B Meaning: A or nothing Meaning: either A or B Chair of Softw are Engineering I ntro – Lecture 9 Chair of Softw are Engineering I ntro – Lecture 9 3
BNF Elements: Repetition BNF Elements: Repetition, once or more 19 20 Graphical representation: Graphical representation: A A { A } + { A } * BNF: BNF: Meaning: sequence of zero or more A Meaning: sequence of one or more A Chair of Softw are Engineering I ntro – Lecture 9 Chair of Softw are Engineering I ntro – Lecture 9 BNF elements: Overview BNF Elements Combined 21 22 Concatenation: A B A B Conditional: instruction else A Optional: [ A ] A condition then instruction if end Choice: A | B B written in BNF: A , Repetition (zero or more): { A }* Conditional [ ] if condition then instruction else instruction end Repetition (at least once): { A }+ A Chair of Softw are Engineering I ntro – Lecture 9 Chair of Softw are Engineering I ntro – Lecture 9 BNF: Conditional with elseif Different Grammar for Conditional 23 24 , Conditional If_part Then_part Else_list end , [ ] Conditional if Then_part_list Else_part end , If_part Boolean_expression if } * , { Then_part_list Then_part elseif Then_part , Then_part then Compound Then_part , Boolean_expression then Compound } * [ , { ] Else_list Elseif_part else Compound , Else_part else Compound , Elseif_part elseif Boolean_expression Then_part Chair of Softw are Engineering I ntro – Lecture 9 Chair of Softw are Engineering I ntro – Lecture 9 4
BNF elements: Overview BNF-E 25 26 Concatenation: A B A B Used in official description of Eiffel. Every Production is one of A Optional: [ A ] � Concatenation A A , B C [ D ] Choice: A | B B � Choice A , B | C | D A A , [ B { terminal B } * ] Repetition (zero or more): { A }* � Repetition A , { B terminal ... } * Repetition (at least once): { A }+ A Chair of Softw are Engineering I ntro – Lecture 9 Chair of Softw are Engineering I ntro – Lecture 9 BNF-E Rules Conditional with elseif (BNF) 27 28 , [ ] Conditional if Then_part_list Else_part end � Every nonterm inal m ust appear on the left-hand side of exactly one production, called its } * , { Then_part_list Then_part elseif Then_part defining production � Every production must be of one kind: Then_part , Boolean_expression then Compound Concatenation, Choice or Repetition , Else_part else Compound Chair of Softw are Engineering I ntro – Lecture 9 Chair of Softw are Engineering I ntro – Lecture 9 BNF-E: Conditional Recursive grammars 29 30 , [ ] Conditional if Then_part_list Else_part end Constructs may be nested } + , { ... Then_part_list Then_part elseif Express this in BNF with recursive grammars Then_part , Boolean_expression then Compound Recursion: circular dependency of productions , Else_part else Compound Chair of Softw are Engineering I ntro – Lecture 9 Chair of Softw are Engineering I ntro – Lecture 9 5
Recursive grammars Recursive grammars 31 32 Conditionals can be nested within conditionals: Production nam e can be used in its own definition � Definition of Then_part_list with repetition: , Else_part else Compound } * , { … Then_part_list Then_part elseif } * , { … Compound Instruction ; � Recursive definition of Then_part_list: , [ ] Then_part_list Then_part elseif Then_part_list | | | , Instruction Conditional Loop Call ... Chair of Softw are Engineering I ntro – Lecture 9 Chair of Softw are Engineering I ntro – Lecture 9 Guidelines for Grammars Guidelines for Grammars 33 34 Keep productions short. Treat lexical constructs like terminals � Identifiers � easier to read � Constant values � better assessm ent of language size Identifier , Letter (Letter | Digit | " _ ")* Conditional , Integer_constant , Digit + Floating_point , [ -] Digit* “ . " Digit + if Boolean_expression then Compound { elseif Boolean_expression then Com pound } * Letter , " A " | " B " | ... | " Z " | " a " | ... | " z " [ else Compound ] end Digit , " 0 " | " 1 " | ... | " 9 “ Chair of Softw are Engineering I ntro – Lecture 9 Chair of Softw are Engineering I ntro – Lecture 9 Guidelines for Grammars Writing a Parser 35 36 Use unambiguous productions. One feature per Production � Applicable production can be found by looking at one lexical element at a time � Concatenation: Sequence of feature calls for Nonterm inals, checks for Term inals Conditional , if Then_part_list [ Else_part ] end � Choice: Conditional with Com pound per alternative Compound , { Instruction } * Instruction , Conditional | Loop | Call | ... � Repetition: Loop Chair of Softw are Engineering I ntro – Lecture 9 Chair of Softw are Engineering I ntro – Lecture 9 6
Recommend
More recommend