⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 expr.atg

📁 COCO類似C的編譯器
💻 ATG
字号:
$C   /* Generate Main Module */
COMPILER Expr
/* This is a simple expression calculator */

/*----------------- Scanner Specifications ----------------------*/

CHARACTERS
    letter = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".
    digit  = "0123456789".
    tab    = CHR(9).
    eol    = CHR(10).

TOKENS
    ident   =  letter {letter|digit}.
    number  =  digit  { digit }.

IGNORE eol+tab

COMMENTS FROM "--" TO eol

/*--------------------Parser Specification -------------------*/

PRODUCTIONS
    Expr = StatSeq .

    StatSeq = SYNC { Stat ";" SYNC} .

    Stat =                          (. int value; .)
       Expression<&value>           (. printf("%d\n", value); .)
      .

    Expression<int *result> =       (. int result1, result2; .)
       Term<&result1>
       {   "+" Term<&result2>       (. result1 += result2; .)
	 | "-" Term<&result2>       (. result1 -= result2; .)
       }                            (. *result = result1; .)
       .

    Term<int *result> =             (. int result1, result2; .)
       Factor<&result1>
       {   '*' Factor<&result2>     (. result1 *= result2; .)
	 | '/' Factor<&result2>     (. result1 /= result2; .)
       }                            (. *result = result1; .)
       .

    Factor<int *result> =           (. int sign = 1; .)
      [ "-"                         (. sign =- 1; .)
      ]
      (   Number<result>
	| "(" Expression<result> ")"
      )                            (. *result *= sign; .)
      .

   Number<int *result>
	=                          (. char buff[20]; .)
	number                     (. S_GetString(S_Pos, S_Len, buff, sizeof(buff) - 1);
				      *result = atoi(buff); .)
	.

/*
   Ident<char *name>
	=
	ident                      (. S_GetString(S_Pos, S_Len, name, 20); .)
	.
*/

END Expr.

⌨️ 快捷键说明

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