11
7.2 Programming Languages - An Introduction to Informatics - 2008. 11. 17 WMN Lab. Hye-Jin Lee

7.2 Programming Languages - An Introduction to Informatics -

Embed Size (px)

DESCRIPTION

7.2 Programming Languages - An Introduction to Informatics -. 2008. 11. 17 WMN Lab. Hye -Jin Lee. outline. Grammar of Languages Syntax and Semantics Backus-Naur Form BNF Definition. Grammar of Languages. languages  a set of sentences sentence  subject predicate - PowerPoint PPT Presentation

Citation preview

Page 1: 7.2 Programming Languages - An Introduction to Informatics -

7.2 ProgrammingLanguages

- An Introduction to Informatics -

2008. 11. 17

WMN Lab. Hye-Jin Lee

Page 2: 7.2 Programming Languages - An Introduction to Informatics -

outline

• Grammar of Languages• Syntax and Semantics• Backus-Naur Form• BNF Definition

Page 3: 7.2 Programming Languages - An Introduction to Informatics -

Grammar of Languages

languages a set of sentences

sentence subject predicate

subject predicate conjunctive sentence

subject noun | pronoun

predicate verb | verb prepositional-phrase

noun MARY | SCHOOL | TOWN

pronoun HE

verb DRIVES | WALKS | GOES

prepositional-phrase preposition noun

preposition TO | FROM

e.g., {HE, MARY,DRIVES, WALKS, GOES, TO, FROM, TOWN, SCHOOL}

Page 4: 7.2 Programming Languages - An Introduction to Informatics -

Grammar of Languages

HE DRIVES FROMS SHOOL

pronoun verb preposition noun

prepositional-phrase

subject predicate

sentence

Figure 7-7 (a) Simple Sentence

Page 5: 7.2 Programming Languages - An Introduction to Informatics -

Syntax and Semantics

• The Syntax of a language is given by a set of composition rules called a grammar that defines the elements and structure of any well-formed sentence.

• The Semantics depends on both the partic-ular words in the sentence and the struc-ture of the sentence.

syntax

semanticsprogramming language

Page 6: 7.2 Programming Languages - An Introduction to Informatics -

Backus-Naur Form

• John Backus and Peter Naur developed a formal notation for specifying pro-gramming language syntax (in 1959/1960)

– Defines the language sentence generated production rule.

– Production rule• Left side – defined target• Right side – representation for the target defini-

tion.

Page 7: 7.2 Programming Languages - An Introduction to Informatics -

BNF Definition

• For example <integer> ::= <sign><digit sequence> | <digit se-

quence> <sign> ::= + | − <digit sequence> ::= <digit><digit sequence> | <digit> <digit> ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9

• Notational convention– Nonterminal symbols is distinguished by enclos-

ing them by brackets “<” and “>”.– ::= “is defined as”, “generates”, “produces”, “may

be rewritten as”, and “consists of”.– Elements of the vocabulary, sometimes called termi-

nal symbols, appear only in the right parts of rules. – | “or”

Page 8: 7.2 Programming Languages - An Introduction to Informatics -

BNF Definition

− 3 6 9

<sign> <digit> <digit> <digit>

<digit sequence>

<digit sequence>

<digit sequence>

<integer>

Figure 7-8 parse of an Integer

Page 9: 7.2 Programming Languages - An Introduction to Informatics -

BNF Definition

• A grammar should be designed not only to pro-duce the correct sentences of the language but also to have syntactic categories that corre-spond to the semantics of the language.

• For example – A + B X C + (E − F) <expression> ::= <term> | <addop><term> | <expression><addop><term>

<term> ::= <factor> | <term><multop> <factor>

<factor> ::= <unsigned integer> | <variable> | (<expression>)

<addop> ::= + | -

<multop> ::= x | /

<unsigned integer> ::= <digit> | <unsigned integer> <digit>

<digit> ::= 0|1|2|3|4|5|6|7|8|9

<variable> ::= <letter> | <variable><letter> | <variable><letter>

<letter> ::= A|B| … | Z

Page 10: 7.2 Programming Languages - An Introduction to Informatics -

BNF Definition

( ( ) ( ) ) ( )

<simple pairs> <simple pairs> <simple pairs>

<pairs> <pairs>

<pairs>

<simple pairs>

<pairs>

Fiqure 7-9 : Parse of a Parenthesis String

A + B x C + ( E - F )

l ao l mo l ao l ao l

v v v v v f f f f f t t t t e t e

e e f

t e

Page 11: 7.2 Programming Languages - An Introduction to Informatics -

Syntax Charts

• Visual expression of the grammar– Terminals– Non-Terminals

– Optional

– Zero-Or-More Repititions

+

number