Headline

Parsing text to trees and walk them with ANTLR4 Visitors

Realtionships

This an ANTLR4 version of Contribution:antlrTrees using the Visitor pattern.

Illustration

Total and Cut are extensions of CompanyBaseVisitor using the visitor pattern (Visitor pattern). The BaseVisitor iterates the whole company, the extensions for Cut and Total overrides only the visit methods of the employees, whereas the PrintVisitor overrides all methods.

Example of TotalVisitor:

@Override public Float visitEmployee(@NotNull CompanyParser.EmployeeContext ctx) { 
   	
    return Float.valueOf(ctx.salary.getText());		
}

To use a visitor invoke the visit method of the wanted visitor and give the company parse tree to the visit as argument.

Example of TotalTestVisitor(some parts are omitted):

	@Test
	public void testTotal() throws RecognitionException, IOException, PrintException {
		CompanyParser parser = Parsing.parse(sampleCompany);
		parser.setBuildParseTree(true);
		ParseTree tree = parser.company(); // top rule
		// Create the visitor, whoch totals
		TotalVisitor t= new TotalVisitor();

		assertEquals(399747, t.visit(tree) , 0); 
		
	}

Usage

This contribution uses Technology:Gradle for building. Technology:Eclipse is supported.

See https://github.com/101companies/101simplejava/blob/master/README.md


There are no revisions for this page.

User contributions

    This user never has never made submissions.

    User edits

    Syntax for editing wiki

    For you are available next options:

    will make text bold.

    will make text italic.

    will make text underlined.

    will make text striked.

    will allow you to paste code headline into the page.

    will allow you to link into the page.

    will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

    will allow you to paste image into the page.

    is list with bullets.

    is list with numbers.

    will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

    will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

    will allow your to insert code snippets from @worker.

    Syntax for editing wiki

    For you are available next options:

    will make text bold.

    will make text italic.

    will make text underlined.

    will make text striked.

    will allow you to paste code headline into the page.

    will allow you to link into the page.

    will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

    will allow you to paste image into the page.

    is list with bullets.

    is list with numbers.

    will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

    will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

    will allow your to insert code snippets from @worker.