Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask Programming Language Expert

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.

Programming Language, Programming

  • Category:- Programming Language
  • Reference No.:- M9132083

Have any Question?


Related Questions in Programming Language

Assignment - proposal literature review research method1

Assignment - Proposal, Literature Review, Research Method 1. Abstract - Summary of the knowledge gap: problems of the existing research - Aim of the research, summary of what this project is to achieve - Summary of the a ...

Overviewthis tasks provides you an opportunity to get

Overview This tasks provides you an opportunity to get feedback on your Learning Summary Report. The Learning Summary Report outlines how the work you have completed demonstrates that you have met all of the unit's learn ...

Structs and enumsoverviewin this task you will create a

Structs and Enums Overview In this task you will create a knight database to help Camelot keep track of all of their knights. Instructions Lets get started. 1. What the topic 5 videos, these will guide you through buildi ...

Php amp session managment assignment -this assignment looks

PHP & SESSION MANAGMENT ASSIGNMENT - This assignment looks at using PHP for creating cookies and session management. Class Exercise - Web Project: Member Registration/Login This exercise will cover adding data connectivi ...

Question 1 what is a computer program what is structured

Question: 1. What is a Computer program? What is structured programming? 2. What is modular programming? Why we use it? 3. Please evaluate Sin (x) by infinite series. Then write an algorithm to implement it with up to th ...

Task working with arraysoverviewin this task you will

Task: Working with Arrays Overview In this task you will create a simple program which will create and work with an array of strings. This array will then be populated with values, printed out to the console, and then, w ...

Question - create a microsoft word macro using vba visual

Question - Create a Microsoft Word macro using VBA (Visual Basic for Applications). Name the macro "highlight." The macro should highlight every third line of text in a document. (Imagine creating highlighting that will ...

Assignment - haskell program for regular expression

Assignment - Haskell Program for Regular Expression Matching Your assignment is to modify the slowgrep.hs Haskell program presented in class and the online notes, according to the instructions below. You may carry out th ...

Task silly name testeroverviewcontrol flow allows us to

Task: Silly Name Tester Overview Control flow allows us to alter the order in which our programs execute. Building on our knowledge of variables, we can now use control flow to create programs that perform more than just ...

Assignmentquestion onegiving the following code snippet

Assignment Question One Giving the following code snippet. What kind of errors you will get and how can you correct it. A. public class HelloJava { public static void main(String args[]) { int x=10; int y=2; System.out.p ...

  • 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