| Using lexicon classes |
![]() Using lexicon classesThe classes have mostly the same structure as the XML lexicon format, and interpreted as the model of the lexicon.
Lexicon lexicon = new Lexicon();
Lexeme A = new Lexeme(new Terminal("A"));
Lexeme B = new Lexeme();
lexicon.addLexeme(A);
lexicon.addLexeme(B);
For the definition the Pattern classes are used.
Concatenation definition = new Concatenation();
definition.addPattern(new CharacterString("foo"));
definition.addPattern(new CharacterString("bar"));
A.setDefinition(definition);
Alternation alternation = new Alternation();
alternation.addPattern(new CharacterString("one"));
alternation.addPattern(new CharacterString("two"));
B.setDefinition(alternation);
As last step you should always validate your model. You can do this simply by using the validate method. Which returns a list of violations. If this list is empty your grammar is valid. Violations violations = lexicon.validate(); if (violations.getViolationCount()>0) throw new IllegalStateException(); |