Syntax Analyser and General Java Help

Reply

Join Date: Nov 2004
Posts: 89
Reputation: MrScruff is an unknown quantity at this point 
Solved Threads: 0
MrScruff's Avatar
MrScruff MrScruff is offline Offline
Junior Poster in Training

Syntax Analyser and General Java Help

 
0
  #1
Dec 1st, 2004
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 -
  1. package comp;
  2.  
  3. import java.io.* ;
  4. public class SyntaxAnalyser
  5. {
  6. String file;
  7. Token nextSymbol;
  8.  
  9. public SyntaxAnalyser(String fileName) {
  10. file = fileName;
  11. }
  12.  
  13. public void parse() throws IOException {
  14.  
  15. LexicalAnalyser lex = new LexicalAnalyser(file);
  16. Token t = null ;
  17. do
  18. {
  19. t = lex.getNextToken() ;
  20. System.out.println(t) ;
  21. }
  22. while (t.symbol != Token.eofSymbol) ;
  23.  
  24.  
  25. //define a set of all terminals/non-terminals which could be the left most token of (a). FIRST(a)
  26. //variable nextSymbol to recognise the next token from the lexical analyser
  27.  
  28. //acceptTerminal Method
  29. if(lex.getNextToken() != null)
  30. {
  31. Token nextSymbol = lex.getNextToken();
  32. }
  33. else
  34. {
  35. System.out.println("There has been an Error with nextSymbol");
  36. }
  37. }
  38.  
  39. //<program> ::= <procedure list>
  40. public void program()
  41. {
  42.  
  43. if(nextSymbol.toString() == "<program>")
  44. {
  45. }
  46.  
  47. }
  48. private void procedureList() {
  49. //call .procedureDefinition(); or .procedureList();.procedureDefinition()
  50. }
  51. //Second method to elimintate left recursion
  52. private void procedureList2(){
  53.  
  54. }
  55. private void procedureDefinition() {
  56. //call procedureDefinition
  57. }
  58. private void procedureHeading() {
  59. }
  60. private void declerationList() {
  61. }
  62. //Second method to eliminate left recursion
  63. private void declerationList2(){
  64.  
  65. }
  66. private void declaration() {
  67. }
  68. private void identifierList() {
  69. }
  70. //Second method to eliminate left recursion
  71. private void identifierList2()
  72. {
  73.  
  74. }
  75. private void block() {
  76. }
  77. private void statementPart() {
  78. }
  79. private void statementList() {
  80. }
  81. private void statement() {
  82. }
  83. private void assignmentStatement() {
  84. }
  85. private void ifStatement() {
  86. }
  87. private void whileStatement() {
  88. }
  89. private void procedureStatement() {
  90. }
  91. private void untilStatement() {
  92. }
  93. private void argumtList() {
  94. }
  95. //Second method to eliminate left recursion
  96. private void argumetList(){
  97.  
  98. }
  99. private void condtion() {
  100. }
  101. private void conditionOperator() {
  102. //if(op == '+' || op == '-' || op == '*' || op == '/')
  103. {
  104. //.getNextChar();
  105. }
  106. }
  107. private void expression() {
  108. }
  109. private void term() {
  110. }
  111. private void factor()
  112. {
  113. }
  114. }

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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC