Your SlideShare is downloading. ×
0
IN4303 2014/15 
Compiler Construction 
Declarative Syntax Definition 
grammars and trees 
Guido Wachsmuth
Exp 
Exp 
Exp Exp Exp 
Grammars and Trees 2 
3 * 7 + 21 
3 * 7 + 21 
parser
check parse 
Grammars and Trees 3 
syntax 
definition 
parse generate 
errors 
table 
generic 
parser
Grammars and Trees 
SDF3 
NaBL 
TS 
Stratego 
ESV 
editor 
SPT 
tests 
4 
syntax definition 
concrete syntax 
abstract syn...
Grammars and Trees 
SDF3 
NaBL 
TS 
Stratego 
ESV 
editor 
SPT 
tests 
5 
syntax definition 
concrete syntax 
abstract syn...
Grammars and Trees 
SDF3 
NaBL 
TS 
Stratego 
ESV 
editor 
SPT 
tests 
6 
syntax definition 
concrete syntax 
abstract syn...
P A R E N T A L 
ADVISORY 
THEORETICAL CONTENT 
Grammars and Trees 7
formal languages 
Grammars and Trees 8
Grammars and Trees 9 
infinite productivity
Grammars and Trees 10 
finite models
Grammars and Trees 11 
Philosophy 
Linguistics 
lexicology 
grammar 
morphology 
syntax 
phonology 
semantics 
Interdiscip...
Grammars and Trees 12 
Philosophy 
Linguistics 
lexicology 
grammar 
morphology 
syntax 
phonology 
semantics 
Interdiscip...
Grammars and Trees 13
Grammars and Trees 13 
vocabulary Σ 
finite, nonempty set of elements (words, letters) 
alphabet
Grammars and Trees 13 
vocabulary Σ 
finite, nonempty set of elements (words, letters) 
alphabet 
string over Σ 
finite se...
Grammars and Trees 13 
vocabulary Σ 
finite, nonempty set of elements (words, letters) 
alphabet 
string over Σ 
finite se...
formal grammars 
Grammars and Trees 14
Grammars and Trees 15 
formal grammar G 
derivation relation ⇒G 
formal language L(G) ⊆ Σ* 
L(G) = {w∈Σ* | S ⇒G* w}
Grammars and Trees 16 
G = (N, Σ, P, S) 
decimal numbers 
Num → Digit Num 
Num → Digit 
Digit → “0” 
Digit → “1” 
Digit → ...
Grammars and Trees 17 
G = (N, Σ, P, S) 
decimal numbers 
Num → Digit Num 
Num → Digit 
Digit → “0” 
Digit → “1” 
Digit → ...
Grammars and Trees 18 
G = (N, Σ, P, S) 
decimal numbers 
Num → Digit Num 
Num → Digit 
Digit → “0” 
Digit → “1” 
Digit → ...
Grammars and Trees 19 
G = (N, Σ, P, S) 
decimal numbers 
Num → Digit Num 
Num → Digit 
Digit → “0” 
Digit → “1” 
Digit → ...
Grammars and Trees 20 
G = (N, Σ, P, S) 
decimal numbers 
Num → Digit Num 
Num → Digit 
Digit → “0” 
Digit → “1” 
Digit → ...
Grammars and Trees 21 
Num ⇒ 
Digit Num ⇒ 
4 Num ⇒ 
4 Digit Num ⇒ 
4 3 Num ⇒ 
4 3 Digit Num ⇒ 
4 3 0 Num ⇒ 
4 3 0 Digit ⇒ ...
Grammars and Trees 21 
Num ⇒ 
Digit Num ⇒ 
4 Num ⇒ 
4 Digit Num ⇒ 
4 3 Num ⇒ 
4 3 Digit Num ⇒ 
4 3 0 Num ⇒ 
4 3 0 Digit ⇒ ...
Grammars and Trees 22 
decimal numbers 
Num ⇒ 
Digit Num ⇒ 
Digit Digit Num ⇒ 
Digit Digit Digit Num ⇒ 
Digit Digit Digit ...
Grammars and Trees 22 
decimal numbers 
Num ⇒ 
Digit Num ⇒ 
Digit Digit Num ⇒ 
Digit Digit Digit Num ⇒ 
Digit Digit Digit ...
Grammars and Trees 23 
G = (N, Σ, P, S) 
binary expressions 
Exp → Num 
Exp → Exp “+” Exp 
Exp → Exp “−” Exp 
Exp → Exp “*...
Grammars and Trees 24 
G = (N, Σ, P, S) 
binary expressions 
Exp → Num 
Exp → Exp “+” Exp 
Exp → Exp “−” Exp 
Exp → Exp “*...
Grammars and Trees 25 
G = (N, Σ, P, S) 
binary expressions 
Exp → Num 
Exp → Exp “+” Exp 
Exp → Exp “−” Exp 
Exp → Exp “*...
Grammars and Trees 26 
G = (N, Σ, P, S) 
binary expressions 
Exp → Num 
Exp → Exp “+” Exp 
Exp → Exp “−” Exp 
Exp → Exp “*...
Grammars and Trees 27 
G = (N, Σ, P, S) 
binary expressions 
Exp → Num 
Exp → Exp “+” Exp 
Exp → Exp “−” Exp 
Exp → Exp “*...
Grammars and Trees 28 
binary expressions 
Exp ⇒ 
Exp + Exp ⇒ 
Exp + Exp * Exp ⇒ 
3 + Exp * Exp ⇒ 
3 + 4 * Exp ⇒ 
3 + 4 * ...
Grammars and Trees 29 
formal grammar G = (N, Σ, P, S) 
nonterminal symbols N 
terminal symbols Σ 
production rules P ⊆ (N...
Grammars and Trees 29 
formal grammar G = (N, Σ, P, S) 
nonterminal symbol 
nonterminal symbols N 
terminal symbols Σ 
pro...
Grammars and Trees 29 
formal grammar G = (N, Σ, P, S) 
nonterminal symbols N 
terminal symbols Σ 
production rules P ⊆ (N...
Grammars and Trees 29 
formal grammar G = (N, Σ, P, S) 
nonterminal symbols N 
terminal symbols Σ 
production rules P ⊆ (N...
Grammars and Trees 29 
formal grammar G = (N, Σ, P, S) 
nonterminal symbols N 
terminal symbols Σ 
production rules P ⊆ (N...
Grammars and Trees 30 
type-0, unrestricted: P ⊆ (N∪Σ)* N (N∪Σ)* × (N∪Σ)* 
type-1, context-sensitive: (a A c, a b c) 
type...
Grammars and Trees 31 
formal grammars 
context-sensitive 
context-free 
regular
Grammars and Trees 32 
formal grammar G 
derivation relation ⇒G 
formal language L(G) ⊆ Σ* 
L(G) = {w∈Σ* | S ⇒G* w}
Grammars and Trees 33 
formal grammar G = (N, Σ, P, S) 
derivation relation ⇒G ⊆ (N∪Σ)* × (N∪Σ)* 
w ⇒G w’ ⇔ 
∃(p, q)∈P: ∃u...
Grammars and Trees 34 
formal languages 
context-sensitive 
context-free 
regular
Derivation is about productivity. 
But what about parsing? 
Exp 
Exp 
Exp Exp Exp 
Grammars and Trees 35 
3 * 7 + 21 
3 * ...
Grammars and Trees 36 
word problem
theoretical computer science 
Grammars and Trees 37 
word problem χL: Σ*→ {0,1} 
w → 1, if w∈L 
w → 0, else 
decidability ...
theoretical computer science 
Grammars and Trees 37 
decidability & complexity 
word problem χL: Σ*→ {0,1} 
w → 1, if w∈L ...
theoretical computer science 
Grammars and Trees 37 
decidability & complexity 
word problem χL: Σ*→ {0,1} 
w → 1, if w∈L ...
theoretical computer science 
Grammars and Trees 37 
decidability & complexity 
word problem χL: Σ*→ {0,1} 
w → 1, if w∈L ...
theoretical computer science 
Grammars and Trees 38 
decidability & complexity 
formal grammars 
context-sensitive 
contex...
theoretical computer science 
Grammars and Trees 38 
decidability & complexity 
formal grammars 
context-sensitive 
contex...
theoretical computer science 
Grammars and Trees 38 
decidability & complexity 
formal grammars 
context-sensitive 
contex...
Grammars and Trees 39 
context-free grammars 
production vs. reduction rules 
! 
Exp → Num 
Exp → Exp “+” Exp 
Exp → Exp “...
Grammars and Trees 39 
context-free grammars 
production vs. reduction rules 
! 
Exp → Num 
Exp → Exp “+” Exp 
Exp → Exp “...
Grammars and Trees 39 
context-free grammars 
production vs. reduction rules 
! 
Exp → Num 
Exp → Exp “+” Exp 
Exp → Exp “...
Grammars and Trees 39 
context-free grammars 
production vs. reduction rules 
! 
Exp → Num 
Exp → Exp “+” Exp 
Exp → Exp “...
Grammars and Trees 40 
binary expressions 
3 + 4 + 5 ⇒ 
Exp + 4 + 5 ⇒ 
Exp + Exp + 5 ⇒ 
Exp + Exp * Exp ⇒ 
Exp + Exp ⇒ 
Ex...
The word problem is about membership. 
But what about structure? 
Exp 
Exp 
Exp Exp Exp 
Grammars and Trees 41 
3 * 7 + 21...
Grammars and Trees 42 
syntax trees
Grammars and Trees 43 
context-free grammars 
Exp 
Num 
tree construction rules 
Exp 
Exp + Exp 
Exp 
Exp − Exp 
Exp 
Exp ...
Grammars and Trees 44 
binary expressions 
tree construction 
3 + 4 * 5
Grammars and Trees 44 
binary expressions 
tree construction 
Exp 
3 + 4 * 5
Grammars and Trees 44 
binary expressions 
tree construction 
Exp Exp 
3 + 4 * 5
Grammars and Trees 44 
binary expressions 
tree construction 
Exp Exp 
Exp 
3 + 4 * 5
Grammars and Trees 44 
binary expressions 
tree construction 
Exp 
Exp 
Exp 
Exp 
3 + 4 * 5
Grammars and Trees 44 
binary expressions 
tree construction 
Exp 
Exp 
Exp 
Exp 
Exp 
3 + 4 * 5
Exp 
Grammars and Trees 45 
binary expressions 
ambiguity 
Exp 
Exp 
Exp 
Exp 
Exp 
3 + 4 * 5 
Exp 
Exp 
Exp 
Exp 
3 + 4 *...
Grammars and Trees 46 
syntax trees 
context-free grammars 
ambiguity 
different trees for same sentence 
derivations 
dif...
parse trees & abstract syntax trees 
Grammars and Trees 47 
parse trees 
syntax trees 
parent node: nonterminal symbol 
ch...
Grammars and Trees 48 
binary expressions 
parse tree & abstract syntax tree 
Exp 
Exp 
Exp 
Exp Exp 
Exp 
3 + ( 4 * 5 )
Const 
Add 
Grammars and Trees 48 
binary expressions 
parse tree & abstract syntax tree 
Const 
Mul 
Const 
3 4 5 
Exp 
E...
Except where otherwise noted, this work is licensed under 
Grammars and Trees 49
Grammars and Trees 50 
attribution 
slide title author license 
1 The Pine, Saint Tropez Paul Signac public domain 
2, 3, ...
Upcoming SlideShare
Loading in...5
×

Declarative Syntax Definition - Grammars and Trees

3,451

Published on

This lecture lays the theoretic foundations for declarative syntax formalisms and syntax-based language processors, which we will discuss later in the course. We introduce the notions of formal languages, formal grammars, and syntax trees, starting from Chomsky's work on formal grammars as generative devices.

We start with a formal model of languages and investigate formal grammars and their derivation relations as finite models of infinite productivity. We further discuss several classes of formal grammars and their corresponding classes of formal languages. In a second step, we introduce the word problem, analyse its decidability and complexity for different classes of formal languages, and discuss consequences of this analysis on language processing. We conclude the lecture with a discussion about parse tree construction, abstract syntax trees, and ambiguities.

Published in: Education, Technology
1 Comment
4 Likes
Statistics
Notes
  • It is complex , but somewhat understandable .if its facilitated further, we will enjoy it the better.....great analysis ...
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total Views
3,451
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
207
Comments
1
Likes
4
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • expressions: binary expressions, method calls\n\nobject-orientation: classes, single inheritance, methods, no overloading\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • property of language\n\nsay what no-one said before\n\nholds for software languages as well\n
  • scientific challenge\n
  • \n
  • linguistic theory\n\nNoam Chomsky\n
  • linguistic theory\n\nNoam Chomsky\n
  • linguistic theory\n\nNoam Chomsky\n
  • restrictions on production rules => grammar classes\n
  • restrictions on production rules => grammar classes\n
  • restrictions on production rules => grammar classes\n
  • restrictions on production rules => grammar classes\n
  • restrictions on production rules => grammar classes\n
  • restrictions on production rules => grammar classes\n
  • restrictions on production rules => grammar classes\n
  • restrictions on production rules => grammar classes\n
  • computer science: lexical syntax\n\ncan write this as a regular grammar\n
  • computer science: lexical syntax\n\ncan write this as a regular grammar\n
  • computer science: lexical syntax\n\ncan write this as a regular grammar\n
  • computer science: lexical syntax\n\ncan write this as a regular grammar\n
  • computer science: lexical syntax\n\ncan write this as a regular grammar\n
  • computer science: lexical syntax\n\ncan write this as a regular grammar\n
  • computer science: lexical syntax\n\ncan write this as a regular grammar\n
  • computer science: lexical syntax\n\ncan write this as a regular grammar\n
  • computer science: lexical syntax\n\ncan write this as a regular grammar\n
  • computer science: lexical syntax\n\ncan write this as a regular grammar\n
  • computer science: lexical syntax\n\ncan write this as a regular grammar\n
  • computer science: lexical syntax\n\ncan write this as a regular grammar\n
  • generative device\n
  • generative device\n
  • generative device\n
  • generative device\n
  • generative device\n
  • generative device\n
  • generative device\n
  • generative device\n
  • generative device\n
  • generative device\n
  • generative device\n
  • generative device\n
  • generative device\n
  • generative device\n
  • generative device\n
  • generative device\n
  • generative device\n
  • generative device\n
  • generative device\n
  • generative device\n
  • generative device\n
  • generative device\n
  • generative device\n
  • generative device\n
  • generative device\n
  • generative device\n
  • generative device\n
  • generative device\n
  • generative device\n
  • generative device\n
  • generative device\n
  • generative device\n
  • generative device\n
  • other derivations possible\n
  • other derivations possible\n
  • other derivations possible\n
  • other derivations possible\n
  • other derivations possible\n
  • other derivations possible\n
  • other derivations possible\n
  • other derivations possible\n
  • other derivations possible\n
  • other derivations possible\n
  • other derivations possible\n
  • other derivations possible\n
  • other derivations possible\n
  • other derivations possible\n
  • other derivations possible\n
  • other derivations possible\n
  • other derivations possible\n
  • other derivations possible\n
  • other derivations possible\n
  • other derivations possible\n
  • other derivations possible\n
  • other derivations possible\n
  • other derivations possible\n
  • other derivations possible\n
  • other derivations possible\n
  • other derivations possible\n
  • other derivations possible\n
  • other derivations possible\n
  • other derivations possible\n
  • other derivations possible\n
  • other derivations possible\n
  • other derivations possible\n
  • other derivations possible\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • X* for zero or more X\n\nempty word\n\nbut no construct for separated repetition\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • All actual, detailed work on assignments must be individual work. \nYou are encouraged to discuss assignments, programming languages used to solve the assignments, their libraries, and general solution techniques in these languages with each other. \nBut if you do so, then you must acknowledge the people with whom you discussed at the top of your submission. \nYou should not look for assignment solutions elsewhere; but if material is taken from elsewhere, then you must acknowledge it. \n\nYou are not permitted to provide or receive any kind of solutions of assignments. \nThis includes partial, incomplete, or erroneous solutions. \nYou are also not permitted to provide or receive programming help from people other than the teaching assistants or the instructor of the course.\n\n
  • any kind of solution: an algorithm, code, intermediate solutions\nplagiarism: copying from others, from the web, mimic another ones solution \n\nAny violation of these rules will be reported as a suspected case of fraud to the Board of Examiners and handled according to the EEMCS Faculty's fraud procedure. \nIf the case is proven, a penalty will be imposed: a minimum of exclusion from the course for the duration of one academic year up to a maximum of a one-year exclusion form all courses at TU Delft. \nFor details on the procedure, see Section 2.1.26 in the faculty's Study Guide for MSc Programmes.\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • round-up on every lecture\n\nwhat to take with you\n\ncheck yourself, pre- and post-paration\n
  • \n
  • \n
  • Transcript of "Declarative Syntax Definition - Grammars and Trees"

    1. 1. IN4303 2014/15 Compiler Construction Declarative Syntax Definition grammars and trees Guido Wachsmuth
    2. 2. Exp Exp Exp Exp Exp Grammars and Trees 2 3 * 7 + 21 3 * 7 + 21 parser
    3. 3. check parse Grammars and Trees 3 syntax definition parse generate errors table generic parser
    4. 4. Grammars and Trees SDF3 NaBL TS Stratego ESV editor SPT tests 4 syntax definition concrete syntax abstract syntax static semantics name binding type system dynamic semantics translation interpretation
    5. 5. Grammars and Trees SDF3 NaBL TS Stratego ESV editor SPT tests 5 syntax definition concrete syntax abstract syntax static semantics name binding type system dynamic semantics translation interpretation
    6. 6. Grammars and Trees SDF3 NaBL TS Stratego ESV editor SPT tests 6 syntax definition concrete syntax abstract syntax static semantics name binding type system dynamic semantics translation interpretation
    7. 7. P A R E N T A L ADVISORY THEORETICAL CONTENT Grammars and Trees 7
    8. 8. formal languages Grammars and Trees 8
    9. 9. Grammars and Trees 9 infinite productivity
    10. 10. Grammars and Trees 10 finite models
    11. 11. Grammars and Trees 11 Philosophy Linguistics lexicology grammar morphology syntax phonology semantics Interdisciplinary Computer Science syntax semantics
    12. 12. Grammars and Trees 12 Philosophy Linguistics lexicology grammar morphology syntax phonology semantics Interdisciplinary Computer Science syntax semantics
    13. 13. Grammars and Trees 13
    14. 14. Grammars and Trees 13 vocabulary Σ finite, nonempty set of elements (words, letters) alphabet
    15. 15. Grammars and Trees 13 vocabulary Σ finite, nonempty set of elements (words, letters) alphabet string over Σ finite sequence of elements chosen from Σ word, sentence, utterance
    16. 16. Grammars and Trees 13 vocabulary Σ finite, nonempty set of elements (words, letters) alphabet string over Σ finite sequence of elements chosen from Σ word, sentence, utterance formal language λ set of strings over a vocabulary Σ λ ⊆ Σ*
    17. 17. formal grammars Grammars and Trees 14
    18. 18. Grammars and Trees 15 formal grammar G derivation relation ⇒G formal language L(G) ⊆ Σ* L(G) = {w∈Σ* | S ⇒G* w}
    19. 19. Grammars and Trees 16 G = (N, Σ, P, S) decimal numbers Num → Digit Num Num → Digit Digit → “0” Digit → “1” Digit → “2” Digit → “3” Digit → “4” Digit → “5” Digit → “6” Digit → “7” Digit → “8” Digit → “9” morphology
    20. 20. Grammars and Trees 17 G = (N, Σ, P, S) decimal numbers Num → Digit Num Num → Digit Digit → “0” Digit → “1” Digit → “2” Digit → “3” Digit → “4” Digit → “5” Digit → “6” Digit → “7” Digit → “8” Digit → “9” morphology Σ: finite set of terminal symbols
    21. 21. Grammars and Trees 18 G = (N, Σ, P, S) decimal numbers Num → Digit Num Num → Digit Digit → “0” Digit → “1” Digit → “2” Digit → “3” Digit → “4” Digit → “5” Digit → “6” Digit → “7” Digit → “8” Digit → “9” morphology Σ: finite set of terminal symbols N: finite set of non-terminal symbols
    22. 22. Grammars and Trees 19 G = (N, Σ, P, S) decimal numbers Num → Digit Num Num → Digit Digit → “0” Digit → “1” Digit → “2” Digit → “3” Digit → “4” Digit → “5” Digit → “6” Digit → “7” Digit → “8” Digit → “9” morphology Σ: finite set of terminal symbols N: finite set of non-terminal symbols S∈N: start symbol
    23. 23. Grammars and Trees 20 G = (N, Σ, P, S) decimal numbers Num → Digit Num Num → Digit Digit → “0” Digit → “1” Digit → “2” Digit → “3” Digit → “4” Digit → “5” Digit → “6” Digit → “7” Digit → “8” Digit → “9” morphology Σ: finite set of terminal symbols N: finite set of non-terminal symbols S∈N: start symbol P⊆N×(N∪Σ)*: set of production rules
    24. 24. Grammars and Trees 21 Num ⇒ Digit Num ⇒ 4 Num ⇒ 4 Digit Num ⇒ 4 3 Num ⇒ 4 3 Digit Num ⇒ 4 3 0 Num ⇒ 4 3 0 Digit ⇒ 4 3 0 3 decimal numbers production Num → Digit Num ⇒ Digit → “4” ⇒ Num → Digit Num ⇒ Digit → “3” ⇒ Num → Digit Num ⇒ Digit → “0” ⇒ Num → Digit ⇒ Digit → “3” ⇒
    25. 25. Grammars and Trees 21 Num ⇒ Digit Num ⇒ 4 Num ⇒ 4 Digit Num ⇒ 4 3 Num ⇒ 4 3 Digit Num ⇒ 4 3 0 Num ⇒ 4 3 0 Digit ⇒ 4 3 0 3 decimal numbers production Num → Digit Num ⇒ Digit → “4” ⇒ Num → Digit Num ⇒ Digit → “3” ⇒ Num → Digit Num ⇒ Digit → “0” ⇒ Num → Digit ⇒ Digit → “3” ⇒ leftmost derivation
    26. 26. Grammars and Trees 22 decimal numbers Num ⇒ Digit Num ⇒ Digit Digit Num ⇒ Digit Digit Digit Num ⇒ Digit Digit Digit Digit ⇒ Digit Digit Digit 3 ⇒ Digit Digit 0 3 ⇒ Digit 3 0 3 ⇒ 4 3 0 3 production Num → Digit Num ⇒ Num → Digit Num ⇒ Num → Digit Num ⇒ Num → Digit ⇒ Digit → “3” ⇒ ⇒ Digit → “0” ⇒ ⇒ Digit → “3” ⇒ Digit → “4” ⇒ ⇒
    27. 27. Grammars and Trees 22 decimal numbers Num ⇒ Digit Num ⇒ Digit Digit Num ⇒ Digit Digit Digit Num ⇒ Digit Digit Digit Digit ⇒ Digit Digit Digit 3 ⇒ Digit Digit 0 3 ⇒ Digit 3 0 3 ⇒ 4 3 0 3 production Num → Digit Num ⇒ Num → Digit Num ⇒ Num → Digit Num ⇒ Num → Digit ⇒ Digit → “3” ⇒ ⇒ Digit → “0” ⇒ ⇒ Digit → “3” ⇒ Digit → “4” ⇒ ⇒ rightmost derivation
    28. 28. Grammars and Trees 23 G = (N, Σ, P, S) binary expressions Exp → Num Exp → Exp “+” Exp Exp → Exp “−” Exp Exp → Exp “*” Exp Exp → Exp “/” Exp Exp → “(” Exp “)” syntax
    29. 29. Grammars and Trees 24 G = (N, Σ, P, S) binary expressions Exp → Num Exp → Exp “+” Exp Exp → Exp “−” Exp Exp → Exp “*” Exp Exp → Exp “/” Exp Exp → “(” Exp “)” syntax Σ: finite set of terminal symbols
    30. 30. Grammars and Trees 25 G = (N, Σ, P, S) binary expressions Exp → Num Exp → Exp “+” Exp Exp → Exp “−” Exp Exp → Exp “*” Exp Exp → Exp “/” Exp Exp → “(” Exp “)” syntax Σ: finite set of terminal symbols N: finite set of non-terminal symbols
    31. 31. Grammars and Trees 26 G = (N, Σ, P, S) binary expressions Exp → Num Exp → Exp “+” Exp Exp → Exp “−” Exp Exp → Exp “*” Exp Exp → Exp “/” Exp Exp → “(” Exp “)” syntax Σ: finite set of terminal symbols N: finite set of non-terminal symbols S∈N: start symbol
    32. 32. Grammars and Trees 27 G = (N, Σ, P, S) binary expressions Exp → Num Exp → Exp “+” Exp Exp → Exp “−” Exp Exp → Exp “*” Exp Exp → Exp “/” Exp Exp → “(” Exp “)” syntax Σ: finite set of terminal symbols N: finite set of non-terminal symbols S∈N: start symbol P⊆N×(N∪Σ)*: set of production rules
    33. 33. Grammars and Trees 28 binary expressions Exp ⇒ Exp + Exp ⇒ Exp + Exp * Exp ⇒ 3 + Exp * Exp ⇒ 3 + 4 * Exp ⇒ 3 + 4 * 5 production Exp → Exp “+” Exp ⇒ Exp → Exp “*” Exp ⇒ Exp → Num ⇒ Exp → Num ⇒ Exp → Num ⇒
    34. 34. Grammars and Trees 29 formal grammar G = (N, Σ, P, S) nonterminal symbols N terminal symbols Σ production rules P ⊆ (N∪Σ)* N (N∪Σ)* × (N∪Σ)* start symbol S∈N
    35. 35. Grammars and Trees 29 formal grammar G = (N, Σ, P, S) nonterminal symbol nonterminal symbols N terminal symbols Σ production rules P ⊆ (N∪Σ)* N (N∪Σ)* × (N∪Σ)* start symbol S∈N
    36. 36. Grammars and Trees 29 formal grammar G = (N, Σ, P, S) nonterminal symbols N terminal symbols Σ production rules P ⊆ (N∪Σ)* N (N∪Σ)* × (N∪Σ)* start symbol S∈N context
    37. 37. Grammars and Trees 29 formal grammar G = (N, Σ, P, S) nonterminal symbols N terminal symbols Σ production rules P ⊆ (N∪Σ)* N (N∪Σ)* × (N∪Σ)* start symbol S∈N replacement
    38. 38. Grammars and Trees 29 formal grammar G = (N, Σ, P, S) nonterminal symbols N terminal symbols Σ production rules P ⊆ (N∪Σ)* N (N∪Σ)* × (N∪Σ)* start symbol S∈N
    39. 39. Grammars and Trees 30 type-0, unrestricted: P ⊆ (N∪Σ)* N (N∪Σ)* × (N∪Σ)* type-1, context-sensitive: (a A c, a b c) type-2, context-free: P ⊆ N × (N∪Σ)* type-3, regular: (A, x) or (A, xB)
    40. 40. Grammars and Trees 31 formal grammars context-sensitive context-free regular
    41. 41. Grammars and Trees 32 formal grammar G derivation relation ⇒G formal language L(G) ⊆ Σ* L(G) = {w∈Σ* | S ⇒G* w}
    42. 42. Grammars and Trees 33 formal grammar G = (N, Σ, P, S) derivation relation ⇒G ⊆ (N∪Σ)* × (N∪Σ)* w ⇒G w’ ⇔ ∃(p, q)∈P: ∃u,v∈(N∪Σ)*: w=u p v ∧ w’=u q v formal language L(G) ⊆ Σ* L(G) = {w∈Σ* | S ⇒G* w}
    43. 43. Grammars and Trees 34 formal languages context-sensitive context-free regular
    44. 44. Derivation is about productivity. But what about parsing? Exp Exp Exp Exp Exp Grammars and Trees 35 3 * 7 + 21 3 * 7 + 21 parser
    45. 45. Grammars and Trees 36 word problem
    46. 46. theoretical computer science Grammars and Trees 37 word problem χL: Σ*→ {0,1} w → 1, if w∈L w → 0, else decidability & complexity
    47. 47. theoretical computer science Grammars and Trees 37 decidability & complexity word problem χL: Σ*→ {0,1} w → 1, if w∈L w → 0, else decidability type-0: semi-decidable type-1, type-2, type-3: decidable
    48. 48. theoretical computer science Grammars and Trees 37 decidability & complexity word problem χL: Σ*→ {0,1} w → 1, if w∈L w → 0, else decidability type-0: semi-decidable type-1, type-2, type-3: decidable complexity type-1: PSPACE-complete type-2, type-3: P
    49. 49. theoretical computer science Grammars and Trees 37 decidability & complexity word problem χL: Σ*→ {0,1} w → 1, if w∈L w → 0, else decidability type-0: semi-decidable type-1, type-2, type-3: decidable complexity type-1: PSPACE-complete type-2, type-3: P PSPACE⊇NP⊇P
    50. 50. theoretical computer science Grammars and Trees 38 decidability & complexity formal grammars context-sensitive context-free regular
    51. 51. theoretical computer science Grammars and Trees 38 decidability & complexity formal grammars context-sensitive context-free regular
    52. 52. theoretical computer science Grammars and Trees 38 decidability & complexity formal grammars context-sensitive context-free regular
    53. 53. Grammars and Trees 39 context-free grammars production vs. reduction rules ! Exp → Num Exp → Exp “+” Exp Exp → Exp “−” Exp Exp → Exp “*” Exp Exp → Exp “/” Exp Exp → “(” Exp “)”
    54. 54. Grammars and Trees 39 context-free grammars production vs. reduction rules ! Exp → Num Exp → Exp “+” Exp Exp → Exp “−” Exp Exp → Exp “*” Exp Exp → Exp “/” Exp Exp → “(” Exp “)” productive form
    55. 55. Grammars and Trees 39 context-free grammars production vs. reduction rules ! Exp → Num Exp → Exp “+” Exp Exp → Exp “−” Exp Exp → Exp “*” Exp Exp → Exp “/” Exp Exp → “(” Exp “)” ! Num → Exp Exp “+” Exp → Exp Exp “−” Exp → Exp Exp “*” Exp → Exp Exp “/” Exp → Exp “(” Exp “)” → Exp productive form
    56. 56. Grammars and Trees 39 context-free grammars production vs. reduction rules ! Exp → Num Exp → Exp “+” Exp Exp → Exp “−” Exp Exp → Exp “*” Exp Exp → Exp “/” Exp Exp → “(” Exp “)” ! Num → Exp Exp “+” Exp → Exp Exp “−” Exp → Exp Exp “*” Exp → Exp Exp “/” Exp → Exp “(” Exp “)” → Exp productive form reductive form
    57. 57. Grammars and Trees 40 binary expressions 3 + 4 + 5 ⇒ Exp + 4 + 5 ⇒ Exp + Exp + 5 ⇒ Exp + Exp * Exp ⇒ Exp + Exp ⇒ Exp reduction Num → Exp ⇒ Num → Exp ⇒ Num → Exp ⇒ Exp “*” Exp → Exp ⇒ Exp “+” Exp → Exp ⇒⇒
    58. 58. The word problem is about membership. But what about structure? Exp Exp Exp Exp Exp Grammars and Trees 41 3 * 7 + 21 3 * 7 + 21 parser
    59. 59. Grammars and Trees 42 syntax trees
    60. 60. Grammars and Trees 43 context-free grammars Exp Num tree construction rules Exp Exp + Exp Exp Exp − Exp Exp Exp * Exp Exp Exp / Exp Exp ( Exp )
    61. 61. Grammars and Trees 44 binary expressions tree construction 3 + 4 * 5
    62. 62. Grammars and Trees 44 binary expressions tree construction Exp 3 + 4 * 5
    63. 63. Grammars and Trees 44 binary expressions tree construction Exp Exp 3 + 4 * 5
    64. 64. Grammars and Trees 44 binary expressions tree construction Exp Exp Exp 3 + 4 * 5
    65. 65. Grammars and Trees 44 binary expressions tree construction Exp Exp Exp Exp 3 + 4 * 5
    66. 66. Grammars and Trees 44 binary expressions tree construction Exp Exp Exp Exp Exp 3 + 4 * 5
    67. 67. Exp Grammars and Trees 45 binary expressions ambiguity Exp Exp Exp Exp Exp 3 + 4 * 5 Exp Exp Exp Exp 3 + 4 * 5
    68. 68. Grammars and Trees 46 syntax trees context-free grammars ambiguity different trees for same sentence derivations different leftmost derivations for same sentence different rightmost derivations for same sentence NOT just different derivations for same sentence
    69. 69. parse trees & abstract syntax trees Grammars and Trees 47 parse trees syntax trees parent node: nonterminal symbol child nodes: terminal symbols abstract syntax trees (ASTs) abstract over terminal symbols convey information at parent nodes abstract over injective production rules
    70. 70. Grammars and Trees 48 binary expressions parse tree & abstract syntax tree Exp Exp Exp Exp Exp Exp 3 + ( 4 * 5 )
    71. 71. Const Add Grammars and Trees 48 binary expressions parse tree & abstract syntax tree Const Mul Const 3 4 5 Exp Exp Exp Exp Exp Exp 3 + ( 4 * 5 )
    72. 72. Except where otherwise noted, this work is licensed under Grammars and Trees 49
    73. 73. Grammars and Trees 50 attribution slide title author license 1 The Pine, Saint Tropez Paul Signac public domain 2, 3, 35, 41 PICOL icons Melih Bilgil CC BY 3.0 9 Writing Caitlin Regan CC BY 2.0 10 Latin Grammar Anthony Nelzin 13, 15, 29-34, 38 Noam Chomsky Fellowsisters CC BY-NC-SA 2.0
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×