Question

In: Computer Science

1. regular expressions as a mechanism to specify tokens, 2. defining context-free grammars for language constructs,...

1. regular expressions as a mechanism to specify tokens,

2. defining context-free grammars for language constructs, and

3. derivation of language terms and expressions based on a context-free grammar.

1 Regex

1. Define the regex for the following description of tokens:

(a) Any string that starts with character

t

(b) Any string of at least length 3 that starts with

t

and ends with

u

(c) Any string that specifies the range of numbers between 11 and 23.

(d) Any string that specifies a date in MM:DD:YYYY format.

2. In C, an identifier is defined as a string of characters (both upper-case and lower-case), digits, and

underscore “_”, starting with either a character or underscore. Define the regex for identifiers in C.

3. Give five strings that conform with the regex:

[0-9]+((E|e)(\+|\-)?[0-9]+)?

Solutions

Expert Solution

a) ^t - Regex for string starting with t (small letter t)

b) ^t.u - Regex for string starting from t and ending on u with 3 character

^ - Matches the beginning of the string, or the beginning of a line if the multiline flag (m) is enabled. This matches a position, not a character.

. - Matches any character except line breaks.

c) [1-2][1-3] ----- First digit in between 1-2 and second digit would be in between 1-3.

d) ^(1[0-2]|0[1-9]):(3[01]|[12][0-9]|0[1-9]):[0-9]{4}$ -----

Explanation: (1[0-2]|0[1-9]) - capturing group 1 which matches for month format , | works as OR condition

(3[01]|[12][0-9]|0[1-9]) - capturing group 2 for dd

[0-9]{4} - year validation

2) ^[_a-zA-Z][_a-zA-Z0-9]{0,30}

Explanation : ^ is used for start charater. {0,30} represnts upto 30 character long

3) strings matching regex:[0-9]+((E|e)(\+|\-)?[0-9]+)?

1e+9
4E-9
2E-5
9e+0
3e-2

Please rate your answer.


Related Solutions

Regular => Context-Free Give a proof by induction on regular expressions that: For any language A,...
Regular => Context-Free Give a proof by induction on regular expressions that: For any language A, if A is regular, then A is also context-free. You may assume that the statements from the previous Closure under Context-Free Languages problem are proved. R is a regular expression if RR is: a for some a∈alphabet Σ, the empty string ε, the empty set ∅, R​1​​∪R​2​​, sometimes written R​1​​∣R​2​​, where R​1​​ and R​2​​ are regular expressions, R​1​​∘R​2​​, sometimes written R​1​​R​2​​, where R​1​​ and...
5 Regular => Context-Free Give a proof by induction on regular expressions that: For any language...
5 Regular => Context-Free Give a proof by induction on regular expressions that: For any language A, if A is regular, then A is also context-free. You may assume that the statements from the previous Closure under Context-Free Languages problem are proved.
Write regular expressions that describes the following language: The language over {0,1,/} that contains all and...
Write regular expressions that describes the following language: The language over {0,1,/} that contains all and only the strings that are base-2 (as above, you can use base-10 if you prefer) representations of rational numbers. Define a representation of a rational number to be either a representation of an integer, or two representations of integers separated by “/”. Leading 0s are allowed this time.
Write regular expressions that describes the following language: The language over {0,1} that contains all and...
Write regular expressions that describes the following language: The language over {0,1} that contains all and only the strings that are base-2 representations of odd positive integers. Do not allow leading 0s. (If you are more comfortable writing bulky regular expressions than you are working in base-2, you may write a regular expression for strings that are base-10 representations of odd integers without leading 0s, using alphabet {0,1,2,3,4,5,6,7,8,9}.)
Give context free grammars for the languages below. To avoid mistakes, I recommend using scratch paper...
Give context free grammars for the languages below. To avoid mistakes, I recommend using scratch paper to check that your grammars produce the right set of string. This question is structured so that nonterminals (and associated rules) from an earlier subquestion might be useful in a grammar for a later sub-question. That’s why, rather than “S” as the start symbol, we will used different start symbols for each part. If you do use a nonterminal from an earlier sub-question, say...
Give context free grammars for the languages below. To avoid mistakes, I recommend using scratch paper...
Give context free grammars for the languages below. To avoid mistakes, I recommend using scratch paper to check that your grammars produce the right set of string. This question is structured so that nonterminals (and associated rules) from an earlier subquestion might be useful in a grammar for a later sub-question. That’s why, rather than “S” as the start symbol, we will used different start symbols for each part. If you do use a nonterminal from an earlier sub-question, say...
Describe the languages specified by the following regular expressions: 1. \\(_+)/ 2. (\(740\)...-...)|(...-...) (The alphabet is...
Describe the languages specified by the following regular expressions: 1. \\(_+)/ 2. (\(740\)...-...)|(...-...) (The alphabet is {1,2,3,4,5,6,7,8,9,0})
Instructions: Using Unix programming language and regular expressions, 1. how many unique ip addresses were seen...
Instructions: Using Unix programming language and regular expressions, 1. how many unique ip addresses were seen note: we only want to look at ipv4 addresses 2. which was most commnly seen ip address on the piece of access.log file below 66.249.75.132 - - [18/Jun/2018:06:41:00 -0500] "GET /~rcoleman/Common/History/Images/?C=N;O=D HTTP/1.1" 200 1976 "-" "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)" 5.255.250.23 - - [18/Jun/2018:06:41:23 -0500] "GET /~rcoleman/Common/CodeVault/Code/DesignPatterns/Images/DP16-Builder.jpg HTTP/1.1" 304 182 "-" "Mozilla/5.0 (compatible; YandexImages/3.0; +http://yandex.com/bots)" 5.255.250.23 - - [18/Jun/2018:06:41:28 -0500] "GET /~rcoleman/CS121/CourseInfo/Images/WinExp.jpg HTTP/1.1" 304 180...
Prove the following Closure Properties with respect to Context Free Languages. 1. Show that Context Free...
Prove the following Closure Properties with respect to Context Free Languages. 1. Show that Context Free Languages are Closed under union(∪), concatenation(·), kleene star(*). (Hint: If L1 and L2 are Context Free languages then write Context Free grammar equivalent to L1 ∪ L2, L1 · L2, L∗ 1 ) 2. Show that if L1 and L2 are Context Free Languages, then L1 ∩ L2 is not necessarily Context Free. (Hint: Use Proof by Counter Example)
Please write an example of a language that is Turing-decidable but not Context-Free. You can just...
Please write an example of a language that is Turing-decidable but not Context-Free. You can just state what the language is; no need to give a full TM algorithm for it. 1b) Please write an example of a language that is not Turing-decidable. You can just state what the language is; no need to give a full TM algorithm for it.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT