51 lines
1.6 KiB
Plaintext
51 lines
1.6 KiB
Plaintext
%parser 23.lexer // desk calculator
|
|
%symbol Expression {
|
|
public virtual int Value { get { return 0; } }
|
|
}
|
|
%node Const : Expression {
|
|
public int m_val;
|
|
public Const(int v) { m_val = v; }
|
|
public override int Value { get { return m_val; } }
|
|
}
|
|
%node Recall : Expression {
|
|
public Variable m_vbl;
|
|
public Recall(Variable v) { m_vbl = v; }
|
|
public override int Value { get { return m_vbl.Value; } }
|
|
}
|
|
%node Sum : Expression {
|
|
public Expression m_left,m_right;
|
|
public Sum(Expression a, Expression b) { m_left=a; m_right = b; }
|
|
public override int Value { get { return m_left.Value + m_right.Value; } }
|
|
}
|
|
%node Product : Expression {
|
|
public Expression m_left,m_right;
|
|
public Product(Expression a, Expression b) { m_left=a; m_right = b; }
|
|
public override int Value { get { return m_left.Value * m_right.Value; } }
|
|
}
|
|
%node Assignment : Expression {
|
|
public Variable m_vbl;
|
|
public Expression m_exp;
|
|
public Assignment(Variable v, Expression e) { m_vbl=v; m_exp = e; }
|
|
public override int Value { get { m_vbl.Value = m_exp.Value; return 0; } }
|
|
}
|
|
%node Bracket : Expression {
|
|
public Expression m_inner;
|
|
public Bracket(Expression e) { m_inner = e; }
|
|
public override int Value { get { return m_inner.Value; } }
|
|
}
|
|
|
|
%right '='
|
|
%left '+'
|
|
%left '*'
|
|
|
|
InputLine :
|
|
| InputLine Expression { Console.WriteLine($2.Value); } ';' '\n'
|
|
;
|
|
Expression : Variable %Recall ($1)
|
|
| Int %Const ($1)
|
|
| Expression '+' Expression %Sum ($1, $3)
|
|
| Expression '*' Expression %Product ($1, $3)
|
|
| '(' Expression ')' %Bracket ($2)
|
|
| Variable '=' Expression %Assignment ($1, $3)
|
|
;
|