|
| 1 | +# C- |
| 2 | +`C MINUS`是C语言的一个子集,该语言的语法在《编译原理与实践》第九章附录中有详细的介绍。 |
| 3 | +## Lexical Conventions |
| 4 | +1. 关键字 |
| 5 | +`else if int return void while` |
| 6 | +2. 专用符号 |
| 7 | +`+ - * / < <= > >= == != = ; , ( ) [ ] { } /* */` |
| 8 | +3. 标识符ID和整数NUM,通过下列正则表达式定义: |
| 9 | +`ID=letter letter*` |
| 10 | +`NUM=digit digit*` |
| 11 | +`letter = a|...|z|A|...|Z` |
| 12 | +`digit = 0|...|9` |
| 13 | + |
| 14 | +4. 注释用`/*...*/`表示,可以超过一行。注释不能嵌套。 |
| 15 | +`/*...*/` |
| 16 | + |
| 17 | +> 思考 |
| 18 | +> 1. 识别C-语言Token的DFA设计 |
| 19 | +> 2. note that: [, ], 和 [] 是三种不同的tokens。[]用于声明数组类型,[]中间不得有空格。 |
| 20 | +
|
| 21 | +## Syntax |
| 22 | +1. program → declaration-list |
| 23 | +2. declaration-list → declaration-list declaration | declaration |
| 24 | +3. declaration → var-declaration | fun-declaration |
| 25 | +4. var-declaration → type-specifier `ID` ; | type-specifier `ID` `[` `NUM` `]`; |
| 26 | +5. type-specifier → `int` | `void` |
| 27 | +6. fun-declaration → type-specifier `ID` `(`params`)` compound-stmt |
| 28 | +7. params → param-list | `void` |
| 29 | +8. param-list→ param-list , param | param |
| 30 | +9. param → type-specifier `ID` | type-specifier `ID` `[]` |
| 31 | +10. compound-stmt → `{` local-declarations statement-list `}` |
| 32 | +11. local-declarations → local-declarations var-declaration | empty |
| 33 | +12. statement-list → statement-list statement | empty |
| 34 | +13. statement → expression-stmt | compound-stmt| selection-stmt |
| 35 | +| iteration-stmt | return-stmt |
| 36 | +14. expression-stmt → expression ; | ; |
| 37 | +15. selection-stmt → `if` `(` expression `)` statement | `if` `(` expression `)` statement `else` statement |
| 38 | +16. iteration-stmt → `while` `(` expression `)` statement |
| 39 | +17. return-stmt → `return` ; | `return` expression ; |
| 40 | +18. expression → var `=` expression | simple-expression |
| 41 | +19. var → `ID` | `ID` `[` expression `]` |
| 42 | +20. simple-expression → additive-expression relop additive- expression | additive-expression |
| 43 | +21. relop → `<=` | `<` | `>` | `>=` | `==` | `!=` |
| 44 | +22. additive-expression → additive-expression addop term | term |
| 45 | +23. addop → `+` | `-` |
| 46 | +24. term → term mulop factor | factor |
| 47 | +25. mulop → `*` | `/` |
| 48 | +26. factor → `(` expression `)` | var | call | `NUM` |
| 49 | +27. call → `ID` `(` args `)` |
| 50 | +28. args → arg-list | empty |
| 51 | +29. arg-list → arg-list , expression | expression |
| 52 | + |
| 53 | +> 思考: |
| 54 | +> 1. C-语言语法的特点,它的CFG |
| 55 | +
|
| 56 | +# Sample Programs of C- |
| 57 | +```c |
| 58 | +int gcd (int u, int v) { /* calculate the gcd of u and v */ |
| 59 | + if (v == 0) return u; |
| 60 | + else return gcd(v, u - u / v * v); /* v,u-u/v*v is equals to u mod v*/ |
| 61 | +} |
| 62 | +int main() { |
| 63 | + int x; int y; int temp; |
| 64 | + x = 72; |
| 65 | + y = 18; |
| 66 | + if (x<y) { |
| 67 | + temp = x; |
| 68 | + x = y; |
| 69 | + y = temp; |
| 70 | + } |
| 71 | + gcd(x,y); |
| 72 | + return 0; |
| 73 | +} |
| 74 | +``` |
0 commit comments