Programming Languages and Compilers CS 421 William Mansky

  • Slides: 54
Download presentation
Programming Languages and Compilers (CS 421) William Mansky http: //courses. engr. illinois. edu/cs 421/

Programming Languages and Compilers (CS 421) William Mansky http: //courses. engr. illinois. edu/cs 421/ Based in part on slides by Mattox Beckman, as updated by Vikram Adve, Gul Agha, Elsa Gunter, and Dennis Griffith 10/17/2021 1

Grammars n n Grammars are formal descriptions of which strings over a given character

Grammars n n Grammars are formal descriptions of which strings over a given character set are in a particular language Language designers write grammar Language implementers use grammar to know what programs to accept Language users use grammar to know how to write legitimate programs 10/17/2021 2

Types of Formal Language Descriptions n n n Regular expressions, regular grammars, finite state

Types of Formal Language Descriptions n n n Regular expressions, regular grammars, finite state automata Context-free grammars, BNF grammars, syntax diagrams Whole family more of grammars and automata – covered in automata theory 10/17/2021 3

Sample Grammar n n n Language: Parenthesized sums of 0’s and 1’s <Sum> :

Sample Grammar n n n Language: Parenthesized sums of 0’s and 1’s <Sum> : : = <Sum >: : = <Sum> : : = 10/17/2021 0 1 <Sum> + <Sum> (<Sum>) 4

BNF Grammars n Start with a set of characters, a, b, c, … n

BNF Grammars n Start with a set of characters, a, b, c, … n n And a set of different characters, X, Y, Z, … n n We call these terminals We call these nonterminals One special nonterminal S called start symbol 10/17/2021 5

BNF Grammars n n BNF rules (aka productions) have form X : : =

BNF Grammars n n BNF rules (aka productions) have form X : : = y where X is any nonterminal and y is a string of terminals and nonterminals BNF grammar is a set of BNF rules such that each nonterminal used appears on the left of some rule (i. e. , at least one production per nonterminal) 10/17/2021 6

Sample Grammar n n n Terminals: 0 1 + ( ) Nonterminals: <Sum> Start

Sample Grammar n n n Terminals: 0 1 + ( ) Nonterminals: <Sum> Start symbol = <Sum> : : = 0 n <Sum >: : = 1 n <Sum> : : = <Sum> + <Sum> n <Sum> : : = (<Sum>) n Can be abbreviated as <Sum> : : = 0 | 1 | <Sum> + <Sum> | (<Sum>) n 10/17/2021 7

BNF Deriviations Given rules X : : = y. Zw and Z : :

BNF Deriviations Given rules X : : = y. Zw and Z : : =v we may write X => y. Zw => yvw n Sequence of such replacements called n derivation n Derivation called right-most if always replace the right-most non-terminal 10/17/2021 8

BNF Derivations n Start with the start symbol: <Sum> => 10/17/2021 9

BNF Derivations n Start with the start symbol: <Sum> => 10/17/2021 9

BNF Derivations n Pick a non-terminal <Sum> => 10/17/2021 10

BNF Derivations n Pick a non-terminal <Sum> => 10/17/2021 10

BNF Derivations Pick a rule and substitute: n <Sum> : : = <Sum> +

BNF Derivations Pick a rule and substitute: n <Sum> : : = <Sum> + <Sum> => <Sum> + <Sum > n 10/17/2021 11

BNF Derivations n Pick a non-terminal: <Sum> => <Sum> + <Sum > 10/17/2021 12

BNF Derivations n Pick a non-terminal: <Sum> => <Sum> + <Sum > 10/17/2021 12

BNF Derivations Pick a rule and substitute: n <Sum> : : = ( <Sum>

BNF Derivations Pick a rule and substitute: n <Sum> : : = ( <Sum> ) <Sum> => <Sum> + <Sum > => ( <Sum> ) + <Sum> n 10/17/2021 13

BNF Derivations n Pick a non-terminal: <Sum> => <Sum> + <Sum > => (

BNF Derivations n Pick a non-terminal: <Sum> => <Sum> + <Sum > => ( <Sum> ) + <Sum> 10/17/2021 14

BNF Derivations Pick a rule and substitute: n <Sum> : : = <Sum> +

BNF Derivations Pick a rule and substitute: n <Sum> : : = <Sum> + <Sum> => <Sum> + <Sum > => ( <Sum> ) + <Sum> => ( <Sum> + <Sum> ) + <Sum> n 10/17/2021 15

BNF Derivations n Pick a non-terminal: <Sum> => <Sum> + <Sum > => (

BNF Derivations n Pick a non-terminal: <Sum> => <Sum> + <Sum > => ( <Sum> ) + <Sum> => ( <Sum> + <Sum> ) + <Sum> 10/17/2021 16

BNF Derivations Pick a rule and substitute: n <Sum >: : = 1 <Sum>

BNF Derivations Pick a rule and substitute: n <Sum >: : = 1 <Sum> => <Sum> + <Sum > => ( <Sum> ) + <Sum> => ( <Sum> + 1 ) + <Sum> n 10/17/2021 17

BNF Derivations n Pick a non-terminal: <Sum> => <Sum> + <Sum > => (

BNF Derivations n Pick a non-terminal: <Sum> => <Sum> + <Sum > => ( <Sum> ) + <Sum> => ( <Sum> + 1 ) + <Sum> 10/17/2021 18

BNF Derivations Pick a rule and substitute: n <Sum >: : = 0 <Sum>

BNF Derivations Pick a rule and substitute: n <Sum >: : = 0 <Sum> => <Sum> + <Sum > => ( <Sum> ) + <Sum> => ( <Sum> + 1 ) + 0 n 10/17/2021 19

BNF Derivations n Pick a non-terminal: <Sum> => <Sum> + <Sum > => (

BNF Derivations n Pick a non-terminal: <Sum> => <Sum> + <Sum > => ( <Sum> ) + <Sum> => ( <Sum> + 1 ) + 0 10/17/2021 20

BNF Derivations Pick a rule and substitute n <Sum> : : = 0 <Sum>

BNF Derivations Pick a rule and substitute n <Sum> : : = 0 <Sum> => <Sum> + <Sum > => ( <Sum> ) + <Sum> => ( <Sum> + 1 ) 0 => ( 0 + 1 ) + 0 n 10/17/2021 21

BNF Derivations n ( 0 + 1 ) + 0 is generated by grammar

BNF Derivations n ( 0 + 1 ) + 0 is generated by grammar <Sum> => <Sum> + <Sum > => ( <Sum> ) + <Sum> => ( <Sum> + 1 ) + 0 => ( 0 + 1 ) + 0 10/17/2021 22

<Sum> : : = 0 | 1 | <Sum> + <Sum> | (<Sum>) <Sum>

<Sum> : : = 0 | 1 | <Sum> + <Sum> | (<Sum>) <Sum> => 10/17/2021 23

BNF Semantics n The language of a BNF grammar is the set of all

BNF Semantics n The language of a BNF grammar is the set of all strings of terminals that can be derived from the Start symbol 10/17/2021 24

Extended BNF Grammars n n n Alternatives: allow rules of from X : :

Extended BNF Grammars n n n Alternatives: allow rules of from X : : = y | z n Abbreviates X : : = y, X : : = z Options: X : : = y[v]z n Abbreviates X : : = yvz, X : : = yz Repetition: X : : = y{v}*z n Can be eliminated by adding new nonterminal V and rules X : : = yz, X : : = y. Vz, V : : = v. V 10/17/2021 25

Regular Grammars Subclass of BNF n Only rules of form <nonterminal>: : =<terminal><nonterminal> or

Regular Grammars Subclass of BNF n Only rules of form <nonterminal>: : =<terminal><nonterminal> or <nonterminal>: : =<terminal> or <nonterminal>: : = ε n Defines same class of languages as regular expressions n Can be used for writing lexers n 10/17/2021 26

Example n n Regular grammar: <Balanced> : : = 0<One. And. More> <Balanced> :

Example n n Regular grammar: <Balanced> : : = 0<One. And. More> <Balanced> : : = 1<Zero. And. More> <One. And. More> : : = 1<Balanced> <Zero. And. More> : : = 0<Balanced> Generates even length strings where every initial substring of even length has same number of 0’s as 1’s 10/17/2021 27

Parse Trees n n Graphical representation of derivation Each node labeled with either non-terminal

Parse Trees n n Graphical representation of derivation Each node labeled with either non-terminal or terminal If node is labeled with a terminal, then it is a leaf (no sub-trees) If node is labeled with a non-terminal, then it has one branch for each character in the right-hand side of the production used on it 10/17/2021 28

Example n n Consider grammar: <exp> : : = <factor> | <factor> + <factor>

Example n n Consider grammar: <exp> : : = <factor> | <factor> + <factor> : : = <bin> | <bin> * <exp> <bin> : : = 0 | 1 Problem: Build parse tree for 1 * 1 + 0 as an <exp> 10/17/2021 29

Example cont. n 1 * 1 + 0: <exp> is the start symbol for

Example cont. n 1 * 1 + 0: <exp> is the start symbol for this parse tree 10/17/2021 30

Example cont. n 1 * 1 + 0: <exp> <factor> Use rule: <exp> :

Example cont. n 1 * 1 + 0: <exp> <factor> Use rule: <exp> : : = <factor> 10/17/2021 31

Example cont. n 1 * 1 + 0: <exp> <factor> <bin> * <exp> Use

Example cont. n 1 * 1 + 0: <exp> <factor> <bin> * <exp> Use rule: <factor> : : = <bin> * <exp> 10/17/2021 32

Example cont. n 1 * 1 + 0: <exp> <factor> <bin> 1 * <exp>

Example cont. n 1 * 1 + 0: <exp> <factor> <bin> 1 * <exp> <factor> + <factor> Use rules: <bin> : : = 1 and <exp> : : = <factor> + <factor> 10/17/2021 33

Example cont. n 1 * 1 + 0: <exp> <factor> <bin> 1 * <exp>

Example cont. n 1 * 1 + 0: <exp> <factor> <bin> 1 * <exp> <factor> + <bin> <factor> <bin> Use rule: <factor> : : = <bin> 10/17/2021 34

Example cont. n 1 * 1 + 0: <exp> <factor> <bin> 1 * <exp>

Example cont. n 1 * 1 + 0: <exp> <factor> <bin> 1 * <exp> <factor> + <bin> 1 Use rules: <bin> : : = 1 | 0 10/17/2021 <factor> <bin> 0 35

Example cont. n 1 * 1 + 0: <exp> <factor> <bin> 1 * <exp>

Example cont. n 1 * 1 + 0: <exp> <factor> <bin> 1 * <exp> <factor> + <factor> <bin> 1 0 Fringe of tree is string generated by grammar 10/17/2021 36

Your Turn: 1 * 0 + 0 * 1 10/17/2021 37

Your Turn: 1 * 0 + 0 * 1 10/17/2021 37

Parse Tree Data Structures n Parse trees may be represented by OCaml datatypes (e.

Parse Tree Data Structures n Parse trees may be represented by OCaml datatypes (e. g. exp) n One datatype for each nonterminal n One constructor for each rule n Defined as mutually recursive collection of datatype declarations 10/17/2021 38

Example n n Recall grammar: <exp> : : = <factor> | <factor> + <factor>

Example n n Recall grammar: <exp> : : = <factor> | <factor> + <factor> : : = <bin> | <bin> * <exp> <bin> : : = 0 | 1 type exp = Factor 2 Exp of factor | Plus of factor * factor and factor = Bin 2 Factor of bin | Mult of bin * exp and bin = Zero | One 10/17/2021 39

Example cont. n 1 * 1 + 0: <exp> <factor> <bin> 1 10/17/2021 *

Example cont. n 1 * 1 + 0: <exp> <factor> <bin> 1 10/17/2021 * <exp> <factor> + <factor> <bin> 1 0 40

Example cont. n 1 * 1 + 0: <exp> <factor> <bin> 1 * <exp>

Example cont. n 1 * 1 + 0: <exp> <factor> <bin> 1 * <exp> <factor> + <factor> <bin> 1 0 Factor 2 Exp(Mult(One, Plus(Bin 2 Factor One, Bin 2 Factor Zero))) 10/17/2021 41

Ambiguous Grammars and Languages n n A BNF grammar is ambiguous if its language

Ambiguous Grammars and Languages n n A BNF grammar is ambiguous if its language contains strings for which there is more than one parse tree If all BNFs for a language are ambiguous then the language is inherently ambiguous 10/17/2021 42

Example: Ambiguous Grammar n 0+1+0 <Sum> <Sum> + <Sum> 0 0 10/17/2021 1 0

Example: Ambiguous Grammar n 0+1+0 <Sum> <Sum> + <Sum> 0 0 10/17/2021 1 0 <Sum> + <Sum> 1 0 43

Example n What is the result for: 3+4*5+6 10/17/2021 44

Example n What is the result for: 3+4*5+6 10/17/2021 44

Example What is the result for: 3+4*5+6 n Possible answers: n n n 10/17/2021

Example What is the result for: 3+4*5+6 n Possible answers: n n n 10/17/2021 41 47 29 77 = = ((3 + 4) * 5) + 6 3 + (4 * (5 + 6)) (3 + (4 * 5)) + 6 = 3 + ((4 * 5) + 6) (3 + 4) * (5 + 6) 45

Example n What is the value of: 7– 5– 2 10/17/2021 46

Example n What is the value of: 7– 5– 2 10/17/2021 46

Example n n What is the value of: 7– 5– 2 Possible answers: In

Example n n What is the value of: 7– 5– 2 Possible answers: In Pascal, C++, SML assoc. left 7 – 5 – 2 = (7 – 5) – 2 = 0 n In APL, associate to right 7 – 5 – 2 = 7 – (5 – 2) = 4 n 10/17/2021 47

Two Major Sources of Ambiguity Lack of determination of operator precedence n Lack of

Two Major Sources of Ambiguity Lack of determination of operator precedence n Lack of determination of operator associativity n n There may be other sources as well 10/17/2021 48

How to Enforce Associativity n n Have at most one recursive call per production

How to Enforce Associativity n n Have at most one recursive call per production When two or more recursive calls would be natural leave right-most one for right associativity, left-most one for left associativity 10/17/2021 49

Example <Sum> : : = 0 | 1 | <Sum> + <Sum> | (<Sum>)

Example <Sum> : : = 0 | 1 | <Sum> + <Sum> | (<Sum>) n Becomes n <Sum> : : = <Num> | <Num> + <Sum> n <Num> : : = 0 | 1 | (<Sum>) n 10/17/2021 50

Operator Precedence n n n Operators of highest precedence evaluated first (bind more tightly)

Operator Precedence n n n Operators of highest precedence evaluated first (bind more tightly) Precedence for infix binary operators as in following table Needs to be reflected in grammar 10/17/2021 51

Precedence Table - Sample Fortan Pascal C/C++ Ada highest ** *, / +, 10/17/2021

Precedence Table - Sample Fortan Pascal C/C++ Ada highest ** *, / +, 10/17/2021 *, /, div, mod +, - ++, -- ** *, /, % *, /, mod +, +, - SML div, mod, /, * +, -, ^ : : 52

First Example Again In any above language, 3 + 4 * 5 + 6

First Example Again In any above language, 3 + 4 * 5 + 6 = 29 n In APL, all infix operators have same precedence n n n Thus we still don’t know what the value is (handled by associativity) How do we handle precedence in grammar? 10/17/2021 53

Predence in Grammar n Higher precedence translates to deeper in the derivation chain Example:

Predence in Grammar n Higher precedence translates to deeper in the derivation chain Example: <exp> : : = <id> | <exp> + <exp> | <exp> * <exp> n n Becomes <exp> : : = <mult_exp> | <exp> + <mult_exp> : : = <id> | <mult_exp> * <id> 10/17/2021 54