Top-down recursive descent parser

Assignment Help Programming Languages
Reference no: EM131652

You will build a recursive-descent parser for the right recursive version of the classic expression grammar found in the text. Take the approach,  because this can free you from the need to write any stack management functions. Plus, most of the work has been done for you.

UNIX doesn't provide any tools for top-down parsers because bottom-up parsers, although more complex, are of more general utility. But since top-down parsers are simpler in structure, writing your own parser in C or C++ is not too difficult. (There is a free Java-based top-down parser generator called ANTLR available.)

You need to have a scanner as the front end to your parser. You should use flex to identify your tokens. One call to flex will return one token. We've already covered a lot of what we need in class. We figured out how to make the grammar right-recursive and we worked out the first sets and when we need them. You should use the most complete form of the grammar, which uses all four operators and parentheses. We won't be using variables, though, just integers.

Remember that a recursive descent parser has a function to match every non-terminal symbol. To match the terminal symbols you could just have some kind of match function. See the Stanford notes below. You still need a scanner and you should use flex for that.

Recursive Descent Parser

The input is a string consisting of identifiers, operators and parentheses. The output is an abstract syntax tree. The input string may be of arbitrary complexity.

It is permissible to truncate the length of identifiers to make your tree look nicer.

The tree should be totally constructed first and then printed, not printed while the parse is still going on.

Here is the full set of operations:

1. Write a flex file that identifies integers, parentheses and +-*/.

2. Add to the flex file code to implement your recursive-descent parser that will take as its input an arithmetic expression.

3. Compile your flex code to create a compiler for the language of arithmetic expressions.

4. Run your compiler with an arithmetic expression as input. The compiler builds the parse tree and then prints it.

Implementation Specification

A few points to consider

• I believe the best approach would probably be to have each non-terminal function return a pointer to the subtree it has built.

• The C equivalent to the C++ new operator is malloc.

• You are free to create a node structure as you wish. However, there is no need to make it very complicated. For example, an operator node has an operator (one character) and two subtree pointers. A leaf node has just a string containing the identifier name.

• A pointer-based data structure is the obvious way to handle the arbitrary complexity of the tree.

• Write your own hand-coded parser following the structure of the pseudocode given in the text. Do not use yacc.

• Please try to do nice error-handling, pointing out where in the input string the error occurs and what kind of error it is. However, you should halt your parse on the first error in a line; don't try to continue. Start a new parse on the next line.

• Note that this kind of stack-based evaluation is essentially the same as postfix notation. You might find some useful information by searching on this topic.

• Possible enhancements are adding the exponentiation operator ^ and adding unary + and - operators. Both of these will require additional levels of operator precedence.

Reference no: EM131652

Questions Cloud

Prepare a power curve for this decision rule : Prepare a power curve for this decision rule
Methods : Assignment describe the basic working of methods. How to write a method in java and how to call a method.
Loops and files : Convert an algorithm using control structures into Java and write a while loop
Explain the median housing price in a community : Explain the median housing price in a community
Top-down recursive descent parser : Write a hand-coded top-down recursive descent parser.
Convert csv file into html file : Convert CSV file into HTML file
Planning strategies for the audit : Planning strategies for the audit
Research paper:soloman v soloman and co. : Complete and annex the Self and Peer Assessment.
Evaluate net investment : What is the net investment in the truck project?

Reviews

Write a Review

Programming Languages Questions & Answers

  Create a custom application using eclipse

Create a custom Application Using Eclipse Android Development

  Design a class-based solution to a problem

To design a class-based solution to a problem using multiple data structures.

  Discussion: html/css

Discussion: HTML/CSS,  "JavaScript Placement"  Please respond to the following: Compare and contrast the process of adding JavaScript and a Cascading Style Sheet to a Website. Determine if they can be used simultaneously in a page. If so, explain wh..

  Design a program that models the worms behavior

Design a program that models the worm's behavior.

  Solve the programming problem

Solve the programming problem

  Create a simple shell

Create a simple shell. Basically your shell should read the line from standard input, parse the line with command and arguments, and operate the command with arguments.

  Write a program that uses the curve class hierarchy

Write a program that uses the curve class hierarchy. The program should define several different objects, output their area, circumference, etc. It should also use the printcurve function.

  Implement functions using x86 assembly

Implement a procedure that mimics a logic unit

  Basics of hypertext markup language

The basics of Hypertext Markup Language and World Wide Web.

  Explain the process to develop a web page locally

Explain the process to develop a Web page locally

  Html/css

"Validating Your Work" Please respond to the following:  Describe attribute minimization. Explain what would happen if you tried to validate a page containing instances of attribute minimization. Propose a solution to this problem.

  Develops bespoke solutions for the rubber industry

Develops bespoke solutions for the rubber industry

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd