Question
Can someone show me where the grammar in bold has to be added? scene SCENE IDENTIFIER number_list images END '.' images image images | image
Can someone show me where the grammar in bold has to be added?
scene SCENE IDENTIFIER number_list images END '.' images image images | image image right_triangle | rectangle right_triangle RIGHT_TRIANGLE COLOR number_list AT number_list HEIGHT NUMBER WIDTH NUMBER ';' rectangle RECTANGLE_ COLOR number_list AT number_list HEIGHT NUMBER WIDTH NUMBER ';' number_list '(' numbers ')' numbers NUMBER | NUMBER ',' numbers In the above grammar, terminal symbols are upper case names or character literals shown in blue and nonterminal symbols are lower case names shown in red. EBNF metacharacters are shown in black. Tokens can be separated by any number of spaces. Identifiers and keywords are strings of alphabetic characters. Both are case sensitive. Numbers are unsigned integers. That program reads in the scene definition file that defines the image objects in a scene and creates those objects, inserts them into the scene and displays that scene. You are to modify the program so that it will parse and display the additional images defined by the expanded grammar shown below with the additions to the grammar highlighted in yellow: scene SCENE IDENTIFIER number_list images END '.' images image images | image image right_triangle | rectangle | parallelogram | regular_polygon | isosceles | text
And check if the other modified language is correct ?
bellow you have the parser function: package project1;
import java.awt.*; import java.io.*; import java.util.*; import javax.swing.*;
class Parser { private JFrame window; private Token token; private Lexer lexer;
// Constructor to create new lexical analyzer from input file
public Parser(File file) throws IOException { lexer = new Lexer(file); }
// Parses the production // scene -> SCENE IDENTIFIER number_list images END '.'
public Scene parseScene() throws LexicalError, SyntaxError, IOException { verifyNextToken(Token.SCENE); verifyNextToken(Token.IDENTIFIER); String window = lexer.getLexeme(); int[] dimensions = getNumberList(2); Scene scene = new Scene(window, dimensions[0], dimensions[1]); parseImages(scene, lexer.getNextToken()); verifyNextToken(Token.PERIOD); return scene; }
// Parses the following productions
// images -> image images | image // image -> right_triangle | rectangle // right_triangle -> RIGHT_TRIANGLE COLOR number_list AT number_list HEIGHT NUMBER WIDTH NUMBER ';' // rectangle -> RECTANGLE_ COLOR number_list AT number_list HEIGHT NUMBER WIDTH NUMBER ';'
private void parseImages(Scene scene, Token imageToken) throws LexicalError, SyntaxError, IOException { int sides, height, width, offset, radius; verifyNextToken(Token.COLOR); String strTextDisplay; int[] colors = getNumberList(3); Color color = new Color(colors[0], colors[1], colors[2]); verifyNextToken(Token.AT); int[] location = getNumberList(2); Point point = new Point(location[0], location[1]); if (imageToken == Token.RIGHT_TRIANGLE) { verifyNextToken(Token.HEIGHT); verifyNextToken(Token.NUMBER); height = lexer.getNumber(); verifyNextToken(Token.WIDTH); verifyNextToken(Token.NUMBER); width = lexer.getNumber(); RightTriangle triangle = new RightTriangle(color, point, height, width); scene.addImage(triangle); } else if (imageToken == Token.RECTANGLE) { verifyNextToken(Token.HEIGHT); verifyNextToken(Token.NUMBER); height = lexer.getNumber(); verifyNextToken(Token.WIDTH); verifyNextToken(Token.NUMBER); width = lexer.getNumber(); Rectangle rectangle = new Rectangle(color, point, height, width); scene.addImage(rectangle); } //********************************************************************************* //modified grammar // right_triangle | rectangle | parallelogram | regular_polygon | isosceles | text // right_triangle -> RIGHT_TRIANGLE COLOR number_list AT number_list HEIGHT NUMBER WIDTH NUMBER ';' // rectangle -> RECTANGLE_ COLOR number_list AT number_list HEIGHT NUMBER WIDTH NUMBER ';' // parallelogram ? PARALLELOGRAM COLOR number_list AT number_list number_list OFFSET NUMBER ';' // regular_polygon ?REGULAR_POLYGON COLOR number_list AT number_list SIDES NUMBER RADIUS NUMBER ';' // isosceles ? ISOSCELES COLOR number_list AT number_list HEIGHT NUMBER WIDTH NUMBER ';' // text ? TEXT COLOR number_list AT number_list STRING ';' //******************************************************************************** //parallelogram else if (imageToken == Token.PARALLELOGRAM) { verifyNextToken(Token.OFFSET); verifyNextToken(Token.NUMBER); offset = lexer.getNumber(); Parallelogram parallelogram = new Parallelogram (color, point, point, offset); scene.addImage(parallelogram); } // regular_polygon else if (imageToken == Token.REGULAR_POLYGON) { verifyNextToken(Token.SIDES); verifyNextToken(Token.NUMBER); sides = lexer.getNumber(); verifyNextToken(Token.RADIUS); verifyNextToken(Token.NUMBER); radius = lexer.getNumber(); Regular_polygon regular_polygon = new Regular_polygon (color, point, sides, radius); scene.addImage(regular_polygon); } //isosceles else if (imageToken == Token.ISOSCELES) { verifyNextToken(Token.HEIGHT); verifyNextToken(Token.NUMBER); height = lexer.getNumber(); verifyNextToken(Token.WIDTH); verifyNextToken(Token.NUMBER); width = lexer.getNumber(); Isosceles isosceles = new Isosceles (color, point, height, width); scene.addImage(isosceles); } //text else if (imageToken ==Token.TEXT){ verifyNextToken(Token.STRING); strTextDisplay = lexer.getLexeme(); Text text = new Text (color, point, strTextDisplay); scene.addImage(text); } else { throw new SyntaxError(lexer.getLineNo(), "Unexpected image name " + imageToken); } verifyNextToken(Token.SEMICOLON); token = lexer.getNextToken(); if (token != Token.END) parseImages(scene, token); }
// Parses the following productions
// number_list -> '(' numbers ')' // numbers -> NUMBER | NUMBER ',' numbers
// Returns an array of the numbers in the number list
private int[] getNumberList(int count) throws LexicalError, SyntaxError, IOException { int[] list = new int[count]; verifyNextToken(Token.LEFT_PAREN); for (int i = 0; i < count; i++) { verifyNextToken(Token.NUMBER); list[i] = lexer.getNumber(); token = lexer.getNextToken(); if (i < count - 1) verifyCurrentToken(Token.COMMA); else verifyCurrentToken(Token.RIGHT_PAREN); } return list; }
// Returns a list of numbers
private int[] getNumberList() throws LexicalError, SyntaxError, IOException { ArrayList
// Verifies that the next token is the expected token
private void verifyNextToken(Token expectedToken) throws LexicalError, SyntaxError, IOException { token = lexer.getNextToken(); verifyCurrentToken(expectedToken); }
// Verifies that the current token is the expected token
private void verifyCurrentToken(Token expectedToken) throws SyntaxError { if (token != expectedToken) throw new SyntaxError(lexer.getLineNo(), "Expecting token " + expectedToken + " not " + token); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started