[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Interpreters



Hi Roger

on Tue, November 26, 2002 9:58 pm s354157@student.uq.edu.au wrote:
>
> Hi,
>
> It might be possible to write your own Parser to do this. There is a
> section in Etienne's thesis on page 50 on custom parsers and it might
> also be worth checking out this example:
>
>
http://schema.cheque.uq.edu.au/~roger/software/snippets/sablecc/CustomParser.java>
> The inner class contains action code which is executed in the same way
> as any AnalysisAdapter (i.e. when the node is parsed). The filter()
> method applies the AnalysisAdapter to the nodes as they are parsed.
>
> I was thinking you could write an AnalysisAdapter that compiles and
> executes the input when, say, a 'semicolon' node is parsed. I'm sure
> others will have comments on this approach...
>
> Roger
>
>
> On Tue, Nov 26, 2002 at 07:56:10PM -0400, Marcelo Morales wrote:
>> Hi
>>
>> I just downloaded SableCC version 2.16.2 and i wish to know how to
>> implement an interpreter... you know, one which does some kind of
>> read-eval-print loop. Any ideas would be really apreciated.
>>
>> By the way, i did join the SableCC mailing list.
>>
>> Thanks in advance
>>
>> MM
>>
>>

This didnt work.

The final application does not behave like an interpreter, it evaluates ...
like one step behind.

Im not sure where to look: it could be the InputStreamReader java class
(something about flushing) or it could be the parsing algorithm.

Thanks for the previous quick answer. I really need your help again.

these are some code and grammar:

public class Interpreter {

    static class CustomParser extends Parser {

	public CustomParser( Lexer lexer ) {
	    super( lexer );
	}

	AstFix fix = new AstFix();
	private class AstFix extends AnalysisAdapter {

	    public void caseAIpipConfig( AIpipConfig node )
	    {
		System.out.println( node.toString() );
		// Do something else...
	    }
	}

	protected void filter()
	{
	    node.apply( fix );
	}
    }

    public static void main( String[] args ) {
        try {
	    Lexer lexer =
		new Lexer(
			  new PushbackReader
			  (
			   new InputStreamReader
			   (
			    System.in ), 1024 ) );

	    Parser parser = new CustomParser( lexer );
            Node ast = parser.parse();
        } catch( Exception e ) {
            e.printStackTrace();
        }

    }

}

Tokens
  blank = ' '+;
  return = (10 | 13);
  number = ['0' .. '9']+;
  dot = '.';
  ipaddress = 'ip address';
  ipnetmask = 'ip netmask';

Ignored Tokens
  blank;

Productions
  statement = config return |
              {more} config return statement;

  config = {ipip} ipaddress ip_address |
           {ipnm} ipnetmask ip_address;

  ip_address = [first_octet]:number [first_dot]:dot
              [second_octet]:number [second_dot]:dot
               [third_octet]:number [third_dot]:dot
              [fourth_octet]:number;

Thanks in advance

MM