Question: a) Write a class Token that can store the value and category of any token. b) Develop a lexical analyzer. You should start by drawing

a) Write a class Token that can store the value and category of any token. b) Develop a lexical analyzer. You should start by drawing on paper a nite state automaton that can recognize types of tokens and then convert the automaton into code. Name this class Lexer and ensure that it has a public method nextToken() which returns a Token. Lexer should have a constructor with the signature Lexer(String input), where input is the string representing the query to be parsed. As mentioned before, the code for recognizing an identier should check to see if the terminal string is a keyword, and if so, then the category of the token should be set to keyword, instead of ID. If nextToken() is called when no input is left, then it should return a token with category EOI (EndOfInput). If the next terminal string is not considered a token by the lexical syntax, then return a token with category Invalid. c) Write a syntactic analyzer which parses the tokens given by Lexer using the recursive descent technique. Name this class Parser. Like Lexer, Parser should have a public constructor with the signature Parser(String input). This input string should be used to create a Lexer object. There should also be a public method run() that will start the parse. As you parse the input, Parser should output (by writing to System.out) the non-terminal that was matched, surrounded by angle-brackets, e.g. . Whenever it applies a new rule, it should indent by a tab. When it has parsed all tokens that constitute a nonterminal, it should print out the non-terminals name, preceded by a slash, and surrounded by angle-brackets, e.g. . When a terminal symbol (token) is matched, it should output the token category, surrounded by angle-brackets, the tokens string, and the token category preceded by a slash and surrounded by angle-brackets (e.g. 42). Whenever the parser comes across a token that does not t the grammar, it should output a message of the form Syntax error: expecting expected-token-category; saw token and immediately exit. Note, the input string is expected to contain exactly one sentence of the form Query. In order to test your le, you will have to create a Test class with a main() method that creates one or more Parser objects using dierent query strings and then calls the run() method on each object. For example, the code: Parser p = new Parser("SELECT C1,C2 FROM T1 WHERE C1=5.23"); p.run(); should have the output given in Figure 1. Although it is not important for completing this assignment, you may be interested to know that this output format is well-formed XML. a) Write a class Token that can store the value and categoryof any token. b) Develop a lexical analyzer. You should start by

Your assignment is to use Java to write a recursive descent parser for a restricted form of SQL, the popular database query language. The lexical syntax is specified by regular expressions: Category Definition 0-9] digit a-zA-Z letter int kid> t, kid> HAND KCond Koperator> ,

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!