02 Syntax Semantics

download 02 Syntax Semantics

of 47

Transcript of 02 Syntax Semantics

  • 7/27/2019 02 Syntax Semantics

    1/47

    Defining a Programming Language

    How are programming languages actually defined? Say that you want to

    invent a nifty new language that you think will take the world bystormwhat do you do?

    There are two basic things that you need to specify:

    What do programs look like in your language (i.e., the syntax)

    Given a program in your language, what does it do when you executeit (i.e., the semantics)

    These are two very different things: the syntax only says what things looklike; it says nothing about what things mean. We tend to use mnemonicnames in our syntax (like if...else, while, etc), but to the computer thosenames are meaningless. Its the semantics that actually specifies how toexecute a program.

    Ben Hardekopf () Syntax and Semantics Spring 2013 1 / 41

  • 7/27/2019 02 Syntax Semantics

    2/47

    Syntax vs Semantics

    Quick Quiz (1 + (2 3)) 3

    What does the above expression evaluate to?

    A. 4 B. 10 C. 13

    Ben Hardekopf () Syntax and Semantics Spring 2013 2 / 41

  • 7/27/2019 02 Syntax Semantics

    3/47

    Syntax vs Semantics

    Quick Quiz (1 + (2 3)) 3

    What does the above expression evaluate to?

    A. 4 B. 10 C. 13

    Of course this is a trick question. The answer is: it depends on thesemantics! This is an important concept to internalize; otherwise you caneasily get tripped up when moving to a new language that has similarsyntax but different semantics.

    For example, when moving from Java to C++ the program excerpt:

    a = new int[5]; a[10] = 42;

    looks exactly the same in the two languages, but will have verydifferent behaviors.

    Ben Hardekopf () Syntax and Semantics Spring 2013 2 / 41

  • 7/27/2019 02 Syntax Semantics

    4/47

    How To Specify Syntax and Semantics

    OK, but how do we specify the syntax and semantics?

    Ben Hardekopf () Syntax and Semantics Spring 2013 3 / 41

  • 7/27/2019 02 Syntax Semantics

    5/47

    How To Specify Syntax and Semantics

    OK, but how do we specify the syntax and semantics?

    Syntax

    Its generally agreed that the best method is Context-Free Grammars,the same mathematical formalism you studied in CS 138; everyone usesthis method. CS 160 covers this topic in great detail.

    Ben Hardekopf () Syntax and Semantics Spring 2013 3 / 41

  • 7/27/2019 02 Syntax Semantics

    6/47

    How To Specify Syntax and Semantics

    OK, but how do we specify the syntax and semantics?

    Syntax

    Its generally agreed that the best method is Context-Free Grammars,the same mathematical formalism you studied in CS 138; everyone usesthis method. CS 160 covers this topic in great detail.

    SemanticsReference implementation

    Too specific, and too complex because of optimizations

    Prose description

    Too imprecise and confusing, too large and unwieldyFormal semantics Requires mathematical sophistication

    Definitional interpreters Goal is clarity and precision, not performance

    Ben Hardekopf () Syntax and Semantics Spring 2013 3 / 41

  • 7/27/2019 02 Syntax Semantics

    7/47

    Section Outline

    Defining Programming Languages

    Defining syntax Concrete and abstract syntax, ASTs The syntax of basic miniJS Implementing basic miniJS syntax in Scala

    Defining semantics Interpreter architectures

    A definitional interpreter for basic miniJS

    Ben Hardekopf () Syntax and Semantics Spring 2013 4 / 41

  • 7/27/2019 02 Syntax Semantics

    8/47

    Defining Syntax

    Well only look at the very basic concepts of defining syntax (this topic iscovered in much greater detail in CS 160). There are two kinds of syntax

    that a language developer needs to worry about: concrete and abstract.

    Definition (Concrete Syntax)

    This is the syntax that is visible to the programmer, that they type into

    the computer to write a program. To the computer, this is just ameaningless sequence of characters.

    Definition (Abstract Syntax)

    This is the internal syntax that the interpreter sees; its a simplified,structured representation of the program that the interpreter can traverse.

    Concrete Syntax parser Abstract Syntax Tree

    Ben Hardekopf () Syntax and Semantics Spring 2013 5 / 41

  • 7/27/2019 02 Syntax Semantics

    9/47

    Abstract Syntax Tree (AST)

    Example (Concrete vs Abstract Syntax)Concrete Expression

    (2/ 1 + 3) 4

    Abstract Syntax Tree

    4+

    3

    12

    In this class well only worry about defining the AST data structure; weassume that the parsing is done for us.

    Ben Hardekopf () Syntax and Semantics Spring 2013 6 / 41

  • 7/27/2019 02 Syntax Semantics

    10/47

    Example: AST for Arithmetic Expressions

    Example (Arithmetic Expressions)a AExp ::= n Z | a1 + a2 | a1 a2 | a1 a2 | a1 a2

    We use the usual convention that a is a metavariable standing for some

    member of AExp and that a1 and a2 are distinct members of AExp. Thinkof the as as non-terminals and the other symbols as terminals.

    This definition says that an arithmetic expression is either an integer n ortwo arithmetic expressions joined together by one of +,,, or . Notice

    that the definition is recursive, in the sense that a is used in its owndefinitionthis simple grammar defines an infinite number of possiblearithmetic expressions.

    Ben Hardekopf () Syntax and Semantics Spring 2013 7 / 41

  • 7/27/2019 02 Syntax Semantics

    11/47

    ASTs Resolve Ambiguity

    The concrete syntax needs to prevent ambiguity syntactically (e.g., by

    adding parentheses and braces). The abstract syntax doesnt need toworry about ambiguity because its represented using an AST.

    Example (Two possible ASTs for the expression 1 + 2 3 4)

    43

    +

    21

    +

    4

    32

    1

    The structure of the AST resolves any possible ambiguity.

    Ben Hardekopf () Syntax and Semantics Spring 2013 8 / 41

  • 7/27/2019 02 Syntax Semantics

    12/47

    AExp AST in Scala

    Scala AST for Arithmetic Expressions

    sealed abstract class AExpcase class Num(n:BigInt) extends AExp

    case class Add(a1:AExp, a2:AExp) extends AExp

    case class Sub(a1:AExp, a2:AExp) extends AExp

    case class Mul(a1:AExp, a2:AExp) extends AExp

    case class Div(a1:AExp, a2:AExp) extends AExp

    Example (AST on left, Scala on Right)

    43

    +

    21

    Sub(Add(Num(1), Num(2)),

    Mul(Num(3), Num(4))

    )

    Ben Hardekopf () Syntax and Semantics Spring 2013 9 / 41

  • 7/27/2019 02 Syntax Semantics

    13/47

    Defining Syntax for Basic miniJS

    As an example, well define the abstract syntax for a simple subset of

    miniJS then show (1) how the abstract syntax differs from the concretesyntax, and (2) how the abstract syntax is defined in Scala.

    Basic miniJS Language Features:

    Integers, booleans, and strings (with appropriate operators)Assignments

    Conditionals (if...else)

    While loops

    Sequencing

    Input/Output

    Ben Hardekopf () Syntax and Semantics Spring 2013 10 / 41

  • 7/27/2019 02 Syntax Semantics

    14/47

    Basic miniJS Abstract Syntax

    n Z b Bool s String x Variable

    t Term ::= c | e

    c Cmd ::= t | x := e | while e t | output ee Exp ::= n | b | s | undef | x | e | e1 e2

    | if e t1 else t2 | input typ | var xi in t

    typ InputType ::= num | str

    UnOp ::= | BinOp ::= + | | | | | | = | = | |