Question: Programming Language Syntax - Parse Trees package easycalc; import java.util.Scanner; import org.antlr.v4.runtime.*; import org.antlr.v4.runtime.tree.*; import easycalc.grammar.*; public class ParserApp { public static void main(String[] args)
Programming Language Syntax - Parse Trees

package easycalc;
import java.util.Scanner; import org.antlr.v4.runtime.*; import org.antlr.v4.runtime.tree.*; import easycalc.grammar.*;
public class ParserApp {
public static void main(String[] args) {
// ====================================================== // Read in multiple lines of input // ====================================================== StringBuilder sb = new StringBuilder(); Scanner scan = new Scanner(System.in); String nextLine = scan.nextLine(); while (!nextLine.contains("$$")) { sb.append(nextLine); scan = new Scanner(System.in); nextLine = scan.nextLine(); } scan.close(); sb.append(nextLine); String str = sb.toString(); // ====================================================== // Create the parse tree from the input stream // ======================================================
CharStream input = CharStreams.fromString(str); EasyCalcLexer lexer = new EasyCalcLexer(input); CommonTokenStream tokens = new CommonTokenStream(lexer); EasyCalcParser parser = new EasyCalcParser(tokens); ParseTree tree = parser.program(); // begin parsing at program rule
// ====================================================== // Print out the LISP-style tree // ====================================================== System.out.println(tree.toStringTree(parser)); } }
Create an ANTLR4 grammar for an easy calculator language based on the following specification. program --> declaration* statement* $$ declaration --> bool identifier ; | int identifier ; | real identifier ; statement --> identifier := expression ; | read identifier ; | write expression; expression --> expression operator expression | identifier | literal (expression ) | to_int ( expression ) | to_real ( expression) | if expression then expression else expression operator --> * ! | + | - | and or | | == literal --> real_number | int_number | true false Create an ANTLR4 grammar for an easy calculator language based on the following specification. program --> declaration* statement* $$ declaration --> bool identifier ; | int identifier ; | real identifier ; statement --> identifier := expression ; | read identifier ; | write expression; expression --> expression operator expression | identifier | literal (expression ) | to_int ( expression ) | to_real ( expression) | if expression then expression else expression operator --> * ! | + | - | and or | | == literal --> real_number | int_number | true false
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
