Академический Документы
Профессиональный Документы
Культура Документы
Roadmap
Ch. 1
Ch. 2
Classification of languages
What make a good language?
Evolution of languages
Ch. 3
Ch. 4
Ch. 5
Ch. 7
Ch. 15
Outline
Introduction
(Sec. 3.1)
The General Problem of Describing Syntax
(Sec. 3.2)
Formal Methods of Describing Syntax (Sec.
3.3)
Attribute Grammars (Sec. 3.4)
Describing the Meanings of Programs:
Dynamic Semantics (Sec. 3.5)
What
Description of a Language
Syntax:
Semantics:
So,
Semantics
<statement>
Detecting
Outline
Introduction
(Sec. 3.1)
The General Problem of Describing Syntax
(Sec. 3.2)
Formal Methods of Describing Syntax (Sec.
3.3)
Attribute Grammars (Sec. 3.4)
Describing the Meanings of Programs:
Dynamic Semantics (Sec. 3.5)
What is a Language?
In
Every
A Sentence in C Language
The
main()
{ printf("hello, world!\n"); }
as
a identifier
b string c(
d) e{ f} g;
So, symbolically Hello World program can be
represented by the sentence: acdeacbdgf
where main and printf are identifiers and
hello, world!\n is a string
10
main()
{ printf("hello, world!\n") }
Compiler will say there is a syntax error
In essence, it says the sentence acdeacbdf is
not in C language
11
Backend:
12
Definition of a Language
The
we actually say
13
Syntax Rules
Consider
14
Syntax Rules
A
A
hierarchical
structure of
language
15
Idea:
Chinese
Syntax
rules
English
Sentence
Alphabet
a,b,c,d,
18
Outline
Introduction
(Sec. 3.1)
The General Problem of Describing Syntax
(Sec. 3.2)
Formal Methods of Describing Syntax (Sec.
3.3)
Issues in Grammar Definitions: Ambiguity,
Precedence, Associativity,
Attribute
1950s
Define a class of languages: context-free
languages
20
BNF Terminologies
A
<verb>
The start symbol, e.g., <sentence>
The set of production rules, e.g.,
21
21
BNF Terminologies
Tokens
syntax
Non-terminals
syntax
The
start symbol is the particular nonterminal that forms the starting point of
generating a sentence of the language
22
22
BNF Rules
A
terminals
A rule tells how LHS can be replaced by RHS, or
how RHS is grouped together to form a larger
syntactic unit (LHS) traversing the parse tree up
and down
A nonterminal can have more than one RHS
A syntactic list can be described using recursion
<ident_list> ident |
ident, <ident_list>
23
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
<program> is the start symbol
a, b, c, const,+,-,;,= are the terminals
24
Derivation
A
25
Derivation
Every
26
Parse Tree
A
hierarchical representation of a
derivation <program>
<stmts>
<stmt>
<var>
<expr>
<term>
<term>
<var>
const
a = b + const
27
28
29
Compiler Note
Compiler
30
Outline
Introduction
(Sec. 3.1)
The General Problem of Describing Syntax
(Sec. 3.2)
Formal Methods of Describing Syntax (Sec.
3.3)
Issues in Grammar Definitions: Ambiguity,
Precedence, Associativity,
Attribute
G2:
G3:
<subexp>
G2 parse tree:
<var>
<subexp>
<var>
<subexp>
What are
the
difference
s?
G3 parse tree:
<var>
c
<subexp>
<subexp>
<subexp>
<var>
a
<var>
<var>
33
Ambiguity in Grammars
If
sentence
<exp> a+b*c
<exp> + <exp> | <exp> * <exp>
| (<exp>)| a | b | c
34
An Ambiguous Grammar
Two
<exp>
<exp>
a
<exp>
<exp>
<exp>
<exp>
b
Means (a+b)*c
+
<exp>
<exp>
*
<exp>
Means a+(b*c)
35
Consequences
The
Cause
of the problem:
Grammar lacks semantic of operator
precedence
Applies when the order of evaluation is not
36
37
Correct Precedence
<exp>
<exp>
G5 parse tree:
<mulexp>
a
<exp>
<mulexp>
<mulexp> * <mulexp>
b
Semantics of Associativity
Grammar
<exp>
<exp>
<exp>
<mulexp>
<exp>
<mulexp>
<mulexp>
<mulexp>
<mulexp>
<exp>
<exp>
<exp>
<exp>
<mulexp>
c
39
Operator Associativity
Applies
a=b=0
right-associative (assignment)
40
Associativity Matters
Addition
is associative in mathematics?
(A + B) + C = A + (B + C)
Addition
is associative in computers?
Subtraction
Correct Associativity
<exp>
<exp>
<exp>
<mulexp>
<rootexp>
+
<mulexp>
<rootexp>
<mulexp>
<rootexp>
c
a
43
44
Different
Parse Trees
<if-stmt>
if
<exp>
then
e1
if
<exp>
<stmt>
else
s2
<if-stmt>
then
<stmt>
<stmt>
e2
s1
<if-stmt>
<exp>
then
e1
<exp>
e2
then
<stmt>
<if-stmt>
<stmt>
s1
else
<stmt>
s2
45
48
Extended BNF
Optional
EBNF