# Recursive Gramm-a-r

Page 1 of 1

## 7 Replies - 1250 Views - Last Post: 08 September 2016 - 11:35 AM

### #1 Dinomite07

Reputation: -3
• Posts: 9
• Joined: 07-September 16

# Recursive Gramm-a-r

Posted 07 September 2016 - 04:35 PM

I was wondering if I am answering these questions correctly. And how would I start off on the pseudo code for the recursive function?

Consider a language of strings that contains only X’s, Y’s and Z’s. A string in this language must begin with an X. If a Y is present in a string, it must be the final character of the string.

a. Write a recursive grammar for this language.
<string> = <X>|<body>|<Y>
<X> = X
<Y> = Y
<Z> = Z
<body> = <X>|<Z>

b. Write all the possible two-character strings of this language.
{X,Y}, {Z,Y}, {X,Z}, {Z,X}

Consider the following grammar:
<G> = empty string |<E>|<V><E>|<E><G><V>
<E> = &|#
<V> = W|A
a. Write pseudo code for a recursive function that returns true if a string is in this language and returns false otherwise.

b. Is the string &W#W in this language?
<E><V><E><V> is not possible.
No

Is This A Good Question/Topic? 0

## Replies To: Recursive Gramm-a-r

### #2 Dinomite07

Reputation: -3
• Posts: 9
• Joined: 07-September 16

## Re: Recursive Gramm-a-r

Posted 07 September 2016 - 05:05 PM

DELETE

IGNORE FIRST TWO POSTS

I was wondering if I am answering these questions correctly.

Consider a language of strings that contains only X’s, Y’s and Z’s. A string in this language must begin with an X. If a Y is present in a string, it must be the final character of the string.

a. Write a recursive grammar for this language.
<X> = X
<Y> = Y
<Z> = Z
<body> = <X>|<Z>
<string> = <X>|<body>|<Y>

b. Write all the possible two-character strings of this language.
{X,Y}, {Z,Y}, {X,Z}, {Z,X}

Consider the following grammar:
<G> = empty string |<E>|<V><E>|<E><G><V>
<E> = &|#
<V> = W|A
a. Write pseudocode for a recursive function that returns true if a string is in this language and returns false otherwise.
IsIdentLegal (Iden: String) Boolean
If (Iden length is 1)
If (Iden is <G> || <E> || <V>)
Return true
Else
Return false
Else If (Iden.length < 1)
Return false

b. Is the string &W#W in this language?
<E><V><E><V> is possible.
Yes

I was wondering if I am answering these questions correctly.

Consider a language of strings that contains only X’s, Y’s and Z’s. A string in this language must begin with an X. If a Y is present in a string, it must be the final character of the string.

a. Write a recursive grammar for this language.
<X> = X
<Y> = Y
<Z> = Z
<body> = <X>|<Z>
<string> = <X>|<body>|<Y>

b. Write all the possible two-character strings of this language.
{X,Y}, {Z,Y}, {X,Z}, {Z,X}

Consider the following grammar:
<G> = empty string |<E>|<V><E>|<E><G><V>
<E> = &|#
<V> = W|A
a. Write pseudocode for a recursive function that returns true if a string is in this language and returns false otherwise.
IsIdentLegal (Iden: String) Boolean
If (Iden length is 1)
If (Iden is <G> || <E> || <V>)
Return true
Else
Return false
Else If (Iden.length < 1)
Return false

b. Is the string &W#W in this language?
<E><V><E><V> is possible.
Yes
[/quote]

### #3 Dinomite07

Reputation: -3
• Posts: 9
• Joined: 07-September 16

## Re: Recursive Gramm-a-r

Posted 07 September 2016 - 05:04 PM

I was wondering if I am answering these questions correctly.

Consider a language of strings that contains only X’s, Y’s and Z’s. A string in this language must begin with an X. If a Y is present in a string, it must be the final character of the string.

a. Write a recursive grammar for this language.
<X> = X
<Y> = Y
<Z> = Z
<body> = <X>|<Z>
<string> = <X>|<body>|<Y>

b. Write all the possible two-character strings of this language.
{X,Y}, {Z,Y}, {X,Z}, {Z,X}

Consider the following grammar:
<G> = empty string |<E>|<V><E>|<E><G><V>
<E> = &|#
<V> = W|A
a. Write pseudocode for a recursive function that returns true if a string is in this language and returns false otherwise.
IsIdentLegal (Iden: String) Boolean
If (Iden length is 1)
If (Iden is <G> || <E> || <V>)
Return true
Else
Return false
Else If (Iden.length < 1)
Return false

b. Is the string &W#W in this language?
<E><V><E><V> is possible.
Yes

### #4 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12315
• Posts: 45,413
• Joined: 27-December 08

## Re: Recursive Gramm-a-r

Posted 07 September 2016 - 05:09 PM

Please do not open duplicate threads. Is there a C/C++ specific question? If not, I will move this to the Computer Science forum.

### #5 Dinomite07

Reputation: -3
• Posts: 9
• Joined: 07-September 16

## Re: Recursive Gramm-a-r

Posted 07 September 2016 - 05:18 PM

macosxnerd101, on 07 September 2016 - 05:09 PM, said:

Please do not open duplicate threads. Is there a C/C++ specific question? If not, I will move this to the Computer Science forum.

Please delete this thread, I am going to make a new one in the CS forum

### #6 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12315
• Posts: 45,413
• Joined: 27-December 08

## Re: Recursive Gramm-a-r

Posted 07 September 2016 - 06:59 PM

I will move this thread to the Computer Science forum. Consider this a formal warning to stop spamming your post. If you are doing so to get edit permissions, I will move you to the Restricted Editors group which will permanently restrict your editing permissions.

### #7 mojo666

Reputation: 408
• Posts: 882
• Joined: 27-June 09

## Re: Recursive Gramm-a-r

Posted 08 September 2016 - 08:42 AM

Quote

a. Write a recursive grammar for this language.
<X> = X
<Y> = Y
<Z> = Z
<body> = <X>|<Z>
<string> = <X>|<body>|<Y>

Typically, a recursive string would need to recurse. An example would be <body> = X<body>|Y. This rule produces all strings of any number of X's followed by a Y. These rules function by replacement. You start with <body> and replace it with one of the two possibilities from the rule until you are left with nothing to replace. So, to produce the string XXXY

```<body>    //Start
X<body>    //I chose first option to replace <body>
XX<body>   //I chose first option
XXX<body>   //I chose first option
XXXY   //I chose second option.  Nothing left to replace
```

With the grammer you produced we can only do the following

```<string> //start
<X> //first option
X //finished

<string> //start
<Y>
Y //finished

<string> //start
<body>
<X>
X //finished

<string> //start
<body>
<Z>
Z //finished
```

Doesn't even come close to what you need.

Quote

b. Write all the possible two-character strings of this language.
{X,Y}, {Z,Y}, {X,Z}, {Z,X}

Strings in the language must begin with X, so this is wrong. You do have 2 of the 3 possible strings though.

Quote

a. Write pseudocode for a recursive function that returns true if a string is in this language and returns false otherwise.

Your pseudo code doesn't work. If I pass the string &W it is supposed to return true. Your code wouldn't even return.

### #8 andrewsw

• blow up my boots

Reputation: 6549
• Posts: 26,553
• Joined: 12-December 12

## Re: Recursive Gramm-a-r

Posted 08 September 2016 - 11:35 AM

grammar not grammer