C H A P T E R T W O
description
Transcript of C H A P T E R T W O
![Page 1: C H A P T E R T W O](https://reader034.fdocuments.us/reader034/viewer/2022051517/5681495c550346895db6ae89/html5/thumbnails/1.jpg)
C H A P T E R T W O
Syntax and Semantic
![Page 2: C H A P T E R T W O](https://reader034.fdocuments.us/reader034/viewer/2022051517/5681495c550346895db6ae89/html5/thumbnails/2.jpg)
2
Introduction
Who must use language definitions? Other language designers Implementors Programmers (the users of the language)
Syntax - the form or structure of the expressions, statements, and program units
Semantics - the meaning of the expressions, statements, and program units
![Page 3: C H A P T E R T W O](https://reader034.fdocuments.us/reader034/viewer/2022051517/5681495c550346895db6ae89/html5/thumbnails/3.jpg)
3
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 4: C H A P T E R T W O](https://reader034.fdocuments.us/reader034/viewer/2022051517/5681495c550346895db6ae89/html5/thumbnails/4.jpg)
4
Organization of Language Description
Tutorials Reference Manuals Formal Definition
![Page 5: C H A P T E R T W O](https://reader034.fdocuments.us/reader034/viewer/2022051517/5681495c550346895db6ae89/html5/thumbnails/5.jpg)
5
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
![Page 6: C H A P T E R T W O](https://reader034.fdocuments.us/reader034/viewer/2022051517/5681495c550346895db6ae89/html5/thumbnails/6.jpg)
6
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
![Page 7: C H A P T E R T W O](https://reader034.fdocuments.us/reader034/viewer/2022051517/5681495c550346895db6ae89/html5/thumbnails/7.jpg)
7
Formal Definition
Precise description of the syntax and semantics
Aimed at specialists Attaches semantic rules to the syntax Conflicting interpretations from English expla
nation Precise formal notation for clarifying
subtle point
![Page 8: C H A P T E R T W O](https://reader034.fdocuments.us/reader034/viewer/2022051517/5681495c550346895db6ae89/html5/thumbnails/8.jpg)
8
Describing Syntax
A sentence is a string of characters over some 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 9: C H A P T E R T W O](https://reader034.fdocuments.us/reader034/viewer/2022051517/5681495c550346895db6ae89/html5/thumbnails/9.jpg)
9
A Program Fragment Viewed As a Stream of Tokens
![Page 10: C H A P T E R T W O](https://reader034.fdocuments.us/reader034/viewer/2022051517/5681495c550346895db6ae89/html5/thumbnails/10.jpg)
10
Describing Syntax
Formal approaches to describing syntax: Recognizers - used in compilers Generators – generate the sentences of a
language (focus of this lecture)
![Page 11: C H A P T E R T W O](https://reader034.fdocuments.us/reader034/viewer/2022051517/5681495c550346895db6ae89/html5/thumbnails/11.jpg)
11
Formal Methods of Describing 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
![Page 12: C H A P T E R T W O](https://reader034.fdocuments.us/reader034/viewer/2022051517/5681495c550346895db6ae89/html5/thumbnails/12.jpg)
12
CFG for Thai
<ประโยค> -> <ประธาน><กร�ยา><กรรม><ประธาน> -> ฉั�น | เธอ | เรา<กร�ยา> -> ก�น | ตี�<กรรม> -> ข้�าว | สุ�น�ข้
<ประโยค> -> <ประธาน><กร�ยา><กรรม>ฉั�น ก�น ข้�าวเธอ ตี� ข้�าว
![Page 13: C H A P T E R T W O](https://reader034.fdocuments.us/reader034/viewer/2022051517/5681495c550346895db6ae89/html5/thumbnails/13.jpg)
13
Formal Methods of Describing 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.
![Page 14: C H A P T E R T W O](https://reader034.fdocuments.us/reader034/viewer/2022051517/5681495c550346895db6ae89/html5/thumbnails/14.jpg)
14
Backus-Naur Form (1959)
Def: A grammar production has the form
A -> ω where A is a nonterminal symbol
ω is a string of nonterminal and terminal symbols
This is a rule; it describes the structure of a while statement
<while_stmt> while ( <logic_expr> ) <stmt>
![Page 15: C H A P T E R T W O](https://reader034.fdocuments.us/reader034/viewer/2022051517/5681495c550346895db6ae89/html5/thumbnails/15.jpg)
15
Formal Methods of Describing Syntax
A rule has a left-hand side (LHS) and a right-hand side (RHS), and consists of terminal 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 16: C H A P T E R T W O](https://reader034.fdocuments.us/reader034/viewer/2022051517/5681495c550346895db6ae89/html5/thumbnails/16.jpg)
16
BNF Nonterminal
Identifier Integer Expression Statement Program
Terminal The basic alphabet from
which programs are constructed
binaryDigit -> 0binaryDigit -> 1
binaryDigit -> 0 | 1
Integer -> Digit | Integer DigitDigit -> 0|1|2|3|4|5|6|7|7|8|9
Integer -> Digit Integer -> Integer DigitInteger -> Integer Integer DigitInteger -> Digit Digit
![Page 17: C H A P T E R T W O](https://reader034.fdocuments.us/reader034/viewer/2022051517/5681495c550346895db6ae89/html5/thumbnails/17.jpg)
17
Formal Methods of Describing Syntax
Syntactic lists are described using recursion
<ident_list> ident
| ident, <ident_list> A derivation is a repeated application of
rules, starting with the start symbol and ending with a sentence (all terminal symbols)
![Page 18: C H A P T E R T W O](https://reader034.fdocuments.us/reader034/viewer/2022051517/5681495c550346895db6ae89/html5/thumbnails/18.jpg)
18
Formal Methods of Describing 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
![Page 19: C H A P T E R T W O](https://reader034.fdocuments.us/reader034/viewer/2022051517/5681495c550346895db6ae89/html5/thumbnails/19.jpg)
19
Derivation
GrammarInteger -> Digit | Integer DigitDigit -> 0|1|2|3|4|5|6|7|7|8|9
Is 352 an Integer? Integer => Integer Digit
=> Integer Integer Digit=> Digit Digit Digit=> 3 Digit Digit=> 35 Digit=> 352
![Page 20: C H A P T E R T W O](https://reader034.fdocuments.us/reader034/viewer/2022051517/5681495c550346895db6ae89/html5/thumbnails/20.jpg)
20
Formal Methods of Describing Syntax
An example derivation:
<program> => <stmts>
=> <stmt>
=> <var> = <expr>
=> a = <expr>
=> a = <term> + <term>
=> a = <var> + <term>
=> a = b + <term>
=> a = b + const
![Page 21: C H A P T E R T W O](https://reader034.fdocuments.us/reader034/viewer/2022051517/5681495c550346895db6ae89/html5/thumbnails/21.jpg)
21
Derivation
Every string of symbols in the derivation is a sentential form
A sentence is a sentential form that has only terminal symbols
A leftmost derivation is one in which the leftmost nonterminal in each sentential form is the one that is expanded
A derivation may be neither leftmost nor rightmost
![Page 22: C H A P T E R T W O](https://reader034.fdocuments.us/reader034/viewer/2022051517/5681495c550346895db6ae89/html5/thumbnails/22.jpg)
22
Parse Tree
A hierarchical representation of a derivation <program>
<stmts>
<stmt>
const
a
<var> = <expr>
<var>
b
<term> + <term>
![Page 23: C H A P T E R T W O](https://reader034.fdocuments.us/reader034/viewer/2022051517/5681495c550346895db6ae89/html5/thumbnails/23.jpg)
23
Parse Tree for the Expression x+2*y
![Page 24: C H A P T E R T W O](https://reader034.fdocuments.us/reader034/viewer/2022051517/5681495c550346895db6ae89/html5/thumbnails/24.jpg)
24
Formal Methods of Describing Syntax
A grammar is ambiguous if it generates a sentential form that has two or more distinct parse trees
![Page 25: C H A P T E R T W O](https://reader034.fdocuments.us/reader034/viewer/2022051517/5681495c550346895db6ae89/html5/thumbnails/25.jpg)
25
An Ambiguous Grammar<AmbExp> <Integer> | <AmbExp> - <AmbExp>
Two Different Parse Trees for the AmbExp 2 – 3 – 4
![Page 26: C H A P T E R T W O](https://reader034.fdocuments.us/reader034/viewer/2022051517/5681495c550346895db6ae89/html5/thumbnails/26.jpg)
26
Is the Grammar Ambiguous?
<expr> <expr> <op> <expr> | const
<op> / | -
![Page 27: C H A P T E R T W O](https://reader034.fdocuments.us/reader034/viewer/2022051517/5681495c550346895db6ae89/html5/thumbnails/27.jpg)
27
Is the Grammar Ambiguous?Yes
<expr> <expr> <op> <expr> | const
<op> / | -
<expr>
<expr> <expr>
<expr> <expr>
<expr>
<expr> <expr>
<expr> <expr>
<op>
<op>
<op>
<op>
const const const const const const- -/ /
<op>
![Page 28: C H A P T E R T W O](https://reader034.fdocuments.us/reader034/viewer/2022051517/5681495c550346895db6ae89/html5/thumbnails/28.jpg)
28
An UnambiguousExpression Grammar
If we use the parse tree to indicate precedence levels of the operators, we cannot have ambiguity
<expr> <expr> - <term> | <term><term> <term> / const | const<expr>
<expr> <term>
<term> <term>
const const
const/
-
![Page 29: C H A P T E R T W O](https://reader034.fdocuments.us/reader034/viewer/2022051517/5681495c550346895db6ae89/html5/thumbnails/29.jpg)
29
Formal Methods of Describing Syntax
Derivation:<expr> => <expr> - <term> => <term> - <term>
=> const - <term>
=> const - <term> / const
=> const - const / const
![Page 30: C H A P T E R T W O](https://reader034.fdocuments.us/reader034/viewer/2022051517/5681495c550346895db6ae89/html5/thumbnails/30.jpg)
30
An Ambiguous If StatementThe “Dangling Else” Grammatical Ambiguity
![Page 31: C H A P T E R T W O](https://reader034.fdocuments.us/reader034/viewer/2022051517/5681495c550346895db6ae89/html5/thumbnails/31.jpg)
31
Formal Methods of Describing 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}*
![Page 32: C H A P T E R T W O](https://reader034.fdocuments.us/reader034/viewer/2022051517/5681495c550346895db6ae89/html5/thumbnails/32.jpg)
32
Formal Methods of Describing 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}
![Page 33: C H A P T E R T W O](https://reader034.fdocuments.us/reader034/viewer/2022051517/5681495c550346895db6ae89/html5/thumbnails/33.jpg)
33
BNF and EBNF
BNF: <expr> <expr> + <term> | <expr> - <term> | <term> <term> <term> * <factor> | <term> / <factor> | <factor> EBNF: <expr> <term> {[+ | -] <term>}* <term> <factor> {[‘*’ | /] <factor>}*
![Page 34: C H A P T E R T W O](https://reader034.fdocuments.us/reader034/viewer/2022051517/5681495c550346895db6ae89/html5/thumbnails/34.jpg)
34
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 35: C H A P T E R T W O](https://reader034.fdocuments.us/reader034/viewer/2022051517/5681495c550346895db6ae89/html5/thumbnails/35.jpg)
35
BNF
<expression > ::= <expression> + <term > - | <expression> <term> | <term>
<term > ::= <term> * <factor> | <term> / <factor> | <factor>
<factor > ::= number| name | ( <expression> )
![Page 36: C H A P T E R T W O](https://reader034.fdocuments.us/reader034/viewer/2022051517/5681495c550346895db6ae89/html5/thumbnails/36.jpg)
36
Extended BNF
<expression > - ::= <term> { ( + | ) <term> }
<term > ::= <factor> { ( * | / ) <factor> }
<factor > ::= ( <expression> ) | name | number
![Page 37: C H A P T E R T W O](https://reader034.fdocuments.us/reader034/viewer/2022051517/5681495c550346895db6ae89/html5/thumbnails/37.jpg)
37
Can be used to visualize rules
Syntax Diagram
expression
+-
term
*
/factor
name
number
expression
factor
term
( )
![Page 38: C H A P T E R T W O](https://reader034.fdocuments.us/reader034/viewer/2022051517/5681495c550346895db6ae89/html5/thumbnails/38.jpg)
38
Conventions for Writing Regular Expressions
![Page 39: C H A P T E R T W O](https://reader034.fdocuments.us/reader034/viewer/2022051517/5681495c550346895db6ae89/html5/thumbnails/39.jpg)
39
Assignment
Draw a parse tree with respect to the BNF grammar on page 35 2 3+ 2 3( + ) 523*5
2 3 *5( + ) 2 3*5+ ( )
![Page 40: C H A P T E R T W O](https://reader034.fdocuments.us/reader034/viewer/2022051517/5681495c550346895db6ae89/html5/thumbnails/40.jpg)
40
Assignment (cont.)
Draw parse trees using the following grammar
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 41: C H A P T E R T W O](https://reader034.fdocuments.us/reader034/viewer/2022051517/5681495c550346895db6ae89/html5/thumbnails/41.jpg)
41
Assignment (cont.)
Write the following grammar in Python or your chosen language by using BNF or EBNF or syntax diagram statement While statement (with some examples) If statement (with some examples)
Write the keywords in that languages
Deadline : next class (28/01/2552)
![Page 42: C H A P T E R T W O](https://reader034.fdocuments.us/reader034/viewer/2022051517/5681495c550346895db6ae89/html5/thumbnails/42.jpg)
42
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 43: C H A P T E R T W O](https://reader034.fdocuments.us/reader034/viewer/2022051517/5681495c550346895db6ae89/html5/thumbnails/43.jpg)
43
Semantics or meaning
Semantic : any property of a construct The semantic of an expression 2+3
Point of view Semantic
An expression evaluator its value : 5
A type checker type integer
An infix-to-postfix translator string: + 2 3
![Page 44: C H A P T E R T W O](https://reader034.fdocuments.us/reader034/viewer/2022051517/5681495c550346895db6ae89/html5/thumbnails/44.jpg)
44
Semantic Methods
Several method for defining semantics The approaches are used for different purpos
es 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 45: C H A P T E R T W O](https://reader034.fdocuments.us/reader034/viewer/2022051517/5681495c550346895db6ae89/html5/thumbnails/45.jpg)
45
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