Docsity

Syntax Meets Semantics - Modern Programming Languages - Lecture Slides, Slides of Advanced Computer Programming

Partial preview of the text

Download Syntax Meets Semantics - Modern Programming Languages - Lecture Slides and more Slides Advanced Computer Programming in PDF only on Docsity!

Where Syntax Meets Semantics

Chapter Three Modern Programming Languages, 2nd ed. (^) Docsity.com 1

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: the language of strings that contain one or more a s, b s or c s separated by minus signs. But...

Docsity.com

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. (^) Docsity.com 4

Outline

 Operators

 Precedence

 Associativity

 Other ambiguities: dangling else

 Cluttered grammars

 Parse trees and EBNF

 Abstract syntax trees

Chapter Three Modern Programming Languages, 2nd ed. (^) Docsity.com 5

Operator Terminology

 Operands are the inputs to an operator, like

1 and 2 in the expression 1+

 Unary operators take one operand: -

 Binary operators take two: 1+

 Ternary operators take three: a?b:c

Chapter Three Modern Programming Languages, 2nd ed. (^) Docsity.com 7

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 -
  • Can be postfix, as in a++

Chapter Three Modern Programming Languages, 2nd ed. (^) Docsity.com 8

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 expressions using parentheses, the operators + and ***** , and the variables a , b and c

Docsity.com

Issue #1: Precedence

Chapter Three Modern Programming Languages, 2nd ed. 11

Our grammar generates this tree for a+bc*. In this 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

Docsity.com

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!

Docsity.com

Precedence In The Grammar

Chapter Three Modern Programming Languages, 2nd ed. 14

To fix the precedence problem, we modify the grammar so that 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

Docsity.com

Outline

 Operators

 Precedence

 Associativity

 Other ambiguities: dangling else

 Cluttered grammars

 Parse trees and EBNF

 Abstract syntax trees

Chapter Three Modern Programming Languages, 2nd ed. (^) Docsity.com 16

Issue #2: Associativity

Chapter Three Modern Programming Languages, 2nd ed. 17

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

< exp >

< exp > + < exp >

c

< mulexp >

< exp > + < exp >

b

a^ < mulexp >

< mulexp >

< exp >

< exp > + < exp >

b

< mulexp >

< exp > + < exp >

a

< mulexp > (^) c

< mulexp >

Docsity.com

Associativity Examples

 C

 ML

 Fortran

Chapter Three Modern Programming Languages, 2nd ed. 19

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

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

a/bc* — most operators are left-associative abc — right-associative (exponentiation)

Docsity.com

Associativity In The Grammar

Chapter Three Modern Programming Languages, 2nd ed. 20

To fix the associativity problem, we modify the grammar to make 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

Docsity.com