Today Today Grammar Principles of Programming Principles of - - PDF document

today today
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

1

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

Principles of Programming Principles of Programming Languages XXII Languages XXII

Today Today

  • Grammar

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)

slide-2
SLIDE 2

2 Grammar: context free Grammar: context free

  • A context free grammar (CFG) is a grammar in which |X| = 1,

i.e. X is a single nonterminal

– LHS: 1 nonterminal – RHS: a sequence of terminals and nonterminals – E.g.

  • S -> ab

(CFG)

  • SA -> ab

(non CFG)

  • CFG is sufficient to describe most of the constructs in

programming languages

  • Programming languages describable by CFG are recognizable

by push down automata (analogues to FSA with a stack)

Grammar: Grammar: backus backus Naur Naur form form

  • Backus Naur Form (BNF) is a metalanguage for describing

programming languages

  • A BNF grammar is a context free grammar
  • Notation:

– Nonterminals are enclosed in angle brackets, i.e. “<“ and “>” – Uses “::= “ instead of “ ” in productions – Productions having the same left hand side can be grouped together using the alteration symbol “|”

e.g <S> ::= a <S> | b <s> |

– Lists are described using recursive rules

  • A rule is recursive if its left-hand side appears on the right-hand side,

e.g. <ident.list> ::= identifier | identifier, <ident.list>

Grammar: BNF recursive rules Grammar: BNF recursive rules

  • Left Recursive BNF Grammar:

– A BNF grammar rule is left recursive if its LHS appears at the left end of the RHS

e.g. <ident.list> ::= <ident.list> , identifier | identifier e.g. A A x | y , yxx

  • Right Recursive BNF Grammar:

– A BNF grammar rule is right recursive if its LHS appears at the right end of the RHS

e.g. <ident.list> ::= identifier | identifier, <ident.list> e.g. A x A | y , xxy The order of recursion has implications on the order of The order of recursion has implications on the order of evaluation and associativity. evaluation and associativity.

Grammar: extended BNF Grammar: extended BNF

  • Notation:

– (…|…|…) Any one of the alterations – […] Optional part – (…)* or {…} or […]* repeat zero or more times – (…)- or {…}- or […]- repeat one or more times – "x"or 'x' terminal symbol – Unquoted words non-terminal symbol

  • Example:

– Using the above notation

< expression > : : = < expression > + < term > | < expression > - < term > | < term >

could be written in the form of an iteration, as follows:

< expression > : : = < term > [ ( + | -) < term > ]*

slide-3
SLIDE 3

3 Grammar: definitions Grammar: definitions… Grammar: derivation Grammar: derivation Grammar: parsing Grammar: parsing

  • A parse tree describes the hierarchical syntactic structure
  • f the sentence based on a given language

Grammar: building parse trees Grammar: building parse trees

slide-4
SLIDE 4

4 Grammar: grammars are not unique Grammar: grammars are not unique Grammar: ambiguity Grammar: ambiguity Grammar: inherently ambiguous Grammar: inherently ambiguous Grammar: sources of ambiguity Grammar: sources of ambiguity

  • 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: ??

slide-5
SLIDE 5

5 Grammar: is this ambiguous? Grammar: is this ambiguous?

< assign > : : = < identifier > = < expression > < identifier > : : = A|B|C < expression > : : = < expression > + < expression > | < expression > - < expression > | ( < expression > ) | < identifier >

Grammar: is this ambiguous? Grammar: is this ambiguous?

< assign > : : = < identifier > = < expression > < identifier > : : = A|B|C < expression > : : = < expression > + < expression > | < expression > - < expression > | ( < expression > ) | < 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?

  • The operation of addition is associative in mathematics. Hence

A + B +C can be performed as either ( A + B ) +C

  • r

A + ( B +C ). The multiply operation is also associative. Therefore one might say the previous ambiguous grammar would be satisfactory for addition and multiplication. But would it? Computer arithmetic is not exact, and one might want be able to control the order…

Grammar: an unambiguous version Grammar: an unambiguous version

< assign > : : = < identifier > = < expression > < identifier > : : = A|B|C < expression > : : = < expression > + < term > | < expression > - < term > | < term > < term > : : = < term >*< factor > | < factor > < factor > : : = ( < expression > ) | < identifier > Tree for A = B + C * A

slide-6
SLIDE 6

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]