Create a function named declaration to handle the production


Assignment

The following pertains to Topic II Predictive Parser assignment.

First, read the attached Word document and then, once you're finished reading it, continue with the explanation below.

There are a variety of ways to complete the assignment but I will provide you with what I believe you need to do within PredictiveParser.java. If you don't complete it exactly the way I have specified below, that is fine, but, based on the way the code already exists, this is most certainly how you will need to complete it.

Add "TRUE", "FALSE", and "NOT" (for !epxr) cases to expr().

Add "TRUE", "FALSE", and "NOT" cases to term().

Add "BOOLEAN" and "INTEGER" cases to program().

Add "DECLARATION," "BOOLEAN" and "INTEGER" cases to stmt().

Add "DECLARATION," "INTEGER," "BOOLEAN," and "SEMICOLON" cases to stmtList().

Add a call to a function idTail() in the ID case within stmt() in PredictiveParser.java. This is necessary to match the updated grammar specified in the directions.

This means you will need to create a new function named idTail() that parses the production: idTail -> := expr | (id);

Therefore, as per the production, it should contain the following cases: ASSIGN, LPAREN, and default.

Create a function named declaration() to handle/parse the production: declaration -> int id | bool id

Therefore, as per the production, it should contain the following cases: INTEGER, BOOLEAN, and default.

Finally, make sure to handle the new lexemes you implemented above to Scanner.java.

Topic 2 Predictive Parser explanation

First, before jumping into the code, make sure you understand how the grammar works. For example, let's understand the stmt line in the grammar provided within the directions:

stmt→ [id idTail; | read id ; | write expr ; | declaraton;

What that's saying is that a statement within the grammar can consist of:

• an id followed by an idTail followed by a semicolon

o an idTail consists of

- a := followed by an expression followed

or

- a ( followed by an id followed by a ) followed by a semicolon

or

• read followed by an id followed by a semicolon

or

• write followed by an expr (an expression) followed by an semicolon

or

• a declaration

o a declaration consists of

- int followed by id

or

- bool followed by an id

The parser needs to be able to handle all of these conditions. Let's look at examples of these.

sum := A + B; // this is an id followed by idTail because the right side is an expression (A+B)

read A; // this is a read followed by an id (A)

write sum / 2; // this is a write followed by an expression (sum / 2)

bool A; // this is a declaration example because we're declaring A as a boolean

int B; // this is another declaration because we're declaring a variable B as integer

Now, let's look at an example of what your parser should output.

For isTest := true;your parser should display the following:

MM♣Mprogram
MM♣MstmtList
MM♣Mstmt
MM♣M ID(isTest)
MM♣MidTail
MM♣M :=
MM♣M expr
MM♣M term
MM♣M factor
MM♣M TRUE
MM♣MfactorTail
MM♣M e
MM♣MtermTail
MM♣M e
MM♣M ;

For sum := A + B;your parser should display the following:

♣Mprogram
MM♣MstmtList
MM♣Mstmt
MM♣M ID(sum)
MM♣MidTail
MM♣M :=
MM♣M expr
MM♣M term
MM♣M factor
MM♣M ID(A)
MM♣MfactorTail
MM♣M e
MM♣MtermTail
MM♣MaddOp
MM♣M +
MM♣M term
MM♣M factor
MM♣M ID(B)
MM♣MfactorTail
MM♣M e
MM♣MtermTail
MM♣M e
MM♣M ;
MM♣MstmtList
MM♣M e

For bool A; your parser should display the following:

MM♣MstmtList
MM♣Mstmt
MM♣M declaration
MM♣M BOOL
MM♣M ID(A)
MM♣M ;

Topic 3 Assignment 1

Q - I can't seem to get the inline comments removed. When I reach the comment operator I've tried looping through until finding a new line to skip over it but it never finds a new line. The comments always output as separated by spaces and are assigned as an ID. Also, the pushbackreader does not have a method of recognizing lines of a text file. Am I on the right track in trying to solve the problem or am I looking at this the wrong way?

A - Check for slashes in public Token next() by adding a new case statement. Within the case, you can call nextChar() to check if the next character is also a slash /. You can then skip to the next line. Be sure to check out the member variable endPos in Scanner.java.

Q - I need some clarity on the 4th task "Modify the Scanner to handle a left (LPAREN) and right (RPAREN) parenthesis tokens. When finished it should be able to support statements like (not ‘cos' is simply an id: write (A * B) c := cos(A)" ) .

Is this task asking me to treat !cos as an ID. For example, the console output should say !cos : ID ? For some reason, the verbiage used is causing me to doubt if I understand what's being asked or not.

A - You need to ensure you have token types named LPAREN and RPAREN. Take a look at scanSingleCharToken().

Q - In the Scanner assignment what exactly does part 5 want us to do? I am also wondering if it is wrong to treat the ';' as was done for the '!' in #3. Should the ';' be included as part of the program's output, like the read statement for example?

"read" : READ

A - You need to ensure that your scanner finds a semicolon at the end of each statement. In other words, each statement should be delimited by a semicolon to distinguish between the various statements in your program. To the last part of your question - yes. So, for the following statement in the input program:

read B;
you should output:
read : READ
B : ID
; : SEMICOLON
: EOF

Q - Should each statement only be delimited by a semi colon, or should it be newlines and semicolons?
A - Delimit by only semicolons.

Topic 3 Assignment 2

Q - In running the predictive parser "read" and "write" are outputting "Illegal" to the parse tree using the given unzipped source file (prior to assignment modifications). Is this a bug in the existing code or is this something I need to rectify? I do not see anywhere this is mentioned in the instructions and the assignment modification do not change this error.

A - The file PredictiveParser.java contains case statements for the various tokens (read, write, etc.). You need to provide logic within that file to handle the various tokens.

Format your assignment according to the following formatting requirements:

1. The answer should be typed, double spaced, using Times New Roman font (size 12), with one-inch margins on all sides.

2. The response also include a cover page containing the title of the assignment, the student's name, the course title, and the date. The cover page is not included in the required page length.

3. Also Include a reference page. The Citations and references should follow APA format. The reference page is not included in the required page length.

Attachment:- Java-Code.rar

Solution Preview :

Prepared by a verified Expert
JAVA Programming: Create a function named declaration to handle the production
Reference No:- TGS02965771

Now Priced at $70 (50% Discount)

Recommended (99%)

Rated (4.3/5)