Ask Java Expert


Home >> Java

Assignment: Programming Language Concepts- Recursive Descent Parsing

Your assignment is to use Java to write a recursive descent parser for a simplified HTML language. The lexical syntax is specified by regular expressions:

Token Extended regular expression definition

STRING (LETTER | DIGIT)+

KEYWORD | | | | |

|

    |
|
  • |
  • In the above, LETTER is any lower or upper-case letter and DIGIT is any digit. An arbitrary number of whitespace can appear between tokens.

    You may already know that ... is the tag for bolded text in HTML, ... for italicized text,

      ...
    for an unordered list, and
  • ...
  • for a list item.

    Note in the above syntax we use a notation for non-terminals that is different from the notation we used in lectures. The reason is that symbols < and > are terminals in the HTML language; so we can no longer use the notation for non-terminals. Instead, we use names in upper-case letters for non-terminals. Therefore, in the above token syntax, KEYWORD is a non-terminal, while is a string of terminals that starts with terminal < and ends with terminal>.

    Using the same notation, the syntax of the simplified HTML language is specified by the following E-BNF grammar, where WEBPAGE is the start non-terminal:

    WEBPAGE -> { TEXT }

    TEXT -> STRING | TEXT | TEXT |

      { LISTITEM }
    LISTITEM ->
  • TEXT
  • Note that { and } are meta-symbols in E-BNF.
    An example expression in the language is as follows:
    google yahoo

    This programming project is broken down into the following series of tasks.

    1. Write a class Token that can store the value and category of any token.

    2. Develop a lexical analyzer. You should start by drawing on paper a finite state automaton that can recognize types of tokens and then convert the automaton into code. Name the 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. 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.

    3. 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 Sys-tem.out) the token that was matched in a new line with indentation reflecting the nesting structure. In particular, when there is a token nested inside a tag such as , the token's indentation should be two spaces more than the indentation of the outer tag. An example will be provided shortly.

    Hint: to have proper indentation, one approach is to make your parser methods for non-terminals WEBPAGE, TEXT, and LISTITEM take an in-dentation level as a parameter. When a parser method is invoked in¬side another parser method, the indentation level should be increased by one.

    Whenever the parser comes across a token that does not fit the gram¬mar, it should output a message of the form "Syntax error: expecting expected-token-category; saw token" and immediately exit.

    Note that for this assignment you are allowed to base your code on the example lexer and parser we discussed in class.

    In order to test your file, you will have to create a Test class with a main() method that creates one or more Parser objects using different query strings and then calls the run() method on each object. For example, the code:


    google


    yahoo


    Figure 1: Sample output.

    Parser parser =

    new Parser (" google yahoo"); parser.run();
    should have the output given in Figure 1.

    Submission format: The electronic version of your program files must be submitted through Canvas.

    Make sure that your code can be compiled and run on those Linux machines in the Westgate W204. We will grade your submission on those machines.

    Make sure your code is well tested. We provided one test for your refer¬ence, but you should design your own test cases to test your code.
    Please zip all your .java and .class files into one single file and submit the zipped file. Please ensure that each of your files has a comment that includes your name, Penn State network user id, and a description of the purpose of the file. Also say which version of the Java you are using (e.g., Java 1.7) and what operating system under which you compiled your program. Please include comments for each method, as well as any complicated logic within methods.

    Java, Programming

    • Category:- Java
    • Reference No.:- M92682889

    Have any Question?


    Related Questions in Java

    Chatbotscreate a small networked chat application that is

    Chatbots Create a small, networked chat application that is populated by bots. Introduction On an old server park, filled with applications from the early days of the internet, a few servers still run one of the earliest ...

    Assignment taskwrite a java console application that allows

    Assignment task Write a java console application that allows the user to read, validate, store, display, sort and search data such as flight departure city (String), flight number (integer), flight distance (integer), fl ...

    Assignment game prototypeoverviewfor this assessment task

    Assignment: Game Prototype Overview For this assessment task you are expected to construct a prototype level/area as a "proof of concept" for the game that you have designed in Assignment 1. The prototype should function ...

    Assignment taskwrite a java console application that allows

    Assignment task Write a java console application that allows the user to read, validate, store, display, sort and search data such as flight departure city (String), flight number (integer), flight distance (integer), fl ...

    In relation to javaa what is constructor the purpose of

    (In relation to Java) A. What is constructor? the purpose of default constructor? B. How do you get a copy of the object but not the reference of the object? C. What are static variables and instance variables? D. Compar ...

    Project descriptionwrite a java program to traverse a

    Project Description: Write a java program to traverse a directory structure (DirWalker.java) of csv files that contain csv files with customer info. A simple sample in provided in with the sample code but you MUST will r ...

    Fundamentals of operating systems and java

    Fundamentals of Operating Systems and Java Programming Purpose of the assessment (with ULO Mapping) This assignment assesses the following Unit Learning Outcomes; students should be able to demonstrate their achievements ...

    Assessment -java program using array of Assessment -JAVA Program using array of objects

    Assessment -JAVA Program using array of objects Objectives This assessment item relates to the course learning outcomes as stated in the Unit Profile. Details For this assignment, you are required to develop a Windowed G ...

    Applied software engineering assignment 1 -learning

    Applied Software Engineering Assignment 1 - Learning outcomes - 1. Understand the notion of software engineering and why it is important. 2. Analyse the risk factors associated with phases of the software development lif ...

    Retail price calculatorwrite a java program that asks the

    Retail Price Calculator Write a JAVA program that asks the user to enter an item's wholesale cost and its markup percentage. It should then display the item's retail price. For example: (If an item's wholesale cost is 5. ...

    • 4,153,160 Questions Asked
    • 13,132 Experts
    • 2,558,936 Questions Answered

    Ask Experts for help!!

    Looking for Assignment Help?

    Start excelling in your Courses, Get help with Assignment

    Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

    Ask Now Help with Problems, Get a Best Answer

    Why might a bank avoid the use of interest rate swaps even

    Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

    Describe the difference between zero coupon bonds and

    Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

    Compute the present value of an annuity of 880 per year

    Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

    Compute the present value of an 1150 payment made in ten

    Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

    Compute the present value of an annuity of 699 per year

    Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As