$29
• Finite State Automaton (FSA)
(a) Specify a DFA using a transition diagram and a formal FSA speci ca-tion <S, s, F, T> (see lecture 2) that recognizes the following language: \All strings of 0’s and 1’s that, when interpreted as a binary number, are divisible by 4. In other words, value(binary number) modulo 4 = 0."
(b) Specify a DFA using a transition diagram and a formal FSA speci ca-tion <S, s, F, T> (see lecture 2) that recognizes the following language: \All strings of 0’s and 1’s that, when interpreted as a binary number, its value modulo 4 = 1. "
• Context-Free Languages
Are the following languages context-free or not? If yes, specify a context-free grammar in BNF notation that generates the language. If not, give an informal argument.
(a) f ambnco j m > n 0, o > 0g , with alphabet = fa, b, cg
(b) f anb3n j n 0 g, with alphabet = fa, bg
(c) f wwR j w 2 and wR is w in reverse g, with alphabet =fa, bg
(d) f anbncndm j n 0, m 0 g, with alphabet = fa, b, c, dg
(e) f w j w has no more than 3 symbolsg, with alphabet =fa, bg
• Derivation, Parse Tree, Ambiguity, Prece-dence & Associativity
A language that is a subset of the language of propositional logic may be de ned as follows:
1
<start> ::= <expr>
<expr> :: = <expr> _ <expr> j
<expr> ^ <expr> j
<expr> ! <expr> j
<const> j <var>
<const> :: = true j false
<var> :: = a j b j c j . . . j z
(a) Give a leftmost and a rightmost derivation for the sentence a _ false ^ b ! false .
(b) Give the corresponding parse trees for the derivations.
(c) Give the corresponding abstract syntax tree (AST).
(d) Show that the above grammar is ambiguous.
(e) Give an unambiguous grammar for the same language that enforces the following precedence and associativity:
^ has highest precedence (binds strongest), followed by _, and then !
^ and _ are left associative, and ! is right associative
(f) Give the parse tree and AST for your new, unambiguous grammar for the sentence
a _ true ^ b ! false _ true .
• Extra-credit Problem | Fix the grammar for dangling if-then-else statement (10% more points)
Recall the simple statement grammar we discussed in class:
2
<start> ::= <stmt>
<stmt> ::= <if-stmt> j <assgn>
<if-stmt> ::= if <expr> then <stmt> j
if <expr> then <stmt> else <stmt>
<assgn> ::= <id> := <d>
<expr> ::= <id> = 0
<d> ::= 0 j 1 j 2 j 3 j 4 j 5 j 6 j 7 j 8 j 9
<id> ::= a j b j c j . . . j z
The above grammar will cause ambiguity for parsing the following com-pound statement:
if x = 0 then if y = 0 then z := 1 else z := 2
Is it possible to change the grammar without changing the language to parse the above statement unambiguously? If not, please give an infor-mal argument. If yes, please provide your solution. Note that you are not supposed to add new terminals (tokens), i.e., delimiters, as it will change the language.
3