我正在尝试制作像 C 一样的编译器。我将我的语法输入到 antlrWorks IDE 中,它没有显示错误,但是当我想查看我的规则的语法图时,它抱怨“无法显示规则 'xxxxx',因为启动状态不是成立”。我没有找到在示例上定义开始符号的任何特殊方法。我把我的语法放在这里可能有人可以帮助我:
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();}
;