How should I add arbitrary type names to this grammar?

21 views
Skip to first unread message

Matti Viljamaa

unread,
May 13, 2017, 10:06:18 AM5/13/17
to antlr-di...@googlegroups.com
How should I add arbitrary type names to this grammar?

I have the problem that adding it the way I'd expect to do it breaks something in the grammar and I'm not sure what breaks.

What I currently have is the rule:

TYPE : 'Int' | 'String' | 'Bool' | 'Object';

and when I change this to

TYPE: 'Int' | 'String' | 'Bool' | 'Object' | ID;

which is followed by:

ID
     : LetterOrDigit
     ;

fragment
LetterOrDigit
    :   [a-zA-Z]+ [0-9$_]* // these are the "java letters or digits" below 0x7F
    |   // covers all characters above 0x7F which are not a surrogate
        ~[\u0000-\u007F\uD800-\uDBFF]
    |   // covers UTF-16 surrogate pairs encodings for U+10000 to U+10FFFF
        [\uD800-\uDBFF] [\uDC00-\uDFFF]
;

Then the parser seems to stop recognizing normal IDs and will not reach the TYPE rule either.

Before the TYPE-rule I merely have the Cool language grammar, like:

grammar CoolGrammar;

s : program;
program : (classdef';')+ EOF;
classdef : 'class ' TYPE ( ' inherits ' TYPE )? '{' ( feature ';' )* '}';
feature : ID '(' (formal (',' formal)* ) ')' ':' TYPE '{' expr '}' | ID ':' TYPE ('<-' expr)?;
expr : ID '<-' expr
     | expr ('@' TYPE)? '.' ID '(' (expr (',' expr)*)? ')'
     | ID '(' expr (',' expr*)? ')'
     | 'if ' expr ' then ' expr ' else ' expr ' fi'
     | 'while ' expr ' loop ' expr ' pool'
     | '{' (expr ';')+ '}'
     | 'let ' formal ('<-' expr)? (',' formal ('<-' expr)? )* ' in ' expr
     | 'case ' expr ' of ' (formal ' => ' expr ';')+ ' esac'
     | 'new ' TYPE
     | 'isvoid ' expr
     | expr '+' expr
     | expr '-' expr
     | expr '*' expr
     | expr '/' expr
     | '~' expr
     | expr '<' expr
     | expr '<=' expr
     | expr '=' expr
     | 'not ' expr
     | '(' expr ')'
     | ID
     | Integer
     | String
     | 'true'
     | 'false';
formal : ID ':' TYPE;
/*
These are special strings that must be matched before
the more general ones below.
*/
TRUETK: 'true';
FALSETK: 'false';

TYPE : ...

Any idea what's causing the problem?

Using the following example program:

class Int {
    c ( d : Int) : String { not 2 };
};

the error is:

line 2:4 mismatched input 'c' expecting {<INVALID>, '}'}
line 3:0 extraneous input '}' expecting {<EOF>, 'class '}

Matti Viljamaa

unread,
May 13, 2017, 10:24:53 AM5/13/17
to antlr-discussion
Perhaps I need to split them like in the following Java.g4 example:

https://github.com/antlr/grammars-v4/blob/master/java/Java.g4#L249

Matti Viljamaa

unread,
May 13, 2017, 11:45:57 AM5/13/17
to antlr-discussion
Or perhaps it's meaningless to distuingish between ID and TYPE?
Reply all
Reply to author
Forward
0 new messages