[antlr-interest] Changing fragments to rules...

John B. Brodie jbb at acm.org
Fri Jan 6 15:20:32 PST 2012


On 01/06/2012 04:52 PM, James Ladd wrote:
> Hi All,
>
> I have a grammar that interprets fine in antlrworks when it contains 'fragments'.
> When I move these into rules because I want to do actions the parsing fails with
> a no viable alternative. I'm really lost as to why - The syntax diagrams looks fine
> there are no error reported during code compilation.
>
> The output in the interpreter shows nodes program->primary->array_constant->array->'('->NoViableAlternative.
>
> This is my input:
>
> #(12.3 Abc + at:put: 'str' $c)
>
> *** Grammar before WITH FRAGMENTS: ***
>
> grammar Temp;
>
> options {
>      language = Java;
> }
> @lexer::members {
>    List<RecognitionException>  exceptions = new ArrayList<RecognitionException>();
>    public List<RecognitionException>  getExceptions() { return exceptions; }
>    public void reportError(RecognitionException e) { super.reportError(e); exceptions.add(e); }
> }
>
> program
>    : primary* WHITESPACE?
>    ;
>
> primary returns [Primary primary]
>    : WHITESPACE?
>      ( IDENTIFIER {primary = new Identifier($IDENTIFIER.text, $IDENTIFIER.line);}
>      | NUMBER {primary = new Number($NUMBER.text, $NUMBER.line);}
>      | SYMBOL_CONSTANT {primary = new SymbolConstant($SYMBOL_CONSTANT.text.substring(1), $SYMBOL_CONSTANT.line);}
>      | CHARACTER_CONSTANT {primary = new CharacterConstant($CHARACTER_CONSTANT.text.substring(1), $CHARACTER_CONSTANT.line);}
>      | STRING {primary = new StringConstant($STRING.text, $STRING.line);}
>      | ARRAY_CONSTANT
>      )  // BLOCK | EXPRESSION
>    ;
>
> WHITESPACE:    (' '|'\t'|'\r'|'\n')+;
> COMMENT:        '"' .* '"' {$channel = HIDDEN;};
> BINARY_SELECTOR:    ('-' (SPECIAL_CHAR)?) | (SPECIAL_CHAR)+;
> KEYWORD:        IDENTIFIER ':';
> IDENTIFIER:        LETTER (LETTER | DIGIT)*;
> NUMBER:        ((NUMBER_LEFT)? ('-')? DIGITS (NUMBER_RIGHT_P1)? (NUMBER_RIGHT_P2)?);
> SYMBOL_CONSTANT:    '#' SYMBOL;
> CHARACTER_CONSTANT:    '$' ('\'' | '"' | SPECIAL_CHAR | NORMAL_CHAR | DIGIT | LETTER);
> STRING:        '\'' (~'\''|'\'\'')* '\'';
> ARRAY_CONSTANT:    '#' ARRAY;
>
> fragment ARRAY:        '(' (ARRAY_ELEMENT)* ')';
> fragment ARRAY_ELEMENT:    WHITESPACE | NUMBER | SYMBOL | STRING | CHARACTER_CONSTANT | ARRAY;
> fragment SYMBOL:        IDENTIFIER | BINARY_SELECTOR | (KEYWORD)+;
> fragment NUMBER_LEFT:        DIGITS 'r';
> fragment NUMBER_RIGHT_P1:    '.' DIGITS;
> fragment NUMBER_RIGHT_P2:    'e' ('-')? DIGITS;
> fragment LETTER:        ('a'..'z' | 'A'..'Z');
> fragment DIGIT:        '0'..'9';
> fragment DIGITS:        DIGIT+;
> fragment SPECIAL_CHAR:        '+'|'/'|'\\'|'*'|'~'|'<'|'>'|'='|'@'|'%'|'|'|'&'|'?'|'!'|',';
> fragment NORMAL_CHAR:        '['|']'|'{'|'}'|'('|')'|'^'|'_'|';'|'$'|'#'|':'|'.'|'\'';
>
>
> *** Grammar after with some fragments turned into rules: ***
>
> grammar Temp;
>
> options {
>      language = Java;
> }
> @lexer::members {
>    List<RecognitionException>  exceptions = new ArrayList<RecognitionException>();
>    public List<RecognitionException>  getExceptions() { return exceptions; }
>    public void reportError(RecognitionException e) { super.reportError(e); exceptions.add(e); }
> }
>
> program
>    : primary* WHITESPACE?
>    ;
>
> primary returns [Primary primary]
>    : WHITESPACE?
>      ( IDENTIFIER {primary = new Identifier($IDENTIFIER.text, $IDENTIFIER.line);}
>      | NUMBER {primary = new Number($NUMBER.text, $NUMBER.line);}
>      | SYMBOL_CONSTANT {primary = new SymbolConstant($SYMBOL_CONSTANT.text.substring(1), $SYMBOL_CONSTANT.line);}
>      | CHARACTER_CONSTANT {primary = new CharacterConstant($CHARACTER_CONSTANT.text.substring(1), $CHARACTER_CONSTANT.line);}
>      | STRING {primary = new StringConstant($STRING.text, $STRING.line);}
>      | array_constant
>      )  // BLOCK | EXPRESSION
>    ;
>
>   array_constant
>    : '#' array
>    ;
>
> array
>    :    '(' (ARRAY_ELEMENT)* ')'
>    ;
>        

ARRAY_ELEMENT is a fragment so the parser will never see it.

> WHITESPACE:    (' '|'\t'|'\r'|'\n')+;
> COMMENT:        '"' .* '"' {$channel = HIDDEN;};
> BINARY_SELECTOR:    ('-' (SPECIAL_CHAR)?) | (SPECIAL_CHAR)+;
> KEYWORD:        IDENTIFIER ':';
> IDENTIFIER:        LETTER (LETTER | DIGIT)*;
> NUMBER:        ((NUMBER_LEFT)? ('-')? DIGITS (NUMBER_RIGHT_P1)? (NUMBER_RIGHT_P2)?);
> SYMBOL_CONSTANT:    '#' SYMBOL;
> CHARACTER_CONSTANT:    '$' ('\'' | '"' | SPECIAL_CHAR | NORMAL_CHAR | DIGIT | LETTER);
> STRING:        '\'' (~'\''|'\'\'')* '\'';
>
> fragment ARRAY_ELEMENT:        WHITESPACE | NUMBER | SYMBOL | STRING | CHARACTER_CONSTANT;
> fragment SYMBOL:        IDENTIFIER | BINARY_SELECTOR | (KEYWORD)+;
> fragment NUMBER_LEFT:        DIGITS 'r';
> fragment NUMBER_RIGHT_P1:    '.' DIGITS;
> fragment NUMBER_RIGHT_P2:    'e' ('-')? DIGITS;
> fragment LETTER:        ('a'..'z' | 'A'..'Z');
> fragment DIGIT:        '0'..'9';
> fragment DIGITS:        DIGIT+;
> fragment SPECIAL_CHAR:        '+'|'/'|'\\'|'*'|'~'|'<'|'>'|'='|'@'|'%'|'|'|'&'|'?'|'!'|',';
> fragment NORMAL_CHAR:        '['|']'|'{'|'}'|'('|')'|'^'|'_'|';'|'$'|'#'|':'|'.'|'\'';
>
>
>
>   		 	   		
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address



More information about the antlr-interest mailing list