0
votes

I am trying to make a compiler like a C .I entered my grammar into antlrWorks IDE and it show no error and but when I want to see the syntax diagram of my rules it complained that " cannot display rule 'xxxxx' because start state not found". I didn't find any special way for defining start symbol on the examples.I put my grammar here may be somebody can help me:

grammar MiniC;

tokens {
    GET='get';
    PUT='put';
    CHANGE='change';
    DATA='data';
    METADATA='metadata';
    DEPENDENCIES='dependencies';
    DEPENDENTS='dependents';
    STATISTICS='statistics';
    FROM='from';
    IN='in';
    ABOUT='about';
    OF='of';
    MAIN='main()';
    ID = 'ID';
    SEMI = ';';
    INT ='int';
    VOID = 'void';
    BOOL ='BOOL';
    FLOAT = 'FLOAT';
    IF = 'if';
    ELSE = 'else';
    RETURN = 'return';
    BREAK ='break';
    WHILE ='while';
};

program
    :    MAIN compound_stmt
    ;

compound_stmt
    :
    '{' local_declarations stmt_list'}'
    ;

local_declarations
    :
    () (var_declarations)*
    ;

var_declarations
    :
    type_specifier ID SEMI 
    |
    type_specifier ID [('0'..'9')*]
    ;

type_specifier 
    :   
    'int'
    |
    'void'
    |
    'bool'
    |
    'float'     
    ;

stmt_list 
    :
    (stmt)*
    ;   

stmt : 
    expression_stmt
    |
    compound_stmt
    |
    selection_stmt
    |
    iteration_stmt
    |
    return_stmt
    |
    break_stmt
    ;

selection_stmt 
    :   IF('('expression')') compound_stmt
        |
        IF('('expression')') compound_stmt ELSE compound_stmt
        ;
iteration_stmt 
    :
        WHILE ('('expression')') compound_stmt; 

return_stmt 
    :
        RETURN SEMI|RETURN expression;

break_stmt 
    :   BREAK;

expression_stmt 
    :
        expression; 

expression
    :   var '=' simple_expression|simple_expression;

simple_expression 
    :
        operand operator operand;
operand :
    ('1'..'9')
    |
    ;

var     :
        ID|ID[('1'..'9')*];

operator 
    :       
    RelOp|LogicOp|ArithOp;

RelOp   :   
    '<='|'<'|'>='|'>'|'!='|'==' ;
LogicOp :   
    '&&'|'||'
    ;

ArithOp :   
    '+'|'-'|'*'|'/'|'%';    


WS
    :) (' '|'\t'|'\n'|'\r')+ {skip();}
    ; 
1
The grammar is far from "true", and I really don't believe ANTLRWorks does not complain when generating a lexer and parser. While glancing over your grammar, I see at least 4 errors. What version of ANTLRWorks are you using? Is it really the grammar you posted, or did you post a different version of it?Bart Kiers
@BartKiers, actually yes, this grammar contains a lot of errors. But the error in AntlrWorks 'cannot display rule 'x....' is shown because of semicolon, which was placed after 'tokens {...}' description.Andremoniy
well, that's my point: the grammar is not 'true'... Did you resolve it now?Bart Kiers
no, until now I didn't find my mistakes in grammar. I omit semicolon as Andremonity said but antlrworks show no error .I am using antlr 3.5-rc-2 and antlrworks 1.5ali

1 Answers

0
votes

First of all, remove semicolon ( ; ) after

tokens {
    ...
} *;*

So, it must be just:

tokens {
    ...
}

This is the main reason why AntlrWorks says "cannot display rule 'xxxxx' because start state not found" (I checked this). But after this correction you will probably see several new errors in your grammar, which are actually present, but this is already an another question.