英文:
How to extend an abstract Syntax Analyzer Java class for a simple programming language like Ada
问题
以下是翻译好的内容:
我需要编写一个语法分析器的Java类。为了做到这一点,我应该扩展一个提供的抽象语法分析器Java类。
我知道这应该是相当简单的,但是我已经花了相当多的时间来尝试入门,但总是感到...卡住了。
这是我提供的抽象类。
public abstract class AbstractSyntaxAnalyser
{
LexicalAnalyser lex ;
Token nextToken ;
Generate myGenerate = null;
public abstract void _statementPart_() throws IOException, CompilationException;
public abstract void acceptTerminal(int symbol) throws IOException, CompilationException;
public void parse( PrintStream ps ) throws IOException
{
ps.println( lex.getFilename() );
myGenerate = new Generate();
try {
nextToken = lex.getNextToken() ;
_statementPart_() ;
acceptTerminal(Token.eofSymbol) ;
myGenerate.reportSuccess() ;
ps.println( "OK\n" );
}
catch( CompilationException ex )
{
ps.println( "Compilation Exception" );
ps.println( ex.toTraceString() );
ps.println( "STOP\n" );
}
}
}
作为一个新的Java用户,我真的很需要关于如何推动这个项目的帮助和指导,因为这只是一系列任务中的第一步,我似乎无法取得任何进展。
祝一切顺利!
英文:
I need to write a Syntax Analyser Java class. To do so, I am supposed to extend an Abstract Syntax Analyzer java class that is provided.
I know this should be quite straightforwards, but I've spent quite a healthy amount of time trying to get started and just feel... stuck.
This is the abstract class I have been provided with.
public abstract class AbstractSyntaxAnalyser
{
LexicalAnalyser lex ;
Token nextToken ;
Generate myGenerate = null;
public abstract void _statementPart_() throws IOException, CompilationException;
public abstract void acceptTerminal(int symbol) throws IOException, CompilationException;
public void parse( PrintStream ps ) throws IOException
{
ps.println( lex.getFilename() );
myGenerate = new Generate();
try {
nextToken = lex.getNextToken() ;
_statementPart_() ;
acceptTerminal(Token.eofSymbol) ;
myGenerate.reportSuccess() ;
ps.println( "OK\n" );
}
catch( CompilationException ex )
{
ps.println( "Compilation Exception" );
ps.println( ex.toTraceString() );
ps.println( "STOP\n" );
}
}
}
As a new Java user, I would really appreciate help and guidance on how to get this project moving as this is merely the first step in a whole list of things to do and I just can't seem to make any headway..
All the best!
答案1
得分: 2
为扩展该类,您需要提供所有抽象方法的实现。一个无操作的示例可以是:
public class MySyntaxAnalyzer extends AbstractSyntaxAnalyzer{
public void _statementPart_() throws IOException, CompilationException{
}
public void acceptTerminal(int symbol) throws IOException, CompilationException{
}
}
英文:
To extend that class you need to provide an implementation of all the abstract methods. A do-nothing example would be
public class MySyntaxAnalyzer extends AbstractSyntaxAnalyzer{
public void _statementPart_() throws IOException, CompilationException{
}
public void acceptTerminal(int symbol) throws IOException, CompilationException{
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论