Assessed Exercise 1, Task 3 |
Introduction. This task is about parsing. You will write a parser for a fragment of the simple programming language given here. Task. Write a parser for the language specified by the CFG below. INT → ... BLOCK → { ENE } ENE → E | E; ENE E → INT | BLOCK | skip The starting symbol
is BLOCK → T_LeftCurlyBracket ENE T_RightCurlyBracket ENE → E | E T_Semicolon ENE E → T_Integer | BLOCK | T_Skip Note that you are not asked to write a parser for the full language given here, only for the fragment above. You are free to write the parser in any form you want, whether 'by hand' or with a parser generator like CUPS, Yacc etc. As the grammar fragment is so simple, I recommend writing a top-down parser 'by hand'. As explained in the lectures, a top-down parser is naturally written using recursion rather than loops. If you find yourself using loop constructs, I recommend to rethink your approach (note: it is possible to write parsers with loops, I just personally find this much more cumbersome). If you use a parser generator to handle this task, I strongly recommend submitting the generator's source files too. I am unable to comprehend and comment on auto-generated parsers without source files: the auto-generated code is too complicated/weird for humans to comprehend directly.
Your parser has to implement the interface interface Parser { public Block parse ( List < Token > input ) throws SyntaxException, Task3Exception; } class SyntaxException extends Exception { public String msg; public SyntaxException ( String _msg ) { msg = _msg; } } class Task3Exception extends Exception { public String msg; public Task3Exception ( String _msg ) { msg = _msg; } }
Here You will also have to implement a
method
class Task3 { public static Parser create () { ... } } For your convenience, here are the remaining definitions in one file. The necessary Java files for all three tasks can be found for convenient download here: Warning. Your code
needs to compile against the signatures given to you
here. Do not change them. You are welcome to add new
classes. For your convenience you can find a
file |