1 / 36

Compiler Designs and Constructions ( Page 92-158 )

Compiler Designs and Constructions ( Page 92-158 ). Chapter 5: Languages and Grammar Objectives: Definition of Languages Types of Languages Dr. Mohsen Chitsaz. Languages. Def: Set of words Set of Strings Elements of a language Alphabet  Word (Token) (Vocabulary)

konala
Download Presentation

Compiler Designs and Constructions ( Page 92-158 )

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Compiler Designs and Constructions (Page 92-158) Chapter 5: Languages and Grammar Objectives: Definition of Languages Types 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. Types of Languages • Natural Languages Example: They run a store • Formal Languages Example: If (Total > Max) Chapter 5: Languages and Grammar

  4. Grammars • Def: G={, R, P, S} G={Vt, Vn, P, S} Vt  Vn=  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  Integer SimpleDatatype  Real SimpleDatatype  Boolean SimpleDatatype  Char • S: Datatype Chapter 5: Languages and Grammar

  7. Types of Grammars (Chomsky Hierarchy) •  LHS RHS • Type Zero: Unrestricted ABC  a a  CaaF • Type One: Context Sensitive || <= |B| A -- > a A  Bc AB  aB Ca  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 Linear A  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

  10. Example • S -->oS1 • S  o1 • Vn={S} • Vt={0,1} • S={S} • P={S  oS1 S  o1} 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. S  0A1 S  01 A  0A A  1A A  1 A  0 S  0A1 S 01 A  BA B  0 B  1 Example • 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  cS • S  bD • S  c • D  cD • D  bS • D 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 Cndn em fp (gh*)p | m>=2, n>=0, p>=1} • Write a grammar for the language L(G) = {bm Cn | 1<= n <= m <= 2n} Chapter 5: Languages and Grammar

  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? 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 • 1 S ----> 0AB • 2 A ----> 1A • 3 B ----> 0S • 4 S ----> 0S • 5 S ----> 1 • 6 A ----> 0 • Derive String of 01001 • Left Derivation: • Right Derivation: Chapter 5: Languages and Grammar

  22. Example • <Expression> ----> <Term> • <Term> ----> <Term> + <Factors> • <Term> ----> <Factors> • <Factors> ----> Id • Sentence id + id Chapter 5: Languages and Grammar

  23. Derivation tree: • Leftmost Derivation: • Rightmost Derivation: Chapter 5: Languages and Grammar

  24. Ambiguous Grammar Example • <E> ----> <E> <OP> <E> • <E> ----> Id • <OP> ----> + • <OP> ----> * • Sentence a+b*cIs 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

  26. + - BNF (Bachus-Naus Form) Chapter 5: Languages and Grammar

  27. q1 FSM 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

  29. Recognizer INPUT - 2 1 . 0 0 0 State = S Memory Head Language -21.000 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 Chapter 5: Languages and Grammar

  36. q2 q1 q4 q3 q4 q4 Acc q2 q3 q2 S T A T E + - . d S q1 q1 q2 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. Chapter 5: Languages and Grammar

More Related