Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

from grammar to parser

807601Feb 7 2008 — edited Feb 9 2008
Hello everyone,i need a little help with the interpreter pattern.What i have to do is to get from a grammar ebnf of a little language (assembly rasp) the parser .I have to use expressly the interpreter pattern since it is a didactical project,by the way i have some doubt about how to translate the production rules into java parser,i mean should i create a method in the parser for every non-terminal elements? until now i have done a class called AnalyserLex that use the StreamTokenizer and of course its goal is to get the next symbol from a source that actually is a file where i wrote few instructions in "assembly rasp" after that i did the Parser.java class who has an instance of the AnalyserLex.Now i dont know how to go ahead,i think i should create a method for the non-terminals even if they dont call recursively each other and more over i should create a class for every non-terminals in order to build the composite,indeed i dont know if im wrong here.
Considering that the grammar isnt too long i wish to let you to take a look.
<programma> ::= <dichiarazioni> <istruzioni>
<dichiarazioni> ::= {<dichiarazione>}
<dichiarazione> ::= <etichetta> RES <interosenzasegno> <finelinea>|<commento><finelinea>
<istruzioni> ::= {<istruzione>}
<istruzione>::= [<etichetta>]<codiceoperativo> [<modo>]
[<operando>] [<commento>] <finelinea> | <commento><finelinea>
<codiceoperativo> ::= 
	LOAD | STORE | READ | WRITE | ADD | SUB | MUL |
	DIV | JZ | JNZ | JGZ | JGEZ | JLZ | JLEZ | JUMP | HALT
<modo> ::= # | @
<finelinea> ::= specificabile a piacere (es. CR/LF)
<etichetta> ::= <identificatore> :
<operando> ::= <intero> | <identificatore>
<commento> ::= ; {<carattere>}
<interosenzasegno> ::= <cifra> {<cifra>}
<intero> ::= [<segno>] <interosenzasegno>
<segno> ::= + | -
<identificatore> ::= <lettera> {<lettera> | <cifra>}
<lettera> ::=	a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|x|y|w|z|
		A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|X|Y|W|Z
<cifra> ::= 0|1|2|3|4|5|6|7|8|9
<carattere> ::= come da alfabeto ASCII
so in the parser i should start with a method called for example "programma()" that presumedly it calls "dichiarazioni()" and then "istruzioni()" am i wrong? anyway i know that it could be so easy for you to do this kind of work but actually im trapped there! thank you in advance for the help.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 8 2008
Added on Feb 7 2008
7 comments
201 views