110 likes | 240 Views
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
E N D
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 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 particular words in the sentence and the structure of the sentence. syntax programming language semantics
Backus-Naur Form • John Backus and Peter Naur developed a formal notation for specifying programming language syntax (in 1959/1960) • Defines the language sentence generated production rule. • Production rule • Left side – defined target • Right side – representation for the target definition.
BNF Definition • For example <integer> ::= <sign><digit sequence> | <digit sequence> <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 enclosing them by brackets “<” and “>”. • ::= “is defined as”, “generates”, “produces”, “may be rewritten as”, and “consists of”. • Elements of the vocabulary, sometimes called terminal 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 produce the correct sentences of the language but also to have syntactic categories that correspond 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 ( ( ) ( ) ) ( ) A + B x C + ( E - F ) <simple pairs> <simple pairs> <simple pairs> l aol mo l ao l aol <pairs> <pairs> v v v v v <pairs> f f f f f <simple pairs> t t t t <pairs> e t e Fiqure 7-9 : Parse of a Parenthesis String e e f t e
Syntax Charts • Visual expression of the grammar • Terminals • Non-Terminals • Optional • Zero-Or-More Repititions + number