Design an abstract syntax and implement a recursive descent parser for expressio
ID: 672716 • Letter: D
Question
Design an abstract syntax and implement a recursive descent parser for expressions to be symbolically differentiated.
The grammar is a Polish-prefix grammar:
Expr --> Op Expr Expr I Primary
Op --> + | - | * I /
Primary -> Integer I Letter
where an Integer is an arbitrary sequence of digits and a Letter is a single lowercase letter representing a mathematical variable. To simplify the assignment, you may assume that the Expr is V' error-free and that the Expr appears on one line with tokens separated by one or more spaces or tabs. The program should display the abstract syntax tree.
How to write program for the following ?
Explanation / Answer
import java.util.*; class TypeMap extends Hashtable { // TypeMap is implemented as a Java Hashtable. // It has a 'display' method added to facilitate experimentation. public void display () { System.out.print("{ "); for (Enumeration e = this.keys(); e.hasMoreElements(); ) { Variable key = (Variable)e.nextElement(); Type t = (Type)this.get(key); System.out.print(""); if (e.hasMoreElements()) System.out.print(", "); } System.out.println(" }"); } } class StaticTypeCheck { public TypeMap typing (Declarations d) { // put the variables and their types into a new // Dictionary (symbol table) and return it. TypeMap map = new TypeMap(); for (int i=0; iRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.