Question: Requirements The first programming project involves writing a program that parses, using recursive descent, a GUI definition language defined in an input file and generates

Requirements

The first programming project involves writing a program that parses, using recursive descent, a GUI definition language defined in an input file and generates the GUI that it defines, using a supplied lexical analyzer. The grammar for this language is defined below:

gui ::=

Window STRING '(' NUMBER ',' NUMBER ')' layout widgets End '.'

layout ::=

Layout layout_type ':'

layout_type ::=

Flow |

Grid '(' NUMBER ',' NUMBER [',' NUMBER ',' NUMBER] ')'

widgets ::=

widget widgets |

widget

widget ::=

Button STRING ';' |

Group radio_buttons End ';' |

Label STRING ';' |

Panel layout widgets End ';' |

Textfield NUMBER ';'

radio_buttons ::=

radio_button radio_buttons |

radio_button

radio_button ::=

Radio STRING ';'

In the above grammar, the red symbols are nonterminals, the blue symbols are tokens and the black punctuation symbols are BNF metasymbols. Among the tokens those in title case are keywords. The character literals are punctuation tokens.

Below is an explanation of the meaning of some of the symbols in the above productions that should help you understand the actions that are to be performed when each of the productions is parsed:

In the window production the string is name that is to appear in the top border of the window and the two numbers are the width and height of the window

In the production for layout_type that define the grid layout, the first two numbers represent the number of rows and columns, and the optional next two the horizontal and vertical gaps

In the production for widget that defines a button, the string is the name of the button

In the production for widget that defines a label, the string is text that is to be placed in the label

In the production for widget that defines a text field, the number is the width of the text field

In the production for radio_button, the string is the label of the button

Your parser should properly handle the fact that panels can be nested in other panels. Recursive productions must be implemented using recursion. Syntactically incorrect input files should detect and report the first error.

Below is an example of an input file:

Window "Calculator" (200, 200) Layout Flow:

Textfield 20;

Panel Layout Grid(4, 3, 5, 5):

Button "7";

Button "8";

Button "9";

Button "4";

Button "5";

Button "6";

Button "1";

Button "2";

Button "3";

Label "";

Button "0";

End;

End.

The above input file should produce the GUI shown below:

Source Code Skeleton for Project 1

The attached .zip file contains two classes, one that defines a lexical analyzer and a second that defines a syntax error. In addition, a third file that contains an enumerated type for the tokens is included. You should use this code when developing the first project.

Sample Input Files

Please post an example of valid input to this program in the "Ask the Professor" discussion area. I will check whether it conforms to the syntax defined by the grammar, and if it it does, show you what the corresponding output should look like.

**********************************************

Lexer.java

// CMSC 330 // Project 1 // Duane J. Jarc // March 25, 2014

// Netbeans under Windows 8

import java.io.*;

// Tnis class provides the lexical analyzer for project 1

class Lexer { private static final int KEYWORDS = 11; private StreamTokenizer tokenizer; private String punctuation = ",:;.()"; private Token[] punctuationTokens = { Token.COMMA, Token.COLON, Token.SEMICOLON, Token.PERIOD, Token.LEFT_PAREN, Token.RIGHT_PAREN };

// Constructor that creates a lexical analyzer object given the source file

public Lexer(String fileName) throws FileNotFoundException { tokenizer = new StreamTokenizer(new FileReader(fileName)); tokenizer.ordinaryChar('.'); tokenizer.quoteChar('"'); }

// Returns the next token in the input stream public Token getNextToken() throws SyntaxError, IOException { int token = tokenizer.nextToken(); switch (token) { case StreamTokenizer.TT_NUMBER: return Token.NUMBER; case StreamTokenizer.TT_WORD: for (Token aToken : Token.values()) { if (aToken.ordinal() == KEYWORDS) break; if (aToken.name().equals(tokenizer.sval.toUpperCase())) return aToken; } throw new SyntaxError(lineNo(), "Invalid token " + getLexeme()); case StreamTokenizer.TT_EOF: return Token.EOF; case '"': return Token.STRING; default: for (int i = 0; i < punctuation.length(); i++) if (token == punctuation.charAt(i)) return punctuationTokens[i]; } return Token.EOF; }

// Returns the lexeme associated with the current token public String getLexeme() { return tokenizer.sval; }

// Returns the numeric value of the current token for numeric tokens public double getValue() { return tokenizer.nval; }

// Returns the currebt line of the input file public int lineNo() { return tokenizer.lineno(); } }

*****************************************************************

Syntax.java

// CMSC 330 // Project 1 // Duane J. Jarc // March 25, 2014 // Netbeans under Windows 8

// Class that defines a syntax error

class SyntaxError extends Exception { // Constructor that creates a synatx error object given the line number and error

public SyntaxError(int line, String description) { super("Line: " + line + " " + description); } }

****************************************************************************

Token.java

// CMSC 330 // Project 1 // Duane J. Jarc // March 25, 2014

// Netbeans under Windows 8

// Enumerated type that defines the list of tokens

enum Token {BUTTON, END, FLOW, GRID, GROUP, LABEL, LAYOUT, PANEL, RADIO, TEXTFIELD, WINDOW, COMMA, COLON, SEMICOLON, PERIOD, LEFT_PAREN, RIGHT_PAREN, STRING, NUMBER, EOF};

*************************************

I need help with the above program using Java (Recusive descent parsing. All the other classes are written except the parser class that can take an input file and create GUI

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!