syntax & semantic introduction organization of language description abstract syntax formal...

31
Syntax & Semantic Introduction Organization of Language Description Abstract Syntax Formal Syntax The Way of Writing Grammars Formal Semantic

Upload: rachel-nichols

Post on 26-Dec-2015

266 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Syntax & Semantic Introduction Organization of Language Description Abstract Syntax Formal Syntax The Way of Writing Grammars Formal Semantic

Syntax & Semantic Introduction Organization of Language

Description Abstract Syntax Formal Syntax The Way of Writing Grammars Formal Semantic

Page 2: Syntax & Semantic Introduction Organization of Language Description Abstract Syntax Formal Syntax The Way of Writing Grammars Formal Semantic

Introduction Language Description : syntax and semantic Syntax : how program in language are built

up Semantic : what program mean Dates represented by D (digit) and Symbol (/)

DD/DD/DDDD -->syntax01/02/2001 -->US = Jan 2, 2001

Others = Feb 1, 2001 Same syntax, different semantic

Page 3: Syntax & Semantic Introduction Organization of Language Description Abstract Syntax Formal Syntax The Way of Writing Grammars Formal Semantic

Organization of Language Description

Tutorials Reference Manuals Formal Definition

Page 4: Syntax & Semantic Introduction Organization of Language Description Abstract Syntax Formal Syntax The Way of Writing Grammars Formal Semantic

Tutorials

What the main constructs of the language are

How they are meant to be used Useful examples for imitating and

adapting Introduce syntax and semantics

gradually

Organization of Language Description

Page 5: Syntax & Semantic Introduction Organization of Language Description Abstract Syntax Formal Syntax The Way of Writing Grammars Formal Semantic

Reference Manuals

Describing the syntax and semantics Organized around the syntax Formal syntax and informal semantic Informal semantic : English

explanations and examples to the syntactic rules

Began with the Algol60 : free of ambiguities

Organization of Language Description

Page 6: Syntax & Semantic Introduction Organization of Language Description Abstract Syntax Formal Syntax The Way of Writing Grammars Formal Semantic

Formal Definition

Precise description of the syntax and semantics

Aimed at specialists Attaches semantic rules to the syntax Conflicting interpretations from English

explanation Precise formal notation for clarifying

subtle point

Organization of Language Description

Page 7: Syntax & Semantic Introduction Organization of Language Description Abstract Syntax Formal Syntax The Way of Writing Grammars Formal Semantic

Abstract Syntax Capture intent, independent of notation Intent : apply operator +to operands a and b

a + b +

(+ a b) a b

ADD a TO b The written representation is different, but

the abstract syntax is the same identifies the meaningful components of

each construct

Page 8: Syntax & Semantic Introduction Organization of Language Description Abstract Syntax Formal Syntax The Way of Writing Grammars Formal Semantic

Formal Syntax Consists of two layers

a lexical layer a grammar layer

Lexical syntax : the spelling of words Grammars : the key formalism for describing

syntax ; universal programming languageExample if (<expr>) <statement> if <expr> then <statement>

Page 9: Syntax & Semantic Introduction Organization of Language Description Abstract Syntax Formal Syntax The Way of Writing Grammars Formal Semantic

Lexical syntax

Tokens/Terminals : units in programming language

Lexical syntax : correspondence between the written representation (spelling) and the tokens or terminals in a grammar

Keywords : alphabetic character sequences ; unit in a language - if , while

Reserved words : keyword that can not be used as names

Formal Syntax

Page 10: Syntax & Semantic Introduction Organization of Language Description Abstract Syntax Formal Syntax The Way of Writing Grammars Formal Semantic

Context-Free Grammars

Concrete syntax : describes its written representation, including lexical details such as the placement of keywords and punctuation marks

Context-free grammar or grammars : notation for specifying concrete syntax

Formal Syntax

Page 11: Syntax & Semantic Introduction Organization of Language Description Abstract Syntax Formal Syntax The Way of Writing Grammars Formal Semantic

Context-Free Grammars (cont.)

Grammar has four parts A set of tokens or terminals : atomic symbols A set of nonterminals : variable representing

construct A set of rules (called productions) : identify

the component of construct• <nonterminal >::= terminal | <nonterminal >

A starting nonterminal : represents a main construct

Page 12: Syntax & Semantic Introduction Organization of Language Description Abstract Syntax Formal Syntax The Way of Writing Grammars Formal Semantic

The Way of Writing Grammars The productions are rules for building string Parse Trees : show how a string can be built

Notation to write grammar - Backus Naur Form (BNF) Extended BNF (EBNF) Syntax charts : graphical notation

Page 13: Syntax & Semantic Introduction Organization of Language Description Abstract Syntax Formal Syntax The Way of Writing Grammars Formal Semantic

BNF : arithmetic expressions

<expression > ::= <expression >+<term >| <expression >- <term >| <term >

<term > ::= <term >* <factor >| <term >/ <factor >| <factor >

<factor > ::= number| name| ‘(’ <expression > ‘)’

The Way of Writing Grammars

Page 14: Syntax & Semantic Introduction Organization of Language Description Abstract Syntax Formal Syntax The Way of Writing Grammars Formal Semantic

EBNF : arithmetic expressions

- <expression > ::= <term > { ( +| ) <term > }

<term > ::= <factor > { ( *|/ ) <factor >}

<factor > ::= ‘(’ <expression > ‘)’|name |number

The Way of Writing Grammars

Page 15: Syntax & Semantic Introduction Organization of Language Description Abstract Syntax Formal Syntax The Way of Writing Grammars Formal Semantic

Syntax Chart : arithmetic expressions

The Way of Writing Grammars

expression

+

-term

*/

factor

name

number

expression

factor

term

( )

Page 16: Syntax & Semantic Introduction Organization of Language Description Abstract Syntax Formal Syntax The Way of Writing Grammars Formal Semantic

Parse Tree : respect to arithmetic expr

essions

number7 * number7 - number4 * number2 *

number3 Expression

Expression

Term

Term

Term

Term

Term

factor

factorfactor

factorfactor

number7

number7

number4

number2

number3

-

* *

*

The Way of Writing Grammars

Page 17: Syntax & Semantic Introduction Organization of Language Description Abstract Syntax Formal Syntax The Way of Writing Grammars Formal Semantic

Assignment Draw a parse tree with respect to

the BNF grammar for arithmetic expression 2 + 3 ( 2 + 3 ) 2 + 3 * 5 ( 2 + 3 ) * 5 2 + ( 3 * 5 )

Page 18: Syntax & Semantic Introduction Organization of Language Description Abstract Syntax Formal Syntax The Way of Writing Grammars Formal Semantic

Assignment (cont.) Draw parse trees using the following gra

mmar S ::= id := expr

| if expr then S | if expr then S else S

| while expr do s | begin SL end

SL ::= S ; S ; SL

a. while expr do id := expr b. begin id := expr end c. if expr then if expr then S else S

Page 19: Syntax & Semantic Introduction Organization of Language Description Abstract Syntax Formal Syntax The Way of Writing Grammars Formal Semantic

Assignment (cont.) Write the following grammar in

Pascal or C or Java by using BNF or EBNF or syntax chart program (heading) statement identifier variable

Page 20: Syntax & Semantic Introduction Organization of Language Description Abstract Syntax Formal Syntax The Way of Writing Grammars Formal Semantic

Formal Semantic Static semantic : “compile-time” properties

- type correctness, translation- determined from the static text of a program, without running the program on actual data.

Dynamic semantic : “run-time” properties- value of expression- the effect of statement- determined by actually doing a computation

Page 21: Syntax & Semantic Introduction Organization of Language Description Abstract Syntax Formal Syntax The Way of Writing Grammars Formal Semantic

Semantics or meaning

Semantic : any property of a construct The semantic of an expression 2+3

Point of view SemanticAn expression evaluator its value : 5

A type checker type integerAn infix-to-postfix translator string: + 2

3

Page 22: Syntax & Semantic Introduction Organization of Language Description Abstract Syntax Formal Syntax The Way of Writing Grammars Formal Semantic

Semantic Methods

Several method for defining semantics

The approaches are used for differen t purposes by different communities.

The values of variables a and b in a+b depend on the environment.

An environment consists of bindings from variable to values.

Page 23: Syntax & Semantic Introduction Organization of Language Description Abstract Syntax Formal Syntax The Way of Writing Grammars Formal Semantic

Static semantic

Dynamic semantic

Semantic Methods (cont.)

Informal semantics Synthesized attributes Attribute grammars Natural semantics Operational semantics Denotational semantics Axiomatic semantics

(or proof rules)

Formal semantics

Page 24: Syntax & Semantic Introduction Organization of Language Description Abstract Syntax Formal Syntax The Way of Writing Grammars Formal Semantic

Synthesized Attributes

Synthesize the meaning of a construct from that of its components

The semantic of a construct : any quantities associated with the construct

attribute : a quantity associated with a construct

X.a : attribute a of X (nonterminal/terminal)

E.val : attribute val of Expression E

Semantic Methods

Page 25: Syntax & Semantic Introduction Organization of Language Description Abstract Syntax Formal Syntax The Way of Writing Grammars Formal Semantic

Synthesized Attributes (cont.)

To define the meaning of a construct Attribute : attach to grammar symbols Semantic rules : attach to production Synthesized attribute N.a : semantic rules

attach to productions with N on the left side Evaluation order

information flows bottom-up in a parse tree attribute value at a node : attributes at the

children of the node (its subtree)

Page 26: Syntax & Semantic Introduction Organization of Language Description Abstract Syntax Formal Syntax The Way of Writing Grammars Formal Semantic

Attribute Grammars

A generalization of synthesized attributes

The meaning of a construct depend on the surrounding context.

Attribute values can flow up and down a parse tree.

The meaning at a node depend on information from the rest of the tree

Semantic Methods

Page 27: Syntax & Semantic Introduction Organization of Language Description Abstract Syntax Formal Syntax The Way of Writing Grammars Formal Semantic

Attribute Grammars (cont.)

To define the meaning of a construct Attribute : attach to grammar symbols, each

attribute designate synthesized or inherited Semantic rules : attach to productions.

If nonterminal N appears on the left side of production, attach semantic rules defining the synthesized attribute of N.If nonterminal A appears on the right side of production, attach semantic rules defining the inherited attribute of A.

Page 28: Syntax & Semantic Introduction Organization of Language Description Abstract Syntax Formal Syntax The Way of Writing Grammars Formal Semantic

Natural Semantics

Associated logical rules with the syntax. The logical rules use to deduce the mea

ning of a construct. Abstract syntax num(val ) for numbers num is a token and val is its associated

value axioms : rules without conditions E : v refers to expression E has value v

Semantic Methods

Page 29: Syntax & Semantic Introduction Organization of Language Description Abstract Syntax Formal Syntax The Way of Writing Grammars Formal Semantic

Natural Semantics (cont.)

Each rule has a name. Conditions appear above a line. The conclusion appears below the line. E ::= num(val)

| plus E1

E2

| times E1

E 2

E 1 : v1 E 2 : v

2(sum)

plus E1

E 2 : v1

+v2

Page 30: Syntax & Semantic Introduction Organization of Language Description Abstract Syntax Formal Syntax The Way of Writing Grammars Formal Semantic

Natural Semantics (cont.)

The value of an expression depend on the values of the variable.

Environments : handle variable by binding names to values

Environment : object with two operations bind(x,v,env) : a new envi. that binds variable x

to value v; the binding of all other variables are as in the envi. env

lookup(x,env) : the value bound to x in envi env

Page 31: Syntax & Semantic Introduction Organization of Language Description Abstract Syntax Formal Syntax The Way of Writing Grammars Formal Semantic

Natural Semantics (cont.) The empty envi. nil bind no variables. env E : v

“In envi. env , expression E has value v.”

env E 1 : v1 env E 2 : v

2(sum)

env plus E1

E 2 : v1

+v2

env x : lookup(x,env)“ In envi. env , variable x has value lookup(x,env)”