calc.g

来自「Java写的词法/语法分析器。可生成JAVA语言或者是C++的词法和语法分析器。」· G 代码 · 共 61 行

G
61
字号
/** This example demonstrates the heterogeneous tree construction *  mechanism.  Compare this example to examples/calc/calc.g *  to see that I use tree node methods not a tree walker to compute *  the result. */class CalcParser extends Parser;options {	buildAST = true;	// uses CommonAST by default}// define a bunch of specific AST nodes to build.// can override at actual reference of tokens in grammar// below.tokens {	PLUS<AST=PLUSNode>;	STAR<AST=MULTNode>;}expr	:	mexpr (PLUS^ mexpr)* SEMI!	;mexpr	:	atom (STAR^ atom)*	;atom:	INT<AST=INTNode>	// could have done in tokens{} section	;class CalcLexer extends Lexer;WS	:	(' '	|	'\t'	|	'\n'	|	'\r')		{ _ttype = Token.SKIP; }	;LPAREN:	'('	;RPAREN:	')'	;STAR:	'*'	;PLUS:	'+'	;SEMI:	';'	;protectedDIGIT	:	'0'..'9'	;INT	:	(DIGIT)+	;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?