Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
316 views3 pages

Solution 1 Regular Expressions and Context-Free Grammars

This document discusses regular expressions, context-free grammars, and parsing: 1. It defines regular expressions for several binary languages and writes regular expressions to describe languages over the alphabet {a,b}. 2. It writes a regular expression to specify floating-point decimal numbers. 3. It defines a context-free grammar and uses it to parse sentences, constructing leftmost and rightmost derivations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
316 views3 pages

Solution 1 Regular Expressions and Context-Free Grammars

This document discusses regular expressions, context-free grammars, and parsing: 1. It defines regular expressions for several binary languages and writes regular expressions to describe languages over the alphabet {a,b}. 2. It writes a regular expression to specify floating-point decimal numbers. 3. It defines a context-free grammar and uses it to parse sentences, constructing leftmost and rightmost derivations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Solution 1

Regular expressions and context-free grammars


1. Describe the languages denoted by the following regular expressions:
a. 0(0|1)*0
The language contains binary numbers that starts with a 0 and
ends with another 0.
b. (0|1)*0(0|1)(0|1)
The language contains binary numbers that ends with 000,011,010,
or 001.
c. 0*10*10*10*
The language contains binary numbers that have exactly 3 digits 1.
2. Let = {a,b}. Write regular expressions for the languages over that contain:
a. All strings beginning with ab.
ab(a|b)*
b. All strings that contain exactly two as.
b*ab*ab*
c. All strings in which every a is followed by a b.
(b|ab)+
3. Floating-point decimals consist in an integer part, a decimal part and an exponent
part. The integer part is a sequence of one or more digits. The decimal part is a
decimal point followed by zero, one or more digits. The exponent part is the
character e or E followed by an optional + or - sign, followed by one or more
digits. The decimal part or the exponent part can be omitted, but not both. Write a
regular expression to specify floating-point decimals.
digit 0|1|2|3|4|5|6|7|8|9
integer_part digit+
decimal_part . digit*
exponent_part (E|e)(+|-)?digit+
floating-point integer_part decimal_part (exponent_part)?
| integer_part exponent_part
4. Consider the following grammar
S(L)|a
LL,S|S
a. What are the terminals, nonterminals, and start symbol?
Terminals: ( ) a ,
Nonterminals: S L
Start symbol: S
b. Find parse trees for the following sentences:
i. (a, a)

ii. (a, (a,a))

iii. (a, ((a,a),(a,a))

c. Construct a leftmost derivation for each of the sentences in (b)


S (L) (L,S) (S,S) (a,S) (a,a)

S (L) (L,S) (S,S) (a,S) (a,(L)) (a,(L,S)) (a,(S,S))


(a,(a,S)) (a,(a,a))
S (L) (L,S) (S,S) (a,S) (a,(L)) (a,(L,S)) (a,(S,S))
(a,(a,S)) (a,(a,(L))) (a,(a,(L,S))) (a,(a,(S,S)))
(a,(a,(a,S))) (a,(a,(a,a)))
d. Construct a rightmost derivation for each of the sentences in (b)
S (L) (L,S) (L,a) (S,a) (a,a)
S (L) (L,S) (L,(L)) (L,(L,S)) (L,(L,a)) (L,(S,a))
(L,(a,a)) (S,(a,a)) (a,(a,a))
S (L) (L,S) (L,(L)) (L,(L,S)) (L,(L,(L,S))) (L,(L,(L,a)))
(L,(L,(S,a))) (L,(L,(a,a))) (L,(S,(a,a))) (L,(a,(a,a)))
(S,(a,(a,a))) (a,(a,(a,a)))
5. Construct a grammar for regular expressions.
R R | T | T
TTF|F
F a | | ( R) | F*

You might also like