03 - Click to edit Master subtitle style Chapter Three...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Click to edit Master subtitle style Chapter Three Modern Programming Languages, 2nd ed. Where Syntax Meets Semantics Chapter Three Modern Programming Languages, 2nd ed. 11 Chapter Three Modern Programming Languages, 2nd ed. Three Equivalent Grammars Chapter Three Modern Programming Languages, 2nd ed. 22 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... Chapter Three Modern Programming Languages, 2nd ed. Chapter Three Modern Programming Languages, 2nd ed. 33 < subexp > < subexp > - < var > a < var > < subexp > - < var > b c G3 parse tree: < subexp > < var > - < subexp > c < var > < var > - < subexp > b a G2 parse tree: Chapter Three Modern Programming Languages, 2nd ed. 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: were 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. 44 Chapter Three Modern Programming Languages, 2nd ed. Outline Operators Precedence Associativity Other ambiguities: dangling else Cluttered grammars Parse trees and EBNF Abstract syntax trees Chapter Three Modern Programming Languages, 2nd ed. 55 Chapter Three Modern Programming Languages, 2nd ed. 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. 66 Chapter Three Modern Programming Languages, 2nd ed. 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. 77 Chapter Three Modern Programming Languages, 2nd ed. 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: (Cant be infix, of course) Can be prefix, as in -1 Can be postfix, as in a++ Chapter Three Modern Programming Languages, 2nd ed....
View Full Document

This note was uploaded on 09/30/2011 for the course CS 6371 taught by Professor Hamlen during the Spring '11 term at University of Texas at Dallas, Richardson.

Page1 / 47

03 - Click to edit Master subtitle style Chapter Three...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online