Chapter 5: Languages and Grammar 1 Compiler Designs and Constructions ( Page 92-158 ) Chapter 5: Languages and Grammar Objectives: Definition of Languages.

Slides:



Advertisements
Similar presentations
Chapter 5 Pushdown Automata
Advertisements

Compiler Designs and Constructions
C O N T E X T - F R E E LANGUAGES ( use a grammar to describe a language) 1.
Lecture # 8 Chapter # 4: Syntax Analysis. Practice Context Free Grammars a) CFG generating alternating sequence of 0’s and 1’s b) CFG in which no consecutive.
CFGs and PDAs Sipser 2 (pages ). Long long ago…
ISBN Chapter 3 Describing Syntax and Semantics.
CFGs and PDAs Sipser 2 (pages ). Last time…
CS 310 – Fall 2006 Pacific University CS310 Pushdown Automata Sections: 2.2 page 109 October 9, 2006.
Languages, grammars, and regular expressions
PZ02A - Language translation
Fall 2005 CSE 467/567 1 Formal languages regular expressions regular languages finite state machines.
1 Foundations of Software Design Lecture 23: Finite Automata and Context-Free Grammars Marti Hearst Fall 2002.
COP4020 Programming Languages
Chapter 6: Top-Down Parser1 Compiler Designs and Constructions Chapter 6: Top-Down Parser Objectives: Types of Parsers Backtracking Vs. Non-backtracking.
Grammars, Languages and Finite-state automata Languages are described by grammars We need an algorithm that takes as input grammar sentence And gives a.
(2.1) Grammars  Definitions  Grammars  Backus-Naur Form  Derivation – terminology – trees  Grammars and ambiguity  Simple example  Grammar hierarchies.
Regular Languages A language is regular over  if it can be built from ;, {  }, and { a } for every a 2 , using operators union ( [ ), concatenation.
Finite-State Machines with No Output
PZ02B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ02B - Regular grammars Programming Language Design.
Compiler Phases: Source program Lexical analyzer Syntax analyzer Semantic analyzer Machine-independent code improvement Target code generation Machine-specific.
1 Chapter 3 Describing Syntax and Semantics. 3.1 Introduction Providing a concise yet understandable description of a programming language is difficult.
Grammars CPSC 5135.
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
Lecture # 9 Chap 4: Ambiguous Grammar. 2 Chomsky Hierarchy: Language Classification A grammar G is said to be – Regular if it is right linear where each.
4b 4b Lexical analysis Finite Automata. Finite Automata (FA) FA also called Finite State Machine (FSM) –Abstract model of a computing entity. –Decides.
C H A P T E R TWO Syntax and Semantic.
Parsing Introduction Syntactic Analysis I. Parsing Introduction 2 The Role of the Parser The Syntactic Analyzer, or Parser, is the heart of the front.
Review: Compiler Phases: Source program Lexical analyzer Syntax analyzer Semantic analyzer Intermediate code generator Code optimizer Code generator Symbol.
CPS 506 Comparative Programming Languages Syntax Specification.
By Neng-Fa Zhou Programming language syntax 4 Three aspects of languages –Syntax How are sentences formed? –Semantics What does a sentence mean? –Pragmatics.
1 Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
1Computer Sciences Department. Book: INTRODUCTION TO THE THEORY OF COMPUTATION, SECOND EDITION, by: MICHAEL SIPSER Reference 3Computer Sciences Department.
CSC312 Automata Theory Lecture # 26 Chapter # 12 by Cohen Context Free Grammars.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 1 Ahmed Ezzat.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 3.
Mid-Terms Exam Scope and Introduction. Format Grades: 100 points -> 20% in the final grade Multiple Choice Questions –8 questions, 7 points each Short.
Deterministic Finite Automata Nondeterministic Finite Automata.
Theory of Languages and Automata By: Mojtaba Khezrian.
Chapter 3 – Describing Syntax CSCE 343. Syntax vs. Semantics Syntax: The form or structure of the expressions, statements, and program units. Semantics:
Chapter 2. Formal Languages Dept. of Computer Engineering, Hansung University, Sung-Dong Kim.
Last Chapter Review Source code characters combination lexemes tokens pattern Non-Formalization Description Formalization Description Regular Expression.
Theory of Languages and Automata By: Mojtaba Khezrian.
Lexical Analyzer in Perspective
Chapter 3 – Describing Syntax
CS 404 Introduction to Compiler Design
Programming Languages Translator
Chapter 3 – Describing Syntax
Theory of Languages and Automata
Table-driven parsing Parsing performed by a finite state machine.
Natural Language Processing - Formal Language -
Context Sensitive Grammar & Turing Machines
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
CSE 105 theory of computation
CS314 – Section 5 Recitation 3
Formal Language Theory
Two issues in lexical analysis
Department of Software & Media Technology
Context-Free Grammars
Review: Compiler Phases:
Context-Free Grammars 1
Lecture 4: Lexical Analysis & Chomsky Hierarchy
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Chapter 3 Syntactic Analysis I.
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
COMPILER CONSTRUCTION
Presentation transcript:

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

Chapter 5: Languages and Grammar2 Languages  Def: Set of words Set of Strings Elements of a language  Alphabet  Word (Token) (Vocabulary)  Grammar  Sentence  Semantic

Chapter 5: Languages and Grammar3  Natural Languages Example: They run a store  Formal Languages Example: If (Total > Max) Types of Languages

Chapter 5: Languages and Grammar4  Def: G={ , R, P, S} G={V t, V n, P, S} V t  V n =  Grammars

Chapter 5: Languages and Grammar5 Example SimpleDatatype = { Integer, Real, Char, Boolean } Datatype  SimpleDatatype SimpleDatatype  Integer SimpleDatatype  Real SimpleDatatype  Boolean SimpleDatatype  Char

Chapter 5: Languages and Grammar6 Example  V t : Terminal Symbols ( word, vocabulary, token } Integer, Char, Real, Boolean  V n : Datatype, SimpleDatatype  P: Datatype  SimpleDatatype SimpleDatatype  Integer SimpleDatatype  Real SimpleDatatype  Boolean SimpleDatatype  Char  S: Datatype

Chapter 5: Languages and Grammar7 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 Grammar8 Types of Grammars (Chomsky Hierarchy)  Type Two: Context Free |  | = 1   Vn  Type Three: Regular Grammar is a CFG with Right linear Right Linear A  w b A  w Left Linear A  B w A  w

Chapter 5: Languages and Grammar9 Context Free Grammar  Context Free Grammar G  Context Free Language of Grammar L(G)  Example:  var  id: ;  integer  real  char  Boolean

Chapter 5: Languages and Grammar10 Example  S -->oS1  S  o1  V n ={S}  V t ={0,1}  S={S}  P={S  oS1 S  o1}

Chapter 5: Languages and Grammar11 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 Grammar12 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 Grammar13 Derivation  S  OS1  00S11   Each of the forms is called Sentential Form of this CFG L(G) = {On 1n; n >= 1}

Chapter 5: Languages and Grammar14 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

Chapter 5: Languages and Grammar15 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 Grammar16 Example  What is the language of this grammar? S  cS S  bD S  c D  cD D  bS D  b

Chapter 5: Languages and Grammar17 Example Write a grammar which produces odd numbers of * ’ s  L(G) = { *n; n>=1; n MOD 2 <>0}

Chapter 5: Languages and Grammar18 Example S  (S) S  () S  b  Write a grammar for the language L(G) = {b m C n d n e m f p (gh*) p | m>=2, n>=0, p>=1}  Write a grammar for the language L(G) = {b m C n | 1<= n <= m <= 2n}

Chapter 5: Languages and Grammar19 Tree Definition:  Node  Edge  Root  Children  Leaf(Terminal Node) Level Height (Depth) Internal Node External Node Preorder Traversal Inorder Traversal Postorder Traversal Implementation of Tree?

Chapter 5: Languages and Grammar20 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 Grammar21 Example  Derive String of Left Derivation : Right Derivation: 1 S ----> 0AB 2 A ----> 1A 3 B ----> 0S 4 S ----> 0S 5 S ----> 1 6 A ----> 0

Chapter 5: Languages and Grammar22 Example  ---->  ----> +  ---->  ----> Id  Sentence id + id

Chapter 5: Languages and Grammar23  Derivation tree:  Leftmost Derivation:  Rightmost Derivation:

Chapter 5: Languages and Grammar24 Ambiguous Grammar Example  ---->  ----> Id  ----> +  ----> *  Sentence a+b*c Is this ambiguous?  Definition

Chapter 5: Languages and Grammar25 Finite State Machine (Automata) FSM (FSA)  Simplified model for digital system Memory Input Output  FSM is used as a language recognizer  Example: Real Number

Chapter 5: Languages and Grammar26 BNF (Bachus-Naus Form) + -

Chapter 5: Languages and Grammar27 FSM q1

Chapter 5: Languages and Grammar28 Grammar  > +  > -  > d  > d  > d  >.  > d  >

Chapter 5: Languages and Grammar29 Language Recognizer Head State = S Memory INPUT

Chapter 5: Languages and Grammar30 Language  Lex: Translate regular expression into lexical analyzer program  Grammar: Write a grammar to recognize the traffic lights

Chapter 5: Languages and Grammar31 Traffic Light  D ----> g D D ----> r S S ----> g D S ----> r S S ----> Language? gggrrggrrg …….

Chapter 5: Languages and Grammar32 Push Down Machine  CFG is accepted by a FSM controlling a Push-down stack Formal Definition:

Chapter 5: Languages and Grammar33 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 Grammar34 Deterministic Finite State Machine Example of a Real Number  Q = {S, q1, q2, q3, q4} q0 = {S} F = {q4}  = {+, -, d,. }

Chapter 5: Languages and Grammar35  = 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 Grammar36 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. +-.d S q1 q2 q1 q2 q3q2 q3 q4 Acc STATESTATE