免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12345下一页
最近访问板块 发新帖
查看: 33647 | 回复: 40

C语言的词法与语法 [复制链接]

论坛徽章:
0
发表于 2007-01-24 14:22 |显示全部楼层
来自网络,回到网络
全新的,没看过

  1. D                       [0-9]
  2. L                       [a-zA-Z_]
  3. H                       [a-fA-F0-9]
  4. E                       [Ee][+-]?{D}+
  5. FS                      (f|F|l|L)
  6. IS                      (u|U|l|L)*

  7. %{
  8. #include <stdio.h>
  9. #include "y.tab.h"

  10. void count();
  11. %}

  12. %%
  13. "/*"                    { comment(); }

  14. "auto"                  { count(); return(AUTO); }
  15. "break"                 { count(); return(BREAK); }
  16. "case"                  { count(); return(CASE); }
  17. "char"                  { count(); return(CHAR); }
  18. "const"                 { count(); return(CONST); }
  19. "continue"              { count(); return(CONTINUE); }
  20. "default"               { count(); return(DEFAULT); }
  21. "do"                    { count(); return(DO); }
  22. "double"                { count(); return(DOUBLE); }
  23. "else"                  { count(); return(ELSE); }
  24. "enum"                  { count(); return(ENUM); }
  25. "extern"                { count(); return(EXTERN); }
  26. "float"                 { count(); return(FLOAT); }
  27. "for"                   { count(); return(FOR); }
  28. "goto"                  { count(); return(GOTO); }
  29. "if"                    { count(); return(IF); }
  30. "int"                   { count(); return(INT); }
  31. "long"                  { count(); return(LONG); }
  32. "register"              { count(); return(REGISTER); }
  33. "return"                { count(); return(RETURN); }
  34. "short"                 { count(); return(SHORT); }
  35. "signed"                { count(); return(SIGNED); }
  36. "sizeof"                { count(); return(SIZEOF); }
  37. "static"                { count(); return(STATIC); }
  38. "struct"                { count(); return(STRUCT); }
  39. "switch"                { count(); return(SWITCH); }
  40. "typedef"               { count(); return(TYPEDEF); }
  41. "union"                 { count(); return(UNION); }
  42. "unsigned"              { count(); return(UNSIGNED); }
  43. "void"                  { count(); return(VOID); }
  44. "volatile"              { count(); return(VOLATILE); }
  45. "while"                 { count(); return(WHILE); }

  46. {L}({L}|{D})*           { count(); return(check_type()); }

  47. 0[xX]{H}+{IS}?          { count(); return(CONSTANT); }
  48. 0{D}+{IS}?              { count(); return(CONSTANT); }
  49. {D}+{IS}?               { count(); return(CONSTANT); }
  50. L?'(\\.|[^\\'])+'       { count(); return(CONSTANT); }

  51. {D}+{E}{FS}?            { count(); return(CONSTANT); }
  52. {D}*"."{D}+({E})?{FS}?  { count(); return(CONSTANT); }
  53. {D}+"."{D}*({E})?{FS}?  { count(); return(CONSTANT); }

  54. L?\"(\\.|[^\\"])*\"     { count(); return(STRING_LITERAL); }

  55. "..."                   { count(); return(ELLIPSIS); }
  56. ">>="                   { count(); return(RIGHT_ASSIGN); }
  57. "<<="                   { count(); return(LEFT_ASSIGN); }
  58. "+="                    { count(); return(ADD_ASSIGN); }
  59. "-="                    { count(); return(SUB_ASSIGN); }
  60. "*="                    { count(); return(MUL_ASSIGN); }
  61. "/="                    { count(); return(DIV_ASSIGN); }
  62. "%="                    { count(); return(MOD_ASSIGN); }
  63. "&="                    { count(); return(AND_ASSIGN); }
  64. "^="                    { count(); return(XOR_ASSIGN); }
  65. "|="                    { count(); return(OR_ASSIGN); }
  66. ">>"                    { count(); return(RIGHT_OP); }
  67. "<<"                    { count(); return(LEFT_OP); }
  68. "++"                    { count(); return(INC_OP); }
  69. "--"                    { count(); return(DEC_OP); }
  70. "->"                    { count(); return(PTR_OP); }
  71. "&&"                    { count(); return(AND_OP); }
  72. "||"                    { count(); return(OR_OP); }
  73. "<="                    { count(); return(LE_OP); }
  74. ">="                    { count(); return(GE_OP); }
  75. "=="                    { count(); return(EQ_OP); }
  76. "!="                    { count(); return(NE_OP); }
  77. ";"                     { count(); return(';'); }
  78. ("{"|"<%")              { count(); return('{'); }
  79. ("}"|"%>")              { count(); return('}'); }
  80. ","                     { count(); return(','); }
  81. ":"                     { count(); return(':'); }
  82. "="                     { count(); return('='); }
  83. "("                     { count(); return('('); }
  84. ")"                     { count(); return(')'); }
  85. ("["|"<:")              { count(); return('['); }
  86. ("]"|":>")              { count(); return(']'); }
  87. "."                     { count(); return('.'); }
  88. "&"                     { count(); return('&'); }
  89. "!"                     { count(); return('!'); }
  90. "~"                     { count(); return('~'); }
  91. "-"                     { count(); return('-'); }
  92. "+"                     { count(); return('+'); }
  93. "*"                     { count(); return('*'); }
  94. "/"                     { count(); return('/'); }
  95. "%"                     { count(); return('%'); }
  96. "<"                     { count(); return('<'); }
  97. ">"                     { count(); return('>'); }
  98. "^"                     { count(); return('^'); }
  99. "|"                     { count(); return('|'); }
  100. "?"                     { count(); return('?'); }

  101. [ \t\v\n\f]             { count(); }
  102. .                       { /* ignore bad characters */ }

  103. %%

  104. yywrap()
  105. {
  106.         return(1);
  107. }


  108. comment()
  109. {
  110.         char c, c1;

  111. loop:
  112.         while ((c = input()) != '*' && c != 0)
  113.                 putchar(c);

  114.         if ((c1 = input()) != '/' && c != 0)
  115.         {
  116.                 unput(c1);
  117.                 goto loop;
  118.         }

  119.         if (c != 0)
  120.                 putchar(c1);
  121. }


  122. int column = 0;

  123. void count()
  124. {
  125.         int i;

  126.         for (i = 0; yytext[i] != '\0'; i++)
  127.         if (yytext[i] == '\n')
  128.                 column = 0;
  129.         else if (yytext[i] == '\t')
  130.                 column += 8 - (column % 8);
  131.         else
  132.                 column++;

  133.         ECHO;
  134. }


  135. int check_type()
  136. {
  137.         /*
  138.          * pseudo code --- this is what it should check
  139.          *
  140.          *       if (yytext == type_name)
  141.          *               return(TYPE_NAME);
  142.          *
  143.          *       return(IDENTIFIER);
  144.          */

  145.         /*
  146.          *       it actually will only return IDENTIFIER
  147.          */

  148.         return(IDENTIFIER);
  149. }

复制代码

论坛徽章:
0
发表于 2007-01-24 14:26 |显示全部楼层

  1. %token IDENTIFIER CONSTANT STRING_LITERAL SIZEOF
  2. %token PTR_OP INC_OP DEC_OP LEFT_OP RIGHT_OP LE_OP GE_OP EQ_OP NE_OP
  3. %token AND_OP OR_OP MUL_ASSIGN DIV_ASSIGN MOD_ASSIGN ADD_ASSIGN
  4. %token SUB_ASSIGN LEFT_ASSIGN RIGHT_ASSIGN AND_ASSIGN
  5. %token XOR_ASSIGN OR_ASSIGN TYPE_NAME

  6. %token TYPEDEF EXTERN STATIC AUTO REGISTER
  7. %token CHAR SHORT INT LONG SIGNED UNSIGNED FLOAT DOUBLE CONST VOLATILE VOID
  8. %token STRUCT UNION ENUM ELLIPSIS

  9. %token CASE DEFAULT IF ELSE SWITCH WHILE DO FOR GOTO CONTINUE BREAK RETURN

  10. %start translation_unit
  11. %%

  12. primary_expression
  13.         : IDENTIFIER
  14.         | CONSTANT
  15.         | STRING_LITERAL
  16.         | '(' expression ')'
  17.         ;

  18. postfix_expression
  19.         : primary_expression
  20.         | postfix_expression '[' expression ']'
  21.         | postfix_expression '(' ')'
  22.         | postfix_expression '(' argument_expression_list ')'
  23.         | postfix_expression '.' IDENTIFIER
  24.         | postfix_expression PTR_OP IDENTIFIER
  25.         | postfix_expression INC_OP
  26.         | postfix_expression DEC_OP
  27.         ;

  28. argument_expression_list
  29.         : assignment_expression
  30.         | argument_expression_list ',' assignment_expression
  31.         ;

  32. unary_expression
  33.         : postfix_expression
  34.         | INC_OP unary_expression
  35.         | DEC_OP unary_expression
  36.         | unary_operator cast_expression
  37.         | SIZEOF unary_expression
  38.         | SIZEOF '(' type_name ')'
  39.         ;

  40. unary_operator
  41.         : '&'
  42.         | '*'
  43.         | '+'
  44.         | '-'
  45.         | '~'
  46.         | '!'
  47.         ;

  48. cast_expression
  49.         : unary_expression
  50.         | '(' type_name ')' cast_expression
  51.         ;

  52. multiplicative_expression
  53.         : cast_expression
  54.         | multiplicative_expression '*' cast_expression
  55.         | multiplicative_expression '/' cast_expression
  56.         | multiplicative_expression '%' cast_expression
  57.         ;

  58. additive_expression
  59.         : multiplicative_expression
  60.         | additive_expression '+' multiplicative_expression
  61.         | additive_expression '-' multiplicative_expression
  62.         ;

  63. shift_expression
  64.         : additive_expression
  65.         | shift_expression LEFT_OP additive_expression
  66.         | shift_expression RIGHT_OP additive_expression
  67.         ;

  68. relational_expression
  69.         : shift_expression
  70.         | relational_expression '<' shift_expression
  71.         | relational_expression '>' shift_expression
  72.         | relational_expression LE_OP shift_expression
  73.         | relational_expression GE_OP shift_expression
  74.         ;

  75. equality_expression
  76.         : relational_expression
  77.         | equality_expression EQ_OP relational_expression
  78.         | equality_expression NE_OP relational_expression
  79.         ;

  80. and_expression
  81.         : equality_expression
  82.         | and_expression '&' equality_expression
  83.         ;

  84. exclusive_or_expression
  85.         : and_expression
  86.         | exclusive_or_expression '^' and_expression
  87.         ;

  88. inclusive_or_expression
  89.         : exclusive_or_expression
  90.         | inclusive_or_expression '|' exclusive_or_expression
  91.         ;

  92. logical_and_expression
  93.         : inclusive_or_expression
  94.         | logical_and_expression AND_OP inclusive_or_expression
  95.         ;

  96. logical_or_expression
  97.         : logical_and_expression
  98.         | logical_or_expression OR_OP logical_and_expression
  99.         ;

  100. conditional_expression
  101.         : logical_or_expression
  102.         | logical_or_expression '?' expression ':' conditional_expression
  103.         ;

  104. assignment_expression
  105.         : conditional_expression
  106.         | unary_expression assignment_operator assignment_expression
  107.         ;

  108. assignment_operator
  109.         : '='
  110.         | MUL_ASSIGN
  111.         | DIV_ASSIGN
  112.         | MOD_ASSIGN
  113.         | ADD_ASSIGN
  114.         | SUB_ASSIGN
  115.         | LEFT_ASSIGN
  116.         | RIGHT_ASSIGN
  117.         | AND_ASSIGN
  118.         | XOR_ASSIGN
  119.         | OR_ASSIGN
  120.         ;

  121. expression
  122.         : assignment_expression
  123.         | expression ',' assignment_expression
  124.         ;

  125. constant_expression
  126.         : conditional_expression
  127.         ;

  128. declaration
  129.         : declaration_specifiers ';'
  130.         | declaration_specifiers init_declarator_list ';'
  131.         ;

  132. declaration_specifiers
  133.         : storage_class_specifier
  134.         | storage_class_specifier declaration_specifiers
  135.         | type_specifier
  136.         | type_specifier declaration_specifiers
  137.         | type_qualifier
  138.         | type_qualifier declaration_specifiers
  139.         ;

  140. init_declarator_list
  141.         : init_declarator
  142.         | init_declarator_list ',' init_declarator
  143.         ;

  144. init_declarator
  145.         : declarator
  146.         | declarator '=' initializer
  147.         ;
复制代码

--------------------------------我是无敌分界线------------------------------------

论坛徽章:
0
发表于 2007-01-24 14:27 |显示全部楼层
字数有限制,接上文

  1. storage_class_specifier
  2.         : TYPEDEF
  3.         | EXTERN
  4.         | STATIC
  5.         | AUTO
  6.         | REGISTER
  7.         ;

  8. type_specifier
  9.         : VOID
  10.         | CHAR
  11.         | SHORT
  12.         | INT
  13.         | LONG
  14.         | FLOAT
  15.         | DOUBLE
  16.         | SIGNED
  17.         | UNSIGNED
  18.         | struct_or_union_specifier
  19.         | enum_specifier
  20.         | TYPE_NAME
  21.         ;

  22. struct_or_union_specifier
  23.         : struct_or_union IDENTIFIER '{' struct_declaration_list '}'
  24.         | struct_or_union '{' struct_declaration_list '}'
  25.         | struct_or_union IDENTIFIER
  26.         ;

  27. struct_or_union
  28.         : STRUCT
  29.         | UNION
  30.         ;

  31. struct_declaration_list
  32.         : struct_declaration
  33.         | struct_declaration_list struct_declaration
  34.         ;

  35. struct_declaration
  36.         : specifier_qualifier_list struct_declarator_list ';'
  37.         ;

  38. specifier_qualifier_list
  39.         : type_specifier specifier_qualifier_list
  40.         | type_specifier
  41.         | type_qualifier specifier_qualifier_list
  42.         | type_qualifier
  43.         ;

  44. struct_declarator_list
  45.         : struct_declarator
  46.         | struct_declarator_list ',' struct_declarator
  47.         ;

  48. struct_declarator
  49.         : declarator
  50.         | ':' constant_expression
  51.         | declarator ':' constant_expression
  52.         ;

  53. enum_specifier
  54.         : ENUM '{' enumerator_list '}'
  55.         | ENUM IDENTIFIER '{' enumerator_list '}'
  56.         | ENUM IDENTIFIER
  57.         ;

  58. enumerator_list
  59.         : enumerator
  60.         | enumerator_list ',' enumerator
  61.         ;

  62. enumerator
  63.         : IDENTIFIER
  64.         | IDENTIFIER '=' constant_expression
  65.         ;

  66. type_qualifier
  67.         : CONST
  68.         | VOLATILE
  69.         ;

  70. declarator
  71.         : pointer direct_declarator
  72.         | direct_declarator
  73.         ;

  74. direct_declarator
  75.         : IDENTIFIER
  76.         | '(' declarator ')'
  77.         | direct_declarator '[' constant_expression ']'
  78.         | direct_declarator '[' ']'
  79.         | direct_declarator '(' parameter_type_list ')'
  80.         | direct_declarator '(' identifier_list ')'
  81.         | direct_declarator '(' ')'
  82.         ;

  83. pointer
  84.         : '*'
  85.         | '*' type_qualifier_list
  86.         | '*' pointer
  87.         | '*' type_qualifier_list pointer
  88.         ;

  89. type_qualifier_list
  90.         : type_qualifier
  91.         | type_qualifier_list type_qualifier
  92.         ;


  93. parameter_type_list
  94.         : parameter_list
  95.         | parameter_list ',' ELLIPSIS
  96.         ;

  97. parameter_list
  98.         : parameter_declaration
  99.         | parameter_list ',' parameter_declaration
  100.         ;

  101. parameter_declaration
  102.         : declaration_specifiers declarator
  103.         | declaration_specifiers abstract_declarator
  104.         | declaration_specifiers
  105.         ;

  106. identifier_list
  107.         : IDENTIFIER
  108.         | identifier_list ',' IDENTIFIER
  109.         ;

  110. type_name
  111.         : specifier_qualifier_list
  112.         | specifier_qualifier_list abstract_declarator
  113.         ;

  114. abstract_declarator
  115.         : pointer
  116.         | direct_abstract_declarator
  117.         | pointer direct_abstract_declarator
  118.         ;

  119. direct_abstract_declarator
  120.         : '(' abstract_declarator ')'
  121.         | '[' ']'
  122.         | '[' constant_expression ']'
  123.         | direct_abstract_declarator '[' ']'
  124.         | direct_abstract_declarator '[' constant_expression ']'
  125.         | '(' ')'
  126.         | '(' parameter_type_list ')'
  127.         | direct_abstract_declarator '(' ')'
  128.         | direct_abstract_declarator '(' parameter_type_list ')'
  129.         ;

  130. initializer
  131.         : assignment_expression
  132.         | '{' initializer_list '}'
  133.         | '{' initializer_list ',' '}'
  134.         ;

  135. initializer_list
  136.         : initializer
  137.         | initializer_list ',' initializer
  138.         ;

  139. statement
  140.         : labeled_statement
  141.         | compound_statement
  142.         | expression_statement
  143.         | selection_statement
  144.         | iteration_statement
  145.         | jump_statement
  146.         ;

  147. labeled_statement
  148.         : IDENTIFIER ':' statement
  149.         | CASE constant_expression ':' statement
  150.         | DEFAULT ':' statement
  151.         ;

  152. compound_statement
  153.         : '{' '}'
  154.         | '{' statement_list '}'
  155.         | '{' declaration_list '}'
  156.         | '{' declaration_list statement_list '}'
  157.         ;

  158. declaration_list
  159.         : declaration
  160.         | declaration_list declaration
  161.         ;

  162. statement_list
  163.         : statement
  164.         | statement_list statement
  165.         ;

  166. expression_statement
  167.         : ';'
  168.         | expression ';'
  169.         ;

  170. selection_statement
  171.         : IF '(' expression ')' statement
  172.         | IF '(' expression ')' statement ELSE statement
  173.         | SWITCH '(' expression ')' statement
  174.         ;

  175. iteration_statement
  176.         : WHILE '(' expression ')' statement
  177.         | DO statement WHILE '(' expression ')' ';'
  178.         | FOR '(' expression_statement expression_statement ')' statement
  179.         | FOR '(' expression_statement expression_statement expression ')' statement
  180.         ;

  181. jump_statement
  182.         : GOTO IDENTIFIER ';'
  183.         | CONTINUE ';'
  184.         | BREAK ';'
  185.         | RETURN ';'
  186.         | RETURN expression ';'
  187.         ;

  188. translation_unit
  189.         : external_declaration
  190.         | translation_unit external_declaration
  191.         ;

  192. external_declaration
  193.         : function_definition
  194.         | declaration
  195.         ;

  196. function_definition
  197.         : declaration_specifiers declarator declaration_list compound_statement
  198.         | declaration_specifiers declarator compound_statement
  199.         | declarator declaration_list compound_statement
  200.         | declarator compound_statement
  201.         ;

  202. %%
  203. #include <stdio.h>

  204. extern char yytext[];
  205. extern int column;

  206. yyerror(s)
  207. char *s;
  208. {
  209.         fflush(stdout);
  210.         printf("\n%*s\n%*s\n", column, "^", column, s);
  211. }
复制代码

论坛徽章:
0
发表于 2007-01-24 14:29 |显示全部楼层
适合使用lex/yacc快速构建C语言相关的工具.

论坛徽章:
0
发表于 2007-01-24 15:00 |显示全部楼层
编译原理除了优化都不懂......

论坛徽章:
0
发表于 2007-01-24 15:22 |显示全部楼层
收藏先,编译都忘完了

论坛徽章:
0
发表于 2007-01-24 15:41 |显示全部楼层
原帖由 朱熹之 于 2007-1-24 15:00 发表于 5楼  
编译原理除了优化都不懂......

编译原理现在基本上只剩下优化了……
其他部分基本上都成熟了。
所以懂优化就是牛人了 :--)

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
发表于 2007-01-24 20:30 |显示全部楼层
收藏先

论坛徽章:
0
发表于 2007-01-24 20:43 |显示全部楼层
--

这个好

没办法,只能支持。

还可以对照 K&R,看看是哪个标准,挺有意思的。

--

论坛徽章:
0
发表于 2007-01-25 09:06 |显示全部楼层
hehe,看了都不懂,这个要用什么编译器呀?
编译原理吗? 没有学过,来凑个热闹!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP