Skip to content

Visitor Guide Solutions

Luis Diogo Couto edited this page Nov 21, 2014 · 1 revision

Exercise 01

public class Exercise01Visitor extends AnalysisAdaptor{

	int setEnumSize = -1;
	
	public int exercise01(){
		return setEnumSize;
	}
	
	@Override
	public void caseASetEnumSetExp(ASetEnumSetExp node)
			throws AnalysisException {
		setEnumSize = node.getMembers().size();
	}
}

Exercise 02

public class Exercise02Visitor extends AnalysisAdaptor {

	String defName = "";

	public String exercise02() {
		return defName;
	}

	@Override
	public void defaultPDefinition(PDefinition node) throws AnalysisException {
		defName = node.getName().getName();
	}
}

Exercise 03

public class Exercise03Visitor extends AnalysisAdaptor {

	int termCount = 0;

	public int exercise03() {
		return termCount;
	}

	@Override
	public void caseAPlusNumericBinaryExp(APlusNumericBinaryExp node)
			throws AnalysisException {
		node.getLeft().apply(this);
		node.getRight().apply(this);
	}
	
	public void caseASubtractNumericBinaryExp(ASubtractNumericBinaryExp node) throws AnalysisException {
		node.getLeft().apply(this);
		node.getRight().apply(this);
	}

	@Override
	public void defaultPExp(PExp node) throws AnalysisException {
		termCount++;
	}

}

Exercise 4.1

public class Exercise41Visitor extends AnswerAdaptor<Integer> {

	@Override
	public Integer createNewReturnValue(INode arg0) throws AnalysisException {
		return 0;
	}

	@Override
	public Integer createNewReturnValue(Object arg0) throws AnalysisException {
		return 0;
	}

	@Override
	public Integer caseASetEnumSetExp(ASetEnumSetExp node)
			throws AnalysisException {
		return node.getMembers().size();
	}

}

Exercise 4.2

public class Exercise42Visitor extends AnswerAdaptor<String>{

	@Override
	public String createNewReturnValue(INode arg0) throws AnalysisException {
		return "";
	}

	@Override
	public String createNewReturnValue(Object arg0) throws AnalysisException {
		return "";
	}

    @Override
    public String defaultPDefinition(PDefinition node) throws AnalysisException {
        return node.getName().getName();
    }

}

Exercise 4.3

public class Exercise43Test {

	@Test
	public void test_01() throws AnalysisException {
		PExp exp = ParseTcFacade.parseExpressionString("1");
		Exercise43Visitor visitor = new Exercise43Visitor();

		int expected = 1;
		int actual = exp.apply(visitor, 0);

		assertEquals(expected, actual);
	}

	@Test
	public void test_02() throws AnalysisException {
		PExp exp = ParseTcFacade.parseExpressionString(" 1 + 2");
		Exercise43Visitor visitor = new Exercise43Visitor();

		int expected = 2;
		int actual = exp.apply(visitor, 0);

		assertEquals(expected, actual);
	}

	@Test
	public void test_03() throws AnalysisException {
		PExp exp = ParseTcFacade.parseExpressionString("3*5 - 4 + 2*2");
		Exercise43Visitor visitor = new Exercise43Visitor();

		int expected = 3;
		int actual = exp.apply(visitor, 0);

		assertEquals(expected, actual);
	}

	@Test
	public void test_04() throws AnalysisException {
		PExp exp = ParseTcFacade.parseExpressionString("-2 + 3 * 4");
		Exercise43Visitor visitor = new Exercise43Visitor();

		int expected = 2;
		int actual = exp.apply(visitor, 0);

		assertEquals(expected, actual);
	}

}