| | |
Syntax Analyser and General Java Help
![]() |
Hello, I am currently working on some coursework from which i have to write a syntax analyser to parse a grammer. Unfortunatly I am finding it hard to understand what I am meant to be doing - and if i am doing it right.
I have looked through many tutorials but they don't fill me with confidence that what i have to do is right. So hopefully you guys will be able to put me in the right direction so i can do some cool coding.
The main problem is:
Write a Java class SyntaxAnalyser This will have:
• a constructor with one String argument, the name of the file from which the tokens are to be read
• a single public method parse (there will, of course, also be some private methods). This is a void method with no
arguments, which is a recursive descent parser for this grammar applied to the file specified (or rather applied to
the tokens returned by the LexicalAnalyser class from the file specified).
This is my current attempt -
As you can see i have created a new method for each "non terminal" in the grammar. It may help you see what the grammar is which is here.
I am not so sure what to do next. I think i will have to fill these methods with actions depending on the grammar - but like i say i don't understand well.
Does the above file look like the correct structure?
Anyway thanks for any help you give. Below are some background links -
coursework questions (pdf)
Other java files.
Once again thanks for any help you give in helping me understand what i have to do
EDIT: i forgot to mention it is a "recrusive descent parser" which means it is a top down parser. The grammar is also in BNF form.
Ill will keep you up to date with any changes i make because i will be working on this constantly
I have looked through many tutorials but they don't fill me with confidence that what i have to do is right. So hopefully you guys will be able to put me in the right direction so i can do some cool coding.

The main problem is:
Write a Java class SyntaxAnalyser This will have:
• a constructor with one String argument, the name of the file from which the tokens are to be read
• a single public method parse (there will, of course, also be some private methods). This is a void method with no
arguments, which is a recursive descent parser for this grammar applied to the file specified (or rather applied to
the tokens returned by the LexicalAnalyser class from the file specified).
This is my current attempt -
Java Syntax (Toggle Plain Text)
package comp; import java.io.* ; public class SyntaxAnalyser { String file; Token nextSymbol; public SyntaxAnalyser(String fileName) { file = fileName; } public void parse() throws IOException { LexicalAnalyser lex = new LexicalAnalyser(file); Token t = null ; do { t = lex.getNextToken() ; System.out.println(t) ; } while (t.symbol != Token.eofSymbol) ; //define a set of all terminals/non-terminals which could be the left most token of (a). FIRST(a) //variable nextSymbol to recognise the next token from the lexical analyser //acceptTerminal Method if(lex.getNextToken() != null) { Token nextSymbol = lex.getNextToken(); } else { System.out.println("There has been an Error with nextSymbol"); } } //<program> ::= <procedure list> public void program() { if(nextSymbol.toString() == "<program>") { } } private void procedureList() { //call .procedureDefinition(); or .procedureList();.procedureDefinition() } //Second method to elimintate left recursion private void procedureList2(){ } private void procedureDefinition() { //call procedureDefinition } private void procedureHeading() { } private void declerationList() { } //Second method to eliminate left recursion private void declerationList2(){ } private void declaration() { } private void identifierList() { } //Second method to eliminate left recursion private void identifierList2() { } private void block() { } private void statementPart() { } private void statementList() { } private void statement() { } private void assignmentStatement() { } private void ifStatement() { } private void whileStatement() { } private void procedureStatement() { } private void untilStatement() { } private void argumtList() { } //Second method to eliminate left recursion private void argumetList(){ } private void condtion() { } private void conditionOperator() { //if(op == '+' || op == '-' || op == '*' || op == '/') { //.getNextChar(); } } private void expression() { } private void term() { } private void factor() { } }
As you can see i have created a new method for each "non terminal" in the grammar. It may help you see what the grammar is which is here.
I am not so sure what to do next. I think i will have to fill these methods with actions depending on the grammar - but like i say i don't understand well.
Does the above file look like the correct structure?
Anyway thanks for any help you give. Below are some background links -
coursework questions (pdf)
Other java files.
Once again thanks for any help you give in helping me understand what i have to do

EDIT: i forgot to mention it is a "recrusive descent parser" which means it is a top down parser. The grammar is also in BNF form.
Ill will keep you up to date with any changes i make because i will be working on this constantly
![]() |
Similar Threads
- VISTA - Java error (Windows Vista and Windows 7)
- General Java Problem (Java)
- ?: syntax problem (C#)
- "Pointers" in Java (Java)
- Java Resources (Java)
- urgent help needed for BE-PROJECT ideas in computer science (C)
- Newbie in java (Java)
Other Threads in the Java Forum
- Previous Thread: manipulating graphs in a GUI
- Next Thread: Lottery Program
| Thread Tools | Search this Thread |
911 addball addressbook android api append applet application apps array arrays automation binary bluetooth businessintelligence button card class client code collision component crashcourse css csv database eclipse ee error fractal free ftp game gis givemetehcodez graphics gui html ide image integer integration j2me japplet java javaarraylist javadoc javafx javaprojects jni jpanel julia jvm linked linux list loan machine map method methods migrate mobile netbeans objects oriented output phone physics printf problem program programming project projects radio recursion replaydirector reporting researchinmotion rotatetext scanner se server service set sms software sort sql string swing test textfield threads tree trolltech ubuntu utility windows





