homework cs441

 

Q2). Write EBNF and syntax graph descriptions for the following:

c). A C switch statement

<switch_stmt> -> switch <expr> <stmt_list>

<stmt_list> -> case <const_expr>: <stmt>; break; [ default : <stmt> ]

 

 

d). A C union definition

<union_defn> -> union <var_list> <union_identifier>;

<var_list> -> <list_of_data-type specifier> <var>

<list_of_data-type specifier> -> int | float | long |char | double

<union_identifier> -> <var>

 

e). A C float literals

 

<float_num> -> <float_lit>

<float_lit> -> 12.4 | 34.98 | 89.67

 

 

Q7). Describe, in English, the language defined by the following grammar:

<S> -> <A> <B> <C>

<A> -> a <A> | a

<B> -> b <B> | b

<C> -> c <C> | c

 

the abstraction,<S> is defined as an instance of the abstractions <A>, <B>, and <C>,

where <A>,<B>,<C> must be defined first. There are two distinct definition listed under

abstractions <A>,<B> and <C>. The two definitions are separated by the symbol '|', meaning

logical OR. Abstraction <A> is defined as a recursion of abstraction <A> itself OR a single

character 'a'. Abstraction <B> is defined as a recursion of abstraction <B> itself OR a single

character 'b'. And finally, abstraction <C> is defined as a recursion of abstraction <C>

itself OR a single character 'c'.

 

Q8). Consider the following grammar:

<S> -> <A> a <B> b

<A> -> <A> b | b

<B> -> a <B> | a

 

Which of the following sentences in the language generated by this grammar?

a). baab

b). bbbab

c). bbaaaaa

d). bbaab

 

ANSWER : a and d

 

Q9). Consider the following grammar:

<S> -> a <S> c <B> | <A> | b

<A> -> c <A> | c

<B> -> d | <A>

 

Which of the following sentences in the language generated by this grammar?

a). abcd

b). acccbd

c). acccbcc

d). acd

e). accc

 

ANSWER : a and e

 

 

Q10). Write a grammar for the language consisting of strings that have n copies of the letter

'a' followed by the same number of copies of the letter 'b', where n > 0. For example , the

strings ab. aaaabbbb, and aaaaaaabbbbbbb are in the language but a,ba and aaabb are not. <br>

 

<program> -> begin <prog_body> end;

<prog_body> -> <assign> <A> <for_stmt> <B>

<assign> -> <identifier> := <int_lit>

 

<A> -> a <sum_a> | a <sum_a> <A>

<sum_a> -> <identifier> + <identifier>

<identifier> -> count_a

<for_stmt> -> for <var> := <expr> to <sum_id> do <B>

<B> -> b