Lexer and Parser
Budget: $10 – $30 USD
Shank.java must contain main. Your main must ensure that there is one and only one argument (args). If there are none or more than 1, it must print an appropriate error message and exit. That one argument will be considered as a filename. Your main must then use File.ReadAllLines to read all of the lines from the file denoted by filename. Your main must instantiate one instance of your Lexer class (to be defined below). You must parse all lines using the lex method of the Lexer class. If lex throws an exception, you must catch the exception, print that there was an exception. You must then print each token out (this is a temporary step to show that it works) once the lexing is complete.
One file must be called Token.java. This file must contain a Token class. The token class is made up of an instance of an enum and a value string. There must be a public accessor for both the enum and the value string; the underlying variables must be private. You may create whatever constructors you choose. The enum must be defined as containing values appropriate to what we will be processing. The definition of the enum should be public, but the instance inside Token must be private. You will find it helpful to create an appropriate “ToString” overload.
The final file must be called Lexer.java. The Lexer class must contain a lex method that accepts a single string and returns a collection (array or list) of Tokens. The lex method must use one or more state machine(s) to iterate over the input string and create appropriate Tokens. Any character not allowed by your state machine(s) should throw an exception. The lexer needs to accumulate characters for some types (consider 123 – we need to accumulate 1, then 2, then 3, then the state machine can tell that the number is complete because the next character is not a number).
Input Output
an empty line EndOfLine
5 NUMBER (5) EndOfLine
5.23 – 8.5 + 3 NUMBER(5.23) MINUS NUMBER(8.5) PLUS NUMBER(3) EndOfLine
8 * -4 + 99999 NUMBER (8) TIMES NUMBER (-4) PLUS NUMBER (99999) EndOfLine
7 4 3 1 Invalid – throws exception
+ - * / Invalid – throws exception
(Parser.java, Node.java, IntegerNode.java, FloatNode.java, MathOpNode.java, Interpreter.java)Shank.java, Token.java, Lexer.java).
and the parser coding description is in the attached file.
One file must be called Token.java. This file must contain a Token class. The token class is made up of an instance of an enum and a value string. There must be a public accessor for both the enum and the value string; the underlying variables must be private. You may create whatever constructors you choose. The enum must be defined as containing values appropriate to what we will be processing. The definition of the enum should be public, but the instance inside Token must be private. You will find it helpful to create an appropriate “ToString” overload.
The final file must be called Lexer.java. The Lexer class must contain a lex method that accepts a single string and returns a collection (array or list) of Tokens. The lex method must use one or more state machine(s) to iterate over the input string and create appropriate Tokens. Any character not allowed by your state machine(s) should throw an exception. The lexer needs to accumulate characters for some types (consider 123 – we need to accumulate 1, then 2, then 3, then the state machine can tell that the number is complete because the next character is not a number).
Input Output
an empty line EndOfLine
5 NUMBER (5) EndOfLine
5.23 – 8.5 + 3 NUMBER(5.23) MINUS NUMBER(8.5) PLUS NUMBER(3) EndOfLine
8 * -4 + 99999 NUMBER (8) TIMES NUMBER (-4) PLUS NUMBER (99999) EndOfLine
7 4 3 1 Invalid – throws exception
+ - * / Invalid – throws exception
(Parser.java, Node.java, IntegerNode.java, FloatNode.java, MathOpNode.java, Interpreter.java)Shank.java, Token.java, Lexer.java).
and the parser coding description is in the attached file.
Related categories:
C Programming
Business, Accounting, Human Resources & Legal
JavaScript
C++ Programming