如何扩展一个抽象的Java语法分析器类,用于类似Ada的简单编程语言。

huangapple go评论80阅读模式
英文:

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{    
   }
}

huangapple
  • 本文由 发表于 2020年3月4日 06:23:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/60516404.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定