4. Implement a recursive descent-parser in C, caller parser.c, for the following grammar (in EBNF) where is th

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

4. Implement a recursive descent-parser in C, caller parser.c, for the following grammar (in EBNF) where is th

Post by answerhappygod »

4 Implement A Recursive Descent Parser In C Caller Parser C For The Following Grammar In Ebnf Where Program Is Th 1
4 Implement A Recursive Descent Parser In C Caller Parser C For The Following Grammar In Ebnf Where Program Is Th 1 (109.59 KiB) Viewed 65 times
4 Implement A Recursive Descent Parser In C Caller Parser C For The Following Grammar In Ebnf Where Program Is Th 2
4 Implement A Recursive Descent Parser In C Caller Parser C For The Following Grammar In Ebnf Where Program Is Th 2 (171.34 KiB) Viewed 65 times
4 Implement A Recursive Descent Parser In C Caller Parser C For The Following Grammar In Ebnf Where Program Is Th 3
4 Implement A Recursive Descent Parser In C Caller Parser C For The Following Grammar In Ebnf Where Program Is Th 3 (104.33 KiB) Viewed 65 times
the output is in the square box we have to code in c and get the
output like the box and the subject is EBNF in programming language
how a parse tree is designed on basis of the function
4. Implement a recursive descent-parser in C, caller parser.c, for the following grammar (in EBNF) where <program> is the starting non-terminal. You can assume that the input source program is contained in an external textfile (e.g., 'front.in'), whose path name can be read from command line or in the main program. Note that whenever a symbol is in apostrophe, such as ?', it implies it is not part of the EBNF metalanguage and is part of the syntax rule itself. <program>+<stmts> <stmts> + <stmt> [: <stmts>] <stmt> - <assign> | <if> | <for> | <while> Cassignid=<expr> <if> -if<expr>' ('{'<stmts>"}'|<stmt>) <for> + for '('<assign>; <expr> ; <assign>)'('{'<stmts>'}'|<stmt>) <while> while '('<expr>"('{'<stmts>"}'|<stmt>) <expr>+ <term {(+/-) <term>} <term> <factor> {(*) <factor> <factor> <const> | <i>('<expr>')' <i> <letter> <letter> <digit> <const> -> <digit> { <digit> } <letter>>a | b|cd|ef|gh|ij|k1| m n o p q r s t u v w x y z <digit>>0111213141516171819 Notice that the output perfectly models this tree: <prognun Example Input: a = a + b*c Output of the parser: Cassign [<program>[<stmts>[<stmt> [<assign>[<id>a]=[<expr>[ <term>(<factor> {<id>a)]]+ [<term>(<factor>{<id>b]]* factor) ) <factors <lachers (<factor>{<id>c]]]] > s! á (هادی ܕܙܘܐ {exer> (terms -sterm>
Helpful Tips: a) Start by implementing (just means copy and make sure they work) the lexical and syntax analyzers in my lecture slides (i.e., textbook, chapters 4.2 to 4.3); this implies the copying of methods including but not limited to lex(), expr (), term(), where each of the syntax analyzer's methods like expr () and term() calls lex () to generate the parse. b) Then, modifications/additions to the code that are necessary for this assignment specification include accounting for more syntax rules, such as those for non-terminals <assign>, <for>, <stmts>. This means the creation of new methods such as assign(), for_statement(), stmts () that will be similar in flavor to the ones you have in a). Note that what was initially a valid statement for the old syntax rules for arithmetic expressions is no longer valid in this new grammar (you can't have stand-alone expressions in the new grammar, such as (sum+47) /total). Also, you will need to modify the code in the lecture slides/textbook in order for the output to be a nested representation of the tree and not just the output of the lexical analyzer + the steps to enter tree. That is, the output, if compared to a valid assignment statement with that expression (say, a=(sum+47)/total) would be different as follows: → Next token is: 25 Next lexeme is Enter <expr> Enter <term> Enter <factor> Next token is: 11 Next lexeme is sum Enter expr> Enter <term> Enter <factor> [<program>[<stmts><stmt>[<assign> Next token is: 21 Next lexeme is + Exit factor> {<id>a)=[<expr>(<term>(<factor><e Exit <term> xpr>(<term>(<factor>(<id>sum]]]+[<t Next token is: 10 Next lexeme is 47 Enter <term> erm>(<factor>[<const>47]]]]]]/[<facto Enter <factor> p>[<id>total]]]]]]]] Next token is: 26 Next lexeme is) Exit cfactor> Next token is: 11 Next lexeme is total Enter <factor> Next token is: -1 Next lexeme is EOE Exit <factor> Exit (term> Exit cexpr> DO output this way. (DON'T output this way) Finally, in the last page of this pdf, I have included more examples of input programs and their printed parse trees to help you test your program.
Sample input output of correct parser.c a = a + b [<program> [<stmts> [<stmt>[<assign>(<id>a)=[<expr>[<term>(<factor>(<id>a]]]+[<term>(<fa ctor>[<id>b]]]]] a = a + b c [<program> [<stmts>[<stmt>[<assign>(<id>a)=[<expr>[<term>(<factor>(<id>a]]]+[<term>(<fa ctor>{<id>b]]*(<factor>{<id>c]]]]]] a = b c - d); e = f /g [<program>[<stmts><stmt>(<assign>(<id>a)=[<expr>[<term>[<factor>[<id>b]]*[<factor>{[< expr>[<term>(<factor> {<id>c]]]- [<term>(<factor>[<id>d]]]]]]];[<stmts>[<stmt>[<assign>(<id>e] =[<expr>[<term>[<facto r>[<id>]]/[<factor>[<id>g]]]]]] if (a + b) c = d [<program> [<stmts> [<stmt>[<if>if((<expr>[<term>(<factor>[<id>a]]]+[<term>(<factor>(<id >bull) [<stmt>[<assign>{<id>c]=[<expr>[<term>(<factor>[<id>d]] if (a) (b = c/d; e-f-g) [<program> [<stmts>[<stmt>[<if>if((<expr>[<term>(<factor>{<id>a]]]]) {[<stmts> [<stmt>[<a ssign>[<id>b]= [<expr>[<term>(<factor>[<id>c]]/[<factor>[<id>d]]]]]]; [<stmts> [<stmt>[<a ssign>(<id>e] =[<expr>[<term>(<factor>[<id>]]]-[<term>(<factor>{<id>g]]]]] if (a) (b=c} [<program>[<stmts> [<stmt>[<if>if([<expr>[<term>(<factor>[<id>a]]]]) {[<stmts> [<stmt>[<a ssign>[<id>b]=[<expr>[<term>(<factor>[<id>c]]]]]] while(a*b) c = 0 [<program>(<stmts> [<stmt>(<while> whilei (<expr><term>[<factor>[<id>a]]*[<factor>[<id>b]]]]) [<stmt>[<assign>[<id>c]=[<ex pr>[<term>(<factor>{<id>d]]]]] while(a) {c = g + e; d = f*b} [<program> (<stmts><stmt> (<while> while(<expr> <term>(<factor><id>a]]]]) {[<stmts>[<stmt>(<assign>(<id>c]=[<expr>[<term >[<factor>[<id>g]]]+[<term>(<factor>[<ide]]]]]];[<stmts><stmt> [<assign>[<id>d] =[<exp r><term>(<factor>[<id>f]]*[<factor>[<id>b]lilllllll while (b) a = f) [<program><stmts> [<stmt> (<while> while([<expr>[<term>(<factor>[<id>b]]]]) {[<stmts>[<stmt>[<assign>[<id>a]=[<expr> <term >[<factor> {<id>f]]]]]]]] for (a=b;c;a=b+d) f = g [<program>(<stmts><stmt> [<for> for([<assign>[<id>a]=[<expr><term>(<factor>[<id>b]]]]];[<expr> <term>(<factor>[<id>c] 1]];[<assign> {<id>a] = [<expr> <term>(<factor>{<id>b]]]+[<term>(<factor>{<id>d]]]]])[<st mt>[<assign>[<id>[]=[<expr>[<term>(<factor>[<id>g]]]]]]] for (f=g;a*b;c=e-d) (a = b+c; d=g} [<program>(<stmts><stmt>[<for> for ([<assign> {<id>f]=[<expr>(<term>(<factor>[<id>g]]]]];[<expr>[<term>(<factor>[<id>a] 1 * (<factor>(<id>b]]]];[<assign> {<id>c]=[<expr> [<term>(<factor>[<ide]]]- [<term>(<factor>{<id>d]]]]]) {[<stmts><stmt>[<assign> {<id>a)=[<expr><term>(<factor>[< id>b]]]+[<term>(<factor> [<id>c]]]]]];[<stmts>[<stmt>(<assign>[<id>d] =[<expr>[<term>[<f actor> {<id>g]]]]]]
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply