Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please I need the ;2. Word or PDF file providing screen shots of successfully co

ID: 3849742 • Letter: P

Question

Please I need the ;2. Word or PDF file providing screen shots of successfully compiling and executing the program. 3. Description of the process and lesson learned while completing this project (to be included in the Word or PDF document). 4. A test plan that contains test cases that include both layout types, all widgets and nested panels. For each test case, the input file should be shown together with the resulting GUI. (to be included in the Word or PDF document).

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. 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 the 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 You 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: Deliverables: Deliverables for this project include the following: 1. Source code correctly implementing all required functionality. 2. Word or PDF file providing screen shots of successfully compiling and executing the program. 3. Description of the process and lesson learned while completing this project (to be included in the Word or PDF document). 4. A test plan that contains test cases that include both layout types, all widgets and nested panels. For each test case, the input file should be shown together with the resulting GUI. (to be included in the Word or PDF document).

Explanation / Answer

import java.io.*;

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

};

public Lexer(String fileName) throws FileNotFoundException

{

tokenizer = new StreamTokenizer(new FileReader(fileName));

tokenizer.ordinaryChar('.');

tokenizer.quoteChar('"');

}

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;

}

public String getLexeme()

{

return tokenizer.sval;

}

public double getValue()

{

return tokenizer.nval;

}

public int lineNo()

{

return tokenizer.lineno();

}

}

enum Token {BUTTON, END, FLOW, GRID, GROUP, LABEL, LAYOUT, PANEL, RADIO, TEXTFIELD, WINDOW,

COMMA, COLON, SEMICOLON, PERIOD, LEFT_PAREN, RIGHT_PAREN,

STRING, NUMBER, EOF};

class SyntaxError extends Exception

{

public SyntaxError(int line, String description)

{

super("Line: " + line + " " + description);

}

}

import javax.swing.*;

import java.awt.FlowLayout;

import java.awt.GridLayout;

import java.io.IOException;

public class Parser {

    

    Token token;

    Token currentLevel;

    Lexer lexer;

    String textGUI;

    JFrame windowFrame;

    JPanel panel;

    ButtonGroup radioGroup;

    JFileChooser fileChooser;

    JRadioButton radioButton;

        public Parser(){

        try {

            fileChooser = new JFileChooser(".");

            if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION){

                 lexer = new Lexer(fileChooser.getSelectedFile().toString());

            }

            token = lexer.getNextToken();

            this.parseGUI();

        } catch (SyntaxError | IOException e) {

            e.printStackTrace();

        }

        

    }

        public static void main(String[] args){

        new Parser();

    }

        

    public boolean parseGUI() throws SyntaxError, IOException{

        int width;

        int height;

        if(token == Token.WINDOW){

            currentLevel = Token.WINDOW;

            windowFrame = new JFrame();

            token = lexer.getNextToken();

            if(token == Token.STRING){

                windowFrame.setTitle(lexer.getLexeme());

                token = lexer.getNextToken();

                if(token == Token.LEFT_PAREN){

                    token = lexer.getNextToken();

                    if(token == Token.NUMBER){

                        width = (int) lexer.getValue();

                        token = lexer.getNextToken();

                        if(token == Token.COMMA){

                            token = lexer.getNextToken();

                            if(token == Token.NUMBER){

                                height = (int) lexer.getValue();

                                token = lexer.getNextToken();

                                if(token == Token.RIGHT_PAREN){

                                    windowFrame.setSize(width, height);

                                    token = lexer.getNextToken();

                                    if(this.getLayout()){

                                        if(this.getWidgets()){

                                            if(token == Token.END){

                                                token = lexer.getNextToken();

                                                if(token == Token.PERIOD){

                                                    windowFrame.setVisible(true);

                                                    return true;

                                                }

                                            }

                                        }

                                    }

                                }

                            }

                        }

                    }

                }

            }

        }

        return false;

    }

    

    private boolean getLayout() throws SyntaxError, IOException {

        if(token == Token.LAYOUT){

            token = lexer.getNextToken();

            if(this.getLayoutType()){

                if(token == Token.COLON){

                    token = lexer.getNextToken();

                    return true;

                }

            }

        }

        return false;

    }

    private boolean getLayoutType() throws SyntaxError, IOException {

        int rows;

        int columns;

        int columnGap;

      int rowGap;

        if(token == Token.FLOW){

            if(currentLevel == Token.WINDOW){

                windowFrame.setLayout(new FlowLayout());

            }else{

                panel.setLayout(new FlowLayout());

            }

            token = lexer.getNextToken();

            return true;

        }else if(token == Token.GRID){

            token = lexer.getNextToken();

            if(token == Token.LEFT_PAREN){

                token = lexer.getNextToken();

                if(token == Token.NUMBER){

                    rows = (int) lexer.getValue();

                    token = lexer.getNextToken();

                    if(token == Token.COMMA){

                        token = lexer.getNextToken();

                        if(token == Token.NUMBER){

                            columns = (int) lexer.getValue();;

                            token = lexer.getNextToken();

                            if(token == Token.RIGHT_PAREN){

                                if(currentLevel == Token.WINDOW){

                                    windowFrame.setLayout(new GridLayout(rows, columns));

                                }else{

                                    panel.setLayout(new GridLayout(rows, columns));

                                }

                                token = lexer.getNextToken();

                                return true;

                            }else if(token == Token.COMMA){

                                token = lexer.getNextToken();

                                if(token == Token.NUMBER){

                                    columnGap = (int) lexer.getValue();

                                    token = lexer.getNextToken();

                                    if(token == Token.COMMA){

                                        token = lexer.getNextToken();

                                        if(token == Token.NUMBER){

                                            rowGap = (int) lexer.getValue();

                                            token = lexer.getNextToken();

                                            if(token == Token.RIGHT_PAREN){

                                                if(currentLevel == Token.WINDOW){

                                                    windowFrame.setLayout(new GridLayout(rows, columns, columnGap, rowGap));

                                                }else{

                                                    panel.setLayout(new GridLayout(rows, columns, columnGap, rowGap));

                                                }

                                                token = lexer.getNextToken();

                                                return true;

                                            }

                                        }

                                    }

                                }

                            }

                        }

                    }

                }

            }

        }

        return false;

    }

    private boolean getWidgets() throws SyntaxError, IOException {

        if(this.getWidget()){

            if(this.getWidgets()){

                return true;

            }

            return true;

        }

        return false;

    }

    private boolean getWidget() throws SyntaxError, IOException {

        int length;

        if(token == Token.BUTTON){

            token = lexer.getNextToken();

            if(token == Token.STRING){

                textGUI = lexer.getLexeme();

                token = lexer.getNextToken();

                if(token == Token.SEMICOLON){

                    if(currentLevel == Token.WINDOW){

                        windowFrame.add(new JButton(textGUI));

                  }else{

                        panel.add(new JButton(textGUI));

                    }

                    token = lexer.getNextToken();

                    return true;

                }

            }

        }else if(token == Token.GROUP){

            radioGroup = new ButtonGroup();

            token = lexer.getNextToken();

            if(getRadioButtons()){

                if(token == Token.END){

                    token = lexer.getNextToken();

                    if(token == Token.SEMICOLON){

                        token = lexer.getNextToken();

                        return true;

                    }

                }

            }

        }else if(token == Token.LABEL){

            token = lexer.getNextToken();

            if(token == Token.STRING){

                textGUI = lexer.getLexeme();

                token = lexer.getNextToken();

                if(token == Token.SEMICOLON){

                    if(currentLevel == Token.WINDOW){

                        windowFrame.add(new JLabel(textGUI));

                    }else{

                        panel.add(new JLabel(textGUI));

                    }

                    token = lexer.getNextToken();

                    return true;

                }

            }

        }else if(token == Token.PANEL){

            if(currentLevel == Token.WINDOW){

                windowFrame.add(panel = new JPanel());

            }else{

                panel.add(panel = new JPanel());

            }

            currentLevel = Token.PANEL;

            token = lexer.getNextToken();

            if(getLayout()){

                if(getWidgets()){

                    if(token == Token.END){

                        token = lexer.getNextToken();

                        if(token == Token.SEMICOLON){

                            token = lexer.getNextToken();

                            return true;

                        }

                    }

                }

            }

        }else if(token == Token.TEXTFIELD){

            token = lexer.getNextToken();

            if(token == Token.NUMBER){

                length = (int) lexer.getValue();

                token = lexer.getNextToken();

                if(token == Token.SEMICOLON){

                    if(currentLevel == Token.WINDOW){

                        windowFrame.add(new JTextField(length));

                    }else{

                        panel.add(new JTextField(length));

                    }

                    token = lexer.getNextToken();

                    return true;

                }

            }

        }

        return false;

    }

    private boolean getRadioButtons() throws SyntaxError, IOException {

        if(getRadioButton()){

            if(getRadioButtons()){

                return true;

            }

            return true;

        }

        return false;

    }

    private boolean getRadioButton() throws SyntaxError, IOException {

        if(token == Token.RADIO){

            token = lexer.getNextToken();

            if(token == Token.STRING){

                textGUI = lexer.getLexeme();

                token = lexer.getNextToken();

                if(token == Token.SEMICOLON){

                    radioButton = new JRadioButton(textGUI);

                   radioGroup.add(radioButton);

                    if(currentLevel == Token.WINDOW){

                        windowFrame.add(radioButton);

                    }else{

                        panel.add(radioButton);

                    }

                    token = lexer.getNextToken();

                    return true;

                }

            }

        }

        return false;

    }

}