360 likes | 474 Views
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)
E N D
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
Languages • Def: Set of words Set of Strings Elements of a language • Alphabet Word (Token) (Vocabulary) • Grammar Sentence • Semantic Chapter 5: Languages and Grammar
Types of Languages • Natural Languages Example: They run a store • Formal Languages Example: If (Total > Max) Chapter 5: Languages and Grammar
Grammars • Def: G={, R, P, S} G={Vt, Vn, P, S} Vt Vn= Chapter 5: Languages and Grammar
Example SimpleDatatype = {Integer, Real, Char, Boolean} Datatype SimpleDatatype SimpleDatatype Integer SimpleDatatype Real SimpleDatatype Boolean SimpleDatatype Char Chapter 5: Languages and Grammar
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
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
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
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
Example • S -->oS1 • S o1 • Vn={S} • Vt={0,1} • S={S} • P={S oS1 S o1} Chapter 5: Languages and Grammar
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
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
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
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
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
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
Example Write a grammar which produces odd numbers of *’s • L(G) = { *n; n>=1; n MOD 2 <>0} Chapter 5: Languages and Grammar
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
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
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
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
Example • <Expression> ----> <Term> • <Term> ----> <Term> + <Factors> • <Term> ----> <Factors> • <Factors> ----> Id • Sentence id + id Chapter 5: Languages and Grammar
Derivation tree: • Leftmost Derivation: • Rightmost Derivation: Chapter 5: Languages and Grammar
Ambiguous Grammar Example • <E> ----> <E> <OP> <E> • <E> ----> Id • <OP> ----> + • <OP> ----> * • Sentence a+b*cIs this ambiguous? • Definition Chapter 5: Languages and Grammar
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
+ - BNF (Bachus-Naus Form) Chapter 5: Languages and Grammar
q1 FSM Chapter 5: Languages and Grammar
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
Recognizer INPUT - 2 1 . 0 0 0 State = S Memory Head Language -21.000 Chapter 5: Languages and Grammar
Language • Lex: Translate regular expression into lexical analyzer program • Grammar:Write a grammar to recognize the traffic lights Chapter 5: Languages and Grammar
Traffic Light • D ----> g DD ----> r SS ----> g DS ----> r SS ---->Language? gggrrggrrg……. Chapter 5: Languages and Grammar
Push Down Machine • CFG is accepted by a FSM controlling a Push-down stack Formal Definition: Chapter 5: Languages and Grammar
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
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
= 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
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