The C minus grammar's antlr's edition

53 views
Skip to first unread message

Gao wei

unread,
Aug 23, 2005, 9:40:25 AM8/23/05
to SDL Eclipse Plugin
/* C-Minus BNF Grammar */

%token ELSE
%token IF
%token INT
%token RETURN
%token VOID
%token WHILE

%token ID
%token NUM

%token LTE
%token GTE
%token EQUAL
%token NOTEQUAL
%%

program : declaration_list ;


declaration_list : declaration_list declaration | declaration ;

declaration : var_declaration | fun_declaration ;

var_declaration : type_specifier ID ';'
| type_specifier ID '[' NUM ']' ';' ;

type_specifier : INT | VOID ;

fun_declaration : type_specifier ID '(' params ')' compound_stmt ;

params : param_list | VOID ;

param_list : param_list ',' param
| param ;

param : type_specifier ID | type_specifier ID '[' ']' ;

compound_stmt : '{' local_declarations statement_list '}' ;

local_declarations : local_declarations var_declaration
| /* empty */ ;

statement_list : statement_list statement
| /* empty */ ;

statement : expression_stmt
| compound_stmt
| selection_stmt
| iteration_stmt
| return_stmt ;

expression_stmt : expression ';'
| ';' ;

selection_stmt : IF '(' expression ')' statement
| IF '(' expression ')' statement ELSE statement ;

iteration_stmt : WHILE '(' expression ')' statement ;

return_stmt : RETURN ';' | RETURN expression ';' ;

expression : var '=' expression | simple_expression ;

var : ID | ID '[' expression ']' ;

simple_expression : additive_expression relop additive_expression
| additive_expression ;

relop : LTE | '<' | '>' | GTE | EQUAL | NOTEQUAL ;

additive_expression : additive_expression addop term | term ;

addop : '+' | '-' ;

term : term mulop factor | factor ;

mulop : '*' | '/' ;

factor : '(' expression ')' | var | call | NUM ;

call : ID '(' args ')' ;

args : arg_list | /* empty */ ;

arg_list : arg_list ',' expression | expression ;

其他都没有什么特别的地方,只是token要决定写在那里,然后就是决定如果使用ast的生成的话,需要决定在什么位置添加^符号和!符号

rock...@gmail.com

unread,
Aug 24, 2005, 7:30:29 AM8/24/05
to SDL Eclipse Plugin
我在想要将这个内容修改成为给予antlr的语法格式的时候,发现他没有定义ID和NUM。这个是从网上找到的一个关于c-minus的yacc的语法文件,网址为:
http://rkirkpat.net/school/cminus.y

yacc中是不是由预定义ID和NUM还是应该另外有一个lex的定义文件,这个文件不完整?

Gao wei

unread,
Aug 24, 2005, 7:55:52 AM8/24/05
to SDL Eclipse Plugin
http://snow.sierranevada.edu/~csci/ExamplesX/C-Syntax.doc
这是一个更好版本的c-minus的EBNF语法说明,终结符使用粗体表示。
我想从这里面将其转化为antlr的语法会更简单一些,只是不知道转化完了可以怎么办。

这个是完全的资料的列表
http://snow.sierranevada.edu/~csci/ExamplesX/00INDEX.txt

Gao wei

unread,
Aug 24, 2005, 8:10:11 AM8/24/05
to SDL Eclipse Plugin
Reply all
Reply to author
Forward
0 new messages