使用调车码算法的抽象语法树

use*_*114 8 algorithm abstract-syntax-tree shunting-yard

我有一个已标记化的中缀表达式,并希望继续创建一个抽象语法树。我了解在这些情况下使用的调车场算法。我只找到了将中缀表达式转换为 RPN 格式的方法,而不是转换为 AST。我可以先创建 RPN 版本,然后从中创建 AST,但这似乎没有必要。

我选择的语言是 JavaScript,但我只需要查看任何语言的示例和/或算法描述。我浏览了 Dragon Book 和 Terence Parr 的书,但都没有给出我想要的答案。

Ale*_*huk -1

请参阅用 dart 编写的简化版本,它同时生成 RPN 和 AST。使用来自维基百科的伪代码来实现。

是该算法的一个很好的直观解释。

试试看

void main() {
  final ast = shunting('(2*3*x+5*y-3*z)/(1+3+2*2)'.split(''), 'xyz'.split(''));
  print(ast);
}

/// imm - immediate value
Ast shunting(
  List<String> body,
  List<String> arguments,
) {
  final tree = <Ast>[];
  final outputQueue = <String>[];
  final operatorStack = <String>[];
  for (final token in body) {
    if (int.tryParse(token) is int) {
      final operand = UnOp('imm', int.parse(token));
      outputQueue.add(token);
      tree.add(operand);
    } else if (arguments.contains(token)) {
      final operand = UnOp('arg', arguments.indexOf(token));
      outputQueue.add(token);
      tree.add(operand);
    } else if (token.isOperator) {
      while (operatorStack.isNotEmpty && (operatorStack.last > token || operatorStack.last.isSamePrecedence(token))) {
        final lastOp = operatorStack.removeLast();
        outputQueue.add(lastOp);
        final second = tree.removeLast();
        final first = tree.removeLast();
        tree.add(BinOp(lastOp, first, second));
      }
      operatorStack.add(token);
    } else if (token == '(') {
      operatorStack.add(token);
    } else if (token == ')') {
      assert(operatorStack.isNotEmpty, 'mismatched parenthesis');
      while (operatorStack.last != '(') {
        final lastOp = operatorStack.removeLast();
        outputQueue.add(lastOp);
        final second = tree.removeLast();
        final first = tree.removeLast();
        tree.add(BinOp(lastOp, first, second));
      }
      operatorStack.removeLast();
    }
  }
  while (operatorStack.isNotEmpty) {
    final lastOp = operatorStack.removeLast();
    outputQueue.add(lastOp);
    final second = tree.removeLast();
    final first = tree.removeLast();
    tree.add(BinOp(lastOp, first, second));
  }
  print('RPN: ${outputQueue.join()}');
  return tree.first;
}

abstract class Ast {
  abstract final String op;
}

class BinOp implements Ast {
  BinOp(
    this.op,
    this._a,
    this._b,
  );

  final Ast _a;
  final Ast _b;

  @override
  final String op;

  Ast a(Ast a) => _a;
  Ast b(Ast b) => _b;

  @override
  String toString() => '{op: $op, a: $_a, b: $_b}';
}

class UnOp implements Ast {
  UnOp(this.op, this.n);

  final int n;

  @override
  final String op;

  @override
  String toString() => '{op: $op, n: $n}';
}

extension Operators on String {
  bool operator >(Object other) {
    assert(other is String, 'Incompatible type comparison');
    return '*/'.split('').contains(this) && '+-'.split('').contains(other);
  }

  bool operator <(Object other) {
    assert(other is String, 'Incompatible type comparison');
    return '-+'.split('').contains(this) && '*/'.split('').contains(other);
  }

  bool get isOperator => '*/+-'.split('').contains(this);

  bool isSamePrecedence(Object other) {
    assert(other is String, 'Incompatible type comparison');
    if ('+-'.split('').contains(this) && '+-'.split('').contains(other)) return true;
    if ('/*'.split('').contains(this) && '/*'.split('').contains(other)) return true;
    return false;
  }
}
Run Code Online (Sandbox Code Playgroud)