CO5570 Anonymous Questions and Answers |
This page lists the various questions and answers. 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.
We have taken the liberty of making some minor typographical corrections to some of the questions as originally put. Although most of the questions here will have been submitted anonymously, this page also serves to answer some questions of general interest to those on the course.
Submission reference: IN3468
Does it matter if my code test against having a terminating Semicolon ";" or it doesn't matter?. For example:
int x; and int x or x := 5 and x := 5;will both pass the test?
Your code must parse according to the grammar. So, is a semicolon optional in those positions? If so, then it's not an error, but if not then it is an error. The grammar tells you all you need to know about what is valid and invalid.
Submission reference: IN3467
Should an empty program return ok or error, i cant seem to find if its mentioned in the brief?
You should be able to tell that from the grammar. Is it possible for both statements and declarations to be empty? If so, then ok, otherwise error.
Submission reference: IN3466
Do we need to remove these lines of code when submitting so the only that actually prints is ok or error. System.out.print("The first token is: "); System.out.println(getTokenDetails());
Yes.
Submission reference: IN3465
Submission reference: IN3464 Would this be better,
code deleted
Yes, but not fully because your method expectToken method has a String return type and you are returning false, null or a String. Also, it isn't clear why you would return/expect an identifier in the general expectToken method.
The idea behind my previous answer was that instead of writing something like:
if(currentToken != null && expectToken(something))
have expectToken do the null check of currentToken and return false if it is null, otherwise go on to check whether the token matches. You have done that in the other two methods.
Submission reference: IN3463
Regarding the question ive sent in about semi colens not working, ive managed to get that problem fixed but in result now my parser always returns error on programs including assignments. Im not sure how this could have happened. Ive got programs like this now returning error which is good,
int x, y, z; print x; print y; print zBut Programs like this are returning error,
int x; x := 5; print x;
code deleted
Sorry, I can't debug from snippets.
Submission reference: IN3462
Submission reference: IN3459 I have thrown an error at the point you are referring to in the parseDeclarations method, when it doesnt find an IDENTIFIER,
code deletedBut this program is still returning ok.
int x; x := 5; print xThrough further testing i have found my parser to do weird things. For example in this test program, it detects a missing semi colen and returns error when the missing semi colen is missing in any place other than the last line.
int x, y, z; print x; print y; print z;But in this test program, it only detects the missing semi colen when the missing semi colen is in the first line
int x; x := 5 print x;
code deleted
I am afraid I can't debug your code from just snippets. It is likely that the context of these calls is wrong in some way. You will just have to be systematic in printing out what the current token is at various points and debug it that way.
Submission reference: IN3460
Submission reference: IN3451 I have fixed my method to indicate an error if there is missing expressions etc. However my parser still doesn't recognise it and returns ok to programs like,
int a; a := 5; if then a := 10; fi; int a; a := 5; if a = 5 then a := 10; else a := 15;
code deletedprivate boolean parseIf() { if (expectKeyword(Keyword.IF)) { if (parseExpression()) { if (expectKeyword(Keyword.THEN)) { if (parseStatements()) { if (expectKeyword(Keyword.ELSE)) { if (!parseStatements()) { throw new SyntaxException("No statement after ELSE"); } } if (!expectKeyword(Keyword.FI)) { throw new SyntaxException("Missing FI"); } return true; } else { throw new SyntaxException("No statements after THEN"); } } else { throw new SyntaxException("Missing then"); } } else { throw new SyntaxException("No expression after IF"); } } return false; } private boolean parseStatement() { if (parsePrint() || parseAssignment() || parseIf()) { return true; } else { return false; } }
It looks like your parseExpression is returning true when there is no expression, or that you ar catching the SyntaxExpression somewhere.
Submission reference: IN3464
is this method correct?
code deleted
No, because if you parse a statement then there must be a semicolon after it, so you need to indicate an error at that point and not simply return false. You should only return false if there was no error but the construct being parsed wasn't present at all.
Also, you should not need to keep testing whether the current token is null or not. You can do that in the expect method. If the token is null then expect can return false because the expected token is not present.
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
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
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?
Yes. So your parser is not parsing a conditional statement correctly.
Keywords: assign4
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
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
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
What does the assignment brief tell you to do?
Keywords: assign4
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 xHere is the method in which semi colen are mentioned
code deleted
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
Submission reference: IN3452
Does the fi need to be followed with a semi colen?
The grammar will always tell you the answer to that, and it is the grammar you should use to code that check. A conditional is a statement and in the statements rule, every statement is followed by a semicolon. So you should not check for a semicolon in parseConditional but in parseStatements.
Keywords: assign
Submission reference: IN3451
Hello, im currently implementing handling conditional statements for my parser and this is what I've made
code deletedWhen 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
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
Submission reference: IN3450
I am having an issue with tokens. Whenever I am trying to identify a term with a typo such as:
int abc; abc := 123ddd;My code assumes I assign 123 to abc and thinks of ddd as another term. How do I stop this. It would be great if you explain how the tokenizer works. I read the code but I am confused about the way it processes tokens. For example: Is "123abc()IF;" one single token and I access different parts via getIntVal, getIdentifier, getKeyword, getSymbol or it divides them into separate tokens such as: "123", "abc", "()", "IF", ";" ?
You don't actually need to understand how the Tokenizer works to be able to use it. It recognises DSL tokens, which are defined in the brief. So the parser just needs to check that the tokens it returns match the grammar at each point.
Since there is a syntax error in the expresson on the right-hand side of the assignment, your parser should be failing at the point it finds an IDENTIFIER (ddd) where there shouldn't be one.
You could determine whether that example is a single token (it isn't) by repeatedly calling nextToken and printing out the details of the current token.
Submission reference: IN3449
is this a correct parser? :
code deletedit prints ok to the example dsl so im guessing its all correct
I am afraid that is a naive assumption. The example file is just one specific example. It's not a complete test suite.
Submission reference: IN3448
why do i keep getting Cannot invoke "Token.ordinal()" because "this.currentToken" is null. when i use
int z; int y; print (z + y);as the input. i couldnt find anything about ordinals in all the classes.
code deleted
If currentToken is null then there is no object so there isn't anything you can do with the variable in that situation.
Submission reference: IN3447
if my parser prints ok to your example dsl code does it mean its fully working?
No. That's just one example input.
Submission reference: IN3446
Do we need to comment our code?
No, you will be assessed on its functionality, but it's a shame that you consider commenting to be optional. It's something you should aim to do as a matter of course.
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License. |