chapter 5: languages and grammar 1 compiler designs and constructions ( page 92-158 ) chapter 5:...
TRANSCRIPT
Chapter 5: Languages and Grammar
1
Compiler Designs and Constructions (Page 92-158)
Chapter 5: Languages and Grammar
Objectives:Definition of LanguagesTypes of Languages
Dr. Mohsen Chitsaz
Chapter 5: Languages and Grammar 2
Languages
Def:Set of words
Set of Strings
Elements of a language Alphabet Word (Token) (Vocabulary) Grammar Sentence Semantic
Chapter 5: Languages and Grammar 3
Natural LanguagesExample: They run a store
Formal LanguagesExample: If (Total > Max)
Types of Languages
Chapter 5: Languages and Grammar 5
Example
SimpleDatatype = {Integer, Real, Char, Boolean}
Datatype SimpleDatatype
SimpleDatatype Integer
SimpleDatatype Real
SimpleDatatype Boolean
SimpleDatatype Char
Chapter 5: Languages and Grammar 6
Example
Vt: Terminal Symbols (word, vocabulary, token}Integer, Char, Real, Boolean
Vn: Datatype, SimpleDatatype P: Datatype SimpleDatatype
SimpleDatatype IntegerSimpleDatatype RealSimpleDatatype BooleanSimpleDatatype Char
S: Datatype
Chapter 5: Languages and Grammar 7
Types of Grammars (Chomsky Hierarchy)
LHS RHS
Type Zero: UnrestrictedABC aa CaaF
Type One: Context Sensitive|| <= |B| A -- > aA BcAB aBCa abc
Chapter 5: Languages and Grammar 8
Types of Grammars (Chomsky Hierarchy)
Type Two: Context Free|| = 1 Vn
Type Three: Regular Grammar is a CFG with Right linear
Right LinearA wb
A w Left Linear
A Bw
A w
Chapter 5: Languages and Grammar 9
Context Free Grammar
Context Free Grammar G Context Free Language of Grammar L(G) Example:
<START> var <REST><REST> id: <SIMPLEDATATYPE>;<SIMPLEDATATYPE> integer<SIMPLEDATATYPE> real<SIMPLEDATATYPE> char<SIMPLEDATATYPE> Boolean
Chapter 5: Languages and Grammar 11
Language
Def: Language of a grammar: is a set of all sentences accepted by that grammar.L(G) = {w|S ->w}
Notation: Kleene Closure(*) Zero or more Positive Closure(+) One or more String Concatenation (.) Union (U)
Chapter 5: Languages and Grammar 12
Language
Example: L(Digit) = {0,1,2,3,4,5,6,7,8,9} L(Alpha) = {a,b, …, z} L(Digit)+ = 2 , 24 L(Alpha)* =a , , ab, abc L(Alpha) U (Digit) =a2, b6
Identifier: L(Alpha).( L(Alpha) U L(Digit) )*
Chapter 5: Languages and Grammar 13
Derivation
000111
S OS1 00S11 000111 Each of the forms is called Sentential
Form of this CFG L(G) = {On 1n; n >= 1}
Chapter 5: Languages and Grammar 14
Example
Write a grammar that produces a set of 0’s & 1’s in any order. It must start with a zero and end with a one. 01 0101 0011 011
Chapter 5: Languages and Grammar 15
Example
S 0A1 S 01 A 0A A 1A A 1 A 0
S 0A1 S 01 A BA B 0 B 1
S A A 01 A
oB1 B oB B 1B B 0 B 1
Chapter 5: Languages and Grammar 16
Example
What is the language of this grammar?S cSS bDS cD cDD bSD b
Chapter 5: Languages and Grammar 17
Example
Write a grammar which produces odd numbers of *’s
L(G) = { *n; n>=1; n MOD 2 <>0}
Chapter 5: Languages and Grammar 18
Example
S (S) S () S b
Write a grammar for the language L(G) = {bm Cn
dn em fp (gh*)p | m>=2, n>=0, p>=1}
Write a grammar for the languageL(G) = {bm Cn | 1<= n <= m <= 2n}
Chapter 5: Languages and Grammar 19
Tree
Definition: Node Edge Root Children Leaf(Terminal Node)
Level Height (Depth)Internal NodeExternal NodePreorder TraversalInorder TraversalPostorder Traversal
Implementation of Tree?
Chapter 5: Languages and Grammar 20
Context Free Grammar
Derivation: How an input sentence can be recognized.
Parsing: Process of finding derivation
Parser: Automation of parsing.- Left Derivation (Leftmost derivation) - Right Derivation (Rightmost derivation)
Chapter 5: Languages and Grammar 21
Example
Derive String of 01001 Left Derivation: Right Derivation:
1 S ----> 0AB2 A ----> 1A3 B ----> 0S4 S ----> 0S5 S ----> 16 A ----> 0
Chapter 5: Languages and Grammar 22
Example
<Expression> ----> <Term> <Term> ----> <Term> + <Factors> <Term> ----> <Factors> <Factors> ----> Id
Sentence id + id
Chapter 5: Languages and Grammar 24
Ambiguous Grammar
Example <E> ----> <E> <OP> <E> <E> ----> Id <OP> ----> + <OP> ----> *
Sentence a+b*c Is this ambiguous?
Definition
Chapter 5: Languages and Grammar 25
Finite State Machine (Automata) FSM (FSA)
Simplified model for digital system Memory Input Output
FSM is used as a language recognizer
Example: Real Number + 2.44
Chapter 5: Languages and Grammar 28
Grammar
<S> -------> + <q1> <S> -------> - <q1> <q1> ------> d <q2> <S> -------> d <q2> <q2> ------> d <q2> <q2> ------> . <q3> <q3> ------> d <q4> <q4> ------> d <q4> <q4> ------>
Chapter 5: Languages and Grammar 30
Language
Lex: Translate regular expression into lexical analyzer program
Grammar:Write a grammar to recognize the traffic lights
Chapter 5: Languages and Grammar 31
Traffic Light
D ----> g DD ----> r SS ----> g DS ----> r SS ---->Language? gggrrggrrg…….
Chapter 5: Languages and Grammar 32
Push Down Machine
CFG is accepted by a FSM controlling a Push-down stack
Formal Definition:
Chapter 5: Languages and Grammar 33
Deterministic Finite State Machine (Q, , ,q0, F)
(Every move is absolutely determined by the current state and next input)
Where: Q: Finite Set of State
: Alphabet q0: Starting State (q)
F: Final States (Q) : State Transition Function
Chapter 5: Languages and Grammar 34
Deterministic Finite State Machine
Example of a Real Number
Q = {S, q1, q2, q3, q4}
q0 = {S} F = {q4} = {+, -, d, . }
Chapter 5: Languages and Grammar 35
= State Transition:(S,+) = q1(S,-) = q1(S, d) = q2(q1, d) = q2(q2, d) = q2(q2, . ) = q3(q3, d) = q4(q4, d) = q4