Implementing a Simple Parser for arithmetic expressions in Java?
-
So I'm confused about this project I have to do and need help (though I'm not too sure if anyone would understand this stuff). Here's what I have to do: "Implement a simple parser for arithmetic expressions in Java. The following is the context-free grammar for the arithmetic expressions. E->E+T | E-T | T T->T*F | T/F | F F->N | (E) | V Here N and V are nonterminals representing integers and variables. For simplicity, you may assume the possible values for integers in this project are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, and the possible variable names are a, b, ..., z and A, B, ..., Y, Z. Extra credits for significantly more generality for either of the two parts. Requirements with hints 1. First, you need to remove left recursions for the above grammar, in order to write procedures. 2. For a given input arithmetic expression, you need to process it symbol by symbol, start it from the beginning, you can input the string incline, or use an array, whatever you feel easy. 3. The rule to write procedures is to use a function for each non-terminal, with the main( ) function corresponding to the start symbol. For each production rule, say A->BC, you can write procedure A as void funA( ) { funB( ); funC( ); } If a production rule has a terminal, say A-> BbC, you can write procedure A as void funA( ) { funB( ); match('b'); funC( ); } Here the function match(char x) is to check whether the input symbol is 'x'. This should be very important. 4. Your program must be supported by running results for at least 10 strikingly different expressions. 5. Do not forget documentations."
-
Answer:
You have been asked to implement this as a recursive descent parser. What you need to do is for a production rule like F-> N| (E) | V For this rule you will write: function F() { if(currToken.isDigit()) { //Add a digit to your parse tree else if(currToken.isParentheses()) { readNextToken(); E(); if(!currToken.isParentheses()) //Set error. etc. else readNextToken(); } else if(currToken.isVariable()) { //Add this to your parse tree. readNextToken(); } else //Set error etc. } If you don't need evaluation and just checking if it is a valid sentence in the language, then you don't need the parse tree stuff. Hope this helps.
Derick at Yahoo! Answers Visit the source
Related Q & A:
- how to process a simple loop in WWW::Mechanize to be more efficient?Best solution by stackoverflow.com
- How to set up a simple subversion workflow?Best solution by Programmers
- How to make a simple Facebook canvas app?Best solution by Stack Overflow
- How to do a simple character controller?Best solution by Stack Overflow
- Where can I find good a tutorial for creating a simple flash movie using Adobe Flash CS4?Best solution by Graphic Design
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.