David Kleinecke
2017-07-17 02:36:04 UTC
Just for laughs I am posting my state machine for C
assignment expressions. This machine implements the
entire section 6.3 syntax in the 1989 C Standard.
The machine as presented is a syntax checker but I
have added comments about what is needed to make it
a compiler. There is a great deal more I could say
but someone familiar with C compilers should be able
to follow this approach. I will welcome comments:
assignment-expression = // reset rvalue
0 OPEN A
# 7
A type-name CLOSE 0 // set rvalue - push cast prefix operator
expression CLOSE 5 // primitive
4 OPEN B
# 7
B type-name CLOSE 4 // push cast prefix operator
expression CLOSE 5 // primitive
6 OPEN C
# 7
C type-name CLOSE 1 // object (size of type)
expression CLOSE 5 // primitive
3 OPEN 9
# 7
9 expression CLOSE 5 // primitive
7 INCREMENT 3 // push prefix operator
DECREMENT 3 // push prefix operator
SIZEOF 6
UnaryOperator 4 // push prefix operator
# 8
8 Identifier 5 // primitive
Constant 5 // primitive
StringLiteral 5 // primitive
5 START expression STOP 5
OPEN D
PERIOD Identifier 5
ARROW Identifier 5
INCREMENT 5
DECREMENT 5
# 1 // object (expanded primitive)
D assignment-expression E // prepare one argument
CLOSE 5
E COMMA D
CLOSE 5
1 AssignmentOperator 2 // ERROR if rvalue
QUERY expression COLON 2 // set rvalue
BinaryOperator 2 // set rvalue
# 1 // clear
2 # 0 // apply operator
assignment expressions. This machine implements the
entire section 6.3 syntax in the 1989 C Standard.
The machine as presented is a syntax checker but I
have added comments about what is needed to make it
a compiler. There is a great deal more I could say
but someone familiar with C compilers should be able
to follow this approach. I will welcome comments:
assignment-expression = // reset rvalue
0 OPEN A
# 7
A type-name CLOSE 0 // set rvalue - push cast prefix operator
expression CLOSE 5 // primitive
4 OPEN B
# 7
B type-name CLOSE 4 // push cast prefix operator
expression CLOSE 5 // primitive
6 OPEN C
# 7
C type-name CLOSE 1 // object (size of type)
expression CLOSE 5 // primitive
3 OPEN 9
# 7
9 expression CLOSE 5 // primitive
7 INCREMENT 3 // push prefix operator
DECREMENT 3 // push prefix operator
SIZEOF 6
UnaryOperator 4 // push prefix operator
# 8
8 Identifier 5 // primitive
Constant 5 // primitive
StringLiteral 5 // primitive
5 START expression STOP 5
OPEN D
PERIOD Identifier 5
ARROW Identifier 5
INCREMENT 5
DECREMENT 5
# 1 // object (expanded primitive)
D assignment-expression E // prepare one argument
CLOSE 5
E COMMA D
CLOSE 5
1 AssignmentOperator 2 // ERROR if rvalue
QUERY expression COLON 2 // set rvalue
BinaryOperator 2 // set rvalue
# 1 // clear
2 # 0 // apply operator