[antlr-interest] xml grammar

Terence Parr parrt at cs.usfca.edu
Tue Nov 15 11:24:24 PST 2005


On Nov 15, 2005, at 11:18 AM, Torsten Curdt wrote:
> I hope I can get away with the provided XML grammar ...but just out
> of curiosity - could you explain that a bit ...or give an example? :)

Sure.  From my html.g file:

OTABLE	
	:	"<table" (WS (ATTR)*)? '>'
	;

protected
ATTR
options {
ignore=WS;
}
	:	WORD ('=' (WORD ('%')? | ('-')? INT | STRING | HEXNUM))?
	;


I.e., call a lexer rule.  Modal programming is usually considered  
poor form ;)  That OTABLE rule is asking for multiple ATTR  
assignments for that tag.  It's a nested structure :)

This ability is unique to context-free lexers such as the scannerless  
LR style parsers and ANTLR LL lexers. :)  Lex modes exist to make up  
for the weakness of not being able to call another lexer rule.  In a  
parser, are you going to switch modes to match the formal arguments  
of a method definition?  Nope.  Same in the lexer for HTML tag  
arguments. :)

Ter


More information about the antlr-interest mailing list