syntax and semantic - silpakorn university · 2012-06-23 · formal methods of describing syntax a...

50
C H A P T E R TWO Syntax and Semantic Syntax and Semantic

Upload: others

Post on 16-Mar-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Syntax and Semantic - Silpakorn University · 2012-06-23 · Formal Methods of Describing Syntax A rule has a left-hand side (LH S) and a right-hand side (R HS), and consists of terminal

C H A P T E R TWO

Syntax and SemanticSyntax and Semantic

Page 2: Syntax and Semantic - Silpakorn University · 2012-06-23 · Formal Methods of Describing Syntax A rule has a left-hand side (LH S) and a right-hand side (R HS), and consists of terminal

Chapter 2 Topics

Introduction Organization of Language Description Describing Syntax Formal Methods of Describing Syntax The Way of Writing Grammars Formal Semantic Semantic

2

Introduction Organization of Language Description Describing Syntax Formal Methods of Describing Syntax The Way of Writing Grammars Formal Semantic Semantic

Page 3: Syntax and Semantic - Silpakorn University · 2012-06-23 · Formal Methods of Describing Syntax A rule has a left-hand side (LH S) and a right-hand side (R HS), and consists of terminal

Introduction

Who must use language definitions? Other language designers Implementers Programmers (the users of the language)

Syntax - the form or structure of theexpressions, statements, and program units

Semantics - the meaning of the expressions,statements, and program units

3

Who must use language definitions? Other language designers Implementers Programmers (the users of the language)

Syntax - the form or structure of theexpressions, statements, and program units

Semantics - the meaning of the expressions,statements, and program units

Page 4: Syntax and Semantic - Silpakorn University · 2012-06-23 · Formal Methods of Describing Syntax A rule has a left-hand side (LH S) and a right-hand side (R HS), and consists of terminal

Introduction

Language description syntax and semantic

Syntax how to write program

Semantic: what does program mean

4

Language description syntax and semantic

Syntax how to write program

Semantic: what does program mean

Page 5: Syntax and Semantic - Silpakorn University · 2012-06-23 · Formal Methods of Describing Syntax A rule has a left-hand side (LH S) and a right-hand side (R HS), and consists of terminal

Introduction

Dates represented byD (digit) and Symbol (/)

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

Others Feb 1, 2001

Same syntax, different semantic

5

Dates represented byD (digit) and Symbol (/)

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

Others Feb 1, 2001

Same syntax, different semantic

Page 6: Syntax and Semantic - Silpakorn University · 2012-06-23 · Formal Methods of Describing Syntax A rule has a left-hand side (LH S) and a right-hand side (R HS), and consists of terminal

Organization of LanguageDescription

Tutorials Reference Manuals Formal Definition

6

Tutorials Reference Manuals Formal Definition

Page 7: Syntax and Semantic - Silpakorn University · 2012-06-23 · Formal Methods of Describing Syntax A rule has a left-hand side (LH S) and a right-hand side (R HS), and consists of terminal

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

7

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

Page 8: Syntax and Semantic - Silpakorn University · 2012-06-23 · Formal Methods of Describing Syntax A rule has a left-hand side (LH S) and a right-hand side (R HS), and consists of terminal

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

8

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

Page 9: Syntax and Semantic - Silpakorn University · 2012-06-23 · Formal Methods of Describing Syntax A rule has a left-hand side (LH S) and a right-hand side (R HS), and consists of terminal

Formal Definition

Precise description of the syntax andsemantics

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

explanation Precise formal notation for clarifying

subtle point

9

Precise description of the syntax andsemantics

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

explanation Precise formal notation for clarifying

subtle point

Page 10: Syntax and Semantic - Silpakorn University · 2012-06-23 · Formal Methods of Describing Syntax A rule has a left-hand side (LH S) and a right-hand side (R HS), and consists of terminal

Syntactic Elements

Character set Identifiers Operator symbols Keywords / Reserved words Comments Separator & Brackets Expression Statements

10

Character set Identifiers Operator symbols Keywords / Reserved words Comments Separator & Brackets Expression Statements

Page 11: Syntax and Semantic - Silpakorn University · 2012-06-23 · Formal Methods of Describing Syntax A rule has a left-hand side (LH S) and a right-hand side (R HS), and consists of terminal

Describing Syntax

A sentence is a string of characters oversome alphabet

A language is a set of sentences A lexeme is the lowest level syntactic unit of

a language (e.g., *, sum, begin) A token is a category of lexemes (e.g.,

identifier)

11

A sentence is a string of characters oversome alphabet

A language is a set of sentences A lexeme is the lowest level syntactic unit of

a language (e.g., *, sum, begin) A token is a category of lexemes (e.g.,

identifier)

Page 12: Syntax and Semantic - Silpakorn University · 2012-06-23 · Formal Methods of Describing Syntax A rule has a left-hand side (LH S) and a right-hand side (R HS), and consists of terminal

A Program Fragment Viewed As aStream of Tokens

12

Page 13: Syntax and Semantic - Silpakorn University · 2012-06-23 · Formal Methods of Describing Syntax A rule has a left-hand side (LH S) and a right-hand side (R HS), and consists of terminal

Describing Syntax

Formal approaches to describing syntax: Recognizers - used in compilers Generators – generate the sentences of a

language (focus of this lecture)

13

Formal approaches to describing syntax: Recognizers - used in compilers Generators – generate the sentences of a

language (focus of this lecture)

Page 14: Syntax and Semantic - Silpakorn University · 2012-06-23 · Formal Methods of Describing Syntax A rule has a left-hand side (LH S) and a right-hand side (R HS), and consists of terminal

Formal Methods ofDescribing Syntax

Context-Free Grammars Developed by Noam Chomsky in the mid-

1950s Language generators, meant to describe the

syntax of natural languages Define a class of languages called context-free

languages

14

Context-Free Grammars Developed by Noam Chomsky in the mid-

1950s Language generators, meant to describe the

syntax of natural languages Define a class of languages called context-free

languages

Page 15: Syntax and Semantic - Silpakorn University · 2012-06-23 · Formal Methods of Describing Syntax A rule has a left-hand side (LH S) and a right-hand side (R HS), and consists of terminal

CFG for Thai

<ประโยค> -> <ประธาน><กริยา><กรรม><ประธาน> -> ฉัน | เธอ | เรา<กริยา> -> กิน | ตี<กรรม> -> ข้าว | สุนัข

<ประโยค> -> <ประธาน><กริยา><กรรม>ฉัน กิน ข้าวเธอ ตี ข้าว

15

<ประโยค> -> <ประธาน><กริยา><กรรม><ประธาน> -> ฉัน | เธอ | เรา<กริยา> -> กิน | ตี<กรรม> -> ข้าว | สุนัข

<ประโยค> -> <ประธาน><กริยา><กรรม>ฉัน กิน ข้าวเธอ ตี ข้าว

Page 16: Syntax and Semantic - Silpakorn University · 2012-06-23 · Formal Methods of Describing Syntax A rule has a left-hand side (LH S) and a right-hand side (R HS), and consists of terminal

Formal Methods ofDescribing Syntax

Backus-Naur Form (1959) Invented by Backus and Naur to describe

Algol 58 BNF is equivalent to context-free grammars A metalanguage is a language used to

describe another language.

16

Backus-Naur Form (1959) Invented by Backus and Naur to describe

Algol 58 BNF is equivalent to context-free grammars A metalanguage is a language used to

describe another language.

Page 17: Syntax and Semantic - Silpakorn University · 2012-06-23 · Formal Methods of Describing Syntax A rule has a left-hand side (LH S) and a right-hand side (R HS), and consists of terminal

Backus-Naur Form (1959)

BNF elements T - terminal symbols N - nonterminal symbols S - start symbol P - set of rules or production

17

BNF elements T - terminal symbols N - nonterminal symbols S - start symbol P - set of rules or production

Page 18: Syntax and Semantic - Silpakorn University · 2012-06-23 · Formal Methods of Describing Syntax A rule has a left-hand side (LH S) and a right-hand side (R HS), and consists of terminal

BNF grammar

Def: A grammar production has the formA -> ω where A is a nonterminal symbol

ω is a string of nonterminal andterminal symbols

This is a rule; it describes the structure of awhile statement

<while_stmt> while ( <logic_expr> ) <stmt>18

Def: A grammar production has the formA -> ω where A is a nonterminal symbol

ω is a string of nonterminal andterminal symbols

This is a rule; it describes the structure of awhile statement

<while_stmt> while ( <logic_expr> ) <stmt>

Page 19: Syntax and Semantic - Silpakorn University · 2012-06-23 · Formal Methods of Describing Syntax A rule has a left-hand side (LH S) and a right-hand side (R HS), and consists of terminal

Formal Methods ofDescribing Syntax

A rule has a left-hand side (LHS) and aright-hand side (RHS), and consists ofterminal and nonterminal symbols

A grammar is a finite nonempty set of rules An abstraction (or nonterminal symbol)

can have more than one RHS<stmt> <single_stmt>

| begin <stmt_list> end19

A rule has a left-hand side (LHS) and aright-hand side (RHS), and consists ofterminal and nonterminal symbols

A grammar is a finite nonempty set of rules An abstraction (or nonterminal symbol)

can have more than one RHS<stmt> <single_stmt>

| begin <stmt_list> end

Page 20: Syntax and Semantic - Silpakorn University · 2012-06-23 · Formal Methods of Describing Syntax A rule has a left-hand side (LH S) and a right-hand side (R HS), and consists of terminal

BNF Nonterminal Identifier Integer Expression Statement Program

Terminal The basic alphabet

from which programsare constructed

binaryDigit -> 0binaryDigit -> 1

binaryDigit -> 0 | 1

Integer -> Digit | Integer DigitDigit -> 0|1|2|3|4|5|6|7|7|8|9

Integer -> DigitInteger -> Integer DigitInteger -> Integer Integer DigitInteger -> Digit Digit

20

Nonterminal Identifier Integer Expression Statement Program

Terminal The basic alphabet

from which programsare constructed

binaryDigit -> 0binaryDigit -> 1

binaryDigit -> 0 | 1

Integer -> Digit | Integer DigitDigit -> 0|1|2|3|4|5|6|7|7|8|9

Integer -> DigitInteger -> Integer DigitInteger -> Integer Integer DigitInteger -> Digit Digit

Page 21: Syntax and Semantic - Silpakorn University · 2012-06-23 · Formal Methods of Describing Syntax A rule has a left-hand side (LH S) and a right-hand side (R HS), and consists of terminal

Formal Methods ofDescribing Syntax

Syntactic lists are described usingrecursion<ident_list> ident

| ident, <ident_list> A derivation is a repeated application of

rules, starting with the start symbol andending with a sentence (all terminalsymbols)

21

Syntactic lists are described usingrecursion<ident_list> ident

| ident, <ident_list> A derivation is a repeated application of

rules, starting with the start symbol andending with a sentence (all terminalsymbols)

Page 22: Syntax and Semantic - Silpakorn University · 2012-06-23 · Formal Methods of Describing Syntax A rule has a left-hand side (LH S) and a right-hand side (R HS), and consists of terminal

Formal Methods ofDescribing Syntax

An example grammar:<program> <stmts><stmts> <stmt> | <stmt> ; <stmts><stmt> <var> = <expr><var> a | b | c | d<expr> <term> + <term> | <term> - <term><term> <var> | const

22

An example grammar:<program> <stmts><stmts> <stmt> | <stmt> ; <stmts><stmt> <var> = <expr><var> a | b | c | d<expr> <term> + <term> | <term> - <term><term> <var> | const

Page 23: Syntax and Semantic - Silpakorn University · 2012-06-23 · Formal Methods of Describing Syntax A rule has a left-hand side (LH S) and a right-hand side (R HS), and consists of terminal

Derivation Grammar

Integer -> Digit | Integer DigitDigit -> 0|1|2|3|4|5|6|7|7|8|9

Is 352 an Integer?Integer => Integer Digit

=> Integer Digit Digit=> Digit Digit Digit=> 3 Digit Digit=> 35 Digit=> 352

23

GrammarInteger -> Digit | Integer DigitDigit -> 0|1|2|3|4|5|6|7|7|8|9

Is 352 an Integer?Integer => Integer Digit

=> Integer Digit Digit=> Digit Digit Digit=> 3 Digit Digit=> 35 Digit=> 352

Page 24: Syntax and Semantic - Silpakorn University · 2012-06-23 · Formal Methods of Describing Syntax A rule has a left-hand side (LH S) and a right-hand side (R HS), and consists of terminal

Derivation

Grammar<assign> -> <id> = <expr><id> -> A | B | C<expr> -> <id> + <expr>

| <id> * <expr>

| ( <expr> )

| <id>

StatementA = B * ( A + C )

Leftmost derivation<assign> => <id> = <expr>

=> A = <expr>=> A = <id> * <expr>=> A = B * <expr>=> A = B * ( <expr> )=> A = B * ( <id> + <expr>)=> A = B * ( A + <expr>)=> A = B * ( A + <id>)=> A = B * ( A + C )

24

Grammar<assign> -> <id> = <expr><id> -> A | B | C<expr> -> <id> + <expr>

| <id> * <expr>

| ( <expr> )

| <id>

StatementA = B * ( A + C )

Leftmost derivation<assign> => <id> = <expr>

=> A = <expr>=> A = <id> * <expr>=> A = B * <expr>=> A = B * ( <expr> )=> A = B * ( <id> + <expr>)=> A = B * ( A + <expr>)=> A = B * ( A + <id>)=> A = B * ( A + C )

Page 25: Syntax and Semantic - Silpakorn University · 2012-06-23 · Formal Methods of Describing Syntax A rule has a left-hand side (LH S) and a right-hand side (R HS), and consists of terminal

Formal Methods ofDescribing Syntax

An example derivation:<program> => <stmts>

=> <stmt>=> <var> = <expr>=> a = <expr>=> a = <term> + <term>=> a = <var> + <term>=> a = b + <term>=> a = b + const

25

An example derivation:<program> => <stmts>

=> <stmt>=> <var> = <expr>=> a = <expr>=> a = <term> + <term>=> a = <var> + <term>=> a = b + <term>=> a = b + const

Page 26: Syntax and Semantic - Silpakorn University · 2012-06-23 · Formal Methods of Describing Syntax A rule has a left-hand side (LH S) and a right-hand side (R HS), and consists of terminal

Derivation

Every string of symbols in the derivation is asentential form

A sentence is a sentential form that has onlyterminal symbols

A leftmost derivation is one in which theleftmost nonterminal in each sentential formis the one that is expanded

A derivation may be neither leftmost norrightmost

26

Every string of symbols in the derivation is asentential form

A sentence is a sentential form that has onlyterminal symbols

A leftmost derivation is one in which theleftmost nonterminal in each sentential formis the one that is expanded

A derivation may be neither leftmost norrightmost

Page 27: Syntax and Semantic - Silpakorn University · 2012-06-23 · Formal Methods of Describing Syntax A rule has a left-hand side (LH S) and a right-hand side (R HS), and consists of terminal

Parse Tree

A hierarchical representation of a derivation<program>

<stmts>

27

<stmt>

const

a

<var> = <expr>

<var>

b

<term> + <term>

Page 28: Syntax and Semantic - Silpakorn University · 2012-06-23 · Formal Methods of Describing Syntax A rule has a left-hand side (LH S) and a right-hand side (R HS), and consists of terminal

Parse Tree for the Expression x+2*y

28

Page 29: Syntax and Semantic - Silpakorn University · 2012-06-23 · Formal Methods of Describing Syntax A rule has a left-hand side (LH S) and a right-hand side (R HS), and consists of terminal

Formal Methods ofDescribing Syntax

A grammar is ambiguous if it generates asentential form that has two or moredistinct parse trees

29

Page 30: Syntax and Semantic - Silpakorn University · 2012-06-23 · Formal Methods of Describing Syntax A rule has a left-hand side (LH S) and a right-hand side (R HS), and consists of terminal

An Ambiguous Grammar<AmbExp> <Integer> | <AmbExp> - <AmbExp>

30

Two Different Parse Trees for the AmbExp 2 – 3 – 4

Page 31: Syntax and Semantic - Silpakorn University · 2012-06-23 · Formal Methods of Describing Syntax A rule has a left-hand side (LH S) and a right-hand side (R HS), and consists of terminal

Is the Grammar Ambiguous?

<expr> <expr> <op> <expr> | const<op> / | -

31

Page 32: Syntax and Semantic - Silpakorn University · 2012-06-23 · Formal Methods of Describing Syntax A rule has a left-hand side (LH S) and a right-hand side (R HS), and consists of terminal

Is the Grammar Ambiguous?Yes

<expr> <expr> <op> <expr> | const<op> / | -

<expr> <expr>

32

<expr> <expr>

<expr> <expr>

<expr> <expr>

<expr> <expr>

<op>

<op>

<op>

<op>

const const const const const const- -/ /

<op>

Page 33: Syntax and Semantic - Silpakorn University · 2012-06-23 · Formal Methods of Describing Syntax A rule has a left-hand side (LH S) and a right-hand side (R HS), and consists of terminal

An UnambiguousExpression Grammar

If we use the parse tree to indicateprecedence levels of the operators, wecannot have ambiguity

<expr> <expr> - <term> | <term><term> <term> / const | const

33

If we use the parse tree to indicateprecedence levels of the operators, wecannot have ambiguity

<expr> <expr> - <term> | <term><term> <term> / const | const<expr>

<expr> <term>

<term> <term>

const const

const/

-

Page 34: Syntax and Semantic - Silpakorn University · 2012-06-23 · Formal Methods of Describing Syntax A rule has a left-hand side (LH S) and a right-hand side (R HS), and consists of terminal

Formal Methods ofDescribing Syntax

Derivation:<expr> => <expr> - <term> => <term> - <term>

=> const - <term>=> const - <term> / const=> const - const / const

34

Derivation:<expr> => <expr> - <term> => <term> - <term>

=> const - <term>=> const - <term> / const=> const - const / const

Page 35: Syntax and Semantic - Silpakorn University · 2012-06-23 · Formal Methods of Describing Syntax A rule has a left-hand side (LH S) and a right-hand side (R HS), and consists of terminal

An Ambiguous If StatementThe “Dangling Else” Grammatical Ambiguity

35

Page 36: Syntax and Semantic - Silpakorn University · 2012-06-23 · Formal Methods of Describing Syntax A rule has a left-hand side (LH S) and a right-hand side (R HS), and consists of terminal

Formal Methods ofDescribing Syntax

Extended BNF (just abbreviations): Notation used in the course textbook Optional parts:<proc_call> -> ident ( <expr_list>)opt

Alternative parts:<term> -> <term> [+ | -] const

Put repetitions (0 or more) in braces ({ })<ident> -> letter {letter | digit}*

36

Extended BNF (just abbreviations): Notation used in the course textbook Optional parts:<proc_call> -> ident ( <expr_list>)opt

Alternative parts:<term> -> <term> [+ | -] const

Put repetitions (0 or more) in braces ({ })<ident> -> letter {letter | digit}*

Page 37: Syntax and Semantic - Silpakorn University · 2012-06-23 · Formal Methods of Describing Syntax A rule has a left-hand side (LH S) and a right-hand side (R HS), and consists of terminal

Formal Methods ofDescribing Syntax

Extended BNF (just abbreviations): Another frequently used notation Optional parts:<proc_call> -> ident [ ( <expr_list>)]

Alternative parts:<term> -> <term> (+ | -) const

Put repetitions (0 or more) in braces ({ })<ident> -> letter {letter | digit}

37

Extended BNF (just abbreviations): Another frequently used notation Optional parts:<proc_call> -> ident [ ( <expr_list>)]

Alternative parts:<term> -> <term> (+ | -) const

Put repetitions (0 or more) in braces ({ })<ident> -> letter {letter | digit}

Page 38: Syntax and Semantic - Silpakorn University · 2012-06-23 · Formal Methods of Describing Syntax A rule has a left-hand side (LH S) and a right-hand side (R HS), and consists of terminal

BNF and EBNF BNF:

<expr> <expr> + <term>| <expr> - <term>| <term>

<term> <term> * <factor>| <term> / <factor>| <factor>

EBNF:<expr> <term> {[+ | -] <term>}*<term> <factor> {[‘*’ | /] <factor>}*

38

BNF:<expr> <expr> + <term>

| <expr> - <term>| <term>

<term> <term> * <factor>| <term> / <factor>| <factor>

EBNF:<expr> <term> {[+ | -] <term>}*<term> <factor> {[‘*’ | /] <factor>}*

Page 39: Syntax and Semantic - Silpakorn University · 2012-06-23 · Formal Methods of Describing Syntax A rule has a left-hand side (LH S) and a right-hand side (R HS), and consists of terminal

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

39

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 40: Syntax and Semantic - Silpakorn University · 2012-06-23 · Formal Methods of Describing Syntax A rule has a left-hand side (LH S) and a right-hand side (R HS), and consists of terminal

BNF

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

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

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

40

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

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

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

Page 41: Syntax and Semantic - Silpakorn University · 2012-06-23 · Formal Methods of Describing Syntax A rule has a left-hand side (LH S) and a right-hand side (R HS), and consists of terminal

Extended BNF

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

| name| number

41

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

| name| number

Page 42: Syntax and Semantic - Silpakorn University · 2012-06-23 · Formal Methods of Describing Syntax A rule has a left-hand side (LH S) and a right-hand side (R HS), and consists of terminal

Can be used to visualize rules

Syntax Diagram

expression

+-

term

42

-term

*/

factor

name

number

expression

factor

( )

Page 43: Syntax and Semantic - Silpakorn University · 2012-06-23 · Formal Methods of Describing Syntax A rule has a left-hand side (LH S) and a right-hand side (R HS), and consists of terminal

Conventions for Writing Regular Expressions

43

Page 44: Syntax and Semantic - Silpakorn University · 2012-06-23 · Formal Methods of Describing Syntax A rule has a left-hand side (LH S) and a right-hand side (R HS), and consists of terminal

Semantics or meaning

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

Point of view SemanticAn expression evaluator its value : 5A type checker type integerAn infix-to-postfix translator string: + 2 3

44

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

Point of view SemanticAn expression evaluator its value : 5A type checker type integerAn infix-to-postfix translator string: + 2 3

Page 45: Syntax and Semantic - Silpakorn University · 2012-06-23 · Formal Methods of Describing Syntax A rule has a left-hand side (LH S) and a right-hand side (R HS), and consists of terminal

Formal Semantic

Static semantic : “compile-time” properties- type correctness, translation- determined from the static text of aprogram,

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

45

Static semantic : “compile-time” properties- type correctness, translation- determined from the static text of aprogram,

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 46: Syntax and Semantic - Silpakorn University · 2012-06-23 · Formal Methods of Describing Syntax A rule has a left-hand side (LH S) and a right-hand side (R HS), and consists of terminal

Semantic Methods

Several method for defining semantics The approaches are used for different

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.

46

Several method for defining semantics The approaches are used for different

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 47: Syntax and Semantic - Silpakorn University · 2012-06-23 · Formal Methods of Describing Syntax A rule has a left-hand side (LH S) and a right-hand side (R HS), and consists of terminal

Assignment

Draw a parse tree using BNF grammaron slide page 40 2 + 3 ( 2 + 3 ) 2 + 3 * 5 ( 2 + 3 ) * 5 2 + ( 3 * 5 )

47

Draw a parse tree using BNF grammaron slide page 40 2 + 3 ( 2 + 3 ) 2 + 3 * 5 ( 2 + 3 ) * 5 2 + ( 3 * 5 )

Page 48: Syntax and Semantic - Silpakorn University · 2012-06-23 · Formal Methods of Describing Syntax A rule has a left-hand side (LH S) and a right-hand side (R HS), and consists of terminal

Assignment (cont.)

Draw parse trees using the followinggrammar

while expr do id := expr begin id := expr end if expr then if expr then

S else S

S ::= id := expr| if expr then S| if expr then Selse S

| while expr do S| begin SL end

SL ::= S ;| S ; SL

48

Draw parse trees using the followinggrammar

while expr do id := expr begin id := expr end if expr then if expr then

S else S

S ::= id := expr| if expr then S| if expr then Selse S

| while expr do S| begin SL end

SL ::= S ;| S ; SL

Page 49: Syntax and Semantic - Silpakorn University · 2012-06-23 · Formal Methods of Describing Syntax A rule has a left-hand side (LH S) and a right-hand side (R HS), and consists of terminal

Assignment (cont.)

Write the grammar in any language by usingBNF or EBNF or syntax diagram

Write the keywords in that languages

Deadline : next class

49

Write the grammar in any language by usingBNF or EBNF or syntax diagram

Write the keywords in that languages

Deadline : next class

Page 50: Syntax and Semantic - Silpakorn University · 2012-06-23 · Formal Methods of Describing Syntax A rule has a left-hand side (LH S) and a right-hand side (R HS), and consists of terminal

References

Books Programming Languages: Principles and Paradigms –

Allen B. Tucker & Robert E. Noonan Concepts of Programming languages – Robert W. Sebesta

Java BNF http://cui.unige.ch/db-

research/Enseignement/analyseinfo/JAVA/BNFindex.html

50

Books Programming Languages: Principles and Paradigms –

Allen B. Tucker & Robert E. Noonan Concepts of Programming languages – Robert W. Sebesta

Java BNF http://cui.unige.ch/db-

research/Enseignement/analyseinfo/JAVA/BNFindex.html