📄 c语法.c
字号:
发信人: InfoMagic (好好学习 天天向上), 信区: PUE
标 题: c 语法
发信站: 武汉白云黄鹤站 (2001年05月23日22:52:01 星期三), 站内信件
#!/bin/sh
# shar: Shell Archiver (v1.22)
#
# Run the following text with /bin/sh to create:
# README
# Makefile
# gram.y
# main.c
# scan.l
#
echo "x - extracting README (Text)"
sed 's/^X//' << 'SHAR_EOF' > README &&
XThe files in this directory contain the ANSI C grammar from the April 30,
1985
Xdraft of the proposed standard. This copy also incorporates all bug fixes I
Xhave seen since the last two postings. With a little work this grammar can
Xbe made to parse the C that most of us know and love (sort of).
X
XThere is one bug fix to the grammar that is in this posting. On line 295
Xof gram.y it previously read declaration_specifiers instead of
Xtype_specifier_list as it does now. I believe the folks at the ANSI
committee
Xmade a mistake since if you replace the line with what the original read
Xyou will end up with 16 shift/reduce errors and 2 reduce/reduce errors
X(the good ones). As it is, it only has 1 shift/reduce error that occurs
Xon the if/else construct. YACC creates the correct parser and I don't want
Xto ugly my grammar up.
X
XAnyway, all cumquats unite and generate this sucker. Then just sit and play
Xwith it. Remember, the grammar accepts things like
X
X "Hello, world"++;
X --1.23456;
X *'a'
X
Xbut this is not a bug, but simply a shuffling of the checking into the
Xsemantic analysis. If you want to hack it up to do lvalue and rvalue
Xchecking, I'm sure the ANSI committee would be glad to have your changes.
XDon't send'em to me though. I don't want'em. Wear this in good health.
X
XJeff Lee
Xgatech!jeff jeff@gatech jeff%gatech.CSNet@CSNet-Relay.ARPA
SHAR_EOF
chmod 0644 README || echo "restore of README fails"
echo "x - extracting Makefile (Text)"
sed 's/^X//' << 'SHAR_EOF' > Makefile &&
XYFLAGS = -dv
XCFLAGS = -O
XLFLAGS =
X
XSRC = gram.y scan.l main.c
XOBJ = gram.o scan.o main.o
X
X$(BIN)/ansi_c : $(OBJ)
X cc $(CFLAGS) $(OBJ)/ansi_c
X
Xscan.o : y.tab.h
X
Xclean :
X rm -f y.tab.h y.output *.o
SHAR_EOF
chmod 0644 Makefile || echo "restore of Makefile fails"
echo "x - extracting gram.y (Text)"
sed 's/^X//' << 'SHAR_EOF' > gram.y &&
X%token IDENTIFIER CONSTANT STRING_LITERAL SIZEOF
X%token PTR_OP INC_OP DEC_OP LEFT_OP RIGHT_OP LE_OP GE_OP EQ_OP NE_OP
X%token AND_OP OR_OP MUL_ASSIGN DIV_ASSIGN MOD_ASSIGN ADD_ASSIGN
X%token SUB_ASSIGN LEFT_ASSIGN RIGHT_ASSIGN AND_ASSIGN
X%token XOR_ASSIGN OR_ASSIGN TYPE_NAME
X
X%token TYPEDEF EXTERN STATIC AUTO REGISTER
X%token CHAR SHORT INT LONG SIGNED UNSIGNED FLOAT DOUBLE CONST VOLATILE VOID
X%token STRUCT UNION ENUM ELIPSIS RANGE
X
X%token CASE DEFAULT IF ELSE SWITCH WHILE DO FOR GOTO CONTINUE BREAK RETURN
X
X%start file
X%%
X
Xprimary_expr
X : identifier
X | CONSTANT
X | STRING_LITERAL
X | '(' expr ')'
X ;
X
Xpostfix_expr
X : primary_expr
X | postfix_expr '[' expr ']'
X | postfix_expr '(' ')'
X | postfix_expr '(' argument_expr_list ')'
X | postfix_expr '.' identifier
X | postfix_expr PTR_OP identifier
X | postfix_expr INC_OP
X | postfix_expr DEC_OP
X ;
X
Xargument_expr_list
X : assignment_expr
X | argument_expr_list ',' assignment_expr
X ;
X
Xunary_expr
X : postfix_expr
X | INC_OP unary_expr
X | DEC_OP unary_expr
X | unary_operator cast_expr
X | SIZEOF unary_expr
X | SIZEOF '(' type_name ')'
X ;
X
Xunary_operator
X : '&'
X | '*'
X | '+'
X | '-'
X | '~'
X | '!'
X ;
X
Xcast_expr
X : unary_expr
X | '(' type_name ')' cast_expr
X ;
X
Xmultiplicative_expr
X : cast_expr
X | multiplicative_expr '*' cast_expr
X | multiplicative_expr '/' cast_expr
X | multiplicative_expr '%' cast_expr
X ;
X
Xadditive_expr
X : multiplicative_expr
X | additive_expr '+' multiplicative_expr
X | additive_expr '-' multiplicative_expr
X ;
X
Xshift_expr
X : additive_expr
X | shift_expr LEFT_OP additive_expr
X | shift_expr RIGHT_OP additive_expr
X ;
X
Xrelational_expr
X : shift_expr
X | relational_expr '<' shift_expr
X | relational_expr '>' shift_expr
X | relational_expr LE_OP shift_expr
X | relational_expr GE_OP shift_expr
X ;
X
Xequality_expr
X : relational_expr
X | equality_expr EQ_OP relational_expr
X | equality_expr NE_OP relational_expr
X ;
X
Xand_expr
X : equality_expr
X | and_expr '&' equality_expr
X ;
X
Xexclusive_or_expr
X : and_expr
X | exclusive_or_expr '^' and_expr
X ;
X
Xinclusive_or_expr
X : exclusive_or_expr
X | inclusive_or_expr '|' exclusive_or_expr
X ;
X
Xlogical_and_expr
X : inclusive_or_expr
X | logical_and_expr AND_OP inclusive_or_expr
X ;
X
Xlogical_or_expr
X : logical_and_expr
X | logical_or_expr OR_OP logical_and_expr
X ;
X
Xconditional_expr
X : logical_or_expr
X | logical_or_expr '?' logical_or_expr ':' conditional_expr
X ;
X
Xassignment_expr
X : conditional_expr
X | unary_expr assignment_operator assignment_expr
X ;
X
Xassignment_operator
X : '='
X | MUL_ASSIGN
X | DIV_ASSIGN
X | MOD_ASSIGN
X | ADD_ASSIGN
X | SUB_ASSIGN
X | LEFT_ASSIGN
X | RIGHT_ASSIGN
X | AND_ASSIGN
X | XOR_ASSIGN
X | OR_ASSIGN
X ;
X
Xexpr
X : assignment_expr
X | expr ',' assignment_expr
X ;
X
Xconstant_expr
X : conditional_expr
X ;
X
Xdeclaration
X : declaration_specifiers ';'
X | declaration_specifiers init_declarator_list ';'
X ;
X
Xdeclaration_specifiers
X : storage_class_specifier
X | storage_class_specifier declaration_specifiers
X | type_specifier
X | type_specifier declaration_specifiers
X ;
X
Xinit_declarator_list
X : init_declarator
X | init_declarator_list ',' init_declarator
X ;
X
Xinit_declarator
X : declarator
X | declarator '=' initializer
X ;
X
Xstorage_class_specifier
X : TYPEDEF
X | EXTERN
X | STATIC
X | AUTO
X | REGISTER
X ;
X
Xtype_specifier
X : CHAR
X | SHORT
X | INT
X | LONG
X | SIGNED
X | UNSIGNED
X | FLOAT
X | DOUBLE
X | CONST
X | VOLATILE
X | VOID
X | struct_or_union_specifier
X | enum_specifier
X | TYPE_NAME
X ;
X
Xstruct_or_union_specifier
X : struct_or_union identifier '{' struct_declaration_list '}'
X | struct_or_union '{' struct_declaration_list '}'
X | struct_or_union identifier
X ;
X
Xstruct_or_union
X : STRUCT
X | UNION
X ;
X
Xstruct_declaration_list
X : struct_declaration
X | struct_declaration_list struct_declaration
X ;
X
Xstruct_declaration
X : type_specifier_list struct_declarator_list ';'
X ;
X
Xstruct_declarator_list
X : struct_declarator
X | struct_declarator_list ',' struct_declarator
X ;
X
Xstruct_declarator
X : declarator
X | ':' constant_expr
X | declarator ':' constant_expr
X ;
X
Xenum_specifier
X : ENUM '{' enumerator_list '}'
X | ENUM identifier '{' enumerator_list '}'
X | ENUM identifier
X ;
X
Xenumerator_list
X : enumerator
X | enumerator_list ',' enumerator
X ;
X
Xenumerator
X : identifier
X | identifier '=' constant_expr
X ;
X
Xdeclarator
X : declarator2
X | pointer declarator2
X ;
X
Xdeclarator2
X : identifier
X | '(' declarator ')'
X | declarator2 '[' ']'
X | declarator2 '[' constant_expr ']'
X | declarator2 '(' ')'
X | declarator2 '(' parameter_type_list ')'
X | declarator2 '(' parameter_identifier_list ')'
X ;
X
Xpointer
X : '*'
X | '*' type_specifier_list
X | '*' pointer
X | '*' type_specifier_list pointer
X ;
X
Xtype_specifier_list
X : type_specifier
X | type_specifier_list type_specifier
X ;
X
Xparameter_identifier_list
X : identifier_list
X | identifier_list ',' ELIPSIS
X ;
X
Xidentifier_list
X : identifier
X | identifier_list ',' identifier
X ;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -