Compiler Designs and Constructions Page 92158 - PowerPoint PPT Presentation

1 / 36
About This Presentation
Title:

Compiler Designs and Constructions Page 92158

Description:

AB aB. Ca abc. Chapter 5: Languages and Grammar. 8. Types ... L(Alpha)* =a , , ab, abc. L(Alpha) U (Digit) =a2, b6. Identifier: L(Alpha).( L(Alpha) U L(Digit) ... – PowerPoint PPT presentation

Number of Views:25
Avg rating:3.0/5.0
Slides: 37
Provided by: scie6
Category:

less

Transcript and Presenter's Notes

Title: Compiler Designs and Constructions Page 92158


1
Compiler Designs and Constructions (Page 92-158)
  • Chapter 5 Languages and Grammar
  • Objectives
  • Definition of Languages
  • Types of Languages
  • Dr. Mohsen Chitsaz

2
Languages
  • Def
  • Set of words
  • Set of Strings
  • Elements of a language
  •  Alphabet ? Word (Token) (Vocabulary)
  • Grammar ? Sentence
  • Semantic

3
Types of Languages
  • Natural Languages
  • Example They run a store
  • Formal Languages
  • Example If (Total gt Max)

4
Grammars
  • Def
  • G?, R, P, S
  • GVt, Vn, P, S
  • Vt ? Vn ?

5
Example
  • SimpleDatatype Integer, Real, Char, Boolean
  • Datatype ? SimpleDatatype
  • SimpleDatatype ? Integer
  • SimpleDatatype ? Real
  • SimpleDatatype ? Boolean
  • SimpleDatatype ? Char

6
Example
  • Vt Terminal Symbols (word, vocabulary, token
  • Integer, Char, Real, Boolean
  • Vn Datatype, SimpleDatatype
  • P Datatype ? SimpleDatatype
  • SimpleDatatype ? Integer
  • SimpleDatatype ? Real
  • SimpleDatatype ? Boolean
  • SimpleDatatype ? Char
  • S Datatype

7
Types of Grammars (Chomsky Hierarchy)
  • ? ? ?
  • LHS RHS 
  • Type Zero Unrestricted
  • ABC ? a
  • a ? CaaF 
  • Type One Context Sensitive
  • ? lt B
  •  A -- gt a
  • A ? Bc
  • AB ? aB
  • Ca ? abc

8
Types of Grammars (Chomsky Hierarchy)
  • Type Two Context Free
  • ? 1
  • ? ? Vn
  • Type Three Regular Grammar is a CFG with Right
    linear
  • Right Linear
  • A ? wb
  • A ? w
  • Left Linear
  • A ? Bw
  • A ? w

9
Context Free Grammar
  • Context Free Grammar G
  • Context Free Language of Grammar L(G)
  • Example
  • ltSTARTgt ? var ltRESTgt
  • ltRESTgt ? id ltSIMPLEDATATYPEgt
  • ltSIMPLEDATATYPEgt ? integer
  • ltSIMPLEDATATYPEgt ? real
  • ltSIMPLEDATATYPEgt ? char
  • ltSIMPLEDATATYPEgt ? Boolean

10
Example
  • S --gtoS1
  • S ? o1
  • VnS
  • Vt0,1
  • SS
  • PS ? oS1
  • S ? o1

11
Language
  • Def Language of a grammar is a set of all
    sentences accepted by that grammar.
  • L(G) wS -gtw
  • Notation
  • Kleene Closure() Zero or more
  • Positive Closure() One or more
  • String Concatenation (.)
  • Union (U)

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) )

13
Derivation
  • 000111
  • S ? OS1 ? 00S11 ? 000111
  • Each of the forms is called Sentential Form of
    this CFG
  • L(G) On 1n n gt 1

14
Example
  • Write a grammar that produces a set of 0s 1s
    in any order. It must start with a zero and end
    with a one.
  • 01
  • 0101
  • 0011
  • 011

15
Example
  • S ? A
  • A ? 01
  • A ? oB1
  • B ? oB
  • B ? 1B
  • B ? 0
  • B ? 1
  • S ? 0A1
  • S ? 01
  • A ? 0A
  • A ? 1A
  • A ? 1
  • A ? 0
  • S ? 0A1
  • S ?01
  • A ? BA
  • B ? 0
  • B ? 1

16
Example
  • What is the language of this grammar?
  • S ? cS
  • S ? bD
  • S ? c
  • D ? cD
  • D ? bS
  • D ?b

17
Example
  • Write a grammar which produces odd numbers of s
  • L(G) n ngt1 n MOD 2 ltgt0 

18
Example
  • S? (S)
  • S ? ()
  • S ? b
  • Write a grammar for the language
  • L(G) bm Cn dn em fp (gh)p mgt2, ngt0, pgt1
  • Write a grammar for the language
  • L(G) bm Cn 1lt n lt m lt 2n

19
Tree
  • Level
  • Height (Depth)
  • Internal Node
  • External Node
  • Preorder Traversal
  • Inorder Traversal
  • Postorder Traversal
  • Definition
  • Node
  • Edge
  • Root
  • Children
  • Leaf(Terminal Node)

Implementation of Tree?
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)

21
Example
  • 1 S ----gt 0AB
  • 2 A ----gt 1A
  • 3 B ----gt 0S
  • 4 S ----gt 0S
  • 5 S ----gt 1
  • 6 A ----gt 0
  • Derive String of 01001
  • Left Derivation
  • Right Derivation

22
Example
  • ltExpressiongt ----gt ltTermgt
  • ltTermgt ----gt ltTermgt ltFactorsgt
  • ltTermgt ----gt ltFactorsgt
  • ltFactorsgt ----gt Id
  • Sentence id id

23
  • Derivation tree
  • Leftmost Derivation
  • Rightmost Derivation

24
Ambiguous Grammar
  • Example
  • ltEgt ----gt ltEgt ltOPgt ltEgt
  • ltEgt ----gt Id
  • ltOPgt ----gt
  • ltOPgt ----gt
  • Sentence abc Is this ambiguous?
  • Definition

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

26
BNF (Bachus-Naus Form)
27
FSM
28
Grammar
  • ltSgt -------gt ltq1gt
  • ltSgt -------gt - ltq1gt
  • ltq1gt ------gt d ltq2gt
  • ltSgt -------gt d ltq2gt
  • ltq2gt ------gt d ltq2gt
  • ltq2gt ------gt . ltq3gt
  • ltq3gt ------gt d ltq4gt
  • ltq4gt ------gt d ltq4gt
  • ltq4gt ------gt

29
Language
-21.000
30
Language
  • Lex Translate regular expression into lexical
    analyzer program
  • GrammarWrite a grammar to recognize the traffic
    lights

31
Traffic Light
  • D ----gt g DD ----gt r SS ----gt g DS ----gt r SS
    ----gtLanguage? gggrrggrrg.

32
Push Down Machine
  • CFG is accepted by a FSM controlling a Push-down
    stack

Formal Definition
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

34
Deterministic Finite State Machine
  • Example of a Real Number
  • Q S, q1, q2, q3, q4q0 S F q4 ?
    , -, d, .

35
  • ? State Transition
  • ?(S,) q1
  • ?(S,-) q1
  • ?(S, d) q2
  • ?(q1, d) q2
  • ?(q2, d) q2
  • ?(q2, . ) q3
  • ?(q3, d) q4
  • ?(q4, d) q4

36
Transition Table
  • input token

DFSM Deterministic Finite State Machine
  • No state with the same outgoing label.
  • No state has more than one transition with the
    same label.
Write a Comment
User Comments (0)
About PowerShow.com