Question
Which part of this program is used for input one context free grammar? And if you want to give another different context free grammar, how to do that?

code:

#include<stdio.h>
#include<stdlib.h> #include<string.h>
#define MaxVtNum 20
#define MaxVnNum 20
#define MaxPLength 20
#define MaxSTLength 50
char stack[20]={'#','E'};
char input[MaxSTLength];
char termin[MaxVtNum]={'i','+','*','(',')','#'}; char non_termin[MaxVnNum]={'E','G','T','H','F'};
struct product{
char left;
char right[MaxPLength];
int length;
};
struct product E,T,G,G1,H,H1,F,F1;
struct product M[MaxVnNum][MaxVtNum];
int flag=1;
int top=1;
int l;

void print_stack(){
}
}
}
}
}
}
int i;
for(i=0;i<=top;i++)
printf("%c",stack[i]);
void print_input(){
int i;
for(i=0;i<flag-1;i++)
printf(" ");
for(i=flag-1;i<=l;i++)
printf("%c",input[i]);
int non_termin_row(char c){
int i;
for(i=0;i<(int)strlen(non_termin);i++)
if(c==non_termin[i])
return i;
printf("Error in non_termin_row()>%c\n",c);
exit(0);
int termin_col(char c){
int i;
for(i=0;i<(int)strlen(termin);i++)
if(c==termin[i])
return i;
printf("Error in termin_col()>%c\n",c);
exit(0);
bool isNT(char c){
int i;
for(i=0;i<(int)strlen(non_termin);i++)
if(c==non_termin[i])
return false;
return true;
bool isT(char c){
int i;
for(i=0;i<(int)strlen(termin)-1;i++)
if(c==termin[i])
return true;
return false;

void main(void){
int i,j=0,k=0;
int flag2=0;
char ch;
char X=' ';
struct product cha;
E.left='E';
strcpy(E.right,"TG");
E.length=2; T.left='T';
strcpy(T.right,"FH");
T.length=2;
G.left='G';
strcpy(G.right,"+TG");
G.length=3;
G1.left='G';
G1.right[0]='~';
G1.length=1;
H.left='H';
strcpy(H.right,"*FH");
H.length=3;
H1.left='H';
H1.right[0]='~';
H1.length=1;
F.left='F';
strcpy(F.right,"(E)");
F.length=3;
F1.left='F';
F1.right[0]='i';
F1.length=1;
M[0][0]=E;M[0][3]=E;
M[1][1]=G;M[1][4]=G1;M[1][5]=G1;
M[2][0]=T;M[2][3]=T;
M[3][1]=H1;M[3][2]=H;M[3][4]=M[3][5]=H1;
M[4][0]=F1;M[4][3]=F;
printf("this program do parsing just for strings consisting with
'i','+','*','(',')', and ended by'#',\n");
printf("please input the one string:");\
do{
scanf("%c",&ch);
if((ch!='i')&&(ch!='+')&&(ch!='*')&&(ch!='(')&&(ch!=')')&&(ch!='# ')){
printf("error\n");
exit(1);
}
input[k]=ch;
k++;
}while(ch!='#'); l=strlen(input);
ch=input[0];
printf("step\t stack\t input string\t rule \n");
while(1){
printf("\n(%d)\t",j++);
print_stack();
printf("\t");
print_input();
printf("\t"); if(flag2==1){
printf("%c->%s",cha.left,cha.right);
flag2=0;
}
printf("\t");
X=stack[top--];
if(X=='#'){
if(X==ch){
}
printf("\tAcc\n");
else
printf("\tERROR\n");
break;
}
else if(isT(X)){
ch=input[flag++];
}
else if(isNT(X)){
cha=M[non_termin_row(X)][termin_col(ch)];
flag2=1;
for(i=(cha.length-1);i>=0;i--)
stack[++top]=cha.right[i];
if(stack[top]=='~')
}
top--;
else{
printf("ERROR in main()>%c\n",X);
exit(0);
}
}

}

Gr. C:\windows\system32\cmd.exe 本程序只能对由(,》构成的以“#”结束的字符串进行分析。 请输入要分析的字符串:#i+i 分析栈 输入串 所用规则 基E E->TG T->FH E->i H->FH F-
0 0
Add a comment Improve this question Transcribed image text
Answer #1

do{

scanf("%c",&ch);

if((ch!='i')&&(ch!='+')&&(ch!='*')&&(ch!='(')&&(ch!=')')&&(ch!='# ')){

printf("error\n");

exit(1);

}

input[k]=ch;

k++;

}while(ch!='#'); l=strlen(input);

This part of the code is used to input context free grammer.

The same part can be changed to provide another input context free grammer.

Add a comment
Know the answer?
Add Answer to:
Which part of this program is used for input one context free grammar? And if you...
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
  • 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...

  • Question 1 Which pre-written C function can be used to determine if two strings are the...

    Question 1 Which pre-written C function can be used to determine if two strings are the same? A) equals B) strcmp C) strlen D) strcpy E) None of the Above Question 2 The function below is most like which existing string function? int f(char a[ ]) {                int count = 0;                while (a[count] != ‘\0’)                               count++;                return count; } A) strcat B) strcmp C) strcpy D) strlen E) None of the Above Question 3 The function...

  • I'm having trouble getting my program to output this statement Enter a sentence: Test! There are...

    I'm having trouble getting my program to output this statement Enter a sentence: Test! There are 5 total characters. There are 1 vowel. There are 1 UPPERCASE letters. There are 3 lowercase letters. There are 1 other characters. Here's my code: #include<string.h> #include<stdio.h> #include<stdbool.h> int main() { char str[100]; int i; int vowels=0; int UC; int LC; int Others; int c; printf("Enter a sentence: \n"); gets(s); LC=countLC(&s); UC=countUC(&s); Others=countOthers(&s); printf("There are %d total characters.\n", ; for(i=0; i<strlen(str); i++){ if(isVowel(str[i])) vowels++;...

  • I'm having trouble getting a certain output with my program Here's my output: Please input a...

    I'm having trouble getting a certain output with my program Here's my output: Please input a value in Roman numeral or EXIT or quit: MM MM = 2000 Please input a value in Roman numeral or EXIT or quit: mxvi Illegal Characters. Please input a value in Roman numeral or EXIT or quit: MXVI MXVI = 969 Please input a value in Roman numeral or EXIT or quit: EXIT Illegal Characters. Here's my desired output: Please input a value in...

  • Writing a program in C please help!! My file worked fine before but when I put...

    Writing a program in C please help!! My file worked fine before but when I put the functions in their own files and made a header file the diplayadj() funtion no longer works properly. It will only print the vertices instead of both the vertices and the adjacent like below. example input form commnd line file: A B B C E X C D A C The directed edges in this example are: A can go to both B and...

  • Please answer problem #5 thank you str.c #include "str.h" #include <stdio.h> int str_len(...

    Please answer problem #5 thank you str.c #include "str.h" #include <stdio.h> int str_len(char *s) {    /* this is here so code compiles */ return 0; } /* array version */ /* concantenate t to the end of s; s must be big enough */ void str_cat(char s[], char t[]) {    int i, j;    i = j = 0;    while (s[i] != '\0')    /* find end of s */        i++;    while ((s[i++] = t[j++]) != '\0') /* copy t */        ;...

  • can u please solve it in c++ format,,,, general format which is #include<stdio.h> .......printf and scanf...

    can u please solve it in c++ format,,,, general format which is #include<stdio.h> .......printf and scanf format dont worry about the answers i have down Q3, What is the output of the program shown below and explain why. #include #include <stdio.h> <string.h> int main(void) ( char str[ 39764 int N strlen(str) int nunj for (int i-0; i t Nǐ.de+ printf(") num (str[] 'e); I/ digits range in the ASCII code is 48-57 if (nue > e) f 9 printf( Xdin,...

  • Please help modify my C program to be able to answer these questions, it seems the...

    Please help modify my C program to be able to answer these questions, it seems the spacing and some functions arn't working as planeed. Please do NOT copy and paste other work as the answer, I need my source code to be modified. Source code: #include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> int main(void) { char title[50]; char col1[50]; char col2[50]; int point[50]; char names[50][50]; printf("Enter a title for the data:\n"); fgets (title, 50, stdin); printf("You entered: %s\n", title);...

  • Please explain how these code run for each line #include<stdio.h> #include<string.h> /** * Part A */...

    Please explain how these code run for each line #include<stdio.h> #include<string.h> /** * Part A */ struct myWord{    char Word[21];    int Length; }; int tokenizeLine(char line[], struct myWord wordList[]); void printList(struct myWord wordList[], int size); void sortList(struct myWord wordList[], int size); /** * main function */ int main() {    struct myWord wordList[20];    char line[100];    printf("Enter an English Sentence:\n");    gets(line);    int size = tokenizeLine(line, wordList);    printf("\n");    printf("Unsorted word list.\n");    printList(wordList, size);...

  • Need help with this C program? I cannot get it to compile. I have to use...

    Need help with this C program? I cannot get it to compile. I have to use Microsoft Studio compiler for my course. It's a word game style program and I can't figure out where the issue is. \* Program *\ // Michael Paul Laessig, 07 / 17 / 2019. /*COP2220 Second Large Program (LargeProg2.c).*/ #define _CRT_SECURE_NO_DEPRECATE //Include the following libraries in the preprocessor directives: stdio.h, string.h, ctype.h #include <stdio.h> /*printf, scanf definitions*/ #include <string.h> /*stings definitions*/ #include <ctype.h> /*toupper, tolower...

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
ADVERTISEMENT