where syntax meets semantics

51
Where Syntax Meets Semantics Chapter Three Modern Programming Languages, 2nd ed. 1 Spring 2012

Upload: tevy

Post on 25-Jan-2016

42 views

Category:

Documents


1 download

DESCRIPTION

Where Syntax Meets Semantics. Spring 2012. Three “Equivalent” Grammars. G1 :< subexp > ::= a | b | c | < subexp > - < subexp > G2 :< subexp > ::= < var > - < subexp > | < var > < var > ::= a | b | c G3 :< subexp > ::= < subexp > - < var > | < var > < var > ::= a | b | c. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Where Syntax Meets Semantics

Where Syntax Meets Semantics

Chapter Three Modern Programming Languages, 2nd ed. 1

Spring 2012

Page 2: Where Syntax Meets Semantics

Three “Equivalent” Grammars

Chapter Three Modern Programming Languages, 2nd ed. 2

G1: <subexp> ::= a | b | c | <subexp> - <subexp>

G2: <subexp> ::= <var> - <subexp> | <var><var> ::= a | b | c

G3: <subexp> ::= <subexp> - <var> | <var><var> ::= a | b | c

These grammars all define the same language: thelanguage of strings that contain one or more as, bs or cs separated by minus signs. But...

Page 3: Where Syntax Meets Semantics

Chapter Three Modern Programming Languages, 2nd ed. 3

<subexp>

<subexp> - <var>

a

<var>

<subexp> - <var>

b

c G3 parse tree:

<subexp>

<var> - <subexp>

c

<var>

<var> - <subexp>

b

a G2 parse tree:

a-(b-c)1-(2-3)=2

(a-b)-c(1-2)-3=-4

They produce different parse trees.

Page 4: Where Syntax Meets Semantics

Why Parse Trees Matter

We want the structure of the parse tree to correspond to the semantics of the string it generates

This makes grammar design much harder: we’re interested in the structure of each parse tree, not just in the generated string

Parse trees are where syntax meets semantics

Chapter Three Modern Programming Languages, 2nd ed. 4

Page 5: Where Syntax Meets Semantics

Outline

Operators Precedence Associativity Other ambiguities: dangling else Cluttered grammars Parse trees and EBNF Abstract syntax trees

Chapter Three Modern Programming Languages, 2nd ed. 5

Page 6: Where Syntax Meets Semantics

Operators

Special syntax for frequently-used simple operations like addition, subtraction, multiplication and division

The word operator refers both to the token used to specify the operation (like + and *) and to the operation itself

Usually predefined, but not always Usually a single token, but not always

Chapter Three Modern Programming Languages, 2nd ed. 6

Page 7: Where Syntax Meets Semantics

Operator Terminology

Operands are the inputs to an operator, like 1 and 2 in the expression 1+2

Unary operators take one operand: -1 Binary operators take two: 1+2 Ternary operators take three: a?b:c

Chapter Three Modern Programming Languages, 2nd ed. 7

Page 8: Where Syntax Meets Semantics

More Operator Terminology

In most programming languages, binary operators use an infix notation: a + b

Sometimes you see prefix notation: + a b Sometimes postfix notation: a b + Unary operators, similarly:

(Can’t be infix, of course) Can be prefix, as in -1 Can be postfix, as in a++

Chapter Three Modern Programming Languages, 2nd ed. 8

Page 9: Where Syntax Meets Semantics

Outline

Operators Precedence Associativity Other ambiguities: dangling else Cluttered grammars Parse trees and EBNF Abstract syntax trees

Chapter Three Modern Programming Languages, 2nd ed. 9

Page 10: Where Syntax Meets Semantics

Working Grammar

Chapter Three Modern Programming Languages, 2nd ed. 10

G4: <exp> ::= <exp> + <exp> | <exp> * <exp> | (<exp>) | a | b | c

This generates a language of arithmetic expressionsusing parentheses, the operators + and *, and thevariables a, b and c

Page 11: Where Syntax Meets Semantics

Issue #1: Precedence

Chapter Three Modern Programming Languages, 2nd ed. 11

Our grammar generates this tree for a+b*c. In left tree,the addition is performed before the multiplication,which is not the usual convention for operator precedence.

<exp>

<exp> * <exp>

a

<exp> + <exp>

b

c

<exp>

<exp> <exp>

<exp>

+

<exp>*a

b c

Page 12: Where Syntax Meets Semantics

Operator Precedence

Applies when the order of evaluation is not completely decided by parentheses

Each operator has a precedence level, and those with higher precedence are performed before those with lower precedence, as if parenthesized

Most languages put * at a higher precedence level than +, so that

a+b*c = a+(b*c)

Chapter Three Modern Programming Languages, 2nd ed. 12

Page 13: Where Syntax Meets Semantics

Precedence Examples

C (15 levels of precedence—too many?)

Pascal (5 levels—not enough?)

Smalltalk (1 level for all binary operators)

Chapter Three Modern Programming Languages, 2nd ed. 13

a = b < c ? * p + b * c : 1 << d ()

a <= 0 or 100 <= a

a + b * c

Error!

Can you read this statement?

or operator has higher order of precedence

Page 14: Where Syntax Meets Semantics

Operator Precedence Chart

Chapter Three Modern Programming Languages, 2nd ed. 14

Operator Type Operator Associativity

Primary Expression Operators () [] . -> expr++ expr-- left-to-right

Unary Operators* & + - ! ~ ++expr --expr (typecast) sizeof

right-to-left

Binary Operators

* / %

left-to-right

+ -

>> <<

< > <= >=

== !=

&

^

|

&&

||

Ternary Operator ?: right-to-left

Assignment Operators= += -= *= /= %= >>= <<= &= ^= |=

right-to-left

Comma , left-to-right

PascalThe priority given to the various operators, from highest to lowest, are NOT Negation / DIV MOD AND + - OR = <> < <= > >= IN The operators are always evaluated left to right

C

Page 15: Where Syntax Meets Semantics

Precedence In The Grammar

Chapter Three Modern Programming Languages, 2nd ed. 15

To fix the precedence problem, we modify the grammar sothat it is forced to put * below + in the parse tree.

G5: <exp> ::= <exp> + <exp> | <mulexp><mulexp> ::= <mulexp> * <mulexp>

| (<exp>) | a | b | c

G4: <exp> ::= <exp> + <exp> | <exp> * <exp> | (<exp>) | a | b | c

Page 16: Where Syntax Meets Semantics

Correct Precedence

Chapter Three Modern Programming Languages, 2nd ed. 16

Our new grammar generates this tree for a+b*c. It generates the same language as before, but no longer generates parsetrees with incorrect precedence.

<exp>

<exp> + <exp>

c

<mulexp> * <mulexp>

b

a

G5 parse tree: <mulexp> <mulexp>

Page 17: Where Syntax Meets Semantics

Higher Level of Precedence

To add a higher level of precedence, add another non-terminal

Chapter Three Modern Programming Languages, 2nd ed. 17

<exp> ::= <exp> + <exp> | <mulexp><mulexp> ::= <mulexp> * <rootexp> | <rootexp><rootexp> ::= (<exp>)| a | b | c

Page 18: Where Syntax Meets Semantics

Outline

Operators Precedence Associativity Other ambiguities: dangling else Cluttered grammars Parse trees and EBNF Abstract syntax trees

Chapter Three Modern Programming Languages, 2nd ed. 18

Page 19: Where Syntax Meets Semantics

Issue #2: Associativity

Chapter Three Modern Programming Languages, 2nd ed. 19

Our grammar G5 generates both these trees for a+b+c. The first one is not the usual convention for operator

associativity. The grammar for a language must generate only one parse

tree for each expression.

<exp>

<exp> + <exp>

c

<mulexp>

<exp> + <exp>

b

<mulexp> a

<mulexp>

<exp>

<exp> + <exp>

b

<mulexp>

<exp> + <exp>

a

<mulexp> c

<mulexp>

Right-associative parse tree. Left-associative parse tree.

Page 20: Where Syntax Meets Semantics

Operator Associativity

Applies when the order of evaluation is not decided by parentheses or by precedence

Left-associative operators group left to right: a+b+c+d = ((a+b)+c)+d

Right-associative operators group right to left: a+b+c+d = a+(b+(c+d))

Most operators in most languages are left-associative, but there are exceptions

Chapter Three Modern Programming Languages, 2nd ed. 20

Page 21: Where Syntax Meets Semantics

Associativity Examples C

ML

Fortran

Chapter Three Modern Programming Languages, 2nd ed. 21

a<<b<<c — most operators are left-associative a=b=0 — right-associative (assignment)

3-2-1 — most operators are left-associative1::2::nil — right-associative (list builder)

a/b*c — most operators are left-associativea**b**c — right-associative (exponentiation)

Page 22: Where Syntax Meets Semantics

Associativity In The Grammar

Chapter Three Modern Programming Languages, 2nd ed. 22

To fix the associativity problem, we modify the grammar tomake trees of +s grow down to the left (and likewise for *s)

G5: <exp> ::= <exp> + <exp> | <mulexp><mulexp> ::= <mulexp> * <mulexp>

| (<exp>) | a | b | c

G6: <exp> ::= <exp> + <mulexp> | <mulexp><mulexp> ::= <mulexp> * <rootexp> | <rootexp><rootexp> ::= (<exp>) | a | b | c

Change <exp> to <mulexp>

Page 23: Where Syntax Meets Semantics

Making a grammar right- or left-associative Associativity has to do with recursive non-

terminal <exp> ::= <exp> + <exp> the recursive non-terminal is

on both side of + so it is left- and right-associative <exp> ::= <exp> + <mulexp> the recursive non-

terminal is on left so left-associative <exp> ::= <mulexp> + <exp> the recursive non-

terminal is on right so right-associative

Chapter Three Modern Programming Languages, 2nd ed. 23

Making a left-associative operator requires a left-recursive production in the language; making a right-associative operator requires a right-recursive production.

Page 24: Where Syntax Meets Semantics

Correct Associativity

Chapter Three Modern Programming Languages, 2nd ed. 24

Our new grammar generates this tree for a+b+c. It generatesthe same language as before, but no longer generates trees withincorrect associativity.

<exp>

<exp> + <mulexp>

b

<rootexp>

<exp> + <mulexp>

<mulexp> c

<rootexp>

a

<rootexp>

Page 25: Where Syntax Meets Semantics

Practice

Chapter Three Modern Programming Languages, 2nd ed. 25

Starting with this grammar:

1.) Add a left-associative & operator, at lower precedencethan any of the others2.) Then add a right-associative ** operator, at higherprecedence than any of the others

G6: <exp> ::= <exp> + <mulexp> | <mulexp><mulexp> ::= <mulexp> * <rootexp> | <rootexp><rootexp> ::= (<exp>)

| a | b | c

Page 26: Where Syntax Meets Semantics

Practice

Chapter Three Modern Programming Languages, 2nd ed. 26

Solution:

1.) Add a left-associative & operator, at lower precedencethan any of the others2.) Then add a right-associative ** operator, at higherprecedence than any of the others

G6: <exp> ::= <exp> & <expplus> | <expplus><expplus> ::= <expplus> + <mulexp> | <mulexp><mulexp> ::= <mulexp> * <expexp> | <expexp><expexp> ::= <rootexp> ** <expexp> | <rootexp><rootexp> ::= (<exp>) | a | b | c

Page 27: Where Syntax Meets Semantics

Outline

Operators Precedence Associativity Other ambiguities: dangling else Cluttered grammars Parse trees and EBNF Abstract syntax trees

Chapter Three Modern Programming Languages, 2nd ed. 27

Page 28: Where Syntax Meets Semantics

Issue #3: Ambiguity G4 was ambiguous: it generated more than

one parse tree for the same string Fixing the associativity and precedence

problems eliminated all the ambiguity This is usually a good thing: the parse tree

corresponds to the meaning of the program, and we don’t want ambiguity about that

Not all ambiguity stems from confusion about precedence and associativity...

Chapter Three Modern Programming Languages, 2nd ed. 28

Page 29: Where Syntax Meets Semantics

Dangling Else In Grammars

Chapter Three Modern Programming Languages, 2nd ed. 29

<stmt> ::= <if-stmt> | s1 | s2<if-stmt> ::= if <expr> then <stmt> else <stmt> | if <expr> then <stmt><expr> ::= e1 | e2

This grammar has a classic “dangling-else ambiguity.” Thestatement we want to derive is

if e1 then if e2 then s1 else s2

and the next slide shows two different parse trees for it...

Page 30: Where Syntax Meets Semantics

Most languages that havethis problem choose thisparse tree: else goes withnearest unmatched then

<if-stmt>

if <exp> then <stmt> else <stmt>

e1 s2

if <exp> then <stmt>

<if-stmt>

e2 s1

<if-stmt>

if <exp> then <stmt>

e1

s2

if <exp> then <stmt> else <stmt>

<if-stmt>

e2 s1 Chapter Three Modern Programming Languages, 2nd ed. 30

Page 31: Where Syntax Meets Semantics

Eliminating The Ambiguity

Chapter Three Modern Programming Languages, 2nd ed. 31

We want to insist that if this expands into an if, that if mustalready have its own else. First, we make a new non-terminal<full-stmt> that generates everything <stmt> generates, exceptthat it can not generate if statements with no else:

<stmt> ::= <if-stmt> | s1 | s2<if-stmt> ::= if <expr> then <stmt> else <stmt> | if <expr> then <stmt><expr> ::= e1 | e2

<full-stmt> ::= <full-if> | s1 | s2<full-if> ::= if <expr> then <full-stmt> else <full-stmt>

Page 32: Where Syntax Meets Semantics

Eliminating The Ambiguity

Chapter Three Modern Programming Languages, 2nd ed. 32

Then we use the new non-terminal here.

The effect is that the new grammar can match an else partwith an if part only if all the nearer if parts are already matched.

<stmt> ::= <if-stmt> | s1 | s2<if-stmt> ::= if <expr> then <full-stmt> else <stmt> | if <expr> then <stmt><expr> ::= e1 | e2

<full-stmt> ::= <full-if> | s1 | s2<full-if> ::= if <expr> then <full-stmt> else <full-stmt>

Page 33: Where Syntax Meets Semantics

Correct Parse Tree

Chapter Three Modern Programming Languages, 2nd ed. 33

<if-stmt>

if <exp> then <stmt>

e1

s2

if <exp> then <full-stmt> else <stmt>

<if-stmt>

e2 s1

Page 34: Where Syntax Meets Semantics

Dangling Else

We fixed the grammar, but… The grammar trouble reflects a problem

with the language, which we did not change A chain of if-then-else constructs can be

very hard for people to read Especially true if some but not all of the

else parts are present

Chapter Three Modern Programming Languages, 2nd ed. 34

Page 35: Where Syntax Meets Semantics

Practice

Chapter Three Modern Programming Languages, 2nd ed. 35

int a=0;if (0==0) if (0==1) a=1;else a=2;

What is the value of a afterthis fragment executes?

Page 36: Where Syntax Meets Semantics

Clearer Styles

Chapter Three Modern Programming Languages, 2nd ed. 36

int a=0;if (0==0) if (0==1) a=1; else a=2;

int a=0;if (0==0) { if (0==1) a=1; else a=2;}

Better: correct indentation

Even better: use of a blockreinforces the structure

Page 37: Where Syntax Meets Semantics

Languages That Don’t Dangle

Some languages define if-then-else in a way that forces the programmer to be more clear Algol does not allow the then part to be

another if statement – though it can be a block containing an if statement

Ada requires each if statement to be terminated with an end if

Python requires nested if statement to be indented

Chapter Three Modern Programming Languages, 2nd ed. 37

Page 38: Where Syntax Meets Semantics

Outline

Operators Precedence Associativity Other ambiguities: dangling else Cluttered grammars Parse trees and EBNF Abstract syntax trees

Chapter Three Modern Programming Languages, 2nd ed. 38

Page 39: Where Syntax Meets Semantics

Clutter The new if-then-else grammar is harder for

people to read than the old one It has a lot of clutter: more productions and

more non-terminals Same with G4, G5 and G6: we eliminated

the ambiguity but made the grammar harder for people to read

This is not always the right trade-off

Chapter Three Modern Programming Languages, 2nd ed. 39

Page 40: Where Syntax Meets Semantics

Reminder: Multiple Audiences In Chapter 2 we saw that grammars have

multiple audiences: Novices want to find out what legal programs

look like Experts—advanced users and language system

implementers—want an exact, detailed definition Tools—parser and scanner generators—want an

exact, detailed definition in a particular, machine-readable form

Tools often need ambiguity eliminated, while people often prefer a more readable grammar

Chapter Three Modern Programming Languages, 2nd ed. 40

Page 41: Where Syntax Meets Semantics

Options

Rewrite grammar to eliminate ambiguity Leave ambiguity but explain in

accompanying text how things like associativity, precedence, and the dangling else should be parsed

Do both in separate grammars

Chapter Three Modern Programming Languages, 2nd ed. 41

Page 42: Where Syntax Meets Semantics

Outline

Operators Precedence Associativity Other ambiguities: dangling else Cluttered grammars Parse trees and EBNF Abstract syntax trees

Chapter Three Modern Programming Languages, 2nd ed. 42

Page 43: Where Syntax Meets Semantics

EBNF and Parse Trees

You know that {x} means "zero or more repetitions of x" in EBNF

So <exp> ::= <mulexp> {+ <mulexp>} should mean a <mulexp> followed by zero or more repetitions of "+ <mulexp>"

But what then is the associativity of that + operator? What kind of parse tree would be generated for a+a+a?

Chapter Three Modern Programming Languages, 2nd ed. 43

Page 44: Where Syntax Meets Semantics

EBNF and Associativity One approach:

Use {} anywhere it helps Add a paragraph of text dealing with ambiguities,

associativity of operators, etc. Another approach:

Define a convention: for example, that the form <exp> ::= <mulexp> {+ <mulexp>} will be used only for left-associative operators

Use explicitly recursive rules for anything unconventional:<expa> ::= <expb> [ = <expa> ]

Chapter Three Modern Programming Languages, 2nd ed. 44

Page 45: Where Syntax Meets Semantics

About Syntax Diagrams

Similar problem: what parse tree is generated?

As in EBNF applications, add a paragraph of text dealing with ambiguities, associativity, precedence, and so on

Chapter Three Modern Programming Languages, 2nd ed. 45

Page 46: Where Syntax Meets Semantics

Outline

Operators Precedence Associativity Other ambiguities: dangling else Cluttered grammars Parse trees and EBNF Abstract syntax trees

Chapter Three Modern Programming Languages, 2nd ed. 46

Page 47: Where Syntax Meets Semantics

Full-Size Grammars

In any realistically large language, there are many non-terminals

Especially true when in the cluttered but unambiguous form needed by parsing tools

Extra non-terminals guide construction of unique parse tree

Once parse tree is found, such non-terminals are no longer of interest

Chapter Three Modern Programming Languages, 2nd ed. 47

Page 48: Where Syntax Meets Semantics

Abstract Syntax Tree

Language systems usually store an abbreviated version of the parse tree called the abstract syntax tree

Details are implementation-dependent Usually, there is a node for every operation,

with a subtree for every operand

Chapter Three Modern Programming Languages, 2nd ed. 48

Page 49: Where Syntax Meets Semantics

Chapter Three Modern Programming Languages, 2nd ed. 49

<exp>

<exp> + <mulexp>

b

<rootexp>

<exp> + <mulexp>

<mulexp> c

<rootexp>

a

<rootexp> +

c

a

+

b

parse tree

abstract syntax tree

Page 50: Where Syntax Meets Semantics

Parsing, Revisited

When a language system parses a program, it goes through all the steps necessary to find the parse tree

But it usually does not construct an explicit representation of the parse tree in memory

Most systems construct an AST instead We will see ASTs again in Chapter 23

Chapter Three Modern Programming Languages, 2nd ed. 50

Page 51: Where Syntax Meets Semantics

Conclusion Grammars define syntax, and more They define not just a set of legal programs,

but a parse tree for each program The structure of a parse tree corresponds to

the order in which different parts of the program are to be executed

Thus, grammars contribute (a little) to the definition of semantics

Chapter Three Modern Programming Languages, 2nd ed. 51