today today
play

Today Today Grammar Principles of Programming Principles of - PDF document

Today Today Grammar Principles of Programming Principles of Programming Languages XXII Languages XXII Wael Aboelsaadat Wael Aboelsaadat wael@cs.toronto.edu wael@cs.toronto.edu http://www.cs.toronto.edu/~wael


  1. Today Today • Grammar Principles of Programming Principles of Programming Languages XXII Languages XXII Wael Aboelsaadat Wael Aboelsaadat wael@cs.toronto.edu wael@cs.toronto.edu http://www.cs.toronto.edu/~wael http://www.cs.toronto.edu/~wael Acknowledgement: Some of the material in these lectures are base d on material by Prof. Diane Horton & Prof. Anthony Bonner Language Specification & Compilation Language Specification & Compilation Grammar: introduction Grammar: introduction • Grammar: – A Grammar is a formalism that describes which sequence of terminals are meaningful in a PL. Mathematically, it is defined as a quadruple ( N , T , P , S ) where: • N is the set of symbols called Nonterminals • T is the set of symbols called Terminals • P is the set of productions • S subsetof N is the nonterminal called the starting symbol – Example: G = (N,T,P,S) where N = {S} , T = {a,b}, P={S->aS, S->bS, S-> } • Production: A production is a rule of the form X � Y where X is a string of – symbols ( terminals or nonterminals ) containing at least one nonterminal, and Y is a string of symbols ( terminals or nonterminals ) 1

  2. Grammar: context free Grammar: context free Grammar: Grammar: backus backus Naur Naur form form • Backus Naur Form (BNF) is a metalanguage for describing • A context free grammar (CFG) is a grammar in which |X| = 1, programming languages i.e. X is a single nonterminal – LHS: 1 nonterminal • A BNF grammar is a context free grammar – RHS: a sequence of terminals and nonterminals – E.g. • Notation: • S -> ab (CFG) • SA -> ab (non CFG) – Nonterminals are enclosed in angle brackets, i.e. “<“ and “>” “ instead of “ � ” in productions – Uses “::= • CFG is sufficient to describe most of the constructs in – Productions having the same left hand side can be grouped programming languages together using the alteration symbol “|” e.g <S> ::= a <S> | b <s> | – Lists are described using recursive rules Programming languages describable by CFG are recognizable • • A rule is recursive if its left-hand side appears on the right-hand side, by push down automata ( analogues to FSA with a stack ) e.g. <ident.list> ::= identifier | identifier, <ident.list> Grammar: BNF recursive rules Grammar: BNF recursive rules Grammar: extended BNF Grammar: extended BNF • Notation: • Left Recursive BNF Grammar: – (…|…|…) Any one of the alterations – A BNF grammar rule is left recursive if its LHS appears at the left – […] Optional part end of the RHS – (…)* or {…} or […]* repeat zero or more times e.g. <ident.list> ::= <ident.list> , identifier | identifier – (…) - or {…} - or […] - e.g. A � A x | y , yxx repeat one or more times – "x"or 'x' terminal symbol – Unquoted words non-terminal symbol • Right Recursive BNF Grammar: – A BNF grammar rule is right recursive if its LHS appears at the right end of the RHS • Example: e.g. <ident.list> ::= identifier | identifier, <ident.list> – Using the above notation e.g. A � x A | y , xxy < expression > : : = < expression > + < term > | < expression > - < term > | < term > could be written in the form of an iteration, as follows: The order of recursion has implications on the order of The order of recursion has implications on the order of < expression > : : = < term > [ ( + | -) < term > ]* evaluation and associativity. evaluation and associativity. 2

  3. Grammar: definitions Grammar: definitions… Grammar: derivation Grammar: derivation Grammar: parsing Grammar: parsing Grammar: building parse trees Grammar: building parse trees • A parse tree describes the hierarchical syntactic structure of the sentence based on a given language 3

  4. Grammar: grammars are not unique Grammar: grammars are not unique Grammar: ambiguity Grammar: ambiguity Grammar: sources of ambiguity Grammar: sources of ambiguity Grammar: inherently ambiguous Grammar: inherently ambiguous • Associativity and precedence of operators – Solution: • Change the grammar to reflect operator precedence X*Y-Z means ((X*Y) – Z) • Extent of a substructure – E.g. • Dangling else – Solution… • Obscure recursion – E.g. • exp � exp exp • A � A B – Solution: ?? 4

  5. Grammar: is this ambiguous? Grammar: is this ambiguous? Grammar: is this ambiguous? Grammar: is this ambiguous? < assign > : : = < identifier > = < expression > < assign > : : = < identifier > = < expression > < identifier > : : = A|B|C < identifier > : : = A|B|C < expression > : : = < expression > + < expression > < expression > : : = < expression > + < expression > | < expression > - < expression > | < expression > - < expression > | ( < expression > ) | ( < expression > ) | < identifier > | < identifier > Yes, because the sentence A = B - C - A has two different parse trees The grammar does not force "normal" left-to-right evaluation of addition and subtraction. Grammar: is it really a problem? Grammar: is it really a problem? Grammar: an unambiguous version Grammar: an unambiguous version • The operation of addition is associative in mathematics. Hence < assign > : : = < identifier > = < expression > A + B + C < identifier > : : = A|B|C < expression > : : = < expression > + < term > can be performed as either | < expression > - < term > ( A + B ) + C or A + ( B + C ). | < term > < term > : : = < term >*< factor > | < factor > The multiply operation is also associative. < factor > : : = ( < expression > ) | < identifier > Therefore one might say the previous ambiguous grammar would be satisfactory for addition and multiplication. Tree for A = B + C * A But would it? Computer arithmetic is not exact, and one might want be able to control the order… 5

  6. Evaluation Evaluation • CSC324H1F - 0101 – Wael Aboelsaadat • Tutorials – Anya Tafliovich (LM157) [Assignments: 1a,2a] – Hojjat Ghaderi (UC328) [Assignments: 1b,3a] – Hyonho Lee (LM123) [Assignments: 2b] 6

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend