Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
lfa:2022:lab10-cfl_2 [2022/12/10 23:58]
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>​ 
 +The  PDA has the following transitions looping on state q: 
 +  * $ \epsilon, S/a $ 
 +  * $ \epsilon, S/xAz $ 
 +  * $ \epsilon, S/SbS $  
 +  * $ \epsilon, S/cS $   
 +  * $ \epsilon, A/SyS $   
 +  * $ \epsilon, A/SyStS $   
 +  * $ a, a/\epsilon $  
 +  * $ b, b/\epsilon $ 
 +  * $ c, c/\epsilon $ 
 +  * $ x, x/\epsilon $ 
 +  * $ y, y/\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 ababa that has 2 different left-derivations \\ S => SbS => abS => abSbS => ababS => ababa \\ S => SbS => SbSbS => abSbS => ababS => ababa \\ 
 +\\ 
 +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 ​variable or a literal) with the operations '​and',​ '​not',​ '​if-then'​ and '​if-then-else'​. 
 +\\ 
 +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>​ 
 + 
  
  
 **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 58: 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>​
 +
  
  
Line 67: Line 116:
 Separate the following input strings into lexemes: Separate the following input strings into lexemes:
   * 010101   * 010101
 +
 +
 +
 +<​hidden> ​
 +Although the entire string is matched by PAIRS and NO_CONSEC_ONE,​ PAIRS is defined first, thus it will be the first picked. \\
 +PAIRS "​010101"​
 +
 +
 +</​hidden>​
 +
 +
   * 1010101011   * 1010101011
 +
 +
 +<​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. \\
 +NO_CONSEC_ONE "​101010101"​ \\ ONES "​1"​
 +</​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>​