Edit this page Backlinks This page is read only. You can view the source, but not change it. Ask your administrator if you think this is wrong. ====== 10. Context-Free Languages & Lexers ====== ===== 10.1. Context-Free Grammar to PDA conversion ===== For each context-free grammar G: \\ - describe L(G) \\ - algoritmically construct a PDA that accepts the same language \\ - run the PDA on the given inputs \\ - is the grammar ambiguous? If yes, write a non ambiguous grammar that generates the same language \\ **10.1.1** input: aaaabb \\ $ S \leftarrow aS | aSb | \epsilon $ \\ <hidden CFG to PDA> The start symbol of the PDA is S. \\ The PDA will only have one state q and it will accept via empty stack. \\ For each nonterminal/rule $ A \leftarrow \gamma $ add a transition **q ---$(\epsilon, A/ \gamma)$--➤ q** and for each terminal c add **q ---$(c, c/ \epsilon)$--➤ q** Thus, our PDA has the following transitions looping on state q: * $ \epsilon, S/aS $ * $ \epsilon, S/aSb $ * $ \epsilon, S/\epsilon $ * $ a, a/\epsilon $ * $ b, b/\epsilon $ Input: aaabb \\ (aaabb, q, S) => (**a**aabb, q, **a**Sb) => (aabb, q, Sb) => (**a**abb, q, **a**Sbb) => (abb, q, Sbb) => (**a**bb, q, **a**Sbb) => (bb, q, Sbb) => (bb, q, bb) => (b, q, b) => ($\epsilon$, q, $\epsilon$) \\ Is the grammar ambiguuous? yes, because there exist 2 different left-derivations for word aaabb \\ S => aSb => aaSbb => aaaSbb => aaabb \\ S => aS => aaSb => aaaSbb => aaabb \\ \\ The accepted language is $ L(G) = \{a^{m}b^{n} | m \ge n \ge 0\} $ \\ \\ Repaired grammar: \\ $ S \leftarrow aS | A \\ A \leftarrow aAb | \epsilon $ </hidden> **10.1.2** input: abaaaaaa \\ $ S \leftarrow aAA \\ A \leftarrow aS | bS | a $ **10.1.3** input: aaabbbbbccc \\ $ S \leftarrow ABC \\ A \leftarrow aA | \epsilon \\ B \leftarrow bbB | b \\ C \leftarrow cC | c $ ===== 10.2. Lexer Spec ===== Given the following specs, construct the lexer DFA as presented in Lecture 14: * PAIRS: $ (10 | 01)* $ * ONES: $ 1+ $ * NO_CONSEC_ONE: $ (1 | \epsilon)(01 | 0)* $ Separate the following input strings into lexemes: * 010101 * 1010101011 * 01110101001 * 01010111111001010 * 1101101001111100001010011001