[antlr-interest] writing a simple tool

Edwards, Waverly Waverly.Edwards at genesys.com
Wed Jan 16 02:58:34 PST 2008


I am currently attempting to write what I hope is a simple tool to
validate an ebnf grammar and export the first and follow set.
Unfortunately, no amount of reading has enabled perform the 'easy' (to
others) task of generating this first and follow set.  I have read
multiple books and texts on the subject.  So I've embarked on a mission
to start by first validating an ebnf grammar and moving forward from
there.

My first stumbling block came early in writing an ebnf grammar.  In my
effort to separate my parser rules so I can apply some type of action I
have created recursive definitions.  I know what they are but I've been
unable figure out how to dig myself out of this hole.

Any and all help will be greatly appreciated.


W.


grammar ebnf;


options {
	k = 1; // force myself to keep grammar LL(1)
}


/*
REFERENCE

www.cs.bris.ac.uk/Teaching/Resources/COMS30122/lectures/02_Scanning.p.pd
f 

Grammar symbols
Here is a 'standard' set of symbols for this unit
The first four are BNF, the rest EBNF

Symol or Expr        ,// MEANING

::                    // is (is defined as)
&                     // then (followed by, always implicit)
|                     // OR 
;                     // end of definition 
?, '[' expression ']' // optional
*, '{' expression '}' // zero or more times
+                     // one or more times
'(' expression ')'    // bracketing (subrule)
 
NOT GOING TO BE IMPLEMENTED ( & )
*/


fragment
HEXDIGIT     :  '0' .. '9' | 'a' .. 'f' | 'A' .. 'F' ;

fragment
CHAR         :  ASCII_SET | ESC_CHAR | HEX_CHAR;

fragment
ASCII_SET    :  '\u0020'..'\u00FF';

DIGIT        :	'0'..'9' ;
LETTER       :	'a'..'z' | 'A'..'Z' ;
ID           :	( LETTER | '_' ) ( LETTER | DIGIT | '_' )* ;
INTEGER      :	DIGIT (DIGIT)* ;	
HEX_CHAR     :  '\\x' HEXDIGIT HEXDIGIT ( HEXDIGIT HEXDIGIT ) ?;  // two
or four digit hex, ASCII or UNICODE
ESC_CHAR     :  '\\' ('\'' | '\\' | 'a' | 'b' | 'n' | 'r' | 't');
LINE_COMMENT : ('//') ~('\n'|'\r')* '\r'? '\n' {$channel=HIDDEN;} ;


start_bnf    :    rule * ;
rule         :    ID ( '=' | '::' ) expression ';' ;
expression   :    term ( '|' term )* ;
term         :    factor  (factor)* ;

//factor       :    ID | literal | range | '(' expression ')' | '{'
expression '}' | '[' expression ']' | expression '+' ;

factor       :    ID | literal | range | subrule | zeroOrMore  |
optional | oneOrMore ;
subrule      :   '(' expression ')';
zeroOrMore   :   '{' expression '}' | expression '*'  ;
optional     :   '[' expression ']' | ( expression '?' );
oneOrMore    :   expression '+';
literal      :    '\''  CHAR * '\'' ;
range        :    '\'' CHAR '\'' '..' '\'' CHAR '\'' ;



More information about the antlr-interest mailing list