0% found this document useful (0 votes)
21 views7 pages

Muhamedsultan Awol Compiler

The document discusses a compiler design assignment submitted by Muhamedsultan Awol to Mr. Debebe K on April 22, 2024 in Dilla, Ethiopia. It contains questions on context-free grammars, derivations, parse trees, ambiguity, and constructing grammars for specific languages.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views7 pages

Muhamedsultan Awol Compiler

The document discusses a compiler design assignment submitted by Muhamedsultan Awol to Mr. Debebe K on April 22, 2024 in Dilla, Ethiopia. It contains questions on context-free grammars, derivations, parse trees, ambiguity, and constructing grammars for specific languages.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

DILLA UNIVERSITY

COLLEGE OF ENGINEERING AND


TECHNOLOGY
SCHOOL OF COMPUTING AND INFORMATICS
DEPARTMENT OF COMPUTER SCIENCE
Compiler design individual assignment

By Muhamedsultan Awol
ID RNS -4803/19

Submitted to Mr. Debebe K


submission date April 22, 2024
Dilla Ethiopia
1. Consider the context-free grammar: S → S S + | S S * | a and the string aa + a*.
a) Give a leftmost derivation for the string.
Therefore, we have S → S S +
S→SS*
S→a
Leftmost derivation for the string "aa + a*":
S→SS*
S→SS+S*
S→aS+S*
S→aa+S*
S→aa+a*
b) Give a rightmost derivation for the string.
S→SS*
S→Sa*
S→SS+a*
S→Sa+a*
S→aa+a*
c) Give a parse tree for the string.
S
___|___
| |
S S*
| |
a a
| |
a *
|
+
d) Is the grammar ambiguous or unambiguous? Justify your answer.
The given grammar is ambiguous because there are multiple leftmost and rightmost derivations
for the same string "aa + a*". For instance, in this grammar, both the "+" and "*" operators have
the same precedence and associativity, leading to ambiguity when parsing expressions containing
both operators. Ambiguity in a grammar means that there can be more than one parse tree for a
particular string, or equivalently, more than one leftmost or rightmost derivation for a string.

e) Describe the language generated by this grammar.


The language generated by this grammar consists of strings that represent arithmetic expressions
using the operators '+' and '*', with 'a' as the only terminal symbol. The grammar allows for
expressions to be composed of multiple terms separated by either addition or multiplication, with
no specific precedence or associativity enforced between the two operators. The language
includes expressions like "a", "aa", "a + a", "a * a", "a + a + a", "a * a * a", and so on

2. Consider the following grammar


Terminals = {a, b }
Non-Terminals = {S, T, F}
Start Symbol = S
Rules = S→ TF
T→ T T T
T→ a
F→ aFb
F→ b
Which of the following strings are derivable from the grammar? Give the parse tree for
derivable strings?
I. ab S→ TF S
S→ aF (T applied by a)
S→ ab (F applied by b) T F

a b
 This strings is derivable from the grammar.
II. aabbb S→ TF
S→ aF (T applied by a)
S→ aaFb (F applied by aFb)
S→ aabb (F applied by b)
 This string is not derivable from the grammar.
III. aba S→ TF
S→ aF (T applied by a)
S→ ab (F applied by b) OR
S→ aabb
 This string is not derivable from the grammar.
IV. aaabb S→ TF S→ TF
S→ aF (T applied by a) S→ TTTF
S→ aaFb (F applied by aFb) S→ aaaF
S→ aaaFbb (F applied by aFb) S→ aaaaFb
S→ aaaabb S→ aaaabb
 This string is not derivable from the grammar.
V. aaaabb S→ TF S
S→ TTTF (T applied by TTT)
S→ aaaF (T applied by a) F T
T T T a F b
S→ aaaaFb (F applied by aFb)
S→ aaaabb (F applied by b)
 This strings is derivable from the grammar.

3. Show that the following CFGs are ambiguous by giving two parse trees for the same string?
3.1) Terminals = { a, b }
Non-Terminals = {S, T}
Start Symbol = S
Rules = S→ STS
S→ b
T→ aT
T→ ε
 Based on this string ‘bbb’.
1st parse tree: S

S T S
b ε S T S
b ε b
2nd parse tree:
s

S T S
S T S ε b
b ε b
3.2) Terminals = { if, then, else, print, id }
Non-Terminals = {S, T}
Start Symbol = S
Rules = S→ if id then S T
S→ print id
T→ else S
T→ ε
1st parse tree: S

If id then S T
print id ε

2nd parse tree:


S

If id then S T
print id else s
print id

4. Construct a CFG for each of the following:


a. All integers with sign (Example: +3, -3)
Terminals = {+3, -3}
Non-Terminals = {S, N}
Start Symbol = S
Rules = S→ +N| -N
N→ D | DN
D→ 0| 1| 2| 3| 4| 5| 6| 7| 8| 9
 Hence: S can generate either a positive sign (+) followed by the integer N, or a negative
sign (-) followed by the integer N.
 N can generate a single digit D or a digit D followed by another integer N.
 D can generate any digit from 0 to 9.
b. The set of all strings over { (, ), [, ]} which form balanced parenthesis. That is, (), ()(),
((()())()), [()()] and ([()[]()]) are in the language but )( , ][ , (() and ([ are not.
Terminals = {(),[]}
Non-Terminals = {S, P, B}
Start Symbol = S
Rules = S → SS | P | B | ε
P → (S)
B → [S]
c. CFG for legal binary postfix expressions:
Terminals = { num, +, -, *, / }
Non-Terminals = { S, E }
Start Symbol = S
Rules:
S→E
E→EE+
E→EE-
E→EE*
E→EE/
E → num
d. Ambiguity of CFGs:
a. The CFG for all integers with sign is unambiguous as each string can be derived in a unique
way.
b. The CFG for balanced parentheses is unambiguous because there is only one way to generate
balanced parentheses.
c. The CFG for legal binary postfix expressions is ambiguous. For example, the string "num num
+ num *" can have multiple parse trees depending on the order of operations. One parse tree
could interpret it as "(num + num) * num" while another could interpret it as "num + (num *
num)". Therefore, the grammar is ambiguous.
In summary, the CFGs in a and b are unambiguous, while the C is ambiguous.

You might also like