1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 5 Mälardalen University 2005
2 Content - Grammars - Linear Grammars - Regular Grammars - Regular Grammars Generate Regular - Languages - Regular vs. Nonregular Languages - Context-Free Languages - Context-Free Grammars - Derivation Trees - Definition: Context-Free Languages - Ambiguity
3 Grammars
4 Grammars express languages Example: the English language
5 barksverb singsverb dognoun birdnoun thearticle a
6 A derivation of “the bird sings”: birdthe verbbirdthe verbnounthe verbnounarticle predicatenounarticle predicatephrasenounsentence sings _
7 A derivation of “a dog barks”: barksdoga verbdoga verbnouna verbnounarticle verbphrasenoun predicatephrasenounsentence _ _
8 The language of the grammar:
9 Notation Non-terminal (Variable) Terminal Production rule
10 Example Derivation of sentence: Grammar:
11 Grammar: Derivation of sentence
12 Other derivations
13 The language of the grammar
14 Formal Definition Grammar Set of variables Set of terminal symbols Start variable Set of production rules
15 Example Grammar
16 Sentential Form A sentence that contains variables and terminals Example sentential formsSentence (sats)
17 We write: Instead of:
18 In general we write if By default ()
19 Example Grammar Derivations
20 baaaaaSbbbbaaSbb S Grammar Example Derivations
21 Another Grammar Example Derivations Grammar
22 More Derivations
23 The Language of a Grammar For a grammar with start variable String of terminals
24 Example For grammar Since
25 Notation
26 Linear Grammars
27 Linear Grammars Grammars with at most one variable (non-terminal) at the right side of a production Examples:
28 A Non-Linear Grammar Grammar
29 Another Linear Grammar Grammar
30 Right-Linear Grammars All productions have form: or Example
31 Left-Linear Grammars All productions have form or Example
32 Regular Grammars
33 Regular Grammars Generate Regular Languages
34 Theorem Languages Generated by Regular Grammars Regular Languages
35 Theorem - Part 1 Languages Generated by Regular Grammars Regular Languages Any regular grammar generates a regular language
36 Theorem - Part 2 Any regular language is generated by a regular grammar Languages Generated by Regular Grammars Regular Languages
37 Proof – Part 1 The language generated by any regular grammar is regular Languages Generated by Regular Grammars Regular Languages
38 The case of Right-Linear Grammars Let be a right-linear grammar We will prove: is regular Proof idea We will construct NFA with
39 Grammar is right-linear Example
40 Construct NFA such that every state is a grammar variable: special final state
41 Add edges for each production:
42
43
44
45
46
47 Grammar NFA
48 In General A right-linear grammar has variables: and productions: or
49 We construct the NFA such that: each variable corresponds to a node: special final state ….
50 For each production: we add transitions and intermediate nodes ………
51 Example
52 The case of Left-Linear Grammars Let be a left-linear grammar We will prove: is regular Proof idea We will construct a right-linear grammar with
53 Since is left-linear grammar the productions look like:
54 Construct right-linear grammar In :
55 Construct right-linear grammar In :
56 It is easy to see that: Since is right-linear, we have: Regular Language Regular Language Regular Language
57 Proof - Part 2 Any regular language is generated by some regular grammar Languages Generated by Regular Grammars Regular Languages
58 Proof idea Any regular language is generated by some regular grammar Construct from a regular grammar such that Since is regular there is an NFA such that
59 Example
60 Convert to a right-linear grammar
61 In General For any transition: Add production: variableterminalvariable
62 For any final state: Add production:
63 Since is right-linear grammar is also a regular grammar with
64 Regular Grammars A regular grammar is any right-linear or left-linear grammar Examples
65 Observation Regular grammars generate regular languages Examples
66 Regular Languages Context-Free Languages Non-regular languages
67 A Nonregular Language DFA must have infinite number of states. Statesare distinct for each
68 Context-Free Languages
69 Context-Free Languages Pushdown Automata Context-Free Grammars stack automaton
70 Context-Free Grammars
71 A context-free grammar A derivation Example
72 A context-free grammar Another derivation
73
74 A context-free grammar A derivation Example
75 A context-free grammar Another derivation
76
77 A context-free grammar A derivation Example
78 A context-free grammar A derivation
79
80 Definition: Context-Free Grammars Grammar Productions of the form: is string of variables and terminals VariablesTerminal symbols Start variables
81 Definition: Context-Free Languages A language is context-free if and only if there is a grammar with
82 Derivation Order Leftmost derivation
83 Derivation Order Rightmost derivation
84 Leftmost derivation
85 Rightmost derivation
86 Derivation Trees
87
88
89
90
91 Derivation Tree
92 yield Derivation Tree
93 Partial Derivation Trees Partial derivation tree
94 Partial derivation tree
95 Partial derivation tree sentential form yield
96 Same derivation tree Sometimes, derivation order doesn’t matter Leftmost: Rightmost:
97 Ambiguity
98 leftmost derivation derivation (* denotes multiplication)
99 derivation leftmost derivation
100 Two derivation trees
101 The grammar is ambiguous! Stringhas two derivation trees
102 stringhas two leftmost derivations The grammar is ambiguous:
103 Definition A context-free grammar is ambiguous if some string has two or more derivation trees (two or more leftmost/rightmost derivations)
104 Why do we care about ambiguity? Let’s see the case
105 Why do we care about ambiguity?
106 Why do we care about ambiguity?
107 Correct result:
108 Ambiguity is bad for programming languages We want to remove ambiguity!
109 We fix the ambiguous grammar… …by introducing parentheses () to indicate grouping, (precedence) Non-ambiguous grammar
110
111 Unique derivation tree
112 The grammar is non-ambiguous Every string has a unique derivation tree
113 Inherent Ambiguity Some context free languages have only ambiguous grammars! Example:
114 The string has two derivation trees
115 Compilers
116 Compiler Program v = 5; if (v>5) x = 12 + v; while (x !=3) { x = x - 3; v = 10; } Add v,v,0 cmp v,5 jmplt ELSE THEN: add x, 12,v ELSE: WHILE: cmp x,3... Machine Code
117 Lexical analyzer parser Compiler program machine code input output
118 A parser “knows” the grammar of the programming language
119 Parser PROGRAM STMT_LIST STMT_LIST STMT; STMT_LIST | STMT; STMT EXPR | IF_STMT | WHILE_STMT | { STMT_LIST } EXPR EXPR + EXPR | EXPR - EXPR | ID IF_STMT if (EXPR) then STMT | if (EXPR) then STMT else STMT WHILE_STMT while (EXPR) do STMT
120 The parser finds the derivation of a particular input * 5 Parser E E + E | E * E | INT E E + E E + E * E 10 + E*E * E * 5 input derivation
121 derivation derivation tree E E + E E + E * E 10 + E*E * E * 5 10 E 2 E 5 E E + E *
122 derivation tree mult a, 2, 5 add b, 10, a machine code 10 E 2 E 5 E E + E *
123 Parsing
124 grammar Parser input string derivation
125 Example: Parser derivation input ?
126 Exhaustive Search Phase 1: All possible derivations of length 1 Find derivation of
127
128 Phase 2 Phase 1
129 Phase 2 Phase 3
130 Final result of exhaustive search Parser derivation input (top-down parsing)