Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
lfa:2022:lab10-cfl_2 [2022/12/11 00:16] alexandra.udrescu01 |
lfa:2022:lab10-cfl_2 [2022/12/29 00:59] (current) alexandra.udrescu01 |
||
---|---|---|---|
Line 8: | Line 8: | ||
- run the PDA on the given inputs \\ | - run the PDA on the given inputs \\ | ||
- is the grammar ambiguous? If yes, write a non ambiguous grammar that generates the same language \\ | - is the grammar ambiguous? If yes, write a non ambiguous grammar that generates the same language \\ | ||
+ | |||
**10.1.1** input: aaaabb \\ | **10.1.1** input: aaaabb \\ | ||
$ S \leftarrow aS | aSb | \epsilon $ \\ | $ S \leftarrow aS | aSb | \epsilon $ \\ | ||
+ | |||
+ | |||
+ | |||
<hidden Solution> | <hidden Solution> | ||
The start symbol of the PDA is S. \\ The PDA will only have one state q and it will accept via empty stack. \\ | The start symbol of the PDA is S. \\ The PDA will only have one state q and it will accept via empty stack. \\ | ||
Line 30: | Line 34: | ||
</hidden> | </hidden> | ||
- | **10.1.2** input: abaaaaaa \\ | + | |
- | $ S \leftarrow aAA \\ A \leftarrow aS | bS | a $ | + | |
+ | |||
+ | **10.1.2** input: xayxcayatabcazz \\ | ||
+ | |||
+ | |||
+ | |||
+ | $ S \leftarrow a | xAz | SbS | cS \\ A \leftarrow SyS | SyStS $ | ||
+ | |||
+ | |||
<hidden Solution> | <hidden Solution> | ||
The PDA has the following transitions looping on state q: | The PDA has the following transitions looping on state q: | ||
- | * $ \epsilon, S/aAA $ | + | * $ \epsilon, S/a $ |
- | * $ \epsilon, A/aS $ | + | * $ \epsilon, S/xAz $ |
- | * $ \epsilon, A/bS $ | + | * $ \epsilon, S/SbS $ |
- | * $ \epsilon, A/a $ | + | * $ \epsilon, S/cS $ |
+ | * $ \epsilon, A/SyS $ | ||
+ | * $ \epsilon, A/SyStS $ | ||
* $ a, a/\epsilon $ | * $ a, a/\epsilon $ | ||
* $ b, b/\epsilon $ | * $ b, b/\epsilon $ | ||
- | + | * $ c, c/\epsilon $ | |
- | Input: abaaaaaa \\ (abaaaaaa, q, S) => (abaaaaaaa, q, aAA) => (baaaaaaa, q, AA) => (baaaaaaa, q, bSA) => (aaaaaaa, q, SA) => (aaaaaaa, q, aAAA) => (aaaaaa, q, AAA) | + | * $ x, x/\epsilon $ |
- | => (aaaaaa, q, aSAA) => (aaaaa, q, SAA) => (aaaaa, q, aAAAA) => (aaaa, q, AAAA) => (aaaa, q, aAAA) => (aaa, q, AAA) => (aaa, q, aAA) => (aa, q, AA) => (aa, q, aA) | + | * $ y, y/\epsilon $ |
- | => (a, q, A) => (a, q, a) => ($\epsilon$, q, $\epsilon$) \\ | + | * $ z, z/\epsilon $ |
+ | * $ t, t/\epsilon $ | ||
+ | Input: xayxcayatabcazz \\ (xayxcayatabcazz, q, S) => (xayxcayatabcazz, q, xAz) => (ayxcayatabcazz, q, Az) => (ayxcayatabcazz, q, SySz) => (ayxcayatabcazz, q, aySz) => (yxcayatabcazz, q, ySz) => (xcayatabcazz, q, Sz) => (xcayatabcazz, q, xAzz) => (cayatabcazz, q, Azz) => (cayatabcazz, q, SyStSzz) => (cayatabcazz, q, cSyStSzz) => (ayatabcazz, q, SyStSzz) => (ayatabcazz, q, ayStSzz) => (yatabcazz, q, yStSzz) => (atabcazz, q, StSzz) => (atabcazz, q, StSzz) => (atabcazz, q, atSzz) => (tabcazz, q, tSzz) => (abcazz, q, Szz) => (abcazz, q, SbSzz) => (abcazz, q, abSzz) => (bcazz, q, bSzz) => (cazz, q, Szz) => (cazz, q, cSzz) => (azz, q, Szz) => (azz, q, azz) => (zz, q, zz) => (z, q, z) => ($\epsilon$, q, $\epsilon$)\\ | ||
\\ | \\ | ||
- | Is the grammar ambiguuous? yes because of word aaaaaa that has 2 different left-derivations \\ S => aAA => aaSA => aaaAAA => aaaaAA => aaaaaA => aaaaaa \\ S => aAA => aaA => aaaS => aaaaAA => aaaaaA => aaaaaa \\ | + | Is the grammar ambiguuous? yes because of word ababa that has 2 different left-derivations \\ S => SbS => abS => abSbS => ababS => ababa \\ S => SbS => SbSbS => abSbS => ababS => ababa \\ |
\\ | \\ | ||
- | The accepted language is L(G) = TODO \\ | + | It is hard to directly explain the language in this form. Another form may be easier. Let's relabel the terminals: a => bool; b => and; c => not; x => if; y => then; z => fi; t => else.\\ |
+ | The grammar becomes: $ S \leftarrow bool | if A fi | S and S | not S \\ A \leftarrow S then S | S then S else S $\\ | ||
+ | The language generated can be described as the language of boolean expressions (considering 'bool' is either a variable or a literal) with the operations 'and', 'not', 'if-then' and 'if-then-else'. | ||
\\ | \\ | ||
- | Repaired grammar: TODO | + | Why is it ambigous? The 'and'/b operator does not define its associativity, and the operators 'and'/b and 'not'/c do not have a clear precedence rule.\\ |
+ | To fix this grammar we will use the following conventions: ababa == (aba)ba and caba == (ca)ba \\ | ||
+ | Repaired grammar: \\ | ||
+ | $ S \leftarrow TbS | T \\ T \leftarrow cT | xAz | a \\ A \leftarrow SyS | SyStS $ \\ | ||
</hidden> | </hidden> | ||
+ | |||
+ | |||
+ | |||
**10.1.3** input: aaabbbbbccc \\ | **10.1.3** input: aaabbbbbccc \\ | ||
+ | |||
$ S \leftarrow ABC \\ A \leftarrow aA | \epsilon \\ B \leftarrow bbB | b \\ C \leftarrow cC | c $ | $ S \leftarrow ABC \\ A \leftarrow aA | \epsilon \\ B \leftarrow bbB | b \\ C \leftarrow cC | c $ | ||
+ | |||
+ | |||
+ | |||
+ | |||
<hidden Solution> | <hidden Solution> | ||
The PDA has the following transitions looping on state q: | The PDA has the following transitions looping on state q: | ||
Line 76: | Line 106: | ||
The accepted language is $ L(G) = \{a^{m}b^{2n + 1}c^{p+1} | m,n,p \ge 0\} $ \\ | The accepted language is $ L(G) = \{a^{m}b^{2n + 1}c^{p+1} | m,n,p \ge 0\} $ \\ | ||
</hidden> | </hidden> | ||
+ | |||
+ | |||
===== 10.2. Lexer Spec ===== | ===== 10.2. Lexer Spec ===== | ||
Line 84: | Line 116: | ||
Separate the following input strings into lexemes: | Separate the following input strings into lexemes: | ||
* 010101 | * 010101 | ||
+ | |||
+ | |||
+ | |||
<hidden> | <hidden> | ||
Although the entire string is matched by PAIRS and NO_CONSEC_ONE, PAIRS is defined first, thus it will be the first picked. \\ | Although the entire string is matched by PAIRS and NO_CONSEC_ONE, PAIRS is defined first, thus it will be the first picked. \\ | ||
- | NO_CONSEC_ONE "010101" | + | PAIRS "010101" |
+ | |||
</hidden> | </hidden> | ||
+ | |||
+ | |||
* 1010101011 | * 1010101011 | ||
+ | |||
+ | |||
<hidden> | <hidden> | ||
First we have a maximal match on "101010101" for regex NO_CONSEC_ONE. The remaining string, "1", is matched by both ONES and NO_CONSEC_ONE, but ONES is defined first. \\ | First we have a maximal match on "101010101" for regex NO_CONSEC_ONE. The remaining string, "1", is matched by both ONES and NO_CONSEC_ONE, but ONES is defined first. \\ | ||
- | NO_CONSEC_ONE "101010101" | + | NO_CONSEC_ONE "101010101" \\ ONES "1" |
- | ONES "1" | + | |
</hidden> | </hidden> | ||
+ | |||
+ | |||
* 01110101001 | * 01110101001 | ||
+ | |||
+ | |||
+ | <hidden> | ||
+ | PAIRS "01" \\ | ||
+ | ONES "11" \\ | ||
+ | NO_CONSEC_ONES "0101001" | ||
+ | </hidden> | ||
+ | |||
+ | |||
* 01010111111001010 | * 01010111111001010 | ||
+ | |||
+ | |||
+ | <hidden> | ||
+ | PAIRS "010101" \\ | ||
+ | ONES "11111" \\ | ||
+ | NO_CONSEC_ONES "001010" | ||
+ | </hidden> | ||
+ | |||
+ | |||
* 1101101001111100001010011001 | * 1101101001111100001010011001 | ||
+ | |||
+ | |||
+ | <hidden> | ||
+ | ONES "11" | ||
+ | PAIRS "01101001" \\ | ||
+ | ONES "1111" \\ | ||
+ | NO_CONSEC_ONES "0000101001" \\ | ||
+ | PAIRS "1001" | ||
+ | </hidden> | ||