Download Introduction to Compilers - Practice Problems 1 Solutions | CMSC 430 and more Exams Computer Science in PDF only on Docsity!
CMSC 430, Practice Problems 1 (Solutions)
- Describe the languages denoted by the following regular expressions: a. 0(0|1)∗ 0 strings beginning and ending in 0 b. ((ε |0)1∗)∗ all strings of 0 and 1 c. (0|1)∗0(0|1)(0|1) strings with 0 as third digit from right
- Regular expressions and languages a. Give a regular expression for all binary numbers including the substring “101”. (0|1)101(0|1) b. Give a regular expression for all binary numbers with an even number of 1’s. 0(1010) c. Give a regular expression for all binary numbers that don’t include “000”. (01 | 001 | 1)*(0 | 00 | ε)
- Finite automata a. Give a NFA that only accepts binary numbers including the substring “101”.
b. Give a NFA that only accepts binary numbers that include either “00” or “11”.
c. Give a NFA that only accepts binary numbers that include both “00” and “11”.
2 3
7
6
1
(^8 9 )
4 5
10 11
2 3
77
6
1
(^8 9 )
4 5
10 11
d. Compute the ε-closure of the start state for each of the NFA above.
- For NFA in (a) ε–closure(1) = {1,2}
- For NFA in (b) ε–closure(1) = {1,2,5}
- For NFA in (c) ε–closure(1) = {1,2,8}
e. Give a DFA that only accepts binary numbers with an odd number of 1’s.
1
2
1
22
f. Give a DFA that only accepts binary numbers that include “000”.
1 2
3
4
1 2
3
44
g. Give a DFA that only accepts binary numbers that don’t include “000”.
(0|01)*0 → NFA
3 4
1 2 (^7 )
εε^ εε
5 6
10 11
3 4
1 2 (^7 )
εε^ εε
5 6
10 11
εε^ εε
(0|01)*0 → NFA → DFA
9,7,1, 3,10,
2,4,12, 8,10,11,9, 7,1,3,
6,8,10, 11,9,7, 1,
9,7,1, 3,10,
2,4,12, 8,10,11,9, 7,1,3,
6,8,10, 11,9,7, 1,
Accept / reject
- “1” {9,7,1,3,10,11} → reject
- “11” {9,7,1,3,10,11} → reject
- “101” {9,7,1,3,10,11} → reject
Minimized DFA Initial partitions: accept ={ {2,4…}} = P1, nonfinal ={ {9,7…}, {6,8…}} = P move({9,7…}, 0) → P move({6,8…}, 0) → P move({9,7…}, 1) → reject move({6,8…}, 1) → reject No need to split P2, minimization done. After cleanup, minimal DFA (different from previous minimal DFA) is
9,7,1, 3,10,
2,4,12, 8,10,11,9, 7,1,3,
6,8,10, 11,9,7, 1,
P1 0
P
P
P
9,7,1, 3,10,
2,4,12, 8,10,11,9, 7,1,3,
6,8,10, 11,9,7, 1,
P1 0
P
P
P
- Describing grammars a. Describe the language accepted by the following grammar: S → abS | a (ab)a b. Describe the language accepted by the following grammar: S → aSb | ε anbn, n ≥ 0 c. Describe the language accepted by the following grammar: S → bSb | A A → aA | ε bnabn, n ≥ 0 d. Describe the language accepted by the following grammar: S → AS | B A → aAc | Aa | ε B→ bBb | ε Strings of a & c with same or fewer c’s than a’s and no prefix has more c’s than a’s, followed by an even number of b’s e. Describe the language accepted by the following grammar: S → S and S | S or S | (S) | true | false Boolean expressions of true & false separated by and & or, with some expressions enclosed in parentheses f. Which of the previous grammars are left recursive? 5d, 5e g. Which of the previous grammars are right recursive? 5a, 5c, 5d, 5e h. Which of the previous grammars are ambiguous? Provide proof. Examples of multiple left-most derivations for the same string 5d: S => AS => AaS => aS => aB => a S => AS => S => AS => AaS => aS => aB => a 5e: S => S and S => S and S and S => true and S and S => true and true and S => true and true and true S => S and S => true and S => true and S and S => true and true and S => true and true and true
- Creating grammars a. Write a grammar for axby, where x = y S → aSb | ε b. Write a grammar for axby, where x > y S → aL L → aL | aLb | ε c. Write a grammar for axby, where x = 2y S → aaSb | ε d. Write a grammar for all strings of a and b that are palindromes. S → aSa | bSb | L L → a | b | ε e. Write a grammar for all strings of a and b that include the substring baa. S → LbaaL L → aL | bL | ε // L = any f. Write a grammar for all strings of a and b with an odd number of a ’s and b ’s. S → EaEbE | EbEaE E → EaEaE | EbEbE | ε | SS // E = even #s
S
S (^) and S
true
true true
S (^) and S
S
S (^) and S
true
true true
S (^) and S
Tree 1 Tree 2 S
S (^) and S
true
true true
S (^) and S
S
S (^) and S
true
true true
S (^) and S
Tree 1 Tree 2
- Consider the following grammar S → aSbS | bSaS | ε a. Prove grammar is ambiguous by finding 2 left-most derivations for ”abab” S ⇒ aSbS ⇒ abS ⇒ abaSbS ⇒ ababS ⇒ abab S ⇒ aSbS ⇒ abSaSbS ⇒ abaSbS ⇒ ababS ⇒ abab b. Prove grammar is ambiguous by finding 2 right-most derivations for ”abab” S ⇒ aSbS ⇒ aSbaSbS ⇒ aSbaSb ⇒ aSbab ⇒ abab S ⇒ aSbS ⇒ aSb ⇒ abSaSb ⇒ abSab ⇒ abab
- Consider the following grammar: S → S and S | S or S | (S) | true | false a. Compute First sets for each production and nonterminal FIRST(true) = { “true” } FIRST(false) = { “false” } FIRST( (S) ) = { “(“ } FIRST( S and S ) = FIRST( S or S ) = FIRST( S ) = { “(“, “true”, “false” } b. Explain why the grammar cannot be parsed by a LL(1) parser First sets of productions intersect, grammar is left recursive
- Consider the following grammar: S → abS | acS | c a. Compute First sets for each production and nonterminal FIRST(abS) = { a } FIRST(acS) = { a } FIRST(c) = { c } FIRST(S) = { a, c } b. Show why the grammar cannot be parsed by a LL(1) parser. First sets of productions overlap FIRST(abS) ∩ FIRST(acS) = { a } ∩ { a } = { a } ≠ ∅∅∅∅ c. Rewrite the grammar so it can be parsed by a LL(1) parser. S → aL | c L → bS | cS d. Write a recursive descent parser for the rewritten grammar. parse_S( ) { if (lookahead == “a”) { match(“a”); // S → aL parse_L( ); } else if (lookahead == “c”) match(“c”); // S → c } else error( ); } parse_L( ) { if (lookahead == “b”) { match(“b”); // L → bS parse_S( ); } else if (lookahead == “c”) { match(“c”); // L → cS parse_S( ); } else error( ); }
- Consider the following grammar Goal → Expr Expr → Term + Expr | Term – Expr | Term Term → Factor * Term | Factor / Term | Factor Factor → num | id
a. Apply left factoring to create a LL(1) grammar b. Compute FIRST and FOLLOW for the modified grammar
Left Factored Grammar FIRST FOLLOW Productions Nonterminals Goal →→→→ Expr { num, id } Goal { num, id } { $ } Expr →→→→ Term Expr’ { num, id } Expr { num, id } { $ } Expr’ →→→→ + Expr’ { + } Expr’ { +, -, εεεε } { $ } | – Expr’ { – } Term { num, id } { +, -, $ } | εεεε { εεεε } Term’ { *, /, εεεε } { +, -, $ } Term →→→→ Factor Term’ { num, id } Factor { num, id } { *, /, +, -, $ } Term’ →→→→ * Term { * } | / Term { / } | εεεε { εεεε } Factor →→→→ num { num } | id { id }
c. Using FIRST and FOLLOW , build the LL(1) table for the new grammar
Term’ → ε
Expr’ → ε
$
Factor Factor → num Factor → id
Term’ Term’^ → ε Term’^ → ε Term’^ →^ * Term^ Term’^ →^ / Term
Term → Factor Term’
Term → Factor Term’ Term
Expr’ Expr’ → + Expr’ Expr’ → - Expr’
Expr → Term Expr’
Expr → Expr Term Expr’
Goal Goal → Expr Goal → Expr
num id + - * /
Term’ → ε
Expr’ → ε
$
Factor Factor → num Factor → id
Term’ Term’^ → ε Term’^ → ε Term’^ →^ * Term^ Term’^ →^ / Term
Term → Factor Term’
Term → Factor Term’ Term
Expr’ Expr’ → + Expr’ Expr’ → - Expr’
Expr → Term Expr’
Expr → Expr Term Expr’
Goal Goal → Expr Goal → Expr
num id + - * /