Why sort dart keywords first when constructing a keyword transition table in the 'keyword_state. dart' file

38 views
Skip to first unread message

he auto

unread,
Nov 17, 2023, 2:18:00 PM11/17/23
to Dart Analyzer Discussion
I am very interested in the design of the 'computeKeywordStateTable' function for the Class  'KeywordState' , as it seems to first sort the keyword list in dictionary order, so that keywords with the same prefix are grouped  into a chunk. But why not simply traverse each keyword word ? Like this:
```
class State {
  String? accept;

  late List<State?> transforms;

  State({this.accept = null}) {
    transforms = List.filled(26, null);
  }

  bool contains(int chr) {
    return transforms[chr - 97] != null;
  }

  addTransform(int chr, {required State nextState}) {
    transforms[chr - 97] = nextState;
  }

  State? transformTo(int chr) {
    return transforms[chr - 97];
  }

  bool isAccept() {
    return accept != null;
  }

  void setAccept(String keyword) {
    this.accept = keyword;
  }

  @override
  String toString() {
    return transforms.toString();
  }
}

......
 State start = new State();
 State? currentState = start;
  for (String keyword in keywords) {
    for (int i = 0; i < keyword.length; i++) {
      int chr = keyword.codeUnitAt(i);
      if (!currentState!.contains(chr)) {
        if (i == keyword.length - 1) {
          State newState = new State(accept: keyword);
          currentState.addTransform(chr, nextState: newState);
          currentState = newState;
        } else {
          State newState = new State();
          currentState.addTransform(chr, nextState: newState);
          currentState = newState;
        }
      } else {
        currentState = currentState.transformTo(chr);
        if (i == keyword.length - 1) {
          currentState!.setAccept(keyword);
        }
      }
    }

    currentState = start;
  }

```

Simply put, there are two issues:
1. Why need to sort keywords first?
2. Why need to recursively perform chunk wise traversal of keywords instead of traversing each word like the example code I showed? Wouldn't this lose performance?
Thank you very much for answering these questions guys.

Brian Wilkerson

unread,
Nov 20, 2023, 5:32:07 PM11/20/23
to analyzer...@dartlang.org
I don't know all of the reasons why the current implementation was written the way it is. I'm sure that performance was a major concern, but I don't know what other considerations influenced the choices.

I also don't know how your proposal compares in terms of performance, either the time required to build the table or the time required to recognize a keyword. The only way to know that would be to benchmark the two implementations, in the latter case against a sufficiently large and varied corpus of Dart code.

--
You received this message because you are subscribed to the Google Groups "Dart Analyzer Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to analyzer-discu...@dartlang.org.
To view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/analyzer-discuss/169ffee5-d72c-4291-a2b9-6e66bad5e11bn%40dartlang.org.

he auto

unread,
Nov 23, 2023, 1:05:05 PM11/23/23
to Dart Analyzer Discussion, brianwilkerson
Ok. Thanks a lot for your answer. I think I got the key point now.
Reply all
Reply to author
Forward
0 new messages