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.:- M9717336

Have any Question?


Related Questions in Programming Language

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 ...

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 ...

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 ...

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 ...

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 ...

Background informationthis assignment tests your

Background Information This assignment tests your understanding of and ability to apply the programming concepts we have covered throughout the unit. The concepts covered in the second half of the unit build upon the fun ...

Task arrays and structsoverviewin this task you will

Task: Arrays and Structs Overview In this task you will continue to work on the knight database to help Camelot keep track of all of their knights. We can now add a kingdom struct to help work with and manage all of the ...

Assignment - horse race meetingthe assignment will assess

Assignment - Horse Race Meeting The Assignment will assess competencies for ICTPRG524 Develop high level object-oriented class specifications. Summary The assignment is to design the classes that are necessary for the ad ...

Task - hand execution of arraysoverviewin this task you

Task - Hand Execution of Arrays Overview In this task you will demonstrate how arrays work by hand executing a number of small code snippets. Instructions Watch the Hand Execution with Arrays video, this shows how to ste ...

Assignment - horse race meetingthe assignment will assess

Assignment - Horse Race Meeting The Assignment will assess competencies for ICTPRG524 Develop high level object-oriented class specifications. Summary The assignment is to design the classes that are necessary for the ad ...

  • 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