20. Regular Language
■ L1, L2: Regular Languages then:
– L = L1 . L2 = { X.W were X ∈ L1 , W ∈ L2 } is Regular Language
– L1 ∪ L2 is Regular Language
– L1 , L2 is Regular Language
– L1 ∩ L2 is Regular Language
202020
21. Regular Expressions
Exercise 1
■ Suppose the only characters are 0 and 1.
■ A regular expression for strings containing 00 as a substring:
(0 | 1)* 00 (0 | 1)*
11011100101
0000
11111011110011111
212020
22. Regular Expressions
Exercise 2
■ Suppose the only characters are 0 and 1.
■ A regular expression for strings of length exactly four:
(0|1)(0|1)(0|1)(0|1)
(0|1){4}
0000
1010
1111
1000
222020
23. Regular Expressions
Exercise 3
■ Suppose the only characters are 0 and 1.
■ A regular expression for strings that contain at most one zero:
1*(0 | ε)1*
1*0?1*
11110111
111111
0111
0
232020
24. Regular Expressions
Exercise 4
■ Suppose that our alphabet is all ASCII characters.
■ A regular expression for even numbers:
(+|-)?(0|1|2|3|4|5|6|7|8|9)*(0|2|4|6|8)
(+|-)?[0123456789]*[02468]
(+|-)?[0-9]*[02468]
42
+1370
-3248
-9999912
242020
25. Regular Expressions
Exercise 5
■ Suppose that our alphabet is a, @, and ., where a represents “some letter”
■ regular expression for email addresses:
a+(.a+)*@a+(.a+)+
cs143@cs.stanford.edu
first.middle.last@mail.site.org
barack.obama@whitehouse.gov
252020
29. Regular Language & Automata
Example numbers
■ Draw a DFA equivalent to the following regular expression:
1) digit = [0-9]
2) nat = digit+
3) signedNat = (+|-)? nat
4) number = signedNat ("." nat)? (E signedNat)?
292020
50. Context Free Grammars
Example 1
■للغة القواعدي النموذج اوجد:
– L = { anbn : a,b ∈ σ , n>=0}
■الحل:
– L = {V , T , S , P} where
■ S → a S b (1)
■ S → a b (2)
– V = {S}
– S = {S}
– T = {a,b}
502020
51. Context Free Grammars
Example 2
■اللغة لدينا ليكنLبالشكل اللغة هذه تعريف نستطيع ،الحسابية للتعابير الممثلة
التالي القواعدي:
– Exp → Exp Op Exp | ( Exp ) | id
– Op → + | - | * | /
■الحل:
– V = {Exp, Op}
– T = { + , - , / , id , ( , ) }
– S = {Exp}
512020
52. Context Free Grammars
Left Most Derivation
■ Exp → Exp Op Exp | ( Exp ) | id
■ Op → + | - | * | /
■التالية السلسلة نشتق كيف:id + id * id
–ال نعوض(None Terminal Symbol)وهكذا أوال اليسار اقصى في الموجود..
–التالية المراحل عبر ذلك يتم:
■ Exp → Exp Op Exp
→ id Op Exp
→ id + Exp
→ id + Exp Op Exp
→ id + id Op Exp
→ id + id * Exp
→ id + id * id
522020
53. Context Free Grammars
Right Most Derivation
■ Exp → Exp Op Exp | ( Exp ) | id
■ Op → + | - | * | /
■على مبدؤها ينطوي االشتقاق في أخرى خوارزمية هنالك:
■ال تعويض(None Terminal Symbol)وهكذا أوال اليمين اقصى في الموجود...
■ Exp → Exp Op Exp
→ Exp Op id
→ Exp * id
→ Exp Op Exp * id
→ Exp Op id * id
→ Exp + id * id
→ id + id * id
532020
55. Parse Tree Example
(number – number ) * number
exp exp op exp
exp op number
exp * number
( exp ) * number
( exp op exp ) * number
( exp op number ) * number
( exp – number ) * number
(number – number ) * number
552020
56. Abstract Syntax Trees
■ A parse tree contains much more information than is necessary for a compiler to
produce executable code.
■ Syntax trees contain just the information needed for translation.
562020
57. Abstract Syntax Trees
Example
■ Show the Parse and Syntax Trees of the derivation of the string 3 + 4 using the
grammar.
exp → exp op exp | ( exp ) | number
op → + | - | * | /
Parse Tree Syntax Tree 572020
58. Context Free Grammars
Ambiguity Example
■ Example 1 The Grammar
– exp → exp op exp | ( exp ) | number
– op → + | - | * | /
■ and the string: 34 – 3 * 42
582020
59. Context Free Grammars
Ambiguity Example
■ exp exp op exp
exp op exp op exp
number op exp op exp
number - exp op exp
number - number op exp
number - number * exp
number - number * number
■ The expression evaluates to (34-3)*42 = 1302 !
59
Parse Tree
Abstract Syntax Tree
2020
60. Context Free Grammars
Ambiguity Example
■ exp exp op exp
number op exp
number - exp
number - exp op exp
number - number op exp
number - number * exp
number - number * number
■ The expression evaluates to 34-(3*42) = - 92
60
Parse Tree
Abstract Syntax Tree
2020
61. Context Free Grammars
Dealing with ambiguity
■ Disambiguating rules : state a rule that specifies in each ambiguous case which of
the parse trees (or syntax trees) is the correct one.
– Advantage: it corrects the ambiguity without changing the grammar.
– Disadvantage: syntax is no longer given by the grammar alone.
■ Changing the grammar into a form that forces the construction of the correct parse
tree.
612020
62. Left-Recursion Removal
A → A α | β β does not begin with A
A → β is the base case
A → A α is the recursive case
A → β A'
A' → α A' | ε
exp → term exp'
exp' → addop term exp' | ε
To remove the recursion we re-write the rule :
Example
exp → exp addop term | term
622020
69. Natural Language Translation
69
The hungry teacher is eating a banana and reading a book
The hungry teacher is eating a book and reading a banana
The hungry teacher are eat a book and reading an banana
Teh mungry teamher are eak a mook and seading an banban
Check for and correct LEXICAL
(spelling) errors
Check for and correct SYNTACTIC
(grammar) errors
Check for and correct SEMANTIC
(meaning) errors
TRANSLATE
يأكل الجائع األستاذموزةكتاب ويقرأ
2020
76. Compiler V.S. Interpreter
■ The machine language target program produced by a compiler is usually much
faster than an interpreter at mapping inputs to outputs.
■ An interpreter, however, can usually give better error diagnostics than a compiler,
because it executes the source program statement by statement.
762020
81. First Sets
(األولى الرموز )مجموعة
■ The First Set of a nonterminal A is the set of terminals that may appear as the first
symbols in a string derived from A.
A → BCD
B → b | ε
C → cd | ε
D → e | ε
First (A) = { b , c , e , ε }
First (B) = { b, ε }
First (C) = { c , ε }
First (D) = { e , ε }
A b
A bcd
A bcde
A be
A cd
A cde
A e
A ε
812020
82. First Sets
(األولى الرموز )مجموعة
■ The First Set of a nonterminal A is the set of terminals that may appear as the first
symbols in a string derived from A.
Let X be a grammar symbol (a terminal or nonterminal) or ε. Then the set First (X)
consisting of terminals, and possibly ε, is defined as follows:
1. If X is a terminal or ε, then First (X) = {X}
2. If X is a nonterminal, then
1. for each production choice X → X1 X2 … Xn , First (X) contains First (X1) – {ε}.
2. and if for some i < n, all the sets First (X1), …, First (Xi) contain ε, then First (X)
contains First (Xi+1) – {ε}.
3. and if all the sets First (X1), …, First (Xn) contain ε, then First (X) also contains ε.
822020
83. First Sets
Example 1
Grammar Rule Pass 1 Pass 2 Pass 3
exp → exp addop term
exp → term
First (exp) = { (
, number}
addop → + First (addop) = {+}
addop → - First (adop) = {+,-}
term → term mulop
factor
term → factor
First (trem) = { ( ,
number}
mulop → * First (mulop) = {*}
factor → ( exp ) First (factor) = { ( }
factor → number
First (factor) = { ( ,
number}
exp → exp addop term| term
addop → + | -
term → term mulop factor| factor
mulop → *
factor → ( exp ) | number
832020
84. First Sets
Example 2
Grammar Rule Pass 1 Pass 2
statement → if-stmt
First (statement) =
{if , other}
statement → other
First (statement) =
{other}
if-stmt → if ( exp )
statement else-part
First (if-stmt) = {if}
else-part → else
statement
First (else-part) =
{else}
else-part → ε
First (else-part) =
{else, ε}
exp → 0 First (exp) = {0}
exp → 1 First (exp) = {0,1}
statement → if-stmt | other
If-stmt → if ( exp ) statement else-part
else-part → else statement | ε
exp → 0 | 1
842020
85. First Sets
More Examples
85
E → TE’
E’ → +TE’ | ε
T → FT’
T’ → *FT’ | ε
F → (E) | id
S → ABC
A → aA | ε
B → bB | cB | ε
C → de | da | dA
2020
86. First Sets
More Examples
86
E → TE’
E’ → +TE’ | ε
T → FT’
T’ → *FT’ | ε
F → (E) | id
First (E) = { ( , id }
First (E’) = { * , ε }
First (T) = { ( , id }
First (T’) = { * , ε }
First (F) = { ( , id }
S → ABC
A → aA | ε
B → bB | cB | ε
C → de | da | dA
First (S) = { a , b , c , d }
First (A) = { a , ε }
First (B) = { b , c , ε }
First (C) = { d }
2020
87. Follow Sets
(الالحقة الرموز )مجموعة
■ The Follow Set of a nonterminal A is the set of terminals that may appear after A in a
string derived from the start symbol.
A → BCD
B → b | ε
C → cd | ε
D → e | ε
Follow (A) = $
Follow (B) = First (CD) – {ε} + Follow (A) = {c, e, ε} – {ε} + {$} = {c, e, $}
Follow (C) = First (D) – {ε} + Follow (A) = {$, e}
Follow (D) = Follow (A) = {$}
872020
88. Follow Sets
(الالحقة الرموز )مجموعة
■ The Follow Set of a nonterminal A is the set of terminals that may appear after A in a
string derived from the start symbol.
Given a nonterminal A, the set Follow(A), consisting of terminals, and possibly $, is
defined as follows:
1. If A is the start symbol, then $ is in Follow(A)
2. If there is a production B → α A γ, then First (γ) – {ε} is in Follow (A).
3. If there is a production B → α A γ such that ε is in First (γ), then Follow (A) contains
Follow (B).
882020
90. Follow Sets
More Example
90
E → TE’
E’ → +TE’ | ε
T → FT’
T’ → *FT’ | ε
F → (E) | id
S → aSb | cd | SAe
A → aAdB | ε
B → bb
2020
91. Follow Sets
More Example
91
E → TE’
E’ → +TE’ | ε
T → FT’
T’ → *FT’ | ε
F → (E) | id
First (E) = { ( , id }
First (E’) = { + , ε }
First (T) = { ( , id }
First (T’) = { * , ε }
First (F) = { ( , id }
Follow (E) = { ) , $ }
Follow (E’) = { ) , $ }
Follow (T) = { + , ) , $ }
Follow (T’) = { + , ) , $ }
Follow (F) = { * , + , ) , $ }
First (S) = { a , c }
First (A) = { a , ε }
First (B) = { b }
Follow (S) = { b , a , e , $ }
Follow (A) = { e , d , $ }
Follow (B) = { e , d , $ }
S → aSb | cd | SAe
A → aAdB | ε
B → bb
2020
92. Types of Parsing
■ Syntax analyzers follow production rules defined by means of context-free grammar.
■ The way the production rules are implemented (derivation) divides parsing into two
types:
– Top-down parsing
– Bottom-up parsing
922020
93. Types of Parsing
■ Top-down Parsing:
– When the parser starts constructing the parse tree from the start symbol and
then tries to transform the start symbol to the input, it is called top-down
parsing.
■ Bottom-up Parsing:
– As the name suggests, bottom-up parsing starts with the input symbols and
tries to construct the parse tree up to the start symbol.
932020
94. Types of Parsing
Example
■ Input string: a + b * c
■ Production rules:
– S → E
– E → E + T
– E → E * T
– E → T
– T → id
942020
95. Types of Parsing
Example
■ Let us start bottom-up parsing: a + b * c
■ Read the input and check if any production matches with the input:
– a + b * c
– T + b * c
– E + b * c
– E + T * c
– E * c
– E * T
– E
– S
952020
96. LL(1)
Simple Predictive Parser
■ Top-down, predictive parsing:
– L: Left-to-right scan of the tokens
– L: Leftmost derivation
– (1): One token of lookahead
■ Construct a leftmost derivation for the sequence of tokens.
■ When expanding a nonterminal, we predict the production to use by looking at the
next token of the input. The decision is forced.
962020
97. LL(1) Parse Tables
E → int
E →(E OpE)
Op → +
Op → *
int ( ) + *
E int (E Op E)
Op + *
972020
98. (1) E → int
(2) E → (E OpE)
(3) Op → +
(4) Op → *
(int + (int * int))LL(1) Parsing
982020
99. (1) E → int
(2) E → (E OpE)
(3) Op → +
(4) Op → *
E (int + (int * int))LL(1) Parsing
992020
100. (1) E → int
(2) E → (E OpE)
(3) Op → +
(4) Op → *
E (int + (int * int))
int ( ) + *
E 1 2
Op 3 4
LL(1) Parsing
1002020
101. (1) E → int
(2) E → (E OpE)
(3) Op → +
(4) Op → *
E$ (int + (int * int))$
int ( ) + *
E 1 2
Op 3 4
LL(1) Parsing
1012020
102. (1) E → int
(2) E → (E OpE)
(3) Op → +
(4) Op → *
E$ (int + (int * int))$
int ( ) + *
E 1 2
Op 3 4
LL(1) Parsing
102
T he $ symbol is t he end - of - input
marker and is used by the parser
t o detect when we have reached
the end o f the input. I t is not
a part o f the grammar.
2020
103. (1) E → int
(2) E → (E OpE)
(3) Op → +
(4) Op → *
E$ (int + (int * int))$
int ( ) + *
E 1 2
Op 3 4
LL(1) Parsing
1032020
104. (1) E → int
(2) E → (E OpE)
(3) Op → +
(4) Op → *
E$ (int + (int * int))$
int ( ) + *
E 1 2
Op 3 4
LL(1) Parsing
104
The f ir st symbol of our guess is a
nonterminal. We then look at our
parsing table t o see what
production t o use.
This is called a predict step.
2020
105. (1) E → int
(2) E → (E OpE)
(3) Op → +
(4) Op → *
E$ (int + (int * int))$
int ( ) + *
E 1 2
Op 3 4
LL(1) Parsing
1052020
106. (1) E → int
(2) E → (E OpE)
(3) Op → +
(4) Op → *
E$ (int + (int * int))$
(E Op E)$ (int + (int * int))$
int ( ) + *
E 1 2
Op 3 4
LL(1) Parsing
1062020
107. (1) E → int
(2) E → (E OpE)
(3) Op → +
(4) Op → *
E$ (int + (int * int))$
(E Op E)$ (int + (int * int))$
int ( ) + *
E 1 2
Op 3 4
LL(1) Parsing
107
The f ir st symbol of our guess is
now a terminal symbol. We thus
match i t against the f i r s t symbol
o f the string t o parse.
This is called a match step.
2020
108. (1) E → int
(2) E → (E OpE)
(3) Op → +
(4) Op → *
E$ (int + (int * int))$
(E Op E)$ (int + (int * int))$
E Op E)$ int + (int * int))$
int ( ) + *
E 1 2
Op 3 4
LL(1) Parsing
1082020
109. (1) E → int
(2) E → (E OpE)
(3) Op → +
(4) Op → *
E$ (int + (int * int))$
(E Op E)$ (int + (int * int))$
E Op E)$ int + (int * int))$
int ( ) + *
E 1 2
Op 3 4
LL(1) Parsing
1092020
110. (1) E → int
(2) E → (E OpE)
(3) Op → +
(4) Op → *
E$ (int + (int * int))$
(E Op E)$ (int + (int * int))$
E Op E)$ int + (int * int))$
int Op E)$ int + (int * int))$
int ( ) + *
E 1 2
Op 3 4
LL(1) Parsing
1102020
111. (1) E → int
(2) E → (E OpE)
(3) Op → +
(4) Op → *
E$ (int + (int * int))$
(E Op E)$ (int + (int * int))$
E Op E)$ int + (int * int))$
int Op E)$ int + (int * int))$
Op E)$ + (int * int))$
int ( ) + *
E 1 2
Op 3 4
LL(1) Parsing
1112020
112. (1) E → int
(2) E → (E OpE)
(3) Op → +
(4) Op → *
E$ (int + (int * int))$
(E Op E)$ (int + (int * int))$
E Op E)$ int + (int * int))$
int Op E)$ int + (int * int))$
Op E)$ + (int * int))$
int ( ) + *
E 1 2
Op 3 4
LL(1) Parsing
1122020
113. (1) E → int
(2) E → (E OpE)
(3) Op → +
(4) Op → *
E$ (int + (int * int))$
(E Op E)$ (int + (int * int))$
E Op E)$ int + (int * int))$
int Op E)$ int + (int * int))$
Op E)$ + (int * int))$
+ E)$ + (int * int))$
int ( ) + *
E 1 2
Op 3 4
LL(1) Parsing
1132020
114. (1) E → int
(2) E → (E OpE)
(3) Op → +
(4) Op → *
E$ (int + (int * int))$
(E Op E)$ (int + (int * int))$
E Op E)$ int + (int * int))$
int Op E)$ int + (int * int))$
Op E)$ + (int * int))$
+ E)$ + (int * int))$
E)$ (int * int))$
int ( ) + *
E 1 2
Op 3 4
LL(1) Parsing
1142020
115. (1) E → int
(2) E → (E OpE)
(3) Op → +
(4) Op → *
E$ (int + (int * int))$
(E Op E)$ (int + (int * int))$
E Op E)$ int + (int * int))$
int Op E)$ int + (int * int))$
Op E)$ + (int * int))$
+ E)$ + (int * int))$
E)$ (int * int))$
int ( ) + *
E 1 2
Op 3 4
LL(1) Parsing
1152020
116. (1) E → int
(2) E → (E OpE)
(3) Op → +
(4) Op → *
E$ (int + (int * int))$
(E Op E)$ (int + (int * int))$
E Op E)$ int + (int * int))$
int Op E)$ int + (int * int))$
Op E)$ + (int * int))$
+ E)$ + (int * int))$
E)$ (int * int))$
(E Op E))$ (int * int))$
int ( ) + *
E 1 2
Op 3 4
LL(1) Parsing
1162020
117. (1) E → int
(2) E → (E OpE)
(3) Op → +
(4) Op → *
E$ (int + (int * int))$
(E Op E)$ (int + (int * int))$
E Op E)$ int + (int * int))$
int Op E)$ int + (int * int))$
Op E)$ + (int * int))$
+ E)$ + (int * int))$
E)$ (int * int))$
(E Op E))$ (int * int))$
E Op E))$ int * int))$int ( ) + *
E 1 2
Op 3 4
LL(1) Parsing
1172020
118. (1) E → int
(2) E → (E OpE)
(3) Op → +
(4) Op → *
E$ (int + (int * int))$
(E Op E)$ (int + (int * int))$
E Op E)$ int + (int * int))$
int Op E)$ int + (int * int))$
Op E)$ + (int * int))$
+ E)$ + (int * int))$
E)$ (int * int))$
(E Op E))$ (int * int))$
E Op E))$ int * int))$int ( ) + *
E 1 2
Op 3 4
LL(1) Parsing
1182020
119. (1) E → int
(2) E → (E OpE)
(3) Op → +
(4) Op → *
int * int))$
E$ (int + (int * int))$
(E Op E)$ (int + (int * int))$
E Op E)$ int + (int * int))$
int Op E)$ int + (int * int))$
Op E)$ + (int * int))$
+ E)$ + (int * int))$
E)$ (int * int))$
(E Op E))$ (int * int))$
E Op E))$ int * int))$
int Op E))$ int * int))$
int ( ) + *
E 1 2
Op 3 4
LL(1) Parsing
1192020
120. (1) E → int
(2) E → (E OpE)
(3) Op → +
(4) Op → *
int * int))$
E$ (int + (int * int))$
(E Op E)$ (int + (int * int))$
E Op E)$ int + (int * int))$
int Op E)$ int + (int * int))$
Op E)$ + (int * int))$
+ E)$ + (int * int))$
E)$ (int * int))$
(E Op E))$ (int * int))$
E Op E))$ int * int))$
int Op E))$ int * int))$
Op E))$ * int))$
int ( ) + *
E 1 2
Op 3 4
LL(1) Parsing
1202020
121. (1) E → int
(2) E → (E OpE)
(3) Op → +
(4) Op → *
int * int))$
E$ (int + (int * int))$
(E Op E)$ (int + (int * int))$
E Op E)$ int + (int * int))$
int Op E)$ int + (int * int))$
Op E)$ + (int * int))$
+ E)$ + (int * int))$
E)$ (int * int))$
(E Op E))$ (int * int))$
E Op E))$ int * int))$
int Op E))$ int * int))$
Op E))$ * int))$
int ( ) + *
E 1 2
Op 3 4
LL(1) Parsing
1212020
122. (1) E → int
(2) E → (E OpE)
(3) Op → +
(4) Op → *
int * int))$
E$ (int + (int * int))$
(E Op E)$ (int + (int * int))$
E Op E)$ int + (int * int))$
int Op E)$ int + (int * int))$
Op E)$ + (int * int))$
+ E)$ + (int * int))$
E)$ (int * int))$
(E Op E))$ (int * int))$
E Op E))$ int * int))$
int Op E))$ int * int))$
Op E))$ * int))$
* E))$ * int))$
int ( ) + *
E 1 2
Op 3 4
LL(1) Parsing
1222020
123. (1) E → int
(2) E → (E OpE)
(3) Op → +
(4) Op → *
int * int))$
E$ (int + (int * int))$
(E Op E)$ (int + (int * int))$
E Op E)$ int + (int * int))$
int Op E)$ int + (int * int))$
Op E)$ + (int * int))$
+ E)$ + (int * int))$
E)$ (int * int))$
(E Op E))$ (int * int))$
E Op E))$ int * int))$
int Op E))$ int * int))$
Op E))$ * int))$
* E))$ * int))$
E))$ int))$
int ( ) + *
E 1 2
Op 3 4
LL(1) Parsing
1232020
124. (1) E → int
(2) E → (E OpE)
(3) Op → +
(4) Op → *
int * int))$
E$ (int + (int * int))$
(E Op E)$ (int + (int * int))$
E Op E)$ int + (int * int))$
int Op E)$ int + (int * int))$
Op E)$ + (int * int))$
+ E)$ + (int * int))$
E)$ (int * int))$
(E Op E))$ (int * int))$
E Op E))$ int * int))$
int Op E))$ int * int))$
Op E))$ * int))$
* E))$ * int))$
E))$ int))$
int ( ) + *
E 1 2
Op 3 4
LL(1) Parsing
1242020
125. (1) E → int
(2) E → (E OpE)
(3) Op → +
(4) Op → *
int * int))$
E$ (int + (int * int))$
(E Op E)$ (int + (int * int))$
E Op E)$ int + (int * int))$
int Op E)$ int + (int * int))$
Op E)$ + (int * int))$
+ E)$ + (int * int))$
E)$ (int * int))$
(E Op E))$ (int * int))$
E Op E))$ int * int))$
int Op E))$ int * int))$
Op E))$ * int))$
* E))$ * int))$
E))$ int))$
int))$ int))$
int ( ) + *
E 1 2
Op 3 4
LL(1) Parsing
1252020
126. (1) E → int
(2) E → (E OpE)
(3) Op → +
(4) Op → *
int * int))$
E$ (int + (int * int))$
(E Op E)$ (int + (int * int))$
E Op E)$ int + (int * int))$
int Op E)$ int + (int * int))$
Op E)$ + (int * int))$
+ E)$ + (int * int))$
E)$ (int * int))$
(E Op E))$ (int * int))$
E Op E))$ int * int))$
int Op E))$ int * int))$
Op E))$ * int))$
* E))$ * int))$
E))$ int))$
int))$ int))$
))$ ))$
int ( ) + *
E 1 2
Op 3 4
LL(1) Parsing
1262020
127. (1) E → int
(2) E → (E OpE)
(3) Op → +
(4) Op → *
int * int))$
E$ (int + (int * int))$
(E Op E)$ (int + (int * int))$
E Op E)$ int + (int * int))$
int Op E)$ int + (int * int))$
Op E)$ + (int * int))$
+ E)$ + (int * int))$
E)$ (int * int))$
(E Op E))$ (int * int))$
E Op E))$ int * int))$
int Op E))$ int * int))$
Op E))$ * int))$
* E))$ * int))$
E))$ int))$
int))$ int))$
))$ ))$
)$ )$
int ( ) + *
E 1 2
Op 3 4
LL(1) Parsing
1272020
128. (1) E → int
(2) E → (E OpE)
(3) Op → +
(4) Op → *
int * int))$
E$ (int + (int * int))$
(E Op E)$ (int + (int * int))$
E Op E)$ int + (int * int))$
int Op E)$ int + (int * int))$
Op E)$ + (int * int))$
+ E)$ + (int * int))$
E)$ (int * int))$
(E Op E))$ (int * int))$
E Op E))$ int * int))$
int Op E))$ int * int))$
Op E))$ * int))$
* E))$ * int))$
E))$ int))$
int))$ int))$
))$ ))$
)$ )$
$ $
int ( ) + *
E 1 2
Op 3 4
LL(1) Parsing
1282020
129. int + int$
LL(1) Error Detection
(1) E → int
(2) E → (E OpE)
(3) Op → +
(4) Op → *
int ( ) + *
E 1 2
Op 3 4
1292020
130. int + int$E$
LL(1) Error Detection
(1) E → int
(2) E → (E OpE)
(3) Op → +
(4) Op → *
int ( ) + *
E 1 2
Op 3 4
1302020
131. int + int$E$
LL(1) Error Detection
(1) E → int
(2) E → (E OpE)
(3) Op → +
(4) Op → *
int ( ) + *
E 1 2
Op 3 4
1312020
132. E$ int + int$
int $ int + int$
LL(1) Error Detection
(1) E → int
(2) E → (E OpE)
(3) Op → +
(4) Op → *
int ( ) + *
E 1 2
Op 3 4
1322020
133. E$ int + int$
int $ int + int$
$ + int$
LL(1) Error Detection
(1) E → int
(2) E → (E OpE)
(3) Op → +
(4) Op → *
int ( ) + *
E 1 2
Op 3 4
1332020
134. E$ int + int$
int $ int + int$
$ + int$
LL(1) Error Detection
(1) E → int
(2) E → (E OpE)
(3) Op → +
(4) Op → *
int ( ) + *
E 1 2
Op 3 4
1342020
136. Constructing LL(1) Parse Tables
Example 1
136
E → TE’
E’ → +TE’ | ε
T → FT’
T’ → *FT’ | ε
F → (E) | id
First (E) = { ( , id }
First (E’) = { + , ε }
First (T) = { ( , id }
First (T’) = { * , ε }
First (F) = { ( , id }
Follow (E) = { ) , $ }
Follow (E’) = { ) , $ }
Follow (T) = { + , ) , $ }
Follow (T’) = { + , ) , $ }
Follow (F) = { * , + , ) , $ }
First (TE’) = { ( , id }
First (+TE’ ) = { + }
First (ε) = { ε }
First (FT’) = { ( , id }
First (*FT’) = { * }
First (ε) = { ε }
First ((E)) = { ( }
First (id) = { id }
2020
137. Constructing LL(1) Parse Tables
Example 1
137
E → TE’ FIRST(TE’)={(,id} ➔ E → TE’ into M[E,(] and M[E,id]
E’ → +TE’ FIRST(+TE’ )={+} ➔ E’ → +TE’ into M[E’,+]
E’ → FIRST()={} ➔ none
but since in FIRST()
and FOLLOW(E’)={$,)} ➔ E’ → into M[E’,$] and M[E’,)]
T → FT’ FIRST(FT’)={(,id} ➔ T → FT’ into M[T,(] and M[T,id]
T’ → *FT’ FIRST(*FT’ )={*} ➔ T’ → *FT’ into M[T’,*]
T’ → FIRST()={} ➔ none
but since in FIRST()
and FOLLOW(T’)={$,),+} ➔ T’ → into M[T’,$], M[T’,)] and M[T’,+]
F → (E) FIRST((E) )={(} ➔ F → (E) into M[F,(]
F → id FIRST(id)={id} ➔ F → id into M[F,id]
2020
138. Constructing LL(1) Parse Tables
Example 1
138
id + * ( ) $
E E → TE’ E → TE’
E’ E’ → +TE’ E’ → E’ →
T T → FT’ T → FT’
T’ T’ → T’ → *FT’ T’ → T’ →
F F → id F → (E)
2020
139. Constructing LL(1) Parse Tables
■ Rules for adding production choices to the table :
– If A → α is a production choice, and there is a derivation α ➔* aβ, then add A
→ α to the table entry M[A, a].
– If A → α is a production choice, and there are derivations α ➔* ε and S$ ➔* β
A a γ, then add A → α to the table entry M[A, a].
■ Clearly, the token a in rule 1 is in First (α), and the token a in rule 2 is in Follow (A)
139
Repeat the following two steps for each nonterminal A and production choice A→ α
1. For each token a in First (α), add A → α to the entry M[A,a]
2. if ε is in First (α), for each element a of Follow A (a token or $), add A → α to M[A,a]
2020
140. Constructing LL(1) Parse Tables
Example 2
140
Repeat the following two steps for each nonterminal A and production choice A→ α
1. For each token a in First (α), add A → α to the entry M[A,a]
2. if ε is in First (α), for each element a of Follow A (a token or $), add A → α to M[A,a]
S → (S) S
S → ε
First ( (S)S ) = { ( }
First (ε) = {ε}
Follow (S) = { $ , ) }
M [N, T] ( ) $
S S → ( S ) S S → ε S → ε
A = S , α = (S) S
A = S , α = ε
2020
141. Constructing LL(1) Parse Tables
Example 3
141
Repeat the following two steps for each nonterminal A and production choice A→ α
1. For each token a in First (α), add A → α to the entry M[A,a]
2. if ε is in First (α), for each element a of Follow A (a token or $), add A → α to M[A,a]
S → A S b
S → C
A → a
C → c C
C →
a b c $
S S → A S b S → C S → C S → C
A A → a
C C → C → c C C →
2020
149. Question 4
■ A grammar that produces more than one parse tree for some sentence is called:
A. Ambiguous.
B. Unambiguous.
C. Regular.
D. None of the mentioned.
1492020
150. Question 4
■ A grammar that produces more than one parse tree for some sentence is called:
A. Ambiguous.
B. Unambiguous.
C. Regular.
D. None of the mentioned.
1502020
151. Question 5
■ An intermediate code form is:
A. Postfix notation.
B. Syntax trees.
C. Three address codes.
D. All of these.
1512020
152. Question 5
■ An intermediate code form is:
A. Postfix notation.
B. Syntax trees.
C. Three address codes.
D. All of these.
1522020
153. Question 6
■ Compiler translates the source code to:
A. Executable code.
B. Machine code.
C. Binary code.
D. Both B and C.
1532020
154. Question 6
■ Compiler translates the source code to:
A. Executable code.
B. Machine code.
C. Binary code.
D. Both B and C.
1542020
155. Question 7
■ Which of the following groups is/are token together into semantic structures?
A. Syntax analyzer.
B. Intermediate code generation.
C. Lexical analyzer.
D. Semantic analyzer.
1552020
156. Question 7
■ Which of the following groups is/are token together into semantic structures?
A. Syntax analyzer.
B. Intermediate code generation.
C. Lexical analyzer.
D. Semantic analyzer.
1562020
157. Question 8
■ _________ is a process of finding a parse tree for a string of tokens:
A. Parsing.
B. Analyzing.
C. Recognizing.
D. Tokenizing.
1572020
158. Question 8
■ _________ is a process of finding a parse tree for a string of tokens:
A. Parsing.
B. Analyzing.
C. Recognizing.
D. Tokenizing.
1582020
159. Question 9
■ What is the action of parsing the source program into proper syntactic classes?
A. Lexical analysis.
B. Syntax analysis.
C. General syntax analysis.
D. Interpretation analysis.
1592020
160. Question 9
■ What is the action of parsing the source program into proper syntactic classes?
A. Lexical analysis.
B. Syntax analysis.
C. General syntax analysis.
D. Interpretation analysis.
1602020
161. Question 10
■ Which of the following languages is generated by the given grammar?
■ S → a S | b S | ε
A. anbm
B. {a,b}*
C. {a,b}n
D. Other.
1612020
162. Question 10
■ Which of the following languages is generated by the given grammar?
■ S → a S | b S | ε
A. anbm
B. {a,b}*
C. {a,b}n
D. Other.
1622020
163. Question 11
■ Which of the following strings is not generated by the following grammar?
■ S → SaSbS|ε
A. aabb
B. abab
C. aababb
D. aabbb
1632020
164. Question 11
■ Which of the following strings is not generated by the following grammar?
■ S → SaSbS|ε
A. aabb
B. abab
C. aababb
D. aabbb
1642020
165. Question 12
■ Which of the following is NOT the set of regular expression R = (ab + abb)* bbab
A. ababbbbab
B. abbbab
C. ababbabbbab
D. abababab
1652020
166. Question 12
■ Which of the following is NOT the set of regular expression R = (ab + abb)* bbab
A. ababbbbab
B. abbbab
C. ababbabbbab
D. abababab
1662020
167. Question 13
■ In a one pass compiler, the syntax analysis is performed:
A. After the lexical analysis.
B. After the semantic analysis.
C. With the lexical analysis.
1672020
168. Question 13
■ In a one pass compiler, the syntax analysis is performed:
A. After the lexical analysis.
B. After the semantic analysis.
C. With the lexical analysis.
1682020
169. Question 14
■ When a syntax error appears, the compiler:
A. Stops Immediately.
B. Stops after collecting few Errors.
C. Depends on the organization of the syntax rules.
1692020
170. Question 14
■ When a syntax error appears, the compiler:
A. Stops Immediately.
B. Stops after collecting few Errors.
C. Depends on the organization of the syntax rules.
1702020
171. Question 15
■ Each deterministic finite automata (DFA) has an equivalent regular expression
A. True.
B. False.
1712020
172. Question 15
■ Each deterministic finite automata (DFA) has an equivalent regular expression
A. True.
B. False.
1722020
173. Question 16
■ Each Non deterministic finite automata (NFA) has an equivalent regular expression
A. True.
B. False.
1732020
174. Question 16
■ Each Non deterministic finite automata (NFA) has an equivalent regular expression
A. True.
B. False.
1742020