XML

CO5570 Anonymous Questions and Answers Keyword Index

This page provides a keyword index to questions and answers. Clicking on a keyword will take you to a page containing all questions and answers for that keyword, grouped by year.

To submit a question, use the anonymous questions page. You may find the keyword index and/or top-level index useful for locating past questions and answers.

Keyword reference for assign4

2023

Question 61 (2023):

Submission reference: IN3459

Submission reference: IN3453 I have added checks for semi colens in the parseStatements and parseDeclaration methods and removed the check in parseIdentifiers, however i still have the same issue.

code deleted

Answer 61:

You haven't appreciated the difference between indicating a syntax error and returning false from a method. Unless something is optional in a rule, once you have committed to, e.g., a declaration by finding the INT keyword, it is an error not to find identifiers, so you should not return false at that point but throw a syntax error.

Keywords: assign4


Question 60 (2023):

Submission reference: IN3457

this test:

int x;
if x = 5 then
    x := x + 1;
else
    x := x - 1;
prints ok shouldn't my parser print an error to this?

Answer 60:

Yes. So your parser is not parsing a conditional statement correctly.

Keywords: assign4


Question 59 (2023):

Submission reference: IN3456

in regards to your answer to question 58 I have included both error and then a system.out message with the error message because in the brief it includes "Note that the message must be written to standard output (System.out in Java) regardless of whether the parse is successful or not" So im confused whether to print error alone or the error message then error

Answer 59:

This is what the brief says:

The program must not output anything other than a single diagnostic message on standard output (System.out) at the end of the parse. The message must be: ok for a successful parse or error for an unsuccessful parse.

I had hoped that 'not output anything other than' would have been clear enough.

Keywords: assign4


Question 58 (2023):

Submission reference: IN3455

my errors include resigning such as: Error: Missing semicolon after statement at line: 3 error is this incorrect or should it just print error

Answer 58:

What does the assignment brief tell you to do?

Keywords: assign4


Question 57 (2023):

Submission reference: IN3453

I have noticed when testing my parser that when a test program that isn't the first line is missing a semi colen, it still passes ok. It only detects a missing semi colen and returns error if the missing semi colen is on the first line. For example, this test file returns error, which it should,

int x
x := 5;
print x;
But this test file returns ok,
int x;
x := 5
print x
Here is the method in which semi colen are mentioned
code deleted

Answer 57:

You should only be checking for semicolons in the parseStatements and parseDeclaration methods because those are the only places in the grammar where there are semicolons. You must use the grammar to drive the coding and not check for things that aren't in the rule when writing the code to parse a rule. The slides show you how to turn each grammar construct into the equivalent parsing code.

Keywords: assign4


Question 55 (2023):

Submission reference: IN3451

Hello, im currently implementing handling conditional statements for my parser and this is what I've made

code deleted
When running tests it correctly shows the program is ok when I use valid programs but when i try to use invalid programs such as missing THEN or no ending FI, it doesnt return error and instead returns ok

Answer 55:

That's because, once you have identified the keyword IF, there must be an expression after it but if there isn't, you just return false rather than indicating an error. The same is true after THEN with statements and ELSE with statements and if there is a missing FI. All of those need error indications.

Keywords: assign4


Question 49 (2023):

Submission reference: IN3445

Would this be full marks?

code deleted

Answer 49:

I can't assess your work ahead of the deadline. You should aim to test it thoroughly to increase your confidence that it works as it should.

Keywords: assign4


Question 48 (2023):

Submission reference: IN3444

i have not yet implemented conditional statements or loops in my parser yet. However when i test the dsl test file on moodle(containing ifs, and while) my parser is still returning ok? is this meant to happen even if i havent implemented certain features yet.

Answer 48:

I have no way of knowing for sure without seeing your code. However, if there are tokens in the DSL file that your parser can't recognise then I would expect it to output error.

Keywords: assign4


Question 47 (2023):

Submission reference: IN3442

is the assignment symbol meant to be ::= or := because in the grammar it shows

program ::=	declarations statements;
but then in the dsl test file it uses
sum := 0;
    num := 1;
So should there be two colons or one?

Answer 47:

See my answer to Question 32 (2023). You must read the whole of the assignment brief, even though the deadline is really close.

Keywords: assign4


Question 45 (2023):

Submission reference: IN3439

Hello, i am trying to add the feature of the ::=. This is my method for it,

code deleted
But when trying to it works using a file containing,
int x;
x := 10;
it returns error, when it should be ok.

Answer 45:

What you are parsing there is an assignment, so the method should be called parseAssignment. You haven't checked that an expression is actually parsed successfully, as it is not optional. Otherwise, the logic looks ok so some other part of the parsing process must be at fault.

Keywords: assign4


Question 44 (2023):

Submission reference: IN3438

is this somewhat a good start to implementing the parseExpression method ? and would I need to also consider IF, ELSE and WHILE statements for expressions?

    private boolean parseExpression(){
	if (parseDeclaration()){ 
	    if (expectSymbol(Symbol.ADD) || expectSymbol(Symbol.SUBTRACT)||
expectSymbol(Symbol.EQUAL_TO) ||
expectSymbol(Symbol.NOT_EQUAL_TO)||expectSymbol(Symbol.LESS_THAN)||expectSy
mbol(Symbol.MORE_THAN)||expectSymbol(Symbol.LESS_THAN_EQUAL_TO)||expectSymb
ol(Symbol.MORE_THAN_EQUAL_TO)){
		getNextToken();
		if (!parseDeclaration()){
		    throw new SyntaxException("error");
		}
	    }
	    return true;
	}
return false;
    }

Answer 44:

That's not a good start at all and I am not sure how you came up with that as an idea. For instance, there is no reason to think that parsing an expression involves parsing a declaration. Please take another look at the grammar rule for an expression, which involves terms and a binaryOp.

IF, WHILE, etc. are part of statements, so they also have no role in parsing expressions.

It looks like you need to go back to the lecture that covered how to convert a grammar to a parser and start again there.

Keywords: assign4


Question 43 (2023):

Submission reference: IN3437

is this code correct for parsing statements, terms and expressions.

code deleted

Answer 43:

No, it is quite a long way off. For instance, if there is more than one statement, parseStatements returns false. It is incorrect in most of the methods you have written. Make sure you refer to the slides on parsing and how to turn a grammar into parsing code.

Even though you are close to the deadline, you need to write the code in small steps and test it systematically rather than trying to write the whole thing in one go.

Keywords: assign4


Question 42 (2023):

Submission reference: IN3435

I've just read through the brief to double check my question, but in 'The Parser Class' section the following is said:

"If they detect an error – such as a missing expression after the token PRINT – then they should throw an exception, as illustrated in the example method. The SyntaxException class has been provided for you."

Do we specifically NEED to throw exceptions if we find an error? Will we actually lose marks for just returning False?

I notice in Main.java the code simply catches a SyntaxException and prints error anyway, so does it particularly matter if we throw an error? For example, if an attempt to declare an identifier twice was made, would it matter if I just return False (as in a deduction of marks), or would I instead need to throw said SyntaxException.

Answer 42:

You don't have to throw an exception. What matters is whether either ok or error is printed.

Keywords: assign4


Question 41 (2023):

Submission reference: IN3434

do we upload a zip of the whole folder? so including the parser the main, tokenizer ,symbol table , keyword, SyntaxException etc or just the parser and main?

Answer 41:

Yes, all your code so that it is a complete program.

Keywords: assign4


Question 40 (2023):

Submission reference: IN3432

Is this test meant to return error. Test this with a single DSL file that contains multiple declarations, such as: int x; int x,y,z;

Answer 40:

You should be able to check that from the grammar. Is it possible to have more than one declaration? Is it ok to have no statements?

Keywords: assign4


Question 39 (2023):

Submission reference: IN3424

Im the process of creating the symbol table in my parse identifiers method. Before i created my symbol table, my method looked like this, and it worked for single declaration.

code deleted
But my new methods make the parser return java.lang.NullPointerException error.
code deleted

Answer 39:

That's probably because the expectToken method moves on to the next token if it matches an IDENTIFIER, so when you ask the Tokenizer for the identifier immediately afterward, it's already gone.

Keywords: assign4


Question 38 (2023):

Submission reference: IN3423

Would you give declarations in the test like this? int a := 3; or is declaring a variable completely separate from initializing it in this lang?

Answer 38:

Reading and understanding the grammar will tell you the answer to that.

What can you write in a declaration? Can you write an assignment in it? Check the grammar.

Keywords: assign4


Question 37 (2023):

Submission reference: IN3419

Submission reference: IN3418 I see that IDENTIFIER is of type Token, does that mean my method would start like this and i would need to create an expectToken method?

code deleted

Answer 37:

Yes, something like that.

Keywords: assign4


Question 36 (2023):

Submission reference: IN3418

I am trying to write the method headers for parseDeclaration and parseIdentifiers. I have followed the suggestions for parseDeclaration but for parseIdentifiers i am getting errors such as Cannot resolve symbol 'IDENTIFIER'. Here are my methods,

code deleted

Answer 36:

IDENTIFIER is not a Keyword. Find out what type it is by looking in the other source files provided.

Keywords: assign4


Question 34 (2023):

Submission reference: IN3414

Why do my assertions not do anything in the IntelliJ IDE? I made a method specifically to test if assertions work and it kept running as if it doesnt.

private void assertFalse(){assert false;}

 public boolean parseProgram()
    {
	assertFalse();
    }

Answer 34:

Assertions are off by default. You need to include -ea as a VM option in the run configuration.

Keywords: assign4


Question 33 (2023):

Submission reference: IN3413

I am getting this weird error from the tokenizer when it reads a minus sign. Every token is evaluated properly until I get to this point. When I run the .dsl program provided I get this error when I reach this assignment:

while.. if..
sum := sum + (num - 2);
Unexpected exception parsing: main.dsl java.lang.IllegalStateException: Unrecognised character: – But for this case the program will run properly:
int sum ,num;
sum := sum + (num - 2);
Why would the tokenizer be able to recognize the minus sign here and not when I run the full file?

Answer 33:

Make sure you have the most up-to-date Example DSL file from the Assessments Section of the Moodle page, or simply edit that character in the file you have to make sure that the '-' character really is a '-' character.

Keywords: assign4


Question 32 (2023):

Submission reference: IN3412

It says in the requirements paper that declaration is ::= but in the example DSL file you provided all the declarations are :=

Answer 32:

In the requirements it also says:

In the grammar be careful to distinguish between those characters not enclosed between single-quote characters (e.g., ::= and ;) and those that look similar but are enclosed (e.g., ':=' and ';').

Keywords: assign4

Referrers: Question 47 (2023)


Question 31 (2023):

Submission reference: IN3409

How can I know for sure that the right identifiers are being parsed while also being recorded in the symbol table. For example for all three of the dsl files:

int x;

int x, y, z;

int x;
int x,y,z;
The output is: "The first token is: KEYWORD INT" "error". How can I be sure that the second line of the third file is even being read?

Answer 31:

The output is just the starting debugging code I provided, so you can get rid of that now you have started.

Each token identified by the Tokenizer has additional information associated with it. Take a look at the source of the Tokenizer class to see what methods you can call to access that information.

Keywords: assign4


Question 30 (2023):

Submission reference: IN3404

if my parser prints ok correct with the first part of the example del file ie

    int num, sum; 
    int ch; 

    sum := 0;
    num := 1;
    ch := 65;

  print sum;
    print ch;

but seems to malfunction at the while loop :

    while ch < 128
    do
	if (num != 0) + (ch != 127)
	then
	    sum := sum + (num – 2);
	    ch := ch - 1;
	else
	    sum := sum - num;
	    ch := ch + 1;
	fi;
	num := num + 1; 
    od;
 
Will I still get a good amount of marks?

Answer 30:

Your parser will be tested with a range of test inputs on the different declaration and types, so if it is only failing on the loop that should not have a very big impact on your overall.

Keywords: assign4


Question 28 (2023):

Submission reference: IN3394

my parser outputs this the n testing it with example.dsl: The first token is: KEYWORD INT ok ok is this compleatly incorrect? I have restarted multiple times and have always ended up with this output

Answer 28:

If you have completed the assignment, then it should just print "ok" as in the spec. Just delete the other output that I put in the starter code.

Keywords: assign4


Question 27 (2023):

Submission reference: IN3391

when I run the assignment4 code with the example.dsl the outputs are The first token is: KEYWORD INT OK. Is it the right output?

Answer 27:

It depends what you mean. If you are parsing the whole of the example.dsl file then the output should just be ok. There should not be any other debugging info printed. If you haven't implemented anything yet then the output will be

The first token is: KEYWORD INT
error

Keywords: assign4


Question 26 (2023):

Submission reference: IN3381

assignment ::=	IDENTIFIER ':=' expression ;
what exactly does this statement mean ? IDENTIFIER ':=' expression ; this is clear and is equivalent to N1 := 6; but i'm confused with assignment ::= IDENTIFIER

Answer 26:

The rule means that an assignment (statement) is defined as consisting of an IDENTIFIER (e.g., sum, price, x, etc.) followed by ':=' (an assignment symbol, like '=' in Java) followed by an expression, where expression is defined later in the grammar.

In terms of writing a parser for it, just follow the rules outlined in the parsing lecture/slides: check that the current token is an identifier, then check that the next token is the assignment symbol, then check that what follows is an expression (by calling the parseExpression) method.

Keywords: assign4


Question 25 (2023):

Submission reference: IN3379

What is the difference between these symbols := ::=

Answer 25:

The spec say, The notation ::= means 'is defined as'. It is part of the grammar notation. The spec also says:

In the grammar be careful to distinguish between those characters not enclosed between single-quote characters (e.g., ::= and ;) and those that look similar but are enclosed (e.g., ':=' and ';').
So, in the following:
assignment ::=	IDENTIFIER ':=' expression ;
:= is a symbol found in the input.

Keywords: assign4


Question 24 (2023):

Submission reference: IN3377

I'm following the suggested implementation steps in the spec.I was just wondering whether in step 4 we are meant to create a new method or should that be implemented in parsedeclaration as I'm confused

Answer 24:

In the parseDeclaration method, when you have IDENTIFIER as a token you should then access the symbol table. Check first whether there is already an entry for it. If there is, then that is an error. If there isn't then add it to the table.

Keywords: assign4


Question 23 (2023):

Submission reference: IN3375

how would i know if a symbol is a comma or semicolon and I cant use strings?

Answer 23:

I am not sure what, 'I cant use strings' means but if you know that a token is a SYMBOL you can call the getSymbol method of the Tokenizer and check whether the symbol is ";" or ",".

Keywords: assign4


Question 22 (2023):

Submission reference: IN3371

what should the output be to the example dsl file you have provided?

Answer 22:

It's a valid program, so the output should be:

ok

Keywords: assign4


Question 21 (2023):

Submission reference: IN3361

For A4 what java files provided do we actually need to modify? is it just the parser or do we need to modify the other java files

Answer 21:

You should only need to add code to the Parser class, but feel free to modify the others if you feel you need to, or add additional classes similarly.

Keywords: assign4

This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License.
Last modified Tue Jan 23 16:52:10 2024
This document is maintained by David Barnes, to whom any comments and corrections should be addressed.