Question

Syntax checker. We will write C code to check to see if an input string can be derived from a gra...

Syntax checker. We will write C code to check to see if an input string can be derived from a grammar. The grammar is:

A ::= 0 B

B ::= 1 A | 2 B | ;

Your program "check.c" should output "yes" if the line of standard input can be derived from this grammar, and "no" otherwise. Here are some examples:

$ ./check

0 ;

yes

$ ./check

0 2 1 ;

no

$

The input values will always be separated by spaces, as shown above. Only edit the file where at the comments 'YOUR CODE HERE'.  

******************** check.c ************************

#include <stdio.h>
#include <string.h>

// Instructions:
// Edit the code below ONLY at the comments showing YOUR CODE HERE.

// Hints:
// You may need additional scanf() statements, but they can be just
// like the scanf() statements already in the code below.

// check whether the input can be derived from this grammar
// A ::= 0 B
// B ::= 1 A | 2 B | ;

int A(), B();

int A() {
int n;
char tok[10];

// read a string from input
n = scanf("%s", &tok);

// YOUR CODE HERE

}

int B() {
int n;
char tok[10];

n = scanf("%s", &tok);

// YOUR CODE HERE

}

int main() {
A();
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <stdio.h>
#include <string.h>

int A(), B();

int A() {
int n;
char tok[10];

// read a string from input
n = scanf("%s", &tok);

// YOUR CODE HERE
//first character should be 0
if(strcmp(tok,"0")==0)
{
   B();
}
else printf("no");
}

int B() {
int n;
char tok[10];

n = scanf("%s", &tok);

// YOUR CODE HERE
//if next character is 2 then call B()
if(strcmp(tok,"2")==0)
{
   B();
}
else if(strcmp(tok,"1")==0) //if next character is 1 then call A()
{
   A();
}
else if(strcmp(tok,";")==0) //if next character is ; then print yes
{
   printf("yes");
}
else printf("no");
}

int main() {
A();
}

output:

0 1 0 2 2 1; yes Process exited after 14.74 seconds with return value 3 Press any key to continue - -

Add a comment
Know the answer?
Add Answer to:
Syntax checker. We will write C code to check to see if an input string can be derived from a gra...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a function that can return the length of a string in C language. Please leave...

    Write a function that can return the length of a string in C language. Please leave comments to explain the code and verify that it runs properly... Example: Input String: China The length of the string is 5. #include <stdio.h> int length(char * p) {     /write your code here } main(){     int len;     char str[20];     printf("Input string:");     scanf("%s", str);     len = length(str);     printf("The length of string is %d. ", len);     getchar();    ...

  • CHALLENGE ACTIVITY 5.71 String library functions. Assign the size of userinput to stringSize. Ex: if userinput...

    CHALLENGE ACTIVITY 5.71 String library functions. Assign the size of userinput to stringSize. Ex: if userinput is 'Hello output is: Size of user Input: 5 1 #include <stdio.h> 2 #include <string.h> 4 int main(void) { char user Input[50]; int stringSize; BO scanf("%s", userInput); /* Your solution goes here" printf("Size of user Input: %d\n", stringsize); return ; Run

  • Here is a serial program in C and parallel program in OpenMP that takes in a string as input and counts the number of occurrences of a character you choose. Why is the runtime for the output for the O...

    Here is a serial program in C and parallel program in OpenMP that takes in a string as input and counts the number of occurrences of a character you choose. Why is the runtime for the output for the OpenMP parallel program much longer? Serial Program #include <stdio.h> #include <string.h> #include <time.h> int main(){    char str[1000], ch;    int i, frequency = 0;    clock_t t; struct timespec ts, ts2;       printf("Enter a string: ");    gets(str);    int len = strlen(str);    printf("Enter a character...

  • getchar() redirect to input textfile in ASCI C programming. Hello, underneath is the code I did...

    getchar() redirect to input textfile in ASCI C programming. Hello, underneath is the code I did and it works. However, my professor is asking to use getchar() and have an input text file to redirect it. How would i do that on my code? Thank you for the help. #include <stdio.h> #include <string.h> int main() { char string[100]; int c = 0, Lcase[26] = {0}, x; int Ucase[26] = {0};    printf("Enter a string\n"); gets(string); while (string[c] != '\0') {...

  • using c language String Challenge Have the function StringChallenge(str) read str which will contain two strings...

    using c language String Challenge Have the function StringChallenge(str) read str which will contain two strings separated by a space. The first string will consist of the following sets of characters: +, *, $, and {N} which is optional. The plus (+) character represents a single alphabetic character, the ($) character represents a number between 1-9, and the asterisk (*) represents a sequence of the same character of length 3 unless it is followed by {N} which represents how many...

  • The following code is a C Program that is written for encrypting and decrypting a string....

    The following code is a C Program that is written for encrypting and decrypting a string. provide a full explanation of the working flow of the program. #include <stdio.h> int main() { int i, x; char str[100]; printf("\n Please enter a valid string: \t"); gets (str); printf ("\n Please choose one of the following options: \n"); printf ("1 = Encrypt the given string. \n"); printf("2 = Decrypt the entered string. \n"); scanf("%d",&x); // using switch case statements switch (x) {...

  • Write a program that prompts the user to input a string. The program then uses the...

    Write a program that prompts the user to input a string. The program then uses the function substr to remove all the vowels from the string. For example, if str=”There”, then after removing all the vowels, str=”Thr”. After removing all the vowels, output the string. Your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel. #include <iostream> #include <string> using namespace std; void removeVowels(string& str); bool isVowel(char ch);...

  • Programming C....... function code is clear but compile is wrong .. I input 8 and compiled...

    Programming C....... function code is clear but compile is wrong .. I input 8 and compiled 2 0 1 1 2 3 5 8 13.. look first number 2 is wrong. The correct answer 8 is 0 1 1 2 3 5 8 13 21.. Need fix code to compile correctly.. Here code.c --------------------------------------------------------------------------------------------------------------------- #include <stdio.h> #include <math.h> int fibonacciIterative( int n ) { int fib[1000]; int i; fib[ 0 ] = 0; fib[ 1 ] = 1; for (...

  • Write a program that can remove spaces from an input string, find the indexes of a...

    Write a program that can remove spaces from an input string, find the indexes of a character within the string and replace that character with another character. Here is an example input: I am an input string a b The first line, "I am an input string" represents the input string. Please put it into a string variable using getline. In the second line "a b", a is the character that needs to be located within the input string, and...

  • Caesar Cipher v3 Decription Description A Caesar cipher is one of the first and most simple...

    Caesar Cipher v3 Decription Description A Caesar cipher is one of the first and most simple encryption methods. It works by shifting all letters in the original message (plaintext) by a certain fixed amount (the amounts represents the encryption key). The resulting encoded text is called ciphertext. Example Key (Shift): 3 Plaintext: Abc Ciphertext: Def Task Your goal is to implement a Caesar cipher program that receives the key and an encrypted paragraph (with uppercase and lowercase letters, punctuations, and...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT