Write a grammar (not necessarily LL(1)) that recognizes various forms of variabl
ID: 3792503 • Letter: W
Question
Write a grammar (not necessarily LL(1)) that recognizes various forms of variables in C++, which should include the simple variable (e.g. a, b, c), array variables (e.g. a[10], a[10][10], a[10][10][10])), class members (e.g. a. data, a.data.name, ...), pointer variables (e.g. a rightarrow data, a rightarrow data>name), and composite variables that are combinations of the above forms (e.g. a rightarrow bb[100][200][300].data rightarrow mdata.bb[100]). You can assume the array index can only be a number.Explanation / Answer
note:s is the start symbol, * indicates zero or more occureances, + indicates one or more occurances
s -->alpha|| index ||alpha.alpha ||pointer ||comb
alpha -->any alphabet from A-Z or a-z ||alpha+_alpha* ||alpha+num*alpha* ||alpha* s* ||alpha*
index-->alpha[num+] ||alpha[num+][num+] ||alpha[num+][num+][num+] || index* s*
num -->any number from 0-9
pointer --> alpha->alpha.alpha || alpha->index || alpha->alpha ||pointer* s*
Eg: variable is a->bb[100][200][300].data->mdata.bb[100]
s-->alpha.alpha
s-->alpha->alpha*s*
s-->alpha->s
s-->alpha->alpha.alpha //let this be @
s-->alpha->alpha.s
s-->alpha->alpha.index
s-->s->alpha.index
s-->alpha->alpha.alpha->alpha.index //from @
s-->a->alpha[num+][num]+[num+].alpha->alpha.alpha[num+]
s-->a->alpha*[100][200][300].alpha*->alpha*.alpha*[100]
s-->a->bb[100][200][300].data->mdata.bb[100]
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.