Upload
cruz-chen
View
25
Download
2
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
7.2 ProgrammingLanguages
- 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
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}
Grammar of Languages
HE DRIVES FROMS SHOOL
pronoun verb preposition noun
prepositional-phrase
subject predicate
sentence
Figure 7-7 (a) Simple Sentence
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
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.
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”
BNF Definition
− 3 6 9
<sign> <digit> <digit> <digit>
<digit sequence>
<digit sequence>
<digit sequence>
<integer>
Figure 7-8 parse of an Integer
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
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
Syntax Charts
• Visual expression of the grammar– Terminals– Non-Terminals
– Optional
– Zero-Or-More Repititions
+
number