1 Star 2 Fork 0

WXQ/TMCC

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
tiny_min_c.g4 3.41 KB
一键复制 编辑 原始数据 按行查看 历史
WXQ 提交于 2023-10-02 17:28 . First Commit, Version 0.1.0
/*------------------------------------------------------------------------------
- Copyright (c) 2023 Xianqing Wang at XiDian University.
- [XD-TMCC] is licensed under Mulan PSL v2.
-
- You can use this software according to the terms and conditions of the Mulan PSL v2.
- You may obtain a copy of Mulan PSL v2 at:
- http://license.coscl.org.cn/MulanPSL2
- THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
- EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
- MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
-
- See the Mulan PSL v2 for more details.
-----------------------------------------------------------------------------*/
/*
* Grammar file for TINY-MIN-C.
*
* In this grammar, there only one LL(2) production, expression,
* other productions are LL(1).
*
* Author: WXQ#XDU
* Date: 2023.09
*/
grammar tiny_min_c;
//
// Syntax Rules
//
// production 1, LL(1)
program : statement + EOF
;
// production 2~5, LL(1)
statement :
selectionStatement
| iterationStatement
| compoundStatement
| expressionStatement
;
// production 6, LL(1)
selectionStatement :
'if' '(' expression ')' statement ( 'else' statement ) ?
;
// production 7, LL(1)
iterationStatement : 'while' '(' expression ')' statement
;
// production 8, LL(1)
compoundStatement : '{' statement* '}'
;
// production 9, LL(1)
// FIRST(expressionStatement) = FIRST(expression) + { ';' }
// = { ID, NUM, (, +, -, ; }
expressionStatement : expression ? ';'
;
// production 10~11, LL(2)
// FIRST(expression) = { ID, NUM, (, +, - }
expression :
relationalExpression // if LA_1 is ID, LA_2 is not '='
| assignmentExpression
;
// production 12, LL(1)
// FIRST(assignmentExpression) = { ID }
assignmentExpression :
TK_ID '=' expression
;
// production 13, LL(1)
// FIRST(relationalExpression) = { ID, NUM, (, +, - }
relationalExpression :
additiveExpression ('<' additiveExpression)?
;
// production 14, LL(1)
// FIRST(additiveExpression) = { ID, NUM, (, +, - }
additiveExpression :
multiplicativeExpression ( ('+' | '-') multiplicativeExpression )*
;
// production 15, LL(1)
// FIRST(multiplicativeExpression) = FIRST(unaryExpression)
// = { ID, NUM, (, +, - }
multiplicativeExpression :
unaryExpression ( ('*' | '/') unaryExpression )*
;
// production 16, LL(1)
// FIRST(unaryExpression) = { ID, NUM, (, +, - }
unaryExpression :
('+' | '-')? primaryExpression
;
// production 17~19, LL(1)
// FIRST(primaryExpression) = { ID, NUM, ( }
primaryExpression :
TK_ID
| TK_NUM
| '(' expression ')'
;
//
// Lexical Rules, partial
//
// key words that supported
TK_KW_IF : 'if' ;
TK_KW_ELSE : 'else' ;
TK_KW_WHILE : 'while' ;
TK_ID : [a-zA-Z_][a-zA-Z_0-9]*
;
TK_NUM : [0-9]+ ('.' [0-9]+)? ([eE][+-][0-9]+)?
;
// Separators
TK_LPAREN : '(' ;
TK_RPAREN : ')' ;
TK_LBRACE : '{' ;
TK_RBRACE : '}' ;
TK_SEMI : ';' ;
// Operators
TK_OP_ASSIGN: '=' ;
TK_OP_LT : '<' ;
TK_OP_LE : '<=' ;
TK_OP_GT : '>' ;
TK_OP_GE : '>=' ;
TK_OP_EQ : '==' ;
TK_OP_NE : '!=' ;
// operators on integer / real number
TK_OP_PLUS : '+' ;
TK_OP_MINUS : '-' ;
TK_OP_MUL : '*' ;
TK_OP_DIV : '/' ;
// discard line comments in lexer
TK_COMMENT : '//' [^\r\n]* -> skip
;
// discard white spaces in lexer
WS : [ \r\n\t]+ -> skip
;
// END of FILE
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/hapywxq/tmcc.git
git@gitee.com:hapywxq/tmcc.git
hapywxq
tmcc
TMCC
master

搜索帮助

D67c1975 1850385 1daf7b77 1850385