Contribution:
antlrParser
Headline
An ANTLR-based parser with semantic actions
Characteristics
An Technology:ANTLR-based parser for a concrete syntax of companies is provided. The underlying grammar is LL(1). The parser uses semantics actions to implement Feature:Total.
Illustration
The data model is implemented as plain textual files:
company "ACME Corporation" {
department "Research" {
manager "Craig" {
address "Redmond"
salary 123456.0
}
employee "Erik" {
address "Utrecht"
salary 12345.0
}
employee "Ralf" {
address "Koblenz"
salary 1234.0
}
}
department "Development" {
manager "Ray" {
address "Redmond"
salary 234567.0
}
department "Dev1" {
manager "Klaus" {
address "Boston"
salary 23456.0
}
department "Dev1.1" {
manager "Karl" {
address "Riga"
salary 2345.0
}
employee "Joe" {
address "Wifi City"
salary 2344.0
}
}
}
}
}
A Parser for Feature:Company will be generated by Technology:ANTLR using src/main/antlr/Company.g.
A Company is defined by:
company :
'company' STRING '{' department* '}' EOF;
department :
'department' STRING '{'
'manager' employee
('employee' employee)*
department*
'}';
employee :
STRING '{'
'address' STRING
'salary' FLOAT
{ total += Double.parseDouble($FLOAT.text); }
'}';
STRING : '"' (~'"')* '"';
FLOAT : ('0'..'9')+ ('.' ('0'..'9')+)?;
WS : (' '|'\r'? '\n'|'\t')+ {skip();};
Feature:Parsing is implemented using the generated Parser:
package org.softlang.company.features;
import org.softlang.company.antlr.CompanyLexer;
import org.softlang.company.antlr.CompanyParser;
import org.antlr.runtime.*;
import java.io.FileInputStream;
import java.io.IOException;
public class Parsing {
public static CompanyParser parse(String s)
throws IOException, RecognitionException {
FileInputStream stream = new FileInputStream(s);
ANTLRInputStream antlr = new ANTLRInputStream(stream);
CompanyLexer lexer = new CompanyLexer(antlr);
CommonTokenStream tokens = new CommonTokenStream(lexer);
CompanyParser parser = new CompanyParser(tokens);
parser.company();
if (parser.error) throw new RecognitionException();
return parser;
}
}
Feature:Total is implemented as part of Feature:Parsing with a semantic action (see Company.g):
employee :
STRING '{'
'address' STRING
'salary' FLOAT
{ total += Double.parseDouble($FLOAT.text); }
'}';
public static double total(String s)
throws IOException, RecognitionException {
/*
* Total is implemented by the generated Parser
* See src/main/antlr/Company.g
*/
CompanyParser parser = Parsing.parse(s);
return parser.total;
}
Test cases are implemented for all Namespace:Features. There is also an invalid input:
company "foobar" {
department "barfoo" {
}
}
Relationships
This contribution shows Feature:Parsing with semantic actions using Technology:ANTLR and Language:Java
For an ANTLER4 version see Contribution:antlr4Parser.
For plain syntax checking see Contribution:antlrAcceptor.
For a basic example of lexing/tokenization see Contribution:antlrLexer.
For Object-Text mapping with Technology:ANTLR see Contribution:antlrObjects.
For walking a parsed Feature:Company-tree with Technology:ANTLR see Contribution:antlrTrees.
Architecture
The contribution follows a standardized structure:
- inputs contains input files for tests
- src/main/antlr contains grammar files for Technology:ANTLR.
- src/main/java contains the following packages:
- org.softlang.company.features for implementations of Functional requirements.
- org.softlang.company.antlr for Technology:ANTLR-generated Java classes (post build).
- src/test/java contains the following packages:
- org.softlang.company.tests for Technology:JUnit test cases.
Usage
This contribution uses Technology:Gradle for building. Technology:Eclipse is supported.
See https://github.com/101companies/101simplejava/blob/master/README.md
Backlinks
There are no revisions for this page.
User contributions
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.