/*
* Title: Street Address
* Description: Dan's implementation of Extended BNF
* Copyright: Copyright (c) 2003 Calgary Alberta Canada
* Company: BNF for Java
*/
package streetAddress;
import bnf.*;
import bnf.syntax.*;
import java.io.*;
/**
* This class is provided to produce an example Parser Object,
* which is a serialized instance of the Syntax class,
* whose components implement an example grammar.
* The grammar which we use in our example is "Street Address",
* a simple grammar for parsing a single text field on somebody's form.
* Here is the BNF:
(* A BNF Example: street address (start symbol is "street address") *)
street address= house number, street id, quadrant;
house number = integer;
street id = ( street number | street name ), street type;
street number = integer;
street name = terminal string;
street type = "ST" | "AVE" | "RD" | "TR";
quadrant = "NW" | "NE" | "SW" | "SE";
*
* The XML version of this Parser Object is in file:
Street Address as a Parser Object in XML
*
*/
class StreetAddress
{
private Syntax syntax;
private MetaIdentifier startSymbol;
public static void main( String[] args )
{
System.out.println( "Creating the 'streetAddress' instance of the Syntax class." );
StreetAddress streetAddress = new StreetAddress(); // the whole thing gets loaded
System.out.println( "Writing the 'streetAddress.xml' file." );
try
{
DataOutputStream out = new DataOutputStream( new FileOutputStream( "streetAddress.parser" ) );
out.writeUTD( streetAddress.toXML() );
}
catch ( IOException ex )
{
ex.printStackTrace();
}
System.out.println( "Writing the 'streetAddress.parser' file." );
try
{
OutputStream out = new FileOutputStream( "streetAddress.parser" );
ObjectOutputStream oos = new ObjectOutputStream( out );
oos.writeObject( streetAddress );
}
catch ( IOException ex )
{
ex.printStackTrace();
}
System.out.println( "Parser Object is created, 3 ways" );
}
/**
* This constructor implements the "street address" example
* by loading a ParserObject (an instance of Syntax)
* with all the components that correspond to the grammar.
* This Java code corresponds exactly to the elements in file
Street Address as a Parser Object in XML.
* I may have taken some liberties with code shortcuts,
* but the object created here should be functionally identical to the XML version.
*/
public StreetAddress()
{
syntax = new Syntax();
startSymbol = new MetaIdentifier( "street address" );
syntax.setStartSymbol( startSymbol );
{ // address 1 = house number, street id, quadrant;
SyntaxRule streetAddress = new SyntaxRule( "street address" );
DefinitionsList defList = new DefinitionsList();
{ // = house number, street id, quadrant
SingleDefinition singleDef = new SingleDefinition();
SyntacticTerm term1 = new SyntacticTerm( new SyntacticFactor( 1, new SyntacticPrimary( new MetaIdentifier( "house number" ) ) ) );
SyntacticTerm term2 = new SyntacticTerm( new SyntacticFactor( 1, new SyntacticPrimary( new MetaIdentifier( "street id" ) ) ) );
SyntacticTerm term3 = new SyntacticTerm( new SyntacticFactor( 1, new SyntacticPrimary( new MetaIdentifier( "quadrant" ) ) ) );
singleDef.add( term1 );
singleDef.add( term2 );
singleDef.add( term3 );
defList.add( singleDef );
}
streetAddress.add( defList );
syntax.add( streetAddress );
}
{ // house number = integer;
SyntaxRule houseNumber = new SyntaxRule( "house number" );
DefinitionsList defList = new DefinitionsList();
{ // = integer
SingleDefinition singleDef = new SingleDefinition();
SyntacticTerm term1 = new SyntacticTerm( new SyntacticFactor( 1, new SyntacticPrimary( new Digit( /* any integer */) ) ) );
singleDef.add( term1 );
defList.add( singleDef );
}
houseNumber.add( defList );
syntax.add( houseNumber );
}
{ // street id = ( street number | street name ), street type;
SyntaxRule streetId = new SyntaxRule( "street id" );
DefinitionsList defList = new DefinitionsList();
{ // = (...), street type
SingleDefinition singleDef = new SingleDefinition();
{ // ( street number | street name )
GroupedSequence group1 = new GroupedSequence();
DefinitionsList defList2 = new DefinitionsList();
{ // = street number
SingleDefinition singleDef2 = new SingleDefinition();
SyntacticTerm term1 = new SyntacticTerm( new SyntacticFactor( 1, new SyntacticPrimary( new MetaIdentifier( "street number" ) ) ) );
singleDef2.add( term1 );
defList2.add( singleDef2 );
}
{ // = street name
SingleDefinition singleDef2 = new SingleDefinition();
SyntacticTerm term2 = new SyntacticTerm( new SyntacticFactor( 1, new SyntacticPrimary( new MetaIdentifier( "steet name" ) ) ) );
singleDef2.add( term2 );
defList2.add( singleDef2 );
}
group1.add( defList2 );
singleDef.add( group1 );
}
SyntacticTerm term3 = new SyntacticTerm( new SyntacticFactor( 1, new SyntacticPrimary( new MetaIdentifier( "steet type" ) ) ) );
singleDef.add( term3 );
defList.add( singleDef );
}
streetId.add( defList );
syntax.add( streetId );
}
{ // street number = integer;
SyntaxRule streetNumber = new SyntaxRule( "street number" );
DefinitionsList defList = new DefinitionsList();
{ // = integer
SingleDefinition singleDef = new SingleDefinition();
SyntacticTerm term1 = new SyntacticTerm( new SyntacticFactor( 1, new SyntacticPrimary( new Digit( /* any integer */) ) ) );
singleDef.add( term1 );
defList.add( singleDef );
}
streetNumber.add( defList );
syntax.add( streetNumber );
}
{ // street name = string;
SyntaxRule streetName = new SyntaxRule( "street name" );
DefinitionsList defList = new DefinitionsList();
{ // = integer
SingleDefinition singleDef = new SingleDefinition();
SyntacticTerm term1 = new SyntacticTerm( new SyntacticFactor( 1, new SyntacticPrimary( new TerminalString() ) ) );
singleDef.add( term1 );
defList.add( singleDef );
}
streetName.add( defList );
syntax.add( streetName );
}
{ // street type = "ST" | "AVE" | "RD" | "TR";
SyntaxRule streetType = new SyntaxRule( "street type" );
DefinitionsList defList = new DefinitionsList();
{ // "ST"
SingleDefinition singleDef = new SingleDefinition();
SyntacticTerm term1 = new SyntacticTerm( new SyntacticFactor( 1, new SyntacticPrimary( new TerminalString( "ST" ) ) ) );
singleDef.add( term1 );
defList.add( singleDef );
}
{ // "AVE"
SingleDefinition singleDef = new SingleDefinition();
SyntacticTerm term1 = new SyntacticTerm( new SyntacticFactor( 1, new SyntacticPrimary( new TerminalString( "AVE" ) ) ) );
singleDef.add( term1 );
defList.add( singleDef );
}
{ // "RD"
SingleDefinition singleDef = new SingleDefinition();
SyntacticTerm term1 = new SyntacticTerm( new SyntacticFactor( 1, new SyntacticPrimary( new TerminalString( "RD" ) ) ) );
singleDef.add( term1 );
defList.add( singleDef );
}
{ // "TR"
SingleDefinition singleDef = new SingleDefinition();
SyntacticTerm term1 = new SyntacticTerm( new SyntacticFactor( 1, new SyntacticPrimary( new TerminalString( "TR" ) ) ) );
singleDef.add( term1 );
defList.add( singleDef );
}
streetType.add( defList );
syntax.add( streetType );
}
{ // quadrant = "NW" | "NE" | "SW" | "SE";
SyntaxRule quadrant = new SyntaxRule( "quadrant" );
DefinitionsList defList = new DefinitionsList();
{ // "NW"
SingleDefinition singleDef = new SingleDefinition();
SyntacticTerm term1 = new SyntacticTerm( new SyntacticFactor( 1, new SyntacticPrimary( new TerminalString( "NW" ) ) ) );
singleDef.add( term1 );
defList.add( singleDef );
}
{ // "NE"
SingleDefinition singleDef = new SingleDefinition();
SyntacticTerm term1 = new SyntacticTerm( new SyntacticFactor( 1, new SyntacticPrimary( new TerminalString( "NE" ) ) ) );
singleDef.add( term1 );
defList.add( singleDef );
}
{ // "SW"
SingleDefinition singleDef = new SingleDefinition();
SyntacticTerm term1 = new SyntacticTerm( new SyntacticFactor( 1, new SyntacticPrimary( new TerminalString( "SW" ) ) ) );
singleDef.add( term1 );
defList.add( singleDef );
}
{ // "SE"
SingleDefinition singleDef = new SingleDefinition();
SyntacticTerm term1 = new SyntacticTerm( new SyntacticFactor( 1, new SyntacticPrimary( new TerminalString( "SE" ) ) ) );
singleDef.add( term1 );
defList.add( singleDef );
}
quadrant.add( defList );
syntax.add( quadrant );
}
}
}