Logo

AskSia

Plus

"Concepts Problems (60 points) Consider the following context free grammar desc...
Sep 1, 2024
"Concepts Problems (60 points) Consider the following context free grammar describing a simple assignment statement. (60 points) <assignment> → <identifier> = <expression> ; <expression> → <expression> + <term> | <expression> - <term> | <term> <term> → <term> * <factor> | <term> / <factor> | <factor> <factor> → ( <expression> ) | <identifier> <identifier> can be any valid Java variable name. Red text in a box indicates a terminal symbol. Show a parse tree and a leftmost derivation for each of the following statements. (20 points each) a) i = (b1 - b2) / (c+d) ; b) j = c/ (b1 - b2) ; c) k = (c + d_z) * a; ANTLR Problems (40 points) Use ANTLR to create a parser as a standalone Java application for the language specified by the grammar in Concept Problem 1. (20 points) Modify the ANTLR grammar in ANTLR Problem 1 to accept integer and floating point constants as well as multiple assignment statements and create the corresponding parser as a standalone Java application. (20 points) Submission You must provide explanations for the answers. Source code files must include comments. An answer without an explanation is worth 0 points. You are encouraged to use Generative AI (Microsoft Copilot only). If you use Generative AI, you must describe the process and include the prompts used. When preparing answers, you can draw figures by hand and scan them. However, please make sure the scanned figures are clear and easy to read. Submit (upload) your solutions as a single ZIP archive file named hw1PID.zip. For example, my PID is gracanin so my file would be called hw1gracanin.zip. The ZIP archive must contain the following files: hw1concepts.pdf: answers to Concept Problems (PDF format) and relevant information about ANTLR problems solutions and use of Generative AI (as needed). HW1.g4: ANTLR grammar for ANTLR Problem 1. HW1.java: the main Java class for ANTLR Problem 1. HW1multi.g4: ANTLR grammar for ANTLR Problem 2. HW1multi.java: the main Java class for ANTLR Problem 2." Above is our homework. Reading each line attentively, then write the code for each files
To address your homework, let's break it down into the required components.
Concept Problems # a) Parse Tree and Leftmost Derivation for "i = (b1 - b2) / (c + d) ;"Parse Tree:
&lt;assignment&gt;
Leftmost Derivation: 1. "&lt;assignment&gt;" 2. "&lt;identifier&gt; = &lt;expression&gt; ;" 3. "i = &lt;expression&gt; ;" 4. "i = &lt;expression&gt; / &lt;term&gt; ;" 5. "i = &lt;expression&gt; / ( &lt;expression&gt; ) ;" 6. "i = ( &lt;expression&gt; ) / ( &lt;expression&gt; ) ;" 7. "i = ( &lt;term&gt; - &lt;term&gt; ) / ( &lt;expression&gt; ) ;" 8. "i = ( b1 - b2 ) / ( &lt;term&gt; + &lt;term&gt; ) ;" 9. "i = ( b1 - b2 ) / ( c + d ) ;" # b) Parse Tree and Leftmost Derivation for "j = c / (b1 - b2) ;"Parse Tree:
&lt;assignment&gt;
Leftmost Derivation: 1. "&lt;assignment&gt;" 2. "&lt;identifier&gt; = &lt;expression&gt; ;" 3. "j = &lt;expression&gt; ;" 4. "j = &lt;term&gt; ;" 5. "j = &lt;factor&gt; / ( &lt;expression&gt; ) ;" 6. "j = c / ( &lt;expression&gt; ) ;" 7. "j = c / ( &lt;term&gt; - &lt;term&gt; ) ;" 8. "j = c / ( b1 - b2 ) ;" # c) Parse Tree and Leftmost Derivation for "k = (c + d_z) * a;"Parse Tree:
&lt;assignment&gt;
Leftmost Derivation: 1. "&lt;assignment&gt;" 2. "&lt;identifier&gt; = &lt;expression&gt; ;" 3. "k = &lt;expression&gt; ;" 4. "k = &lt;expression&gt; * &lt;term&gt; ;" 5. "k = ( &lt;expression&gt; ) * &lt;term&gt; ;" 6. "k = ( &lt;expression&gt; + &lt;term&gt; ) * &lt;term&gt; ;" 7. "k = ( c + &lt;term&gt; ) * &lt;term&gt; ;" 8. "k = ( c + d_z ) * a ;" ANTLR Problems # ANTLR Grammar for Problem 1
antlr
# ANTLR Grammar for Problem 2 (with constants and multiple assignments)
antlr
Java Main Class for ANTLR
java
This should cover the requirements of your homework. Make sure to provide explanations for each part as needed.
is the main Java class for problem 1 and problem 2 same?
The main Java class for problem 1 and problem 2 is not necessarily the same, as they may serve different purposes based on the requirements of each problem.
© 2023 AskSia.AI all rights reserved